@@ -50,7 +50,7 @@ static struct rt_mutex sal_core_lock;
5050static rt_bool_t init_ok = RT_FALSE ;
5151
5252/**
53- * SAL (Socket Abstraction Layer) initialization .
53+ * SAL (Socket Abstraction Layer) initialize .
5454 *
5555 * @return result
5656 * >= 0: initialize success
@@ -64,7 +64,7 @@ int sal_init(void)
6464 }
6565
6666 /* clean sal socket table */
67- memset (& socket_table , 0 , sizeof (socket_table ));
67+ rt_memset (& socket_table , 0 , sizeof (socket_table ));
6868 /* create sal socket lock */
6969 rt_mutex_init (& sal_core_lock , "sal_lock" , RT_IPC_FLAG_FIFO );
7070
@@ -76,12 +76,12 @@ int sal_init(void)
7676INIT_COMPONENT_EXPORT (sal_init );
7777
7878/**
79- * this function will register the current protocol family to the global array of protocol families.
79+ * This function will register protocol family to the global array of protocol families.
8080 *
81- * @param pf protocol families structure
81+ * @param pf protocol family object
8282 *
83- * @return 0 : protocol families structure index
84- * -1 : the global array of available protocol families is full
83+ * @return >= 0 : protocol family object index
84+ * -1 : the global array of available protocol families is full
8585 */
8686int sal_proto_family_register (const struct proto_family * pf )
8787{
@@ -91,6 +91,18 @@ int sal_proto_family_register(const struct proto_family *pf)
9191 /* disable interrupt */
9292 level = rt_hw_interrupt_disable ();
9393
94+ /* check protocol family is already registered */
95+ for (idx = 0 ; idx < SAL_PROTO_FAMILIES_NUM ; idx ++ )
96+ {
97+ if (rt_strcmp (proto_families [idx ].name , pf -> name ) == 0 )
98+ {
99+ /* enable interrupt */
100+ rt_hw_interrupt_enable (level );
101+ LOG_E ("%s protocol family is already registered!" , pf -> name );
102+ return -1 ;
103+ }
104+ }
105+
94106 /* find an empty protocol family entry */
95107 for (idx = 0 ; idx < SAL_PROTO_FAMILIES_NUM && proto_families [idx ].create ; idx ++ );
96108
@@ -99,10 +111,10 @@ int sal_proto_family_register(const struct proto_family *pf)
99111 {
100112 /* enable interrupt */
101113 rt_hw_interrupt_enable (level );
102-
103114 return -1 ;
104115 }
105116
117+ rt_strncpy (proto_families [idx ].name , pf -> name , rt_strlen (pf -> name ));
106118 proto_families [idx ].family = pf -> family ;
107119 proto_families [idx ].sec_family = pf -> sec_family ;
108120 proto_families [idx ].create = pf -> create ;
@@ -119,11 +131,62 @@ int sal_proto_family_register(const struct proto_family *pf)
119131}
120132
121133/**
122- * this function will get socket structure by sal socket descriptor
134+ * This function removes a previously registered protocol family object.
135+ *
136+ * @param pf protocol family object
137+ *
138+ * @return >=0 : unregister protocol family index
139+ * -1 : unregister failed
140+ */
141+ int sal_proto_family_unregister (const struct proto_family * pf )
142+ {
143+ int idx = 0 ;
144+
145+ RT_ASSERT (pf != RT_NULL );
146+
147+ for (idx = 0 ; idx < SAL_PROTO_FAMILIES_NUM ; idx ++ )
148+ {
149+ if (rt_strcmp (proto_families [idx ].name , pf -> name ) == 0 )
150+ {
151+ rt_memset (& proto_families [idx ], 0x00 , sizeof (struct proto_family ));
152+
153+ return idx ;
154+ }
155+ }
156+
157+ return -1 ;
158+ }
159+
160+ /**
161+ * This function will get protocol family by name.
162+ *
163+ * @param name protocol family name
164+ *
165+ * @return protocol family object
166+ */
167+ struct proto_family * sal_proto_family_find (const char * name )
168+ {
169+ int idx = 0 ;
170+
171+ RT_ASSERT (name != RT_NULL );
172+
173+ for (idx = 0 ; idx < SAL_PROTO_FAMILIES_NUM ; idx ++ )
174+ {
175+ if (rt_strcmp (proto_families [idx ].name , name ) == 0 )
176+ {
177+ return & proto_families [idx ];
178+ }
179+ }
180+
181+ return RT_NULL ;
182+ }
183+
184+ /**
185+ * This function will get sal socket object by sal socket descriptor.
123186 *
124187 * @param socket sal socket index
125188 *
126- * @return socket structure of the current sal socket index
189+ * @return sal socket object of the current sal socket index
127190 */
128191struct sal_socket * sal_get_socket (int socket )
129192{
@@ -145,7 +208,7 @@ struct sal_socket *sal_get_socket(int socket)
145208}
146209
147210/**
148- * this function will lock sal socket.
211+ * This function will lock sal socket.
149212 *
150213 * @note please don't invoke it on ISR.
151214 */
@@ -161,7 +224,7 @@ static void sal_lock(void)
161224}
162225
163226/**
164- * this function will lock sal socket.
227+ * This function will lock sal socket.
165228 *
166229 * @note please don't invoke it on ISR.
167230 */
@@ -171,7 +234,7 @@ static void sal_unlock(void)
171234}
172235
173236/**
174- * this function will get protocol family structure by family type
237+ * This function will get protocol family structure by family type
175238 *
176239 * @param family protocol family
177240 *
@@ -201,17 +264,17 @@ static struct proto_family *get_proto_family(int family)
201264}
202265
203266/**
204- * this function will initialize socket structure and set socket options
267+ * This function will initialize sal socket object and set socket options
205268 *
206269 * @param family protocol family
207270 * @param type socket type
208271 * @param protocol transfer Protocol
209- * @param res socket structure address
272+ * @param res sal socket object address
210273 *
211274 * @return 0 : socket initialize success
212275 * -1 : input the wrong family
213276 * -2 : input the wrong socket type
214- * -3 : get protocol family structure failed
277+ * -3 : get protocol family object failed
215278 * -4 : set socket options failed
216279 */
217280static int socket_init (int family , int type , int protocol , struct sal_socket * * res )
@@ -234,7 +297,7 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r
234297 sock -> type = type ;
235298 sock -> protocol = protocol ;
236299
237- /* get socket protocol family structure */
300+ /* get socket protocol family object */
238301 if ((pf = get_proto_family (family )) == RT_NULL )
239302 {
240303 return -3 ;
@@ -301,11 +364,6 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
301364
302365}
303366
304- /**
305- * this function will return a empty sal socket structure address
306- *
307- * @return sal socket structure address
308- */
309367static int socket_new (void )
310368{
311369 struct sal_socket * sock ;
@@ -415,7 +473,7 @@ int sal_shutdown(int socket, int how)
415473
416474 if (sock -> ops -> shutdown ((int ) sock -> user_data , how ) == 0 )
417475 {
418- memset (sock , 0x00 , sizeof (struct sal_socket ));
476+ rt_memset (sock , 0x00 , sizeof (struct sal_socket ));
419477 return 0 ;
420478 }
421479
@@ -622,7 +680,7 @@ int sal_closesocket(int socket)
622680
623681 if (sock -> ops -> closesocket ((int ) sock -> user_data ) == 0 )
624682 {
625- memset (sock , 0x00 , sizeof (struct sal_socket ));
683+ rt_memset (sock , 0x00 , sizeof (struct sal_socket ));
626684 return 0 ;
627685 }
628686
@@ -669,12 +727,18 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
669727struct hostent * sal_gethostbyname (const char * name )
670728{
671729 int i ;
730+ struct hostent * hst ;
672731
673732 for (i = 0 ; i < SAL_PROTO_FAMILIES_NUM ; ++ i )
674733 {
675734 if (proto_families [i ].gethostbyname )
676735 {
677- return proto_families [i ].gethostbyname (name );
736+ hst = proto_families [i ].gethostbyname (name );
737+ if (hst != RT_NULL )
738+ {
739+ return hst ;
740+ }
741+ continue ;
678742 }
679743 }
680744
@@ -684,13 +748,18 @@ struct hostent *sal_gethostbyname(const char *name)
684748int sal_gethostbyname_r (const char * name , struct hostent * ret , char * buf ,
685749 size_t buflen , struct hostent * * result , int * h_errnop )
686750{
687- int i ;
751+ int i , res ;
688752
689753 for (i = 0 ; i < SAL_PROTO_FAMILIES_NUM ; ++ i )
690754 {
691755 if (proto_families [i ].gethostbyname_r )
692756 {
693- return proto_families [i ].gethostbyname_r (name , ret , buf , buflen , result , h_errnop );
757+ res = proto_families [i ].gethostbyname_r (name , ret , buf , buflen , result , h_errnop );
758+ if (res == 0 )
759+ {
760+ return res ;
761+ }
762+ continue ;
694763 }
695764 }
696765
@@ -716,13 +785,18 @@ int sal_getaddrinfo(const char *nodename,
716785 const struct addrinfo * hints ,
717786 struct addrinfo * * res )
718787{
719- int i ;
788+ int i , ret ;
720789
721790 for (i = 0 ; i < SAL_PROTO_FAMILIES_NUM ; ++ i )
722791 {
723792 if (proto_families [i ].getaddrinfo )
724793 {
725- return proto_families [i ].getaddrinfo (nodename , servname , hints , res );
794+ ret = proto_families [i ].getaddrinfo (nodename , servname , hints , res );
795+ if (ret == 0 )
796+ {
797+ return ret ;
798+ }
799+ continue ;
726800 }
727801 }
728802
0 commit comments