EtcPal  HEAD (unstable)
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 #define ETCPAL_IP_PKTINFO 20
139 
144 #define ETCPAL_IPV6_PKTINFO 21
145 
150 /* clang-format on */
151 
152 /* ETCPAL_IPPROTO_TCP: TODO */
153 
155 typedef struct EtcPalLinger
156 {
157  int onoff;
158  int linger;
160 
162 typedef struct EtcPalMreq
163 {
169 
171 typedef struct EtcPalGroupReq
172 {
175  unsigned int ifindex;
179 
181 typedef struct EtcPalMsgHdr
182 {
184  void* buf;
185  size_t buflen;
186  void* control;
187  size_t controllen;
188  int flags;
190 
192 typedef struct EtcPalCMsgHdr
193 {
194  bool valid;
195  size_t len;
196  int level;
197  int type;
198  void* pd;
200 
202 #define ETCPAL_CMSG_IS_VALID(cmsg) ((cmsg)->valid)
203 
205 typedef struct EtcPalPktInfo
206 {
208  unsigned int ifindex;
210 
215 #define ETCPAL_SHUT_RD 0
216 #define ETCPAL_SHUT_WR 1
217 #define ETCPAL_SHUT_RDWR 2
226 #define ETCPAL_AF_UNSPEC 0u
227 #define ETCPAL_AF_INET 1u
228 #define ETCPAL_AF_INET6 2u
237 #define ETCPAL_SOCK_STREAM 0u
238 #define ETCPAL_SOCK_DGRAM 1u
247 #define ETCPAL_MSG_TRUNC 0x2
248 #define ETCPAL_MSG_CTRUNC 0x4
258 #define ETCPAL_CONTROL_SIZE_IP_PKTINFO ETCPAL_PLATFORM_IN_PKTINFO_SPACE
259 
265 #define ETCPAL_CONTROL_SIZE_IPV6_PKTINFO ETCPAL_PLATFORM_IN6_PKTINFO_SPACE
266 
272 #define ETCPAL_MAX_CONTROL_SIZE_PKTINFO \
273  (ETCPAL_CONTROL_SIZE_IP_PKTINFO > ETCPAL_CONTROL_SIZE_IPV6_PKTINFO ? ETCPAL_CONTROL_SIZE_IP_PKTINFO \
274  : ETCPAL_CONTROL_SIZE_IPV6_PKTINFO)
275 
280 /********************** Mimic sys/socket.h functions *************************/
281 
282 #ifdef __cplusplus
283 extern "C" {
284 #endif
285 
293  int level,
294  int option_name,
295  void* option_value,
296  size_t* option_len);
298 int etcpal_recv(etcpal_socket_t id, void* buffer, size_t length, int flags);
299 int etcpal_recvfrom(etcpal_socket_t id, void* buffer, size_t length, int flags, EtcPalSockAddr* address);
300 int etcpal_recvmsg(etcpal_socket_t id, EtcPalMsgHdr* msg, int flags);
302 bool etcpal_cmsg_nxthdr(EtcPalMsgHdr* msgh, const EtcPalCMsgHdr* cmsg, EtcPalCMsgHdr* nxthdr);
304 int etcpal_send(etcpal_socket_t id, const void* message, size_t length, int flags);
305 /* sendmsg - not implemented */
306 int etcpal_sendto(etcpal_socket_t id, const void* message, size_t length, int flags, const EtcPalSockAddr* dest_addr);
308  int level,
309  int option_name,
310  const void* option_value,
311  size_t option_len);
313 etcpal_error_t etcpal_socket(unsigned int family, unsigned int type, etcpal_socket_t* id);
314 /* socketpair - not implemented */
315 
316 /**************************** Mimic fcntl() API ******************************/
317 
320 
321 /**************************** Mimic poll() API *******************************/
322 
327 #define ETCPAL_POLL_IN 0x1u
328 #define ETCPAL_POLL_OUT 0x2u
329 #define ETCPAL_POLL_CONNECT 0x4u
330 #define ETCPAL_POLL_OOB 0x8u
331 #define ETCPAL_POLL_ERR 0x10u
337 #define ETCPAL_POLL_VALID_INPUT_EVENT_MASK 0x0fu
338 
340 typedef struct EtcPalPollEvent
341 {
345  void* user_data;
347 
351  etcpal_socket_t socket,
352  etcpal_poll_events_t events,
353  void* user_data);
355  etcpal_socket_t socket,
356  etcpal_poll_events_t new_events,
357  void* new_user_data);
360 
361 /************************ Mimic getaddrinfo() API ****************************/
362 
368 #define ETCPAL_AI_PASSIVE 0x01
369 #define ETCPAL_AI_CANONNAME 0x02
370 #define ETCPAL_AI_NUMERICHOST 0x04
377 typedef struct EtcPalAddrinfo
378 {
379  int ai_flags;
380  int ai_family;
383  char* ai_canonname;
385  void* pd[2];
387 
388 etcpal_error_t etcpal_getaddrinfo(const char* hostname,
389  const char* service,
390  const EtcPalAddrinfo* hints,
391  EtcPalAddrinfo* result);
392 
394 
395 /* Call with any of the EtcPalAddrinfos in the list to free the whole list */
397 
398 #ifdef __cplusplus
399 }
400 #endif
401 
406 #endif /* ETCPAL_SOCKET_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
bool etcpal_cmsg_firsthdr(EtcPalMsgHdr *msgh, EtcPalCMsgHdr *firsthdr)
Get the first control (ancillary) message associated with the passed in message.
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.
bool etcpal_cmsg_nxthdr(EtcPalMsgHdr *msgh, const EtcPalCMsgHdr *cmsg, EtcPalCMsgHdr *nxthdr)
Get the next valid control (ancillary) message after the passed in control message.
struct EtcPalCMsgHdr EtcPalCMsgHdr
Ancillary data received from etcpal_recvmsg.
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.
struct EtcPalPktInfo EtcPalPktInfo
Information about a datagram packet received with etcpal_recvmsg.
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:349
struct EtcPalMsgHdr EtcPalMsgHdr
Message data received from etcpal_recvmsg.
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.
bool etcpal_cmsg_to_pktinfo(const EtcPalCMsgHdr *cmsg, EtcPalPktInfo *pktinfo)
Convert a control (ancillary) message to EtcPalPktInfo.
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.
int etcpal_recvmsg(etcpal_socket_t id, EtcPalMsgHdr *msg, int flags)
Receive both datagram and ancillary 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:378
EtcPalSockAddr ai_addr
Address of host.
Definition: socket.h:384
int ai_socktype
i.e.
Definition: socket.h:381
int ai_family
i.e.
Definition: socket.h:380
int ai_protocol
i.e.
Definition: socket.h:382
int ai_flags
i.e.
Definition: socket.h:379
char * ai_canonname
Canonical name for host.
Definition: socket.h:383
void * pd[2]
Used by internal platform logic; don't touch.
Definition: socket.h:385
Ancillary data received from etcpal_recvmsg.
Definition: socket.h:193
bool valid
Whether or not this represents a valid cmsg.
Definition: socket.h:194
size_t len
Length of the ancillary data.
Definition: socket.h:195
void * pd
Used by internal platform logic; don't touch.
Definition: socket.h:198
int type
Protocol-specific type.
Definition: socket.h:197
int level
Originating protocol.
Definition: socket.h:196
Option value for ETCPAL_MCAST_JOIN_GROUP and ETCPAL_MCAST_LEAVE_GROUP.
Definition: socket.h:172
unsigned int ifindex
Index of network interface on which to join the multicast group (see Network Interface Indexes).
Definition: socket.h:175
EtcPalIpAddr group
Multicast group to join.
Definition: socket.h:177
An IP address.
Definition: inet.h:75
Option value for ETCPAL_SO_LINGER.
Definition: socket.h:156
int linger
Linger time in seconds.
Definition: socket.h:158
int onoff
0 = off, nonzero = on
Definition: socket.h:157
Option value for ETCPAL_IP_ADD_MEMBERSHIP and ETCPAL_IP_DROP_MEMBERSHIP.
Definition: socket.h:163
EtcPalIpAddr group
Multicast group to join.
Definition: socket.h:167
EtcPalIpAddr netint
Address of network interface on which to join the multicast group.
Definition: socket.h:165
Message data received from etcpal_recvmsg.
Definition: socket.h:182
EtcPalSockAddr name
The address of the sender (set by etcpal_recvmsg)
Definition: socket.h:183
void * buf
Packet data (allocated by the caller, filled in by etcpal_recvmsg)
Definition: socket.h:184
void * control
Ancillary data (allocated by the caller, filled in by etcpal_recvmsg)
Definition: socket.h:186
int flags
Flags on received message (set by etcpal_recvmsg)
Definition: socket.h:188
size_t controllen
Ancillary data buffer length (set by the caller)
Definition: socket.h:187
size_t buflen
Packet data buffer length (set by the caller)
Definition: socket.h:185
Information about a datagram packet received with etcpal_recvmsg.
Definition: socket.h:206
unsigned int ifindex
The index of the interface the packet was received on.
Definition: socket.h:208
EtcPalIpAddr addr
The destination address of the packet.
Definition: socket.h:207
A description of an event that occurred on a socket, for usage with etcpal_poll_wait().
Definition: socket.h:341
void * user_data
The user data that was given when this socket was added.
Definition: socket.h:345
etcpal_error_t err
More information about an error that occurred on the socket.
Definition: socket.h:344
etcpal_poll_events_t events
Event(s) that occurred on the socket.
Definition: socket.h:343
etcpal_socket_t socket
Socket which had activity.
Definition: socket.h:342
An IP address with associated interface and port.
Definition: inet.h:311