lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_socket.h
1 /******************************************************************************
2  * Copyright 2018 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of lwpa. For more information, go to:
17  * https://github.com/ETCLabs/lwpa
18  ******************************************************************************/
19 
20 /* lwpa_socket.h: Platform-neutral BSD-modeled network socket
21  * implementation. */
22 #ifndef _LWPA_SOCKET_H_
23 #define _LWPA_SOCKET_H_
24 
25 #include <stddef.h>
26 #include "lwpa_common.h"
27 #include "lwpa_bool.h"
28 #include "lwpa_error.h"
29 #include "lwpa_inet.h"
30 #include "lwpa_socket_plat.h"
31 
47 /* clang-format off */
48 
51 #define LWPA_MSG_PEEK 0x1
54 /* Note: no flags are currently implemented for lwpa_sendto() */
55 
59 #define LWPA_SOL_SOCKET 0
60 #define LWPA_IPPROTO_IP 1
61 #define LWPA_IPPROTO_ICMPV6 2
62 #define LWPA_IPPROTO_IPV6 3
63 #define LWPA_IPPROTO_TCP 4
64 #define LWPA_IPPROTO_UDP 5
72 #define LWPA_SO_BROADCAST 0
73 #define LWPA_SO_ERROR 1
75 #define LWPA_SO_KEEPALIVE 2
76 #define LWPA_SO_LINGER 3
77 #define LWPA_SO_RCVBUF 4
79 #define LWPA_SO_SNDBUF 5
81 #define LWPA_SO_RCVTIMEO 6
82 #define LWPA_SO_SNDTIMEO 7
83 #define LWPA_SO_REUSEADDR 8
84 #define LWPA_SO_REUSEPORT 9
85 #define LWPA_SO_TYPE 10
93 #define LWPA_IP_TTL 11
94 #define LWPA_IP_MULTICAST_IF 12
95 #define LWPA_IP_MULTICAST_TTL 13
96 #define LWPA_IP_MULTICAST_LOOP 14
97 #define LWPA_MCAST_JOIN_GROUP 15
98 #define LWPA_MCAST_LEAVE_GROUP 16
101 /* clang-format on */
102 
103 /* LWPA_IPPROTO_TCP: TODO */
104 
106 typedef struct LwpaLinger
107 {
108  int onoff;
109  int linger;
111 
113 typedef struct LwpaMreq
114 {
120 
123 #define LWPA_SHUT_RD 0
124 #define LWPA_SHUT_WR 1
125 #define LWPA_SHUT_RDWR 2
130 #define LWPA_AF_UNSPEC 0
131 #define LWPA_AF_INET 1
132 #define LWPA_AF_INET6 2
137 #define LWPA_STREAM 0
138 #define LWPA_DGRAM 1
141 /********************** Mimic sys/socket.h functions *************************/
142 
143 #ifdef __cplusplus
144 extern "C" {
145 #endif
146 
147 lwpa_error_t lwpa_socket_init(void *platform_data);
148 void lwpa_socket_deinit();
149 
156 lwpa_error_t lwpa_getsockopt(lwpa_socket_t id, int level, int option_name, void *option_value, size_t *option_len);
157 lwpa_error_t lwpa_listen(lwpa_socket_t id, int backlog);
158 int lwpa_recv(lwpa_socket_t id, void *buffer, size_t length, int flags);
159 int lwpa_recvfrom(lwpa_socket_t id, void *buffer, size_t length, int flags, LwpaSockaddr *address);
160 /* recvmsg - not implemented */
161 int lwpa_send(lwpa_socket_t id, const void *message, size_t length, int flags);
162 /* sendmsg - not implemented */
163 int lwpa_sendto(lwpa_socket_t id, const void *message, size_t length, int flags, const LwpaSockaddr *dest_addr);
164 lwpa_error_t lwpa_setsockopt(lwpa_socket_t id, int level, int option_name, const void *option_value, size_t option_len);
166 lwpa_socket_t lwpa_socket(unsigned int family, unsigned int type);
167 /* int protocol - not necessary */
168 /* socketpair - not implemented */
169 
170 /**************************** Mimic fcntl() API ******************************/
171 
172 lwpa_error_t lwpa_setblocking(lwpa_socket_t id, bool blocking);
173 
174 /**************************** Mimic poll() API *******************************/
175 
181 #define LWPA_POLLIN 0x1
182 #define LWPA_POLLOUT 0x2
183 #define LWPA_POLLPRI 0x4
184 #define LWPA_POLLERR 0x8
188 typedef struct LwpaPollfd
189 {
191  short events;
192  short revents;
195 
196 int lwpa_poll(LwpaPollfd *fds, size_t nfds, int timeout_ms);
197 
198 /************************ Mimic getaddrinfo() API ****************************/
199 
205 #define LWPA_AI_PASSIVE 0x01
206 #define LWPA_AI_CANONNAME 0x02
207 #define LWPA_AI_NUMERICHOST 0x04
212 typedef struct LwpaAddrinfo
213 {
214  int ai_flags;
215  int ai_family;
218  char *ai_canonname;
220  void *pd[2];
223 
224 lwpa_error_t lwpa_getaddrinfo(const char *hostname, const char *service, const LwpaAddrinfo *hints,
225  LwpaAddrinfo *result);
226 
227 bool lwpa_nextaddr(LwpaAddrinfo *ai);
228 
229 /* Call with any of the LwpaAddrinfos in the list to free the whole
230  * list */
232 
233 /************************* Mimic inet_xtox() API *****************************/
234 
236 #define LWPA_INET_ADDRSTRLEN 16
238 #define LWPA_INET6_ADDRSTRLEN 46
239 
240 lwpa_error_t lwpa_inet_ntop(const LwpaIpAddr *src, char *dest, size_t size);
241 lwpa_error_t lwpa_inet_pton(lwpa_iptype_t type, const char *src, LwpaIpAddr *dest);
242 
243 #ifdef __cplusplus
244 }
245 #endif
246 
249 #endif /* _LWPA_SOCKET_H_ */
lwpa_error_t
A set of error codes that can be returned by library functions.
Definition: lwpa_error.h:37
lwpa_iptype_t
Used to identify the type of IP address contained in a LwpaIpAddr.
Definition: lwpa_inet.h:40
lwpa_socket_t lwpa_socket(unsigned int family, unsigned int type)
Create a socket.
Definition: lwpa_socket.c:437
lwpa_error_t lwpa_getsockname(lwpa_socket_t id, LwpaSockaddr *address)
Get socket name.
Definition: lwpa_socket.c:223
struct LwpaPollfd LwpaPollfd
A description for a socket to poll with lwpa_poll().
struct LwpaAddrinfo LwpaAddrinfo
A structure containing name and address information about an internet host.
lwpa_error_t lwpa_inet_pton(lwpa_iptype_t type, const char *src, LwpaIpAddr *dest)
Convert IPv4 and IPv6 addresses from text to binary form.
Definition: lwpa_socket.c:614
lwpa_error_t lwpa_getpeername(lwpa_socket_t id, LwpaSockaddr *address)
Get the name of the connected peer socket.
Definition: lwpa_socket.c:217
void lwpa_socket_deinit()
Deinitialize the lwpa_socket module.
Definition: lwpa_socket.c:148
lwpa_error_t lwpa_socket_init(void *platform_data)
Initialize the lwpa_socket module.
Definition: lwpa_socket.c:141
lwpa_error_t lwpa_getsockopt(lwpa_socket_t id, int level, int option_name, void *option_value, size_t *option_len)
Get an option value for a socket.
Definition: lwpa_socket.c:241
lwpa_error_t lwpa_getaddrinfo(const char *hostname, const char *service, const LwpaAddrinfo *hints, LwpaAddrinfo *result)
Get address information for a named internet host and/or service.
Definition: lwpa_socket.c:515
lwpa_error_t lwpa_connect(lwpa_socket_t id, const LwpaSockaddr *address)
Initiate a connection on a socket.
Definition: lwpa_socket.c:211
lwpa_error_t lwpa_bind(lwpa_socket_t id, const LwpaSockaddr *address)
Bind a name to a socket.
Definition: lwpa_socket.c:191
lwpa_error_t lwpa_setblocking(lwpa_socket_t id, bool blocking)
Change the blocking behavior of a socket.
Definition: lwpa_socket.c:469
void lwpa_freeaddrinfo(LwpaAddrinfo *ai)
Free an addrinfo list returned by lwpa_getaddrinfo().
Definition: lwpa_socket.c:580
int lwpa_recv(lwpa_socket_t id, void *buffer, size_t length, int flags)
Receive data on a connected socket.
Definition: lwpa_socket.c:253
UNDEFINED lwpa_socket_t
A socket handle.
Definition: lwpa_socket.dox:10
struct LwpaMreq LwpaMreq
Option value for LWPA_MCAST_JOIN_GROUP and LWPA_MCAST_LEAVE_GROUP.
int lwpa_recvfrom(lwpa_socket_t id, void *buffer, size_t length, int flags, LwpaSockaddr *address)
Receive data on a socket.
Definition: lwpa_socket.c:259
lwpa_error_t lwpa_close(lwpa_socket_t id)
Close a socket.
Definition: lwpa_socket.c:206
lwpa_error_t lwpa_listen(lwpa_socket_t id, int backlog)
Listen for connections on a socket.
Definition: lwpa_socket.c:247
lwpa_error_t lwpa_accept(lwpa_socket_t id, LwpaSockaddr *address, lwpa_socket_t *conn_sock)
Accept a connection on a socket.
Definition: lwpa_socket.c:185
lwpa_error_t lwpa_inet_ntop(const LwpaIpAddr *src, char *dest, size_t size)
Convert IPv4 and IPv6 addresses from binary to text form.
Definition: lwpa_socket.c:586
lwpa_error_t lwpa_setsockopt(lwpa_socket_t id, int level, int option_name, const void *option_value, size_t option_len)
Set an option value on a socket.
Definition: lwpa_socket.c:304
int lwpa_sendto(lwpa_socket_t id, const void *message, size_t length, int flags, const LwpaSockaddr *dest_addr)
Send data on a socket.
Definition: lwpa_socket.c:288
int lwpa_send(lwpa_socket_t id, const void *message, size_t length, int flags)
Send data on a connected socket.
Definition: lwpa_socket.c:282
lwpa_error_t lwpa_shutdown(lwpa_socket_t id, int how)
Shut down part of a full-duplex connection.
Definition: lwpa_socket.c:449
struct LwpaLinger LwpaLinger
Option value for LWPA_SO_LINGER.
int lwpa_poll(LwpaPollfd *fds, size_t nfds, int timeout_ms)
Wait for some event on a set of sockets.
Definition: lwpa_socket.c:448
bool lwpa_nextaddr(LwpaAddrinfo *ai)
Get the next address from the list returned by lwpa_getaddrinfo().
Definition: lwpa_socket.c:544
A structure containing name and address information about an internet host.
Definition: lwpa_socket.h:213
LwpaSockaddr ai_addr
Address of host.
Definition: lwpa_socket.h:219
int ai_socktype
i.e.
Definition: lwpa_socket.h:216
int ai_family
i.e.
Definition: lwpa_socket.h:215
int ai_protocol
i.e.
Definition: lwpa_socket.h:217
int ai_flags
i.e.
Definition: lwpa_socket.h:214
char * ai_canonname
Canonical name for host.
Definition: lwpa_socket.h:218
void * pd[2]
Used by internal platform logic; don't touch.
Definition: lwpa_socket.h:220
An IP address.
Definition: lwpa_inet.h:55
Option value for LWPA_SO_LINGER.
Definition: lwpa_socket.h:107
int linger
Linger time in seconds.
Definition: lwpa_socket.h:109
int onoff
0 = off, nonzero = on
Definition: lwpa_socket.h:108
Option value for LWPA_MCAST_JOIN_GROUP and LWPA_MCAST_LEAVE_GROUP.
Definition: lwpa_socket.h:114
LwpaIpAddr group
Multicast group to join.
Definition: lwpa_socket.h:118
LwpaIpAddr netint
Address of network interface on which to join the multicast group.
Definition: lwpa_socket.h:116
A description for a socket to poll with lwpa_poll().
Definition: lwpa_socket.h:189
lwpa_error_t err
Any error that occurred on the socket.
Definition: lwpa_socket.h:193
short events
Events to poll for.
Definition: lwpa_socket.h:191
lwpa_socket_t fd
Socket to poll.
Definition: lwpa_socket.h:190
short revents
Events that occurred on the socket.
Definition: lwpa_socket.h:192
An IP address with associated interface and port.
Definition: lwpa_inet.h:176