sal_socket.h 8.0 KB

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