EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
socket.h
1 /******************************************************************************
2  * Copyright 2022 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 EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/socket.h: Platform-neutral BSD-modeled network socket implementation. */
21 
22 #ifndef ETCPAL_SOCKET_H_
23 #define ETCPAL_SOCKET_H_
24 
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include "etcpal/common.h"
28 #include "etcpal/error.h"
29 #include "etcpal/inet.h"
30 
57 typedef uint32_t etcpal_poll_events_t;
58 
59 #include "etcpal/os_socket.h" /* The os-specific socket definitions */
60 
61 /* clang-format off */
62 
67 #define ETCPAL_MSG_PEEK 0x1
72 /* Note: no flags are currently implemented for etcpal_sendto() */
73 
79 #define ETCPAL_SOL_SOCKET 0
80 #define ETCPAL_IPPROTO_IP 1
81 #define ETCPAL_IPPROTO_ICMPV6 2
82 #define ETCPAL_IPPROTO_IPV6 3
83 #define ETCPAL_IPPROTO_TCP 4
84 #define ETCPAL_IPPROTO_UDP 5
95 #define ETCPAL_SO_BROADCAST 0
96 #define ETCPAL_SO_ERROR 1
97 #define ETCPAL_SO_KEEPALIVE 2
98 #define ETCPAL_SO_LINGER 3
99 #define ETCPAL_SO_RCVBUF 4
100 #define ETCPAL_SO_SNDBUF 5
101 #define ETCPAL_SO_RCVTIMEO 6
102 #define ETCPAL_SO_SNDTIMEO 7
103 #define ETCPAL_SO_REUSEADDR 8
104 #define ETCPAL_SO_REUSEPORT 9
105 #define ETCPAL_SO_TYPE 10
118 #define ETCPAL_IP_TTL 11
120 #define ETCPAL_IP_MULTICAST_IF 12
122 #define ETCPAL_IP_MULTICAST_TTL 13
123 #define ETCPAL_IP_MULTICAST_LOOP 14
126 #define ETCPAL_IP_ADD_MEMBERSHIP 15
129 #define ETCPAL_IP_DROP_MEMBERSHIP 16
130 #define ETCPAL_MCAST_JOIN_GROUP 17
131 #define ETCPAL_MCAST_LEAVE_GROUP 18
132 #define ETCPAL_IPV6_V6ONLY 19
138 /* clang-format on */
139 
140 /* ETCPAL_IPPROTO_TCP: TODO */
141 
143 typedef struct EtcPalLinger
144 {
145  int onoff;
146  int linger;
148 
150 typedef struct EtcPalMreq
151 {
157 
159 typedef struct EtcPalGroupReq
160 {
163  unsigned int ifindex;
167 
172 #define ETCPAL_SHUT_RD 0
173 #define ETCPAL_SHUT_WR 1
174 #define ETCPAL_SHUT_RDWR 2
183 #define ETCPAL_AF_UNSPEC 0u
184 #define ETCPAL_AF_INET 1u
185 #define ETCPAL_AF_INET6 2u
194 #define ETCPAL_SOCK_STREAM 0u
195 #define ETCPAL_SOCK_DGRAM 1u
200 /********************** Mimic sys/socket.h functions *************************/
201 
202 #ifdef __cplusplus
203 extern "C" {
204 #endif
205 
213  int level,
214  int option_name,
215  void* option_value,
216  size_t* option_len);
218 int etcpal_recv(etcpal_socket_t id, void* buffer, size_t length, int flags);
219 int etcpal_recvfrom(etcpal_socket_t id, void* buffer, size_t length, int flags, EtcPalSockAddr* address);
220 /* recvmsg - not implemented */
221 int etcpal_send(etcpal_socket_t id, const void* message, size_t length, int flags);
222 /* sendmsg - not implemented */
223 int etcpal_sendto(etcpal_socket_t id, const void* message, size_t length, int flags, const EtcPalSockAddr* dest_addr);
225  int level,
226  int option_name,
227  const void* option_value,
228  size_t option_len);
230 etcpal_error_t etcpal_socket(unsigned int family, unsigned int type, etcpal_socket_t* id);
231 /* socketpair - not implemented */
232 
233 /**************************** Mimic fcntl() API ******************************/
234 
237 
238 /**************************** Mimic poll() API *******************************/
239 
244 #define ETCPAL_POLL_IN 0x1u
245 #define ETCPAL_POLL_OUT 0x2u
246 #define ETCPAL_POLL_CONNECT 0x4u
247 #define ETCPAL_POLL_OOB 0x8u
248 #define ETCPAL_POLL_ERR 0x10u
254 #define ETCPAL_POLL_VALID_INPUT_EVENT_MASK 0x0fu
255 
257 typedef struct EtcPalPollEvent
258 {
262  void* user_data;
264 
268  etcpal_socket_t socket,
269  etcpal_poll_events_t events,
270  void* user_data);
272  etcpal_socket_t socket,
273  etcpal_poll_events_t new_events,
274  void* new_user_data);
277 
278 /************************ Mimic getaddrinfo() API ****************************/
279 
285 #define ETCPAL_AI_PASSIVE 0x01
286 #define ETCPAL_AI_CANONNAME 0x02
287 #define ETCPAL_AI_NUMERICHOST 0x04
294 typedef struct EtcPalAddrinfo
295 {
296  int ai_flags;
297  int ai_family;
300  char* ai_canonname;
302  void* pd[2];
304 
305 etcpal_error_t etcpal_getaddrinfo(const char* hostname,
306  const char* service,
307  const EtcPalAddrinfo* hints,
308  EtcPalAddrinfo* result);
309 
311 
312 /* Call with any of the EtcPalAddrinfos in the list to free the whole list */
314 
315 #ifdef __cplusplus
316 }
317 #endif
318 
323 #endif /* ETCPAL_SOCKET_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
struct EtcPalGroupReq EtcPalGroupReq
Option value for ETCPAL_MCAST_JOIN_GROUP and ETCPAL_MCAST_LEAVE_GROUP.
void etcpal_poll_context_deinit(EtcPalPollContext *context)
Invalidate a previously-initialized EtcPalPollContext structure.
etcpal_error_t etcpal_setsockopt(etcpal_socket_t id, int level, int option_name, const void *option_value, size_t option_len)
Set an option value on a socket.
int etcpal_recvfrom(etcpal_socket_t id, void *buffer, size_t length, int flags, EtcPalSockAddr *address)
Receive data on a socket.
etcpal_error_t etcpal_getsockname(etcpal_socket_t id, EtcPalSockAddr *address)
Get socket name.
struct EtcPalLinger EtcPalLinger
Option value for ETCPAL_SO_LINGER.
etcpal_error_t etcpal_poll_modify_socket(EtcPalPollContext *context, etcpal_socket_t socket, etcpal_poll_events_t new_events, void *new_user_data)
Change the set of events or user data associated with a monitored socket.
etcpal_error_t etcpal_listen(etcpal_socket_t id, int backlog)
Listen for connections on a socket.
etcpal_error_t etcpal_shutdown(etcpal_socket_t id, int how)
Shut down part of a full-duplex connection.
int etcpal_send(etcpal_socket_t id, const void *message, size_t length, int flags)
Send data on a connected socket.
etcpal_error_t etcpal_getaddrinfo(const char *hostname, const char *service, const EtcPalAddrinfo *hints, EtcPalAddrinfo *result)
Get address information for a named internet host and/or service.
uint32_t etcpal_poll_events_t
Event flags for the etcpal_poll_*() API functions.
Definition: socket.h:57
etcpal_error_t etcpal_getsockopt(etcpal_socket_t id, int level, int option_name, void *option_value, size_t *option_len)
Get an option value for a socket.
bool etcpal_nextaddr(EtcPalAddrinfo *ai)
Get the next address from the list returned by etcpal_getaddrinfo().
etcpal_error_t etcpal_getblocking(etcpal_socket_t id, bool *blocking)
Get the current blocking behavior of a socket.
etcpal_error_t etcpal_setblocking(etcpal_socket_t id, bool blocking)
Change the blocking behavior of a socket.
void etcpal_poll_remove_socket(EtcPalPollContext *context, etcpal_socket_t socket)
Remove a monitored socket from an EtcPalPollContext.
etcpal_error_t etcpal_bind(etcpal_socket_t id, const EtcPalSockAddr *address)
Bind a name to a socket.
etcpal_error_t etcpal_accept(etcpal_socket_t id, EtcPalSockAddr *address, etcpal_socket_t *conn_sock)
Accept a connection on a socket.
void etcpal_freeaddrinfo(EtcPalAddrinfo *ai)
Free an addrinfo list returned by etcpal_getaddrinfo().
PLATFORM_DEFINED EtcPalPollContext
A structure that holds context information for poll operations on multiple sockets.
Definition: socket.dox:285
etcpal_error_t etcpal_poll_add_socket(EtcPalPollContext *context, etcpal_socket_t socket, etcpal_poll_events_t events, void *user_data)
Add a new socket to an EtcPalPollContext.
etcpal_error_t etcpal_connect(etcpal_socket_t id, const EtcPalSockAddr *address)
Initiate a connection on a socket.
struct EtcPalPollEvent EtcPalPollEvent
A description of an event that occurred on a socket, for usage with etcpal_poll_wait().
etcpal_error_t etcpal_close(etcpal_socket_t id)
Close a socket.
int etcpal_recv(etcpal_socket_t id, void *buffer, size_t length, int flags)
Receive data on a connected socket.
etcpal_error_t etcpal_socket(unsigned int family, unsigned int type, etcpal_socket_t *id)
Create a socket.
etcpal_error_t etcpal_poll_wait(EtcPalPollContext *context, EtcPalPollEvent *event, int timeout_ms)
Wait for an event on a set of sockets defined by an EtcPalPollContext.
etcpal_error_t etcpal_getpeername(etcpal_socket_t id, EtcPalSockAddr *address)
Get the name of the connected peer socket.
struct EtcPalAddrinfo EtcPalAddrinfo
A structure containing name and address information about an internet host.
PLATFORM_DEFINED etcpal_socket_t
A socket handle.
Definition: socket.dox:11
int etcpal_sendto(etcpal_socket_t id, const void *message, size_t length, int flags, const EtcPalSockAddr *dest_addr)
Send data on a socket.
etcpal_error_t etcpal_poll_context_init(EtcPalPollContext *context)
Create a new context for waiting on events on multiple sockets.
struct EtcPalMreq EtcPalMreq
Option value for ETCPAL_IP_ADD_MEMBERSHIP and ETCPAL_IP_DROP_MEMBERSHIP.
A structure containing name and address information about an internet host.
Definition: socket.h:295
EtcPalSockAddr ai_addr
Address of host.
Definition: socket.h:301
int ai_socktype
i.e.
Definition: socket.h:298
int ai_family
i.e.
Definition: socket.h:297
int ai_protocol
i.e.
Definition: socket.h:299
int ai_flags
i.e.
Definition: socket.h:296
char * ai_canonname
Canonical name for host.
Definition: socket.h:300
void * pd[2]
Used by internal platform logic; don't touch.
Definition: socket.h:302
Option value for ETCPAL_MCAST_JOIN_GROUP and ETCPAL_MCAST_LEAVE_GROUP.
Definition: socket.h:160
unsigned int ifindex
Index of network interface on which to join the multicast group (see Network Interface Indexes).
Definition: socket.h:163
EtcPalIpAddr group
Multicast group to join.
Definition: socket.h:165
An IP address.
Definition: inet.h:75
Option value for ETCPAL_SO_LINGER.
Definition: socket.h:144
int linger
Linger time in seconds.
Definition: socket.h:146
int onoff
0 = off, nonzero = on
Definition: socket.h:145
Option value for ETCPAL_IP_ADD_MEMBERSHIP and ETCPAL_IP_DROP_MEMBERSHIP.
Definition: socket.h:151
EtcPalIpAddr group
Multicast group to join.
Definition: socket.h:155
EtcPalIpAddr netint
Address of network interface on which to join the multicast group.
Definition: socket.h:153
A description of an event that occurred on a socket, for usage with etcpal_poll_wait().
Definition: socket.h:258
void * user_data
The user data that was given when this socket was added.
Definition: socket.h:262
etcpal_error_t err
More information about an error that occurred on the socket.
Definition: socket.h:261
etcpal_poll_events_t events
Event(s) that occurred on the socket.
Definition: socket.h:260
etcpal_socket_t socket
Socket which had activity.
Definition: socket.h:259
An IP address with associated interface and port.
Definition: inet.h:311