/posixlib/src/main/scala/scala/scalanative/posix/sys/socket.scala

https://github.com/scala-native/scala-native · Scala · 238 lines · 184 code · 54 blank · 0 comment · 0 complexity · b79fc5dd4323c8d0839283345d4b5a41 MD5 · raw file

  1. package scala.scalanative
  2. package posix
  3. package sys
  4. import scalanative.unsafe._
  5. @extern
  6. object socket {
  7. type socklen_t = CUnsignedInt
  8. type sa_family_t = CUnsignedShort
  9. type _14 = Nat.Digit2[Nat._1, Nat._4]
  10. type sockaddr =
  11. CStruct2[sa_family_t, // sa_family
  12. CArray[CChar, _14]] // sa_data, size = 14 in OS X and Linux
  13. type sockaddr_storage = CStruct1[sa_family_t] // ss_family
  14. type msghdr = CStruct7[Ptr[Byte], // msg_name
  15. socklen_t, // msg_namelen
  16. Ptr[uio.iovec], // msg_iov
  17. CInt, // msg_iovlen
  18. Ptr[Byte], // msg_control
  19. socklen_t, // msg_crontrollen
  20. CInt] // msg_flags
  21. type cmsghdr = CStruct3[socklen_t, // cmsg_len
  22. CInt, // cmsg_level
  23. CInt] // cmsg_type
  24. type iovec = uio.iovec
  25. type linger = CStruct2[CInt, // l_onoff
  26. CInt] // l_linger
  27. @name("scalanative_SCM_RIGHTS")
  28. def SCM_RIGHTS: CInt = extern
  29. @name("scalanative_SOCK_DGRAM")
  30. def SOCK_DGRAM: CInt = extern
  31. @name("scalanative_SOCK_RAW")
  32. def SOCK_RAW: CInt = extern
  33. @name("scalanative_SOCK_SEQPACKET")
  34. def SOCK_SEQPACKET: CInt = extern
  35. @name("scalanative_SOCK_STREAM")
  36. def SOCK_STREAM: CInt = extern
  37. @name("scalanative_SOL_SOCKET")
  38. def SOL_SOCKET: CInt = extern
  39. @name("scalanative_SO_ACCEPTCONN")
  40. def SO_ACCEPTCONN: CInt = extern
  41. @name("scalanative_SO_BROADCAST")
  42. def SO_BROADCAST: CInt = extern
  43. @name("scalanative_SO_DEBUG")
  44. def SO_DEBUG: CInt = extern
  45. @name("scalanative_SO_DONTROUTE")
  46. def SO_DONTROUTE: CInt = extern
  47. @name("scalanative_SO_ERROR")
  48. def SO_ERROR: CInt = extern
  49. @name("scalanative_SO_KEEPALIVE")
  50. def SO_KEEPALIVE: CInt = extern
  51. @name("scalanative_SO_LINGER")
  52. def SO_LINGER: CInt = extern
  53. @name("scalanative_SO_OOBINLINE")
  54. def SO_OOBINLINE: CInt = extern
  55. @name("scalanative_SO_RCVBUF")
  56. def SO_RCVBUF: CInt = extern
  57. @name("scalanative_SO_RCVLOWAT")
  58. def SO_RCVLOWAT: CInt = extern
  59. @name("scalanative_SO_RCVTIMEO")
  60. def SO_RCVTIMEO: CInt = extern
  61. @name("scalanative_SO_REUSEADDR")
  62. def SO_REUSEADDR: CInt = extern
  63. @name("scalanative_SO_SNDBUF")
  64. def SO_SNDBUF: CInt = extern
  65. @name("scalanative_SO_SNDLOWAT")
  66. def SO_SNDLOWAT: CInt = extern
  67. @name("scalanative_SO_SNDTIMEO")
  68. def SO_SNDTIMEO: CInt = extern
  69. @name("scalanative_SO_TYPE")
  70. def SO_TYPE: CInt = extern
  71. @name("scalanative_SOMAXCONN")
  72. def SOMAXCONN: CInt = extern
  73. @name("scalanative_MSG_CTRUNC")
  74. def MSG_CTRUNC: CInt = extern
  75. @name("scalanative_MSG_DONTROUTE")
  76. def MSG_DONTROUTE: CInt = extern
  77. @name("scalanative_MSG_EOR")
  78. def MSG_EOR: CInt = extern
  79. @name("scalanative_MSG_OOB")
  80. def MSG_OOB: CInt = extern
  81. @name("scalanative_MSG_NOSIGNAL")
  82. def MSG_NOSIGNAL: CInt = extern // returns 0 on macOS
  83. @name("scalanative_MSG_PEEK")
  84. def MSG_PEEK: CInt = extern
  85. @name("scalanative_MSG_TRUNC")
  86. def MSG_TRUNC: CInt = extern
  87. @name("scalanative_MSG_WAITALL")
  88. def MSG_WAITALL: CInt = extern
  89. @name("scalanative_AF_INET")
  90. def AF_INET: CInt = extern
  91. @name("scalanative_AF_INET6")
  92. def AF_INET6: CInt = extern
  93. @name("scalanative_AF_UNIX")
  94. def AF_UNIX: CInt = extern
  95. @name("scalanative_AF_UNSPEC")
  96. def AF_UNSPEC: CInt = extern
  97. @name("scalanative_getsockname")
  98. def getsockname(socket: CInt,
  99. address: Ptr[sockaddr],
  100. address_len: Ptr[socklen_t]): CInt = extern
  101. @name("scalanative_socket")
  102. def socket(domain: CInt, tpe: CInt, protocol: CInt): CInt = extern
  103. @name("scalanative_connect")
  104. def connect(socket: CInt,
  105. address: Ptr[sockaddr],
  106. address_len: socklen_t): CInt = extern
  107. @name("scalanative_bind")
  108. def bind(socket: CInt, address: Ptr[sockaddr], address_len: socklen_t): CInt =
  109. extern
  110. @name("scalanative_listen")
  111. def listen(socket: CInt, backlog: CInt): CInt = extern
  112. @name("scalanative_accept")
  113. def accept(socket: CInt,
  114. address: Ptr[sockaddr],
  115. address_len: Ptr[socklen_t]): CInt = extern
  116. @name("scalanative_setsockopt")
  117. def setsockopt(socket: CInt,
  118. level: CInt,
  119. option_name: CInt,
  120. options_value: Ptr[Byte],
  121. option_len: socklen_t): CInt = extern
  122. @name("scalanative_getsockopt")
  123. def getsockopt(socket: CInt,
  124. level: CInt,
  125. option_name: CInt,
  126. options_value: Ptr[Byte],
  127. option_len: Ptr[socklen_t]): CInt = extern
  128. @name("scalanative_recv")
  129. def recv(socket: CInt,
  130. buffer: Ptr[Byte],
  131. length: CSize,
  132. flags: CInt): CSSize = extern
  133. @name("scalanative_send")
  134. def send(socket: CInt,
  135. buffer: Ptr[Byte],
  136. length: CSize,
  137. flags: CInt): CSSize = extern
  138. @name("scalanative_shutdown")
  139. def shutdown(socket: CInt, how: CInt): CInt = extern
  140. }
  141. object socketOps {
  142. import socket._
  143. implicit class sockaddrOps(val ptr: Ptr[sockaddr]) extends AnyVal {
  144. def sa_family: sa_family_t = ptr._1
  145. def sa_data: CArray[CChar, _14] = ptr._2
  146. def sa_family_=(v: sa_family_t): Unit = ptr._1 = v
  147. def sa_data_=(v: CArray[CChar, _14]): Unit = ptr._2 = v
  148. }
  149. implicit class sockaddr_storageOps(val ptr: Ptr[sockaddr_storage])
  150. extends AnyVal {
  151. def ss_family: sa_family_t = ptr._1
  152. def ss_family_=(v: sa_family_t): Unit = ptr._1 = v
  153. }
  154. implicit class msghdrOps(val ptr: Ptr[msghdr]) extends AnyVal {
  155. def msg_name: Ptr[Byte] = ptr._1
  156. def msg_namelen: socklen_t = ptr._2
  157. def msg_iov: Ptr[uio.iovec] = ptr._3
  158. def msg_iovlen: CInt = ptr._4
  159. def msg_control: Ptr[Byte] = ptr._5
  160. def msg_controllen: socklen_t = ptr._6
  161. def msg_flags: CInt = ptr._7
  162. def msg_name_=(v: Ptr[Byte]): Unit = ptr._1 = v
  163. def msg_namelen_=(v: socklen_t): Unit = ptr._2 = v
  164. def msg_iov_=(v: Ptr[uio.iovec]): Unit = ptr._3 = v
  165. def msg_iovlen_=(v: CInt): Unit = ptr._4 = v
  166. def msg_control_=(v: Ptr[Byte]): Unit = ptr._5 = v
  167. def msg_controllen_=(v: socklen_t): Unit = ptr._6 = v
  168. def msg_flags_=(v: CInt): Unit = ptr._7 = v
  169. }
  170. implicit class cmsghdrOps(val ptr: Ptr[cmsghdr]) extends AnyVal {
  171. def cmsg_len: socklen_t = ptr._1
  172. def cmsg_level: CInt = ptr._2
  173. def cmsg_type: CInt = ptr._3
  174. def cmsg_len_=(v: socklen_t): Unit = ptr._1 = v
  175. def cmsg_level_=(v: CInt): Unit = ptr._2 = v
  176. def cmsg_type_=(v: CInt): Unit = ptr._3 = v
  177. }
  178. implicit class lingerOps(val ptr: Ptr[linger]) extends AnyVal {
  179. def l_onoff: CInt = ptr._1
  180. def l_linger: CInt = ptr._2
  181. def l_onoff_=(v: CInt): Unit = ptr._1 = v
  182. def l_linger_=(v: CInt): Unit = ptr._2 = v
  183. }
  184. }