xref: /nrf52832-nimble/rt-thread/components/net/sal_socket/include/sal.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-05-17     ChenYong     First version
9  */
10 
11 #ifndef SAL_H__
12 #define SAL_H__
13 
14 #include <rtdevice.h>
15 
16 #ifdef SAL_USING_POSIX
17 #include <dfs_file.h>
18 #endif
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
25 typedef uint32_t socklen_t;
26 #endif
27 
28 /* SAL socket magic word */
29 #define SAL_SOCKET_MAGIC               0x5A10
30 
31 /* The maximum number of sockets structure */
32 #ifndef SAL_SOCKETS_NUM
33 #define SAL_SOCKETS_NUM                DFS_FD_MAX
34 #endif
35 
36 /* The maximum number of protocol families */
37 #ifndef SAL_PROTO_FAMILIES_NUM
38 #define SAL_PROTO_FAMILIES_NUM         4
39 #endif
40 
41 /* SAL socket offset */
42 #ifndef SAL_SOCKET_OFFSET
43 #define SAL_SOCKET_OFFSET              0
44 #endif
45 
46 struct sal_socket_ops
47 {
48     int (*socket)     (int domain, int type, int protocol);
49     int (*closesocket)(int s);
50     int (*bind)       (int s, const struct sockaddr *name, socklen_t namelen);
51     int (*listen)     (int s, int backlog);
52     int (*connect)    (int s, const struct sockaddr *name, socklen_t namelen);
53     int (*accept)     (int s, struct sockaddr *addr, socklen_t *addrlen);
54     int (*sendto)     (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
55     int (*recvfrom)   (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
56     int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
57     int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
58     int (*shutdown)   (int s, int how);
59     int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
60     int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
61     int (*ioctlsocket)(int s, long cmd, void *arg);
62 #ifdef SAL_USING_POSIX
63     int (*poll)       (struct dfs_fd *file, struct rt_pollreq *req);
64 #endif
65 };
66 
67 struct sal_proto_ops
68 {
69     struct hostent* (*gethostbyname)  (const char *name);
70     int             (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
71     int             (*getaddrinfo)    (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
72     void            (*freeaddrinfo)   (struct addrinfo *ai);
73 };
74 
75 struct sal_socket
76 {
77     uint32_t magic;                    /* SAL socket magic word */
78 
79     int socket;                        /* SAL socket descriptor */
80     int domain;
81     int type;
82     int protocol;
83 
84     const struct sal_socket_ops *ops;  /* socket options */
85 
86     void *user_data;                   /* user-specific data */
87 #ifdef SAL_USING_TLS
88     void *user_data_tls;               /* user-specific TLS data */
89 #endif
90 };
91 
92 struct sal_proto_family
93 {
94     int family;                        /* primary protocol families type */
95     int sec_family;                    /* secondary protocol families type */
96     int (*create)(struct sal_socket *sal_socket, int type, int protocol);   /* register socket options */
97 
98     struct sal_proto_ops *ops;             /* protocol family options */
99 };
100 
101 /* SAL(Socket Abstraction Layer) initialize */
102 int sal_init(void);
103 
104 struct sal_socket *sal_get_socket(int sock);
105 
106 /* SAL protocol family register and unregister operate */
107 int sal_proto_family_register(const struct sal_proto_family *pf);
108 int sal_proto_family_unregister(int family);
109 rt_bool_t sal_proto_family_is_registered(int family);
110 struct sal_proto_family *sal_proto_family_find(int family);
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* SAL_H__ */
117