sal_socket.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-24 ChenYong First version
  9. */
  10. #ifndef SAL_SOCKET_H__
  11. #define SAL_SOCKET_H__
  12. #include <stddef.h>
  13. #include <arpa/inet.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
  18. typedef uint32_t socklen_t;
  19. #endif
  20. #if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
  21. typedef uint8_t sa_family_t;
  22. #endif
  23. /* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED
  24. to prevent this code from redefining it. */
  25. #if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
  26. typedef uint16_t in_port_t;
  27. #endif
  28. /* Socket protocol types (TCP/UDP/RAW) */
  29. #define SOCK_STREAM 1
  30. #define SOCK_DGRAM 2
  31. #define SOCK_RAW 3
  32. #define SOCK_PACKET 10
  33. #define SOCK_NONBLOCK 04000
  34. #define SOCK_CLOEXEC 02000000
  35. #define SOCK_MAX (SOCK_CLOEXEC + 1)
  36. /* Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c) */
  37. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  38. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  39. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  40. #define SO_PASSCRED 16
  41. #define SO_PEERCRED 17
  42. #define SO_BINDTODEVICE 25
  43. #define SO_ATTACH_FILTER 26
  44. #define SO_DETACH_FILTER 27
  45. #define SO_SNDBUFFORCE 32
  46. #define SO_RCVBUFFORCE 33
  47. #define SO_PROTOCOL 38
  48. #define SO_DOMAIN 39
  49. /* Additional options, not kept in so_options */
  50. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  51. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  52. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  53. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  54. #define SO_LINGER 0x0080 /* linger on close if data present */
  55. #define SO_DONTLINGER ((int)(~SO_LINGER))
  56. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  57. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  58. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  59. #define SO_RCVBUF 0x1002 /* receive buffer size */
  60. #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
  61. #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
  62. #define SO_SNDTIMEO 0x1005 /* send timeout */
  63. #define SO_RCVTIMEO 0x1006 /* receive timeout */
  64. #define SO_ERROR 0x1007 /* get error status and clear */
  65. #define SO_TYPE 0x1008 /* get socket type */
  66. #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
  67. #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
  68. /* Level number for (get/set)sockopt() to apply to socket itself */
  69. #define SOL_SOCKET 0xfff /* options for socket level */
  70. #define SOL_NETLINK 270
  71. #define AF_UNSPEC 0
  72. #define AF_UNIX 1
  73. #define AF_INET 2
  74. #define AF_INET6 10
  75. #define AF_NETLINK 16
  76. #define AF_CAN 29 /* Controller Area Network */
  77. #define AF_AT 45 /* AT socket */
  78. #define AF_WIZ 46 /* WIZnet socket */
  79. #define PF_UNIX AF_UNIX
  80. #define PF_INET AF_INET
  81. #define PF_INET6 AF_INET6
  82. #define PF_NETLINK AF_NETLINK
  83. #define PF_UNSPEC AF_UNSPEC
  84. #define PF_CAN AF_CAN
  85. #define PF_AT AF_AT
  86. #define PF_WIZ AF_WIZ
  87. #define AF_MAX (AF_WIZ + 1) /* For now.. */
  88. #define IPPROTO_IP 0
  89. #define IPPROTO_ICMP 1
  90. #define IPPROTO_TCP 6
  91. #define IPPROTO_UDP 17
  92. #define IPPROTO_IPV6 41
  93. #define IPPROTO_ICMPV6 58
  94. #define IPPROTO_UDPLITE 136
  95. #define IPPROTO_RAW 255
  96. /* Flags we can use with send and recv */
  97. #define MSG_PEEK 0x01 /* Peeks at an incoming message */
  98. #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
  99. #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
  100. #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
  101. #define MSG_MORE 0x10 /* Sender will send more */
  102. #define MSG_ERRQUEUE 0x2000 /* Fetch message from error queue */
  103. #define MSG_CONFIRM 0x0800 /* Confirm path validity */
  104. /* Options for level IPPROTO_IP */
  105. #define IP_TOS 1
  106. #define IP_TTL 2
  107. /* Options for level IPPROTO_TCP */
  108. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  109. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  110. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  111. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  112. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  113. /* Options and types related to multicast membership */
  114. #define IP_ADD_MEMBERSHIP 3
  115. #define IP_DROP_MEMBERSHIP 4
  116. /* Options and types for UDP multicast traffic handling */
  117. #define IP_MULTICAST_TTL 5
  118. #define IP_MULTICAST_IF 6
  119. #define IP_MULTICAST_LOOP 7
  120. typedef struct ip_mreq
  121. {
  122. struct in_addr imr_multiaddr; /* IP multicast address of group */
  123. struct in_addr imr_interface; /* local IP address of interface */
  124. } ip_mreq;
  125. /* The Type of Service provides an indication of the abstract parameters of the quality of service desired */
  126. #define IPTOS_TOS_MASK 0x1E
  127. #define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
  128. #define IPTOS_LOWDELAY 0x10
  129. #define IPTOS_THROUGHPUT 0x08
  130. #define IPTOS_RELIABILITY 0x04
  131. #define IPTOS_LOWCOST 0x02
  132. #define IPTOS_MINCOST IPTOS_LOWCOST
  133. /* The Network Control precedence designation is intended to be used within a network only */
  134. #define IPTOS_PREC_MASK 0xe0
  135. #define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
  136. #define IPTOS_PREC_NETCONTROL 0xe0
  137. #define IPTOS_PREC_INTERNETCONTROL 0xc0
  138. #define IPTOS_PREC_CRITIC_ECP 0xa0
  139. #define IPTOS_PREC_FLASHOVERRIDE 0x80
  140. #define IPTOS_PREC_FLASH 0x60
  141. #define IPTOS_PREC_IMMEDIATE 0x40
  142. #define IPTOS_PREC_PRIORITY 0x20
  143. #define IPTOS_PREC_ROUTINE 0x00
  144. #define SCM_RIGHTS 0x01 /* rw: access rights (array of int) */
  145. #define SCM_CREDENTIALS 0x02 /* rw: struct ucred */
  146. #define SCM_SECURITY 0x03 /* rw: security label */
  147. /* Options for shatdown type */
  148. #ifndef SHUT_RD
  149. #define SHUT_RD 0
  150. #define SHUT_WR 1
  151. #define SHUT_RDWR 2
  152. #endif
  153. struct sockaddr
  154. {
  155. uint8_t sa_len;
  156. sa_family_t sa_family;
  157. char sa_data[14];
  158. };
  159. /* Structure describing the address of an AF_LOCAL (aka AF_UNIX) socket. */
  160. struct sockaddr_un
  161. {
  162. unsigned short sa_family;
  163. char sun_path[108]; /* Path name. */
  164. };
  165. #if NETDEV_IPV4
  166. /* members are in network byte order */
  167. struct sockaddr_in
  168. {
  169. uint8_t sin_len;
  170. sa_family_t sin_family;
  171. in_port_t sin_port;
  172. struct in_addr sin_addr;
  173. #define SIN_ZERO_LEN 8
  174. char sin_zero[SIN_ZERO_LEN];
  175. };
  176. #endif /* NETDEV_IPV4 */
  177. #if NETDEV_IPV6
  178. struct sockaddr_in6
  179. {
  180. uint8_t sin6_len; /* length of this structure */
  181. sa_family_t sin6_family; /* AF_INET6 */
  182. in_port_t sin6_port; /* Transport layer port # */
  183. uint32_t sin6_flowinfo; /* IPv6 flow information */
  184. struct in6_addr sin6_addr; /* IPv6 address */
  185. uint32_t sin6_scope_id; /* Set of interfaces for scope */
  186. };
  187. #endif /* NETDEV_IPV6 */
  188. struct sockaddr_storage
  189. {
  190. uint8_t s2_len;
  191. sa_family_t ss_family;
  192. char s2_data1[2];
  193. uint32_t s2_data2[3];
  194. #if NETDEV_IPV6
  195. uint32_t s2_data3[3];
  196. #endif /* NETDEV_IPV6 */
  197. };
  198. #ifdef RT_USING_MUSLLIBC
  199. #ifndef __DEFINED_struct_iovec
  200. struct iovec
  201. {
  202. void *iov_base;
  203. size_t iov_len;
  204. };
  205. #endif
  206. #endif
  207. struct msghdr
  208. {
  209. void *msg_name;
  210. socklen_t msg_namelen;
  211. struct iovec *msg_iov;
  212. int msg_iovlen;
  213. void *msg_control;
  214. socklen_t msg_controllen;
  215. int msg_flags;
  216. };
  217. /* RFC 3542, Section 20: Ancillary Data */
  218. struct cmsghdr
  219. {
  220. size_t cmsg_len; /* number of bytes, including header */
  221. int cmsg_level; /* originating protocol */
  222. int cmsg_type; /* protocol-specific type */
  223. };
  224. #define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))
  225. #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long)-1))
  226. #define CMSG_DATA(cmsg) ((void *)(cmsg) + sizeof(struct cmsghdr))
  227. #define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))
  228. #define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))
  229. #define __CMSG_FIRSTHDR(ctl, len) \
  230. ((len) >= sizeof(struct cmsghdr) ? (struct cmsghdr *)(ctl) : (struct cmsghdr *)NULL)
  231. #define CMSG_FIRSTHDR(msg) __CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
  232. #define CMSG_OK(mhdr, cmsg) \
  233. ((cmsg)->cmsg_len >= sizeof(struct cmsghdr) && \
  234. (cmsg)->cmsg_len <= (unsigned long)((mhdr)->msg_controllen - ((char *)(cmsg) - (char *)(mhdr)->msg_control)))
  235. #define for_each_cmsghdr(cmsg, msg) \
  236. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
  237. static inline struct cmsghdr *__cmsg_nxthdr(void *_ctl, size_t _size, struct cmsghdr *_cmsg)
  238. {
  239. struct cmsghdr *_ptr;
  240. _ptr = (struct cmsghdr *)(((unsigned char *)_cmsg) + CMSG_ALIGN(_cmsg->cmsg_len));
  241. if ((unsigned long)((char *)(_ptr + 1) - (char *)_ctl) > _size)
  242. {
  243. return (struct cmsghdr *)NULL;
  244. }
  245. return _ptr;
  246. }
  247. static inline struct cmsghdr *cmsg_nxthdr(struct msghdr *_msg, struct cmsghdr *_cmsg)
  248. {
  249. return __cmsg_nxthdr(_msg->msg_control, _msg->msg_controllen, _cmsg);
  250. }
  251. #define IFNAMSIZ 16
  252. struct sal_ifmap
  253. {
  254. unsigned long int mem_start;
  255. unsigned long int mem_end;
  256. unsigned short int base_addr;
  257. unsigned char irq;
  258. unsigned char dma;
  259. unsigned char port;
  260. };
  261. struct sal_ifreq
  262. {
  263. union
  264. {
  265. char ifrn_name[IFNAMSIZ];
  266. } ifr_ifrn;
  267. union
  268. {
  269. struct sockaddr ifru_addr;
  270. struct sockaddr ifru_dstaddr;
  271. struct sockaddr ifru_broadaddr;
  272. struct sockaddr ifru_netmask;
  273. struct sockaddr ifru_hwaddr;
  274. short int ifru_flags;
  275. int ifru_ivalue;
  276. int ifru_mtu;
  277. struct sal_ifmap ifru_map;
  278. char ifru_slave[IFNAMSIZ];
  279. char ifru_newname[IFNAMSIZ];
  280. char *ifru_data;
  281. } ifr_ifru;
  282. };
  283. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen);
  284. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen);
  285. int sal_shutdown(int socket, int how);
  286. int sal_getpeername (int socket, struct sockaddr *name, socklen_t *namelen);
  287. int sal_getsockname (int socket, struct sockaddr *name, socklen_t *namelen);
  288. int sal_getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen);
  289. int sal_setsockopt (int socket, int level, int optname, const void *optval, socklen_t optlen);
  290. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen);
  291. int sal_listen(int socket, int backlog);
  292. int sal_sendmsg(int socket, const struct msghdr *message, int flags);
  293. int sal_recvmsg(int socket, struct msghdr *message, int flags);
  294. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  295. struct sockaddr *from, socklen_t *fromlen);
  296. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  297. const struct sockaddr *to, socklen_t tolen);
  298. int sal_socket(int domain, int type, int protocol);
  299. int sal_socketpair(int domain, int type, int protocol, int *fds);
  300. int sal_closesocket(int socket);
  301. int sal_ioctlsocket(int socket, long cmd, void *arg);
  302. #ifdef __cplusplus
  303. }
  304. #endif
  305. #endif /* SAL_SOCKET_H__ */