@@ -41,6 +41,13 @@ struct sal_socket_table
4141 struct sal_socket * * sockets ;
4242};
4343
44+ /* record the netdev and res table*/
45+ struct sal_netdev_res_table
46+ {
47+ struct addrinfo * res ;
48+ struct netdev * netdev ;
49+ };
50+
4451#ifdef SAL_USING_TLS
4552/* The global TLS protocol options */
4653static struct sal_proto_tls * proto_tls ;
@@ -50,6 +57,7 @@ static struct sal_proto_tls *proto_tls;
5057static struct sal_socket_table socket_table ;
5158static struct rt_mutex sal_core_lock ;
5259static rt_bool_t init_ok = RT_FALSE ;
60+ static struct sal_netdev_res_table sal_dev_res_tbl [SAL_SOCKETS_NUM ];
5361
5462#define IS_SOCKET_PROTO_TLS (sock ) (((sock)->protocol == PROTOCOL_TLS) || \
5563 ((sock)->protocol == PROTOCOL_DTLS))
9098 ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
9199 (pf)->netdb_ops->ops) \
92100
101+ #define SAL_NETDBOPS_VALID (netdev , pf , ops ) \
102+ ((netdev) && \
103+ ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
104+ (pf)->netdb_ops->ops) \
105+
93106/**
94107 * SAL (Socket Abstraction Layer) initialize.
95108 *
@@ -116,6 +129,9 @@ int sal_init(void)
116129 return -1 ;
117130 }
118131
132+ /*init the dev_res table */
133+ rt_memset (sal_dev_res_tbl , 0 , sizeof (sal_dev_res_tbl ));
134+
119135 /* create sal socket lock */
120136 rt_mutex_init (& sal_core_lock , "sal_lock" , RT_IPC_FLAG_FIFO );
121137
@@ -1086,41 +1102,69 @@ int sal_getaddrinfo(const char *nodename,
10861102{
10871103 struct netdev * netdev = netdev_default ;
10881104 struct sal_proto_family * pf ;
1105+ int ret = 0 ;
1106+ rt_uint32_t i = 0 ;
10891107
10901108 if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , getaddrinfo ))
10911109 {
1092- return pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
1110+ ret = pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
10931111 }
10941112 else
10951113 {
10961114 /* get the first network interface device with up status */
10971115 netdev = netdev_get_first_by_flags (NETDEV_FLAG_UP );
10981116 if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , getaddrinfo ))
10991117 {
1100- return pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
1118+ ret = pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
1119+ }
1120+ else
1121+ {
1122+ ret = -1 ;
11011123 }
11021124 }
11031125
1104- return -1 ;
1126+ if (ret == RT_EOK )
1127+ {
1128+ /*record the netdev and res*/
1129+ for (i = 0 ; i < SAL_SOCKETS_NUM ; i ++ )
1130+ {
1131+ if (sal_dev_res_tbl [i ].res == RT_NULL )
1132+ {
1133+ sal_dev_res_tbl [i ].res = * res ;
1134+ sal_dev_res_tbl [i ].netdev = netdev ;
1135+ break ;
1136+ }
1137+ }
1138+
1139+ RT_ASSERT ((i < SAL_SOCKETS_NUM ));
1140+
1141+ }
1142+
1143+ return ret ;
11051144}
11061145
11071146void sal_freeaddrinfo (struct addrinfo * ai )
11081147{
1109- struct netdev * netdev = netdev_default ;
1110- struct sal_proto_family * pf ;
1148+ struct netdev * netdev = RT_NULL ;
1149+ struct sal_proto_family * pf = RT_NULL ;
1150+ rt_uint32_t i = 0 ;
11111151
1112- if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , freeaddrinfo ))
1113- {
1114- pf -> netdb_ops -> freeaddrinfo (ai );
1115- }
1116- else
1152+ /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
1153+ for (i = 0 ; i < SAL_SOCKETS_NUM ; i ++ )
11171154 {
1118- /* get the first network interface device with up status */
1119- netdev = netdev_get_first_by_flags (NETDEV_FLAG_UP );
1120- if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , freeaddrinfo ))
1155+ if (sal_dev_res_tbl [i ].res == ai )
11211156 {
1122- pf -> netdb_ops -> freeaddrinfo (ai );
1157+ netdev = sal_dev_res_tbl [i ].netdev ;
1158+ sal_dev_res_tbl [i ].res = RT_NULL ;
1159+ sal_dev_res_tbl [i ].netdev = RT_NULL ;
1160+ break ;
11231161 }
11241162 }
1163+ RT_ASSERT ((i < SAL_SOCKETS_NUM ));
1164+
1165+ if (SAL_NETDBOPS_VALID (netdev , pf , freeaddrinfo ))
1166+ {
1167+ pf -> netdb_ops -> freeaddrinfo (ai );
1168+ }
11251169}
11261170
0 commit comments