@@ -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
@@ -1087,41 +1103,69 @@ int sal_getaddrinfo(const char *nodename,
10871103{
10881104 struct netdev * netdev = netdev_default ;
10891105 struct sal_proto_family * pf ;
1106+ int ret = 0 ;
1107+ rt_uint32_t i = 0 ;
10901108
10911109 if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , getaddrinfo ))
10921110 {
1093- return pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
1111+ ret = pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
10941112 }
10951113 else
10961114 {
10971115 /* get the first network interface device with up status */
10981116 netdev = netdev_get_first_by_flags (NETDEV_FLAG_UP );
10991117 if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , getaddrinfo ))
11001118 {
1101- return pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
1119+ ret = pf -> netdb_ops -> getaddrinfo (nodename , servname , hints , res );
1120+ }
1121+ else
1122+ {
1123+ ret = -1 ;
11021124 }
11031125 }
11041126
1105- return -1 ;
1127+ if (ret == RT_EOK )
1128+ {
1129+ /*record the netdev and res*/
1130+ for (i = 0 ; i < SAL_SOCKETS_NUM ; i ++ )
1131+ {
1132+ if (sal_dev_res_tbl [i ].res == RT_NULL )
1133+ {
1134+ sal_dev_res_tbl [i ].res = * res ;
1135+ sal_dev_res_tbl [i ].netdev = netdev ;
1136+ break ;
1137+ }
1138+ }
1139+
1140+ RT_ASSERT ((i < SAL_SOCKETS_NUM ));
1141+
1142+ }
1143+
1144+ return ret ;
11061145}
11071146
11081147void sal_freeaddrinfo (struct addrinfo * ai )
11091148{
1110- struct netdev * netdev = netdev_default ;
1111- struct sal_proto_family * pf ;
1149+ struct netdev * netdev = RT_NULL ;
1150+ struct sal_proto_family * pf = RT_NULL ;
1151+ rt_uint32_t i = 0 ;
11121152
1113- if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , freeaddrinfo ))
1114- {
1115- pf -> netdb_ops -> freeaddrinfo (ai );
1116- }
1117- else
1153+ /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
1154+ for (i = 0 ; i < SAL_SOCKETS_NUM ; i ++ )
11181155 {
1119- /* get the first network interface device with up status */
1120- netdev = netdev_get_first_by_flags (NETDEV_FLAG_UP );
1121- if (SAL_NETDEV_NETDBOPS_VALID (netdev , pf , freeaddrinfo ))
1156+ if (sal_dev_res_tbl [i ].res == ai )
11221157 {
1123- pf -> netdb_ops -> freeaddrinfo (ai );
1158+ netdev = sal_dev_res_tbl [i ].netdev ;
1159+ sal_dev_res_tbl [i ].res = RT_NULL ;
1160+ sal_dev_res_tbl [i ].netdev = RT_NULL ;
1161+ break ;
11241162 }
11251163 }
1164+ RT_ASSERT ((i < SAL_SOCKETS_NUM ));
1165+
1166+ if (SAL_NETDBOPS_VALID (netdev , pf , freeaddrinfo ))
1167+ {
1168+ pf -> netdb_ops -> freeaddrinfo (ai );
1169+ }
11261170}
11271171
0 commit comments