socket_linux.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package net
  2. // +build linux
  3. /*
  4. Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures.
  5. For other protocols and their features, see subdirectories of this package.
  6. */
  7. /*
  8. Copyright 2022 Tetralux <[email protected]>
  9. Copyright 2022 Colin Davidson <[email protected]>
  10. Copyright 2022 Jeroen van Rijn <[email protected]>.
  11. Made available under Odin's BSD-3 license.
  12. List of contributors:
  13. Tetralux: Initial implementation
  14. Colin Davidson: Linux platform code, OSX platform code, Odin-native DNS resolver
  15. Jeroen van Rijn: Cross platform unification, code style, documentation
  16. */
  17. import "core:c"
  18. import "core:os"
  19. import "core:time"
  20. Socket_Option :: enum c.int {
  21. Reuse_Address = c.int(os.SO_REUSEADDR),
  22. Keep_Alive = c.int(os.SO_KEEPALIVE),
  23. Out_Of_Bounds_Data_Inline = c.int(os.SO_OOBINLINE),
  24. TCP_Nodelay = c.int(os.TCP_NODELAY),
  25. Linger = c.int(os.SO_LINGER),
  26. Receive_Buffer_Size = c.int(os.SO_RCVBUF),
  27. Send_Buffer_Size = c.int(os.SO_SNDBUF),
  28. Receive_Timeout = c.int(os.SO_RCVTIMEO_NEW),
  29. Send_Timeout = c.int(os.SO_SNDTIMEO_NEW),
  30. }
  31. @(private)
  32. _create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
  33. c_type, c_protocol, c_family: int
  34. switch family {
  35. case .IP4: c_family = os.AF_INET
  36. case .IP6: c_family = os.AF_INET6
  37. case:
  38. unreachable()
  39. }
  40. switch protocol {
  41. case .TCP: c_type = os.SOCK_STREAM; c_protocol = os.IPPROTO_TCP
  42. case .UDP: c_type = os.SOCK_DGRAM; c_protocol = os.IPPROTO_UDP
  43. case:
  44. unreachable()
  45. }
  46. sock, ok := os.socket(c_family, c_type, c_protocol)
  47. if ok != os.ERROR_NONE {
  48. err = Create_Socket_Error(ok)
  49. return
  50. }
  51. switch protocol {
  52. case .TCP: return TCP_Socket(sock), nil
  53. case .UDP: return UDP_Socket(sock), nil
  54. case:
  55. unreachable()
  56. }
  57. }
  58. @(private)
  59. _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (skt: TCP_Socket, err: Network_Error) {
  60. if endpoint.port == 0 {
  61. return 0, .Port_Required
  62. }
  63. family := family_from_endpoint(endpoint)
  64. sock := create_socket(family, .TCP) or_return
  65. skt = sock.(TCP_Socket)
  66. // NOTE(tetra): This is so that if we crash while the socket is open, we can
  67. // bypass the cooldown period, and allow the next run of the program to
  68. // use the same address immediately.
  69. _ = set_option(skt, .Reuse_Address, true)
  70. sockaddr := _endpoint_to_sockaddr(endpoint)
  71. res := os.connect(os.Socket(skt), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
  72. if res != os.ERROR_NONE {
  73. err = Dial_Error(res)
  74. return
  75. }
  76. if options.no_delay {
  77. _ = _set_option(sock, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
  78. }
  79. return
  80. }
  81. @(private)
  82. _bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
  83. sockaddr := _endpoint_to_sockaddr(ep)
  84. s := any_socket_to_socket(skt)
  85. res := os.bind(os.Socket(s), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
  86. if res != os.ERROR_NONE {
  87. err = Bind_Error(res)
  88. }
  89. return
  90. }
  91. @(private)
  92. _listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_Socket, err: Network_Error) {
  93. assert(backlog > 0 && i32(backlog) < max(i32))
  94. family := family_from_endpoint(interface_endpoint)
  95. sock := create_socket(family, .TCP) or_return
  96. skt = sock.(TCP_Socket)
  97. // NOTE(tetra): This is so that if we crash while the socket is open, we can
  98. // bypass the cooldown period, and allow the next run of the program to
  99. // use the same address immediately.
  100. //
  101. // TODO(tetra, 2022-02-15): Confirm that this doesn't mean other processes can hijack the address!
  102. set_option(sock, .Reuse_Address, true) or_return
  103. bind(sock, interface_endpoint) or_return
  104. res := os.listen(os.Socket(skt), backlog)
  105. if res != os.ERROR_NONE {
  106. err = Listen_Error(res)
  107. return
  108. }
  109. return
  110. }
  111. @(private)
  112. _accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Network_Error) {
  113. sockaddr: os.SOCKADDR_STORAGE_LH
  114. sockaddrlen := c.int(size_of(sockaddr))
  115. client_sock, ok := os.accept(os.Socket(sock), cast(^os.SOCKADDR) &sockaddr, &sockaddrlen)
  116. if ok != os.ERROR_NONE {
  117. err = Accept_Error(ok)
  118. return
  119. }
  120. client = TCP_Socket(client_sock)
  121. source = _sockaddr_storage_to_endpoint(&sockaddr)
  122. if options.no_delay {
  123. _ = _set_option(client, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
  124. }
  125. return
  126. }
  127. @(private)
  128. _close :: proc(skt: Any_Socket) {
  129. s := any_socket_to_socket(skt)
  130. os.close(os.Handle(os.Socket(s)))
  131. }
  132. @(private)
  133. _recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
  134. if len(buf) <= 0 {
  135. return
  136. }
  137. res, ok := os.recv(os.Socket(skt), buf, 0)
  138. if ok != os.ERROR_NONE {
  139. err = TCP_Recv_Error(ok)
  140. return
  141. }
  142. return int(res), nil
  143. }
  144. @(private)
  145. _recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: Network_Error) {
  146. if len(buf) <= 0 {
  147. return
  148. }
  149. from: os.SOCKADDR_STORAGE_LH = ---
  150. fromsize := c.int(size_of(from))
  151. // NOTE(tetra): On Linux, if the buffer is too small to fit the entire datagram payload, the rest is silently discarded,
  152. // and no error is returned.
  153. // However, if you pass MSG_TRUNC here, 'res' will be the size of the incoming message, rather than how much was read.
  154. // We can use this fact to detect this condition and return .Buffer_Too_Small.
  155. res, ok := os.recvfrom(os.Socket(skt), buf, os.MSG_TRUNC, cast(^os.SOCKADDR) &from, &fromsize)
  156. if ok != os.ERROR_NONE {
  157. err = UDP_Recv_Error(ok)
  158. return
  159. }
  160. bytes_read = int(res)
  161. remote_endpoint = _sockaddr_storage_to_endpoint(&from)
  162. if bytes_read > len(buf) {
  163. // NOTE(tetra): The buffer has been filled, with a partial message.
  164. bytes_read = len(buf)
  165. err = .Buffer_Too_Small
  166. }
  167. return
  168. }
  169. @(private)
  170. _send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
  171. for bytes_written < len(buf) {
  172. limit := min(int(max(i32)), len(buf) - bytes_written)
  173. remaining := buf[bytes_written:][:limit]
  174. res, ok := os.send(os.Socket(skt), remaining, 0)
  175. if ok != os.ERROR_NONE {
  176. err = TCP_Send_Error(ok)
  177. return
  178. }
  179. bytes_written += int(res)
  180. }
  181. return
  182. }
  183. @(private)
  184. _send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
  185. toaddr := _endpoint_to_sockaddr(to)
  186. res, os_err := os.sendto(os.Socket(skt), buf, 0, cast(^os.SOCKADDR) &toaddr, size_of(toaddr))
  187. if os_err != os.ERROR_NONE {
  188. err = UDP_Send_Error(os_err)
  189. return
  190. }
  191. bytes_written = int(res)
  192. return
  193. }
  194. @(private)
  195. _shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
  196. s := any_socket_to_socket(skt)
  197. res := os.shutdown(os.Socket(s), int(manner))
  198. if res != os.ERROR_NONE {
  199. return Shutdown_Error(res)
  200. }
  201. return
  202. }
  203. @(private)
  204. _set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
  205. level := os.SOL_SOCKET if option != .TCP_Nodelay else os.IPPROTO_TCP
  206. // NOTE(tetra, 2022-02-15): On Linux, you cannot merely give a single byte for a bool;
  207. // it _has_ to be a b32.
  208. // I haven't tested if you can give more than that.
  209. bool_value: b32
  210. int_value: i32
  211. timeval_value: os.Timeval
  212. ptr: rawptr
  213. len: os.socklen_t
  214. switch option {
  215. case
  216. .Reuse_Address,
  217. .Keep_Alive,
  218. .Out_Of_Bounds_Data_Inline,
  219. .TCP_Nodelay:
  220. // TODO: verify whether these are options or not on Linux
  221. // .Broadcast,
  222. // .Conditional_Accept,
  223. // .Dont_Linger:
  224. switch x in value {
  225. case bool, b8:
  226. x2 := x
  227. bool_value = b32((^bool)(&x2)^)
  228. case b16:
  229. bool_value = b32(x)
  230. case b32:
  231. bool_value = b32(x)
  232. case b64:
  233. bool_value = b32(x)
  234. case:
  235. panic("set_option() value must be a boolean here", loc)
  236. }
  237. ptr = &bool_value
  238. len = size_of(bool_value)
  239. case
  240. .Linger,
  241. .Send_Timeout,
  242. .Receive_Timeout:
  243. t, ok := value.(time.Duration)
  244. if !ok do panic("set_option() value must be a time.Duration here", loc)
  245. micros := i64(time.duration_microseconds(t))
  246. timeval_value.microseconds = int(micros % 1e6)
  247. timeval_value.seconds = (micros - i64(timeval_value.microseconds)) / 1e6
  248. ptr = &timeval_value
  249. len = size_of(timeval_value)
  250. case
  251. .Receive_Buffer_Size,
  252. .Send_Buffer_Size:
  253. // TODO: check for out of range values and return .Value_Out_Of_Range?
  254. switch i in value {
  255. case i8, u8: i2 := i; int_value = os.socklen_t((^u8)(&i2)^)
  256. case i16, u16: i2 := i; int_value = os.socklen_t((^u16)(&i2)^)
  257. case i32, u32: i2 := i; int_value = os.socklen_t((^u32)(&i2)^)
  258. case i64, u64: i2 := i; int_value = os.socklen_t((^u64)(&i2)^)
  259. case i128, u128: i2 := i; int_value = os.socklen_t((^u128)(&i2)^)
  260. case int, uint: i2 := i; int_value = os.socklen_t((^uint)(&i2)^)
  261. case:
  262. panic("set_option() value must be an integer here", loc)
  263. }
  264. ptr = &int_value
  265. len = size_of(int_value)
  266. }
  267. skt := any_socket_to_socket(s)
  268. res := os.setsockopt(os.Socket(skt), int(level), int(option), ptr, len)
  269. if res != os.ERROR_NONE {
  270. return Socket_Option_Error(res)
  271. }
  272. return nil
  273. }
  274. @(private)
  275. _set_blocking :: proc(socket: Any_Socket, should_block: bool) -> (err: Network_Error) {
  276. socket := any_socket_to_socket(socket)
  277. flags, getfl_err := os.fcntl(int(socket), os.F_GETFL, 0)
  278. if getfl_err != os.ERROR_NONE {
  279. return Set_Blocking_Error(getfl_err)
  280. }
  281. if should_block {
  282. flags &= ~int(os.O_NONBLOCK)
  283. } else {
  284. flags |= int(os.O_NONBLOCK)
  285. }
  286. _, setfl_err := os.fcntl(int(socket), os.F_SETFL, flags)
  287. if setfl_err != os.ERROR_NONE {
  288. return Set_Blocking_Error(setfl_err)
  289. }
  290. return nil
  291. }
  292. @(private)
  293. _endpoint_to_sockaddr :: proc(ep: Endpoint) -> (sockaddr: os.SOCKADDR_STORAGE_LH) {
  294. switch a in ep.address {
  295. case IP4_Address:
  296. (^os.sockaddr_in)(&sockaddr)^ = os.sockaddr_in {
  297. sin_port = u16be(ep.port),
  298. sin_addr = transmute(os.in_addr) a,
  299. sin_family = u16(os.AF_INET),
  300. }
  301. return
  302. case IP6_Address:
  303. (^os.sockaddr_in6)(&sockaddr)^ = os.sockaddr_in6 {
  304. sin6_port = u16be(ep.port),
  305. sin6_addr = transmute(os.in6_addr) a,
  306. sin6_family = u16(os.AF_INET6),
  307. }
  308. return
  309. }
  310. unreachable()
  311. }
  312. @(private)
  313. _sockaddr_storage_to_endpoint :: proc(native_addr: ^os.SOCKADDR_STORAGE_LH) -> (ep: Endpoint) {
  314. switch native_addr.ss_family {
  315. case u16(os.AF_INET):
  316. addr := cast(^os.sockaddr_in) native_addr
  317. port := int(addr.sin_port)
  318. ep = Endpoint {
  319. address = IP4_Address(transmute([4]byte) addr.sin_addr),
  320. port = port,
  321. }
  322. case u16(os.AF_INET6):
  323. addr := cast(^os.sockaddr_in6) native_addr
  324. port := int(addr.sin6_port)
  325. ep = Endpoint {
  326. address = IP6_Address(transmute([8]u16be) addr.sin6_addr),
  327. port = port,
  328. }
  329. case:
  330. panic("native_addr is neither IP4 or IP6 address")
  331. }
  332. return
  333. }
  334. @(private)
  335. _sockaddr_basic_to_endpoint :: proc(native_addr: ^os.SOCKADDR) -> (ep: Endpoint) {
  336. switch native_addr.sa_family {
  337. case u16(os.AF_INET):
  338. addr := cast(^os.sockaddr_in) native_addr
  339. port := int(addr.sin_port)
  340. ep = Endpoint {
  341. address = IP4_Address(transmute([4]byte) addr.sin_addr),
  342. port = port,
  343. }
  344. case u16(os.AF_INET6):
  345. addr := cast(^os.sockaddr_in6) native_addr
  346. port := int(addr.sin6_port)
  347. ep = Endpoint {
  348. address = IP6_Address(transmute([8]u16be) addr.sin6_addr),
  349. port = port,
  350. }
  351. case:
  352. panic("native_addr is neither IP4 or IP6 address")
  353. }
  354. return
  355. }