socket.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <Kernel/API/POSIX/sys/types.h>
  8. #include <Kernel/API/POSIX/sys/uio.h>
  9. #include <Kernel/API/POSIX/sys/un.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #define AF_MASK 0xff
  14. #define AF_UNSPEC 0
  15. #define AF_LOCAL 1
  16. #define AF_UNIX AF_LOCAL
  17. #define AF_INET 2
  18. #define AF_INET6 3
  19. #define AF_MAX 4
  20. #define PF_LOCAL AF_LOCAL
  21. #define PF_UNIX PF_LOCAL
  22. #define PF_INET AF_INET
  23. #define PF_INET6 AF_INET6
  24. #define PF_UNSPEC AF_UNSPEC
  25. #define PF_MAX AF_MAX
  26. #define SOCK_TYPE_MASK 0xff
  27. #define SOCK_STREAM 1
  28. #define SOCK_DGRAM 2
  29. #define SOCK_RAW 3
  30. #define SOCK_RDM 4
  31. #define SOCK_SEQPACKET 5
  32. #define SOCK_NONBLOCK 04000
  33. #define SOCK_CLOEXEC 02000000
  34. #define SHUT_RD 0
  35. #define SHUT_WR 1
  36. #define SHUT_RDWR 2
  37. #define IPPROTO_IP 0
  38. #define IPPROTO_ICMP 1
  39. #define IPPROTO_IGMP 2
  40. #define IPPROTO_IPIP 4
  41. #define IPPROTO_TCP 6
  42. #define IPPROTO_UDP 17
  43. #define IPPROTO_IPV6 41
  44. #define IPPROTO_ESP 50
  45. #define IPPROTO_AH 51
  46. #define IPPROTO_ICMPV6 58
  47. #define IPPROTO_RAW 255
  48. #define MSG_TRUNC 0x1
  49. #define MSG_CTRUNC 0x2
  50. #define MSG_PEEK 0x4
  51. #define MSG_OOB 0x8
  52. #define MSG_DONTROUTE 0x10
  53. #define MSG_WAITALL 0x20
  54. #define MSG_DONTWAIT 0x40
  55. #define MSG_NOSIGNAL 0x80
  56. #define MSG_EOR 0x100
  57. typedef uint16_t sa_family_t;
  58. struct cmsghdr {
  59. socklen_t cmsg_len;
  60. int cmsg_level;
  61. int cmsg_type;
  62. };
  63. struct msghdr {
  64. void* msg_name;
  65. socklen_t msg_namelen;
  66. struct iovec* msg_iov;
  67. int msg_iovlen;
  68. void* msg_control;
  69. socklen_t msg_controllen;
  70. int msg_flags;
  71. };
  72. // These three are non-POSIX, but common:
  73. #define CMSG_ALIGN(x) (((x) + sizeof(void*) - 1) & ~(sizeof(void*) - 1))
  74. #define CMSG_SPACE(x) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(x))
  75. #define CMSG_LEN(x) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (x))
  76. static inline struct cmsghdr* CMSG_FIRSTHDR(struct msghdr* msg)
  77. {
  78. if (msg->msg_controllen < sizeof(struct cmsghdr))
  79. return (struct cmsghdr*)0;
  80. return (struct cmsghdr*)msg->msg_control;
  81. }
  82. static inline struct cmsghdr* CMSG_NXTHDR(struct msghdr* msg, struct cmsghdr* cmsg)
  83. {
  84. struct cmsghdr* next = (struct cmsghdr*)((char*)cmsg + CMSG_ALIGN(cmsg->cmsg_len));
  85. unsigned offset = (char*)next - (char*)msg->msg_control;
  86. if (msg->msg_controllen < offset + sizeof(struct cmsghdr))
  87. return (struct cmsghdr*)0;
  88. return next;
  89. }
  90. static inline void* CMSG_DATA(struct cmsghdr* cmsg)
  91. {
  92. return (void*)(cmsg + 1);
  93. }
  94. struct sockaddr {
  95. sa_family_t sa_family;
  96. char sa_data[14];
  97. };
  98. struct ucred {
  99. pid_t pid;
  100. uid_t uid;
  101. gid_t gid;
  102. };
  103. struct linger {
  104. int l_onoff;
  105. int l_linger;
  106. };
  107. #define SOL_SOCKET 1
  108. #define SOMAXCONN 128
  109. enum {
  110. SO_RCVTIMEO,
  111. SO_SNDTIMEO,
  112. SO_TYPE,
  113. SO_ERROR,
  114. SO_PEERCRED,
  115. SO_RCVBUF,
  116. SO_SNDBUF,
  117. SO_DEBUG,
  118. SO_REUSEADDR,
  119. SO_BINDTODEVICE,
  120. SO_KEEPALIVE,
  121. SO_TIMESTAMP,
  122. SO_BROADCAST,
  123. SO_LINGER,
  124. SO_ACCEPTCONN,
  125. SO_DONTROUTE,
  126. SO_OOBINLINE,
  127. SO_SNDLOWAT,
  128. SO_RCVLOWAT,
  129. };
  130. #define SO_RCVTIMEO SO_RCVTIMEO
  131. #define SO_SNDTIMEO SO_SNDTIMEO
  132. #define SO_TYPE SO_TYPE
  133. #define SO_ERROR SO_ERROR
  134. #define SO_PEERCRED SO_PEERCRED
  135. #define SO_DEBUG SO_DEBUG
  136. #define SO_REUSEADDR SO_REUSEADDR
  137. #define SO_BINDTODEVICE SO_BINDTODEVICE
  138. #define SO_KEEPALIVE SO_KEEPALIVE
  139. #define SO_TIMESTAMP SO_TIMESTAMP
  140. #define SO_BROADCAST SO_BROADCAST
  141. #define SO_SNDBUF SO_SNDBUF
  142. #define SO_RCVBUF SO_RCVBUF
  143. #define SO_LINGER SO_LINGER
  144. #define SO_ACCEPTCONN SO_ACCEPTCONN
  145. #define SO_DONTROUTE SO_DONTROUTE
  146. #define SO_OOBINLINE SO_OOBINLINE
  147. #define SO_SNDLOWAT SO_SNDLOWAT
  148. #define SO_RCVLOWAT SO_RCVLOWAT
  149. enum {
  150. SCM_TIMESTAMP,
  151. SCM_RIGHTS,
  152. };
  153. #define SCM_TIMESTAMP SCM_TIMESTAMP
  154. #define SCM_RIGHTS SCM_RIGHTS
  155. struct sockaddr_storage {
  156. sa_family_t ss_family;
  157. union {
  158. char data[sizeof(struct sockaddr_un)];
  159. void* alignment;
  160. };
  161. };
  162. #ifdef __cplusplus
  163. }
  164. #endif