sal_socket.h 6.8 KB

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