Skip to content

Commit 192b177

Browse files
committed
[at][sal] Add query and unregistered protocol family functions
1 parent 0d737de commit 192b177

File tree

7 files changed

+159
-32
lines changed

7 files changed

+159
-32
lines changed

components/net/sal_socket/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ if GetDepend('SAL_USING_LWIP'):
1616

1717
if GetDepend('SAL_USING_AT'):
1818
src += Glob('impl/af_inet_at.c')
19+
20+
if GetDepend('SAL_USING_LWIP') or GetDepend('SAL_USING_AT'):
21+
CPPPATH += [cwd + '/impl']
1922

2023
if GetDepend('SAL_USING_POSIX'):
2124
src += Glob('dfs_net/*.c')
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* File : af_inet.h
3+
* This file is part of RT-Thread RTOS
4+
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc.,
18+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Change Logs:
21+
* Date Author Notes
22+
* 2018-08-25 ChenYong First version
23+
*/
24+
25+
#ifndef __AF_INET_H__
26+
#define __AF_INET_H__
27+
28+
#include <rtthread.h>
29+
30+
#ifdef SAL_USING_LWIP
31+
/* lwIP protocol family register */
32+
int lwip_inet_init(void);
33+
#endif
34+
35+
#ifdef SAL_USING_AT
36+
/* AT protocol family register */
37+
int at_inet_init(void);
38+
#endif
39+
40+
#endif /* __AF_INET_H__ */

components/net/sal_socket/impl/af_inet_at.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sal.h>
2727

2828
#include <at_socket.h>
29+
#include <af_inet.h>
2930

3031
#ifdef SAL_USING_POSIX
3132
#include <dfs_poll.h>
@@ -107,6 +108,7 @@ static int at_create(struct sal_socket *socket, int type, int protocol)
107108
}
108109

109110
static const struct proto_family at_inet_family_ops = {
111+
"at",
110112
AF_AT,
111113
AF_INET,
112114
at_create,

components/net/sal_socket/impl/af_inet_lwip.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#endif
3535

3636
#include <sal.h>
37+
#include <af_inet.h>
3738

3839
#if LWIP_VERSION < 0x2000000
3940
#define SELWAIT_T int
@@ -284,6 +285,7 @@ static int inet_create(struct sal_socket *socket, int type, int protocol)
284285
}
285286

286287
static const struct proto_family lwip_inet_family_ops = {
288+
"lwip",
287289
AF_INET,
288290
AF_INET,
289291
inet_create,

components/net/sal_socket/include/sal.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ struct sal_socket
8282

8383
struct proto_family
8484
{
85-
int family; /* primary protocol families type*/
86-
int sec_family; /* secondary protocol families type*/
85+
char name[RT_NAME_MAX];
86+
int family; /* primary protocol families type */
87+
int sec_family; /* secondary protocol families type */
8788
int (*create)(struct sal_socket *sal_socket, int type, int protocol); /* register socket options */
8889

8990
struct hostent* (*gethostbyname) (const char *name);
@@ -92,10 +93,14 @@ struct proto_family
9293
int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
9394
};
9495

95-
/* SAL socket initialization */
96+
/* SAL(Socket Abstraction Layer) initialize */
9697
int sal_init(void);
9798

98-
int sal_proto_family_register(const struct proto_family *pf);
9999
struct sal_socket *sal_get_socket(int sock);
100100

101+
/* protocol family register and unregister operate */
102+
int sal_proto_family_register(const struct proto_family *pf);
103+
int sal_proto_family_unregister(const struct proto_family *pf);
104+
struct proto_family *sal_proto_family_find(const char *name);
105+
101106
#endif /* SAL_H__ */

components/net/sal_socket/socket/net_netdb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int gethostbyname_r(const char *name, struct hostent *ret, char *buf,
3838
{
3939
return sal_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
4040
}
41+
RTM_EXPORT(gethostbyname_r);
4142

4243
void freeaddrinfo(struct addrinfo *ai)
4344
{

components/net/sal_socket/src/sal_socket.c

Lines changed: 102 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static struct rt_mutex sal_core_lock;
5050
static 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)
7676
INIT_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
*/
8686
int 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
*/
128191
struct 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
*/
217280
static 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-
*/
309367
static 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)
669727
struct 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)
684748
int 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

Comments
 (0)