sal_socket.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * File : sal_socket.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-24 ChenYong First version
  23. */
  24. #ifndef SAL_SOCKET_H__
  25. #define SAL_SOCKET_H__
  26. #include "sal_ipaddr.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
  31. typedef uint32_t socklen_t;
  32. #endif
  33. #if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
  34. typedef uint8_t sa_family_t;
  35. #endif
  36. /* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED
  37. to prevent this code from redefining it. */
  38. #if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
  39. typedef uint16_t in_port_t;
  40. #endif
  41. /* Socket protocol types (TCP/UDP/RAW) */
  42. #define SOCK_STREAM 1
  43. #define SOCK_DGRAM 2
  44. #define SOCK_RAW 3
  45. #define SOCK_MAX (SOCK_RAW + 1)
  46. /* Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c) */
  47. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  48. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  49. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  50. /* Additional options, not kept in so_options */
  51. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  52. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  53. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  54. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  55. #define SO_LINGER 0x0080 /* linger on close if data present */
  56. #define SO_DONTLINGER ((int)(~SO_LINGER))
  57. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  58. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  59. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  60. #define SO_RCVBUF 0x1002 /* receive buffer size */
  61. #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
  62. #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
  63. #define SO_SNDTIMEO 0x1005 /* send timeout */
  64. #define SO_RCVTIMEO 0x1006 /* receive timeout */
  65. #define SO_ERROR 0x1007 /* get error status and clear */
  66. #define SO_TYPE 0x1008 /* get socket type */
  67. #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
  68. #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
  69. /* Level number for (get/set)sockopt() to apply to socket itself */
  70. #define SOL_SOCKET 0xfff /* options for socket level */
  71. #define AF_UNSPEC 0
  72. #define AF_INET 2
  73. #define AF_INET6 10
  74. #define AF_CAN 29 /* Controller Area Network */
  75. #define AF_AT 45 /* AT socket */
  76. #define AF_WIZ 46 /* WIZnet socket */
  77. #define PF_INET AF_INET
  78. #define PF_INET6 AF_INET6
  79. #define PF_UNSPEC AF_UNSPEC
  80. #define PF_CAN AF_CAN
  81. #define PF_AT AF_AT
  82. #define PF_WIZ AF_WIZ
  83. #define AF_MAX (AF_WIZ + 1) /* For now.. */
  84. #define IPPROTO_IP 0
  85. #define IPPROTO_ICMP 1
  86. #define IPPROTO_TCP 6
  87. #define IPPROTO_UDP 17
  88. #define IPPROTO_IPV6 41
  89. #define IPPROTO_ICMPV6 58
  90. #define IPPROTO_UDPLITE 136
  91. #define IPPROTO_RAW 255
  92. /* Flags we can use with send and recv */
  93. #define MSG_PEEK 0x01 /* Peeks at an incoming message */
  94. #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
  95. #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
  96. #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
  97. #define MSG_MORE 0x10 /* Sender will send more */
  98. /* Options for level IPPROTO_TCP */
  99. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  100. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  101. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  102. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  103. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  104. /* Options and types related to multicast membership */
  105. #define IP_ADD_MEMBERSHIP 3
  106. #define IP_DROP_MEMBERSHIP 4
  107. typedef struct ip_mreq
  108. {
  109. struct in_addr imr_multiaddr; /* IP multicast address of group */
  110. struct in_addr imr_interface; /* local IP address of interface */
  111. } ip_mreq;
  112. /* Options for shatdown type */
  113. #ifndef SHUT_RD
  114. #define SHUT_RD 0
  115. #define SHUT_WR 1
  116. #define SHUT_RDWR 2
  117. #endif
  118. struct sockaddr
  119. {
  120. uint8_t sa_len;
  121. sa_family_t sa_family;
  122. char sa_data[14];
  123. };
  124. /* members are in network byte order */
  125. struct sockaddr_in
  126. {
  127. uint8_t sin_len;
  128. sa_family_t sin_family;
  129. in_port_t sin_port;
  130. struct in_addr sin_addr;
  131. #define SIN_ZERO_LEN 8
  132. char sin_zero[SIN_ZERO_LEN];
  133. };
  134. struct sockaddr_storage
  135. {
  136. uint8_t s2_len;
  137. sa_family_t ss_family;
  138. char s2_data1[2];
  139. uint32_t s2_data2[3];
  140. #if SAL_IPV6
  141. u32_t s2_data3[3];
  142. #endif /* SAL_IPV6 */
  143. };
  144. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen);
  145. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen);
  146. int sal_shutdown(int socket, int how);
  147. int sal_getpeername (int socket, struct sockaddr *name, socklen_t *namelen);
  148. int sal_getsockname (int socket, struct sockaddr *name, socklen_t *namelen);
  149. int sal_getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen);
  150. int sal_setsockopt (int socket, int level, int optname, const void *optval, socklen_t optlen);
  151. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen);
  152. int sal_listen(int socket, int backlog);
  153. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  154. struct sockaddr *from, socklen_t *fromlen);
  155. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  156. const struct sockaddr *to, socklen_t tolen);
  157. int sal_socket(int domain, int type, int protocol);
  158. int sal_closesocket(int socket);
  159. int sal_ioctlsocket(int socket, long cmd, void *arg);
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* SAL_SOCKET_H__ */