1
0

sal_socket.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_MAX (SOCK_RAW + 1)
  33. /* Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c) */
  34. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  35. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  36. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  37. /* Additional options, not kept in so_options */
  38. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  39. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  40. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  41. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  42. #define SO_LINGER 0x0080 /* linger on close if data present */
  43. #define SO_DONTLINGER ((int)(~SO_LINGER))
  44. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  45. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  46. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  47. #define SO_RCVBUF 0x1002 /* receive buffer size */
  48. #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
  49. #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
  50. #define SO_SNDTIMEO 0x1005 /* send timeout */
  51. #define SO_RCVTIMEO 0x1006 /* receive timeout */
  52. #define SO_ERROR 0x1007 /* get error status and clear */
  53. #define SO_TYPE 0x1008 /* get socket type */
  54. #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
  55. #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
  56. /* Level number for (get/set)sockopt() to apply to socket itself */
  57. #define SOL_SOCKET 0xfff /* options for socket level */
  58. #define AF_UNSPEC 0
  59. #define AF_INET 2
  60. #define AF_INET6 10
  61. #define AF_CAN 29 /* Controller Area Network */
  62. #define AF_AT 45 /* AT socket */
  63. #define AF_WIZ 46 /* WIZnet socket */
  64. #define PF_INET AF_INET
  65. #define PF_INET6 AF_INET6
  66. #define PF_UNSPEC AF_UNSPEC
  67. #define PF_CAN AF_CAN
  68. #define PF_AT AF_AT
  69. #define PF_WIZ AF_WIZ
  70. #define AF_MAX (AF_WIZ + 1) /* For now.. */
  71. #define IPPROTO_IP 0
  72. #define IPPROTO_ICMP 1
  73. #define IPPROTO_TCP 6
  74. #define IPPROTO_UDP 17
  75. #define IPPROTO_IPV6 41
  76. #define IPPROTO_ICMPV6 58
  77. #define IPPROTO_UDPLITE 136
  78. #define IPPROTO_RAW 255
  79. /* Flags we can use with send and recv */
  80. #define MSG_PEEK 0x01 /* Peeks at an incoming message */
  81. #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
  82. #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
  83. #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
  84. #define MSG_MORE 0x10 /* Sender will send more */
  85. /* Options for level IPPROTO_IP */
  86. #define IP_TOS 1
  87. #define IP_TTL 2
  88. /* Options for level IPPROTO_TCP */
  89. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  90. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  91. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  92. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  93. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  94. /* Options and types related to multicast membership */
  95. #define IP_ADD_MEMBERSHIP 3
  96. #define IP_DROP_MEMBERSHIP 4
  97. /* Options and types for UDP multicast traffic handling */
  98. #define IP_MULTICAST_TTL 5
  99. #define IP_MULTICAST_IF 6
  100. #define IP_MULTICAST_LOOP 7
  101. typedef struct ip_mreq
  102. {
  103. struct in_addr imr_multiaddr; /* IP multicast address of group */
  104. struct in_addr imr_interface; /* local IP address of interface */
  105. } ip_mreq;
  106. /* The Type of Service provides an indication of the abstract parameters of the quality of service desired */
  107. #define IPTOS_TOS_MASK 0x1E
  108. #define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
  109. #define IPTOS_LOWDELAY 0x10
  110. #define IPTOS_THROUGHPUT 0x08
  111. #define IPTOS_RELIABILITY 0x04
  112. #define IPTOS_LOWCOST 0x02
  113. #define IPTOS_MINCOST IPTOS_LOWCOST
  114. /* The Network Control precedence designation is intended to be used within a network only */
  115. #define IPTOS_PREC_MASK 0xe0
  116. #define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
  117. #define IPTOS_PREC_NETCONTROL 0xe0
  118. #define IPTOS_PREC_INTERNETCONTROL 0xc0
  119. #define IPTOS_PREC_CRITIC_ECP 0xa0
  120. #define IPTOS_PREC_FLASHOVERRIDE 0x80
  121. #define IPTOS_PREC_FLASH 0x60
  122. #define IPTOS_PREC_IMMEDIATE 0x40
  123. #define IPTOS_PREC_PRIORITY 0x20
  124. #define IPTOS_PREC_ROUTINE 0x00
  125. /* Options for shatdown type */
  126. #ifndef SHUT_RD
  127. #define SHUT_RD 0
  128. #define SHUT_WR 1
  129. #define SHUT_RDWR 2
  130. #endif
  131. struct sockaddr
  132. {
  133. uint8_t sa_len;
  134. sa_family_t sa_family;
  135. char sa_data[14];
  136. };
  137. #if NETDEV_IPV4
  138. /* members are in network byte order */
  139. struct sockaddr_in
  140. {
  141. uint8_t sin_len;
  142. sa_family_t sin_family;
  143. in_port_t sin_port;
  144. struct in_addr sin_addr;
  145. #define SIN_ZERO_LEN 8
  146. char sin_zero[SIN_ZERO_LEN];
  147. };
  148. #endif /* NETDEV_IPV4 */
  149. #if NETDEV_IPV6
  150. struct sockaddr_in6
  151. {
  152. uint8_t sin6_len; /* length of this structure */
  153. sa_family_t sin6_family; /* AF_INET6 */
  154. in_port_t sin6_port; /* Transport layer port # */
  155. uint32_t sin6_flowinfo; /* IPv6 flow information */
  156. struct in6_addr sin6_addr; /* IPv6 address */
  157. uint32_t sin6_scope_id; /* Set of interfaces for scope */
  158. };
  159. #endif /* NETDEV_IPV6 */
  160. struct sockaddr_storage
  161. {
  162. uint8_t s2_len;
  163. sa_family_t ss_family;
  164. char s2_data1[2];
  165. uint32_t s2_data2[3];
  166. #if NETDEV_IPV6
  167. uint32_t s2_data3[3];
  168. #endif /* NETDEV_IPV6 */
  169. };
  170. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen);
  171. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen);
  172. int sal_shutdown(int socket, int how);
  173. int sal_getpeername (int socket, struct sockaddr *name, socklen_t *namelen);
  174. int sal_getsockname (int socket, struct sockaddr *name, socklen_t *namelen);
  175. int sal_getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen);
  176. int sal_setsockopt (int socket, int level, int optname, const void *optval, socklen_t optlen);
  177. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen);
  178. int sal_listen(int socket, int backlog);
  179. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  180. struct sockaddr *from, socklen_t *fromlen);
  181. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  182. const struct sockaddr *to, socklen_t tolen);
  183. int sal_socket(int domain, int type, int protocol);
  184. int sal_closesocket(int socket);
  185. int sal_ioctlsocket(int socket, long cmd, void *arg);
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. #endif /* SAL_SOCKET_H__ */