MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / socket_init

Function socket_init

components/net/sal/src/sal_socket.c:456–526  ·  view source on GitHub ↗

* This function will initialize sal socket object and set socket options * * @param family protocol family * @param type socket type * @param protocol transfer Protocol * @param res sal socket object address * * @return 0 : socket initialize success * -1 : input the wrong family * -2 : input the wrong socket type * -3 : get network interface failed

Source from the content-addressed store, hash-verified

454 * -4 : invalid protocol or combo
455 */
456static int socket_init(int family, int type, int protocol, struct sal_socket **res)
457{
458 struct sal_socket *sock;
459 struct sal_proto_family *pf;
460 struct netdev *netdv_def = netdev_default;
461 struct netdev *netdev = RT_NULL;
462 rt_bool_t flag = RT_FALSE;
463
464 /* Existing range checks for family and type */
465 if (family < 0 || family > AF_MAX)
466 {
467 LOG_E("Invalid family: %d (must be 0 ~ %d)", family, AF_MAX);
468 return -1;
469 }
470
471 if (type < 0 || type > SOCK_MAX)
472 {
473 LOG_E("Invalid type: %d (must be 0 ~ %d)", type, SOCK_MAX);
474 return -2;
475 }
476
477 /* Range check for protocol */
478 if (!VALID_PROTOCOL(protocol))
479 {
480 LOG_E("Invalid protocol: %d (must be 0 ~ %d)", protocol, IPPROTO_RAW);
481 rt_set_errno(EINVAL);
482 return -4;
483 }
484
485 sock = *res;
486 sock->domain = family;
487 sock->type = type;
488 sock->protocol = protocol;
489
490 /* Combo compatibility check */
491 if (!VALID_COMBO(family, type, protocol))
492 {
493 LOG_E("Invalid combo: domain=%d, type=%d, protocol=%d", family, type, protocol);
494 rt_set_errno(EINVAL);
495 return -4;
496 }
497
498 /* Existing netdev selection logic */
499 if (netdv_def && netdev_is_up(netdv_def))
500 {
501 /* check default network interface device protocol family */
502 pf = (struct sal_proto_family *)netdv_def->sal_user_data;
503 if (pf != RT_NULL && pf->skt_ops && (pf->family == family || pf->sec_family == family))
504 {
505 sock->netdev = netdv_def;
506 flag = RT_TRUE;
507 }
508 }
509
510 if (flag == RT_FALSE)
511 {
512 /* get network interface device by protocol family */
513 netdev = netdev_get_by_family(family);

Callers 2

sal_acceptFunction · 0.85
sal_socketFunction · 0.85

Calls 2

rt_set_errnoFunction · 0.85
netdev_get_by_familyFunction · 0.85

Tested by

no test coverage detected