1 /**
2  * Copyright (c) 2023-2024 Marcin Niestroj
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __DRIVERS_NET_NSOS_H__
8 #define __DRIVERS_NET_NSOS_H__
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 /* Protocol families. */
14 #define NSOS_MID_PF_UNSPEC       0          /**< Unspecified protocol family.  */
15 #define NSOS_MID_PF_INET         1          /**< IP protocol family version 4. */
16 #define NSOS_MID_PF_INET6        2          /**< IP protocol family version 6. */
17 
18 /* Address families. */
19 #define NSOS_MID_AF_UNSPEC      NSOS_MID_PF_UNSPEC   /**< Unspecified address family.   */
20 #define NSOS_MID_AF_INET        NSOS_MID_PF_INET     /**< IP protocol family version 4. */
21 #define NSOS_MID_AF_INET6       NSOS_MID_PF_INET6    /**< IP protocol family version 6. */
22 
23 /** Protocol numbers from IANA/BSD */
24 enum nsos_mid_net_ip_protocol {
25 	NSOS_MID_IPPROTO_IP = 0,            /**< IP protocol (pseudo-val for setsockopt() */
26 	NSOS_MID_IPPROTO_ICMP = 1,          /**< ICMP protocol   */
27 	NSOS_MID_IPPROTO_IGMP = 2,          /**< IGMP protocol   */
28 	NSOS_MID_IPPROTO_IPIP = 4,          /**< IPIP tunnels    */
29 	NSOS_MID_IPPROTO_TCP = 6,           /**< TCP protocol    */
30 	NSOS_MID_IPPROTO_UDP = 17,          /**< UDP protocol    */
31 	NSOS_MID_IPPROTO_IPV6 = 41,         /**< IPv6 protocol   */
32 	NSOS_MID_IPPROTO_ICMPV6 = 58,       /**< ICMPv6 protocol */
33 	NSOS_MID_IPPROTO_RAW = 255,         /**< RAW IP packets  */
34 };
35 
36 /** Socket type */
37 enum nsos_mid_net_sock_type {
38 	NSOS_MID_SOCK_STREAM = 1,           /**< Stream socket type   */
39 	NSOS_MID_SOCK_DGRAM,                /**< Datagram socket type */
40 	NSOS_MID_SOCK_RAW                   /**< RAW socket type      */
41 };
42 
43 #define NSOS_MID_MSG_PEEK 0x02
44 #define NSOS_MID_MSG_TRUNC 0x20
45 #define NSOS_MID_MSG_DONTWAIT 0x40
46 #define NSOS_MID_MSG_WAITALL 0x100
47 
48 struct nsos_mid_sockaddr {
49 	uint16_t sa_family;      /* Address family */
50 	char     sa_data[];      /* Socket address */
51 };
52 
53 struct nsos_mid_sockaddr_in {
54 	uint16_t sin_family;     /* AF_INET */
55 	uint16_t sin_port;       /* Port number */
56 	uint32_t sin_addr;       /* IPv4 address */
57 };
58 
59 struct nsos_mid_sockaddr_in6 {
60 	uint16_t sin6_family;    /* AF_INET6 */
61 	uint16_t sin6_port;      /* Port number */
62 	uint8_t  sin6_addr[16];
63 	uint32_t sin6_scope_id;  /* Set of interfaces for a scope */
64 };
65 
66 struct nsos_mid_sockaddr_storage {
67 	union {
68 		struct nsos_mid_sockaddr_in sockaddr_in;
69 		struct nsos_mid_sockaddr_in6 sockaddr_in6;
70 	};
71 };
72 
73 struct nsos_mid_pollfd {
74 	int fd;
75 	short events;
76 	short revents;
77 
78 	void (*cb)(struct nsos_mid_pollfd *pollfd_mid);
79 };
80 
81 struct nsos_mid_addrinfo {
82 	int                       ai_flags;
83 	int                       ai_family;
84 	int                       ai_socktype;
85 	int                       ai_protocol;
86 	size_t                    ai_addrlen;
87 	struct nsos_mid_sockaddr *ai_addr;
88 	char                     *ai_canonname;
89 	struct nsos_mid_addrinfo *ai_next;
90 };
91 
92 struct nsos_mid_iovec {
93 	void  *iov_base;
94 	size_t iov_len;
95 };
96 
97 struct nsos_mid_msghdr {
98 	void                  *msg_name;       /* optional socket address, big endian */
99 	size_t                 msg_namelen;    /* size of socket address */
100 	struct nsos_mid_iovec *msg_iov;        /* scatter/gather array */
101 	size_t                 msg_iovlen;     /* number of elements in msg_iov */
102 	void                  *msg_control;    /* ancillary data */
103 	size_t                 msg_controllen; /* ancillary data buffer len */
104 	int                    msg_flags;      /* flags on received message */
105 };
106 
nsos_socket_flag_convert(int * flags_a,int flag_a,int * flags_b,int flag_b)107 static inline void nsos_socket_flag_convert(int *flags_a, int flag_a,
108 					    int *flags_b, int flag_b)
109 {
110 	if ((*flags_a) & flag_a) {
111 		*flags_a &= ~flag_a;
112 		*flags_b |= flag_b;
113 	}
114 }
115 
116 int nsos_adapt_get_errno(void);
117 
118 int nsos_adapt_socket(int family, int type, int proto);
119 
120 int nsos_adapt_bind(int fd, const struct nsos_mid_sockaddr *addr, size_t addrlen);
121 int nsos_adapt_connect(int fd, const struct nsos_mid_sockaddr *addr, size_t addrlen);
122 int nsos_adapt_listen(int fd, int backlog);
123 int nsos_adapt_accept(int fd, struct nsos_mid_sockaddr *addr, size_t *addrlen);
124 int nsos_adapt_sendto(int fd, const void *buf, size_t len, int flags,
125 		      const struct nsos_mid_sockaddr *addr, size_t addrlen);
126 int nsos_adapt_sendmsg(int fd, const struct nsos_mid_msghdr *msg_mid, int flags);
127 int nsos_adapt_recvfrom(int fd, void *buf, size_t len, int flags,
128 			struct nsos_mid_sockaddr *addr, size_t *addrlen);
129 int nsos_adapt_getsockopt(int fd, int level, int optname,
130 			  void *optval, size_t *optlen);
131 int nsos_adapt_setsockopt(int fd, int level, int optname,
132 			  const void *optval, size_t optlen);
133 
134 void nsos_adapt_poll_add(struct nsos_mid_pollfd *pollfd);
135 void nsos_adapt_poll_remove(struct nsos_mid_pollfd *pollfd);
136 void nsos_adapt_poll_update(struct nsos_mid_pollfd *pollfd);
137 
138 int nsos_adapt_fcntl_getfl(int fd);
139 int nsos_adapt_fcntl_setfl(int fd, int flags);
140 
141 int nsos_adapt_fionread(int fd, int *avail);
142 
143 int nsos_adapt_dup(int oldfd);
144 
145 int nsos_adapt_getaddrinfo(const char *node, const char *service,
146 			   const struct nsos_mid_addrinfo *hints,
147 			   struct nsos_mid_addrinfo **res,
148 			   int *system_errno);
149 void nsos_adapt_freeaddrinfo(struct nsos_mid_addrinfo *res);
150 
151 #endif /* __DRIVERS_NET_NSOS_H__ */
152