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

Function sal_bind

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

Source from the content-addressed store, hash-verified

705}
706
707int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
708{
709 struct sal_socket *sock;
710 struct sal_proto_family *pf;
711 ip_addr_t input_ipaddr;
712
713 RT_ASSERT(name);
714
715 /* get the socket object by socket descriptor */
716 SAL_SOCKET_OBJ_GET(sock, socket);
717
718#define IS_INET_ADDR_FAMILY(_af) ((_af) == AF_INET) || ((_af) == AF_INET6)
719 if (IS_INET_ADDR_FAMILY(name->sa_family))
720 {
721 /* bind network interface by ip address */
722 sal_sockaddr_to_ipaddr(name, &input_ipaddr);
723
724 /* check input ipaddr is default netdev ipaddr */
725 if (!ip_addr_isany_val(input_ipaddr))
726 {
727 struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL;
728 struct netdev *new_netdev = RT_NULL;
729
730 new_netdev = netdev_get_by_ipaddr(&input_ipaddr);
731 if (new_netdev == RT_NULL)
732 {
733 return -1;
734 }
735
736 /* get input and local ip address proto_family */
737 SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind);
738 SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind);
739
740 /* check the network interface protocol family type */
741 if (input_pf->family != local_pf->family)
742 {
743 int new_socket = -1;
744
745 /* protocol family is different, close old socket and create new socket by input ip address */
746 local_pf->skt_ops->closesocket(socket);
747
748 new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol);
749 if (new_socket < 0)
750 {
751 return -1;
752 }
753 sock->netdev = new_netdev;
754 sock->user_data = (void *)(size_t)new_socket;
755 }
756 }
757 }
758 /* check and get protocol families by the network interface device */
759 SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind);
760 return pf->skt_ops->bind((int)(size_t)sock->user_data, name, namelen);
761}
762
763int sal_shutdown(int socket, int how)
764{

Callers 8

bindFunction · 0.85
server_thread_entryFunction · 0.85
TC_sal_socket_bindFunction · 0.85
TC_sal_socket_listenFunction · 0.85
TC_sal_socket_acceptFunction · 0.85
TC_sal_socket_send_recvFunction · 0.85

Calls 2

sal_sockaddr_to_ipaddrFunction · 0.85
netdev_get_by_ipaddrFunction · 0.85

Tested by

no test coverage detected