socket_linux.odin 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #+build linux
  2. package net
  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. Copyright 2024 Feoramund <[email protected]>.
  12. Made available under Odin's BSD-3 license.
  13. List of contributors:
  14. Tetralux: Initial implementation
  15. Colin Davidson: Linux platform code, OSX platform code, Odin-native DNS resolver
  16. Jeroen van Rijn: Cross platform unification, code style, documentation
  17. flysand: Move dependency from core:os to core:sys/linux
  18. Feoramund: FreeBSD platform code
  19. */
  20. import "core:c"
  21. import "core:time"
  22. import "core:sys/linux"
  23. Socket_Option :: enum c.int {
  24. Reuse_Address = c.int(linux.Socket_Option.REUSEADDR),
  25. Keep_Alive = c.int(linux.Socket_Option.KEEPALIVE),
  26. Out_Of_Bounds_Data_Inline = c.int(linux.Socket_Option.OOBINLINE),
  27. TCP_Nodelay = c.int(linux.Socket_TCP_Option.NODELAY),
  28. Linger = c.int(linux.Socket_Option.LINGER),
  29. Receive_Buffer_Size = c.int(linux.Socket_Option.RCVBUF),
  30. Send_Buffer_Size = c.int(linux.Socket_Option.SNDBUF),
  31. Receive_Timeout = c.int(linux.Socket_Option.RCVTIMEO),
  32. Send_Timeout = c.int(linux.Socket_Option.SNDTIMEO),
  33. }
  34. // Wrappers and unwrappers for system-native types
  35. @(private="file")
  36. _unwrap_os_socket :: proc "contextless" (sock: Any_Socket)->linux.Fd {
  37. return linux.Fd(any_socket_to_socket(sock))
  38. }
  39. @(private="file")
  40. _wrap_os_socket :: proc "contextless" (sock: linux.Fd, protocol: Socket_Protocol)->Any_Socket {
  41. switch protocol {
  42. case .TCP: return TCP_Socket(Socket(sock))
  43. case .UDP: return UDP_Socket(Socket(sock))
  44. case:
  45. unreachable()
  46. }
  47. }
  48. @(private="file")
  49. _unwrap_os_family :: proc "contextless" (family: Address_Family)->linux.Address_Family {
  50. switch family {
  51. case .IP4: return .INET
  52. case .IP6: return .INET6
  53. case:
  54. unreachable()
  55. }
  56. }
  57. @(private="file")
  58. _unwrap_os_proto_socktype :: proc "contextless" (protocol: Socket_Protocol)->(linux.Protocol, linux.Socket_Type) {
  59. switch protocol {
  60. case .TCP: return .TCP, .STREAM
  61. case .UDP: return .UDP, .DGRAM
  62. case:
  63. unreachable()
  64. }
  65. }
  66. @(private="file")
  67. _unwrap_os_addr :: proc "contextless" (endpoint: Endpoint)->(linux.Sock_Addr_Any) {
  68. switch address in endpoint.address {
  69. case IP4_Address:
  70. return {
  71. ipv4 = {
  72. sin_family = .INET,
  73. sin_port = u16be(endpoint.port),
  74. sin_addr = ([4]u8)(endpoint.address.(IP4_Address)),
  75. },
  76. }
  77. case IP6_Address:
  78. return {
  79. ipv6 = {
  80. sin6_port = u16be(endpoint.port),
  81. sin6_addr = transmute([16]u8)endpoint.address.(IP6_Address),
  82. sin6_family = .INET6,
  83. },
  84. }
  85. case:
  86. unreachable()
  87. }
  88. }
  89. @(private="file")
  90. _wrap_os_addr :: proc "contextless" (addr: linux.Sock_Addr_Any)->(Endpoint) {
  91. #partial switch addr.family {
  92. case .INET:
  93. return {
  94. address = cast(IP4_Address) addr.sin_addr,
  95. port = cast(int) addr.sin_port,
  96. }
  97. case .INET6:
  98. return {
  99. port = cast(int) addr.sin6_port,
  100. address = transmute(IP6_Address) addr.sin6_addr,
  101. }
  102. case:
  103. unreachable()
  104. }
  105. }
  106. _create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (Any_Socket, Network_Error) {
  107. family := _unwrap_os_family(family)
  108. proto, socktype := _unwrap_os_proto_socktype(protocol)
  109. sock, errno := linux.socket(family, socktype, {.CLOEXEC}, proto)
  110. if errno != .NONE {
  111. return {}, Create_Socket_Error(errno)
  112. }
  113. return _wrap_os_socket(sock, protocol), nil
  114. }
  115. @(private)
  116. _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (TCP_Socket, Network_Error) {
  117. errno: linux.Errno
  118. if endpoint.port == 0 {
  119. return 0, .Port_Required
  120. }
  121. // Create new TCP socket
  122. os_sock: linux.Fd
  123. os_sock, errno = linux.socket(_unwrap_os_family(family_from_endpoint(endpoint)), .STREAM, {.CLOEXEC}, .TCP)
  124. if errno != .NONE {
  125. // TODO(flysand): should return invalid file descriptor here casted as TCP_Socket
  126. return {}, Create_Socket_Error(errno)
  127. }
  128. // NOTE(tetra): This is so that if we crash while the socket is open, we can
  129. // bypass the cooldown period, and allow the next run of the program to
  130. // use the same address immediately.
  131. reuse_addr: b32 = true
  132. _ = linux.setsockopt(os_sock, linux.SOL_SOCKET, linux.Socket_Option.REUSEADDR, &reuse_addr)
  133. addr := _unwrap_os_addr(endpoint)
  134. errno = linux.connect(linux.Fd(os_sock), &addr)
  135. if errno != .NONE {
  136. return cast(TCP_Socket) os_sock, Dial_Error(errno)
  137. }
  138. // NOTE(tetra): Not vital to succeed; error ignored
  139. no_delay: b32 = cast(b32) options.no_delay
  140. _ = linux.setsockopt(os_sock, linux.SOL_TCP, linux.Socket_TCP_Option.NODELAY, &no_delay)
  141. return cast(TCP_Socket) os_sock, nil
  142. }
  143. @(private)
  144. _bind :: proc(sock: Any_Socket, endpoint: Endpoint) -> (Network_Error) {
  145. addr := _unwrap_os_addr(endpoint)
  146. errno := linux.bind(_unwrap_os_socket(sock), &addr)
  147. if errno != .NONE {
  148. return Bind_Error(errno)
  149. }
  150. return nil
  151. }
  152. @(private)
  153. _listen_tcp :: proc(endpoint: Endpoint, backlog := 1000) -> (TCP_Socket, Network_Error) {
  154. errno: linux.Errno
  155. assert(backlog > 0 && i32(backlog) < max(i32))
  156. // Figure out the address family and address of the endpoint
  157. ep_family := _unwrap_os_family(family_from_endpoint(endpoint))
  158. ep_address := _unwrap_os_addr(endpoint)
  159. // Create TCP socket
  160. os_sock: linux.Fd
  161. os_sock, errno = linux.socket(ep_family, .STREAM, {.CLOEXEC}, .TCP)
  162. if errno != .NONE {
  163. // TODO(flysand): should return invalid file descriptor here casted as TCP_Socket
  164. return {}, Create_Socket_Error(errno)
  165. }
  166. // NOTE(tetra): This is so that if we crash while the socket is open, we can
  167. // bypass the cooldown period, and allow the next run of the program to
  168. // use the same address immediately.
  169. //
  170. // TODO(tetra, 2022-02-15): Confirm that this doesn't mean other processes can hijack the address!
  171. do_reuse_addr: b32 = true
  172. errno = linux.setsockopt(os_sock, linux.SOL_SOCKET, linux.Socket_Option.REUSEADDR, &do_reuse_addr)
  173. if errno != .NONE {
  174. return cast(TCP_Socket) os_sock, Listen_Error(errno)
  175. }
  176. // Bind the socket to endpoint address
  177. errno = linux.bind(os_sock, &ep_address)
  178. if errno != .NONE {
  179. return cast(TCP_Socket) os_sock, Bind_Error(errno)
  180. }
  181. // Listen on bound socket
  182. errno = linux.listen(os_sock, cast(i32) backlog)
  183. if errno != .NONE {
  184. return cast(TCP_Socket) os_sock, Listen_Error(errno)
  185. }
  186. return cast(TCP_Socket) os_sock, nil
  187. }
  188. @(private)
  189. _bound_endpoint :: proc(sock: Any_Socket) -> (ep: Endpoint, err: Network_Error) {
  190. addr: linux.Sock_Addr_Any
  191. errno := linux.getsockname(_unwrap_os_socket(sock), &addr)
  192. if errno != .NONE {
  193. err = Listen_Error(errno)
  194. return
  195. }
  196. ep = _wrap_os_addr(addr)
  197. return
  198. }
  199. @(private)
  200. _accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (tcp_client: TCP_Socket, endpoint: Endpoint, err: Network_Error) {
  201. addr: linux.Sock_Addr_Any
  202. client_sock, errno := linux.accept(linux.Fd(sock), &addr)
  203. if errno != .NONE {
  204. return {}, {}, Accept_Error(errno)
  205. }
  206. // NOTE(tetra): Not vital to succeed; error ignored
  207. val: b32 = cast(b32) options.no_delay
  208. _ = linux.setsockopt(client_sock, linux.SOL_TCP, linux.Socket_TCP_Option.NODELAY, &val)
  209. return TCP_Socket(client_sock), _wrap_os_addr(addr), nil
  210. }
  211. @(private)
  212. _close :: proc(sock: Any_Socket) {
  213. linux.close(_unwrap_os_socket(sock))
  214. }
  215. @(private)
  216. _recv_tcp :: proc(tcp_sock: TCP_Socket, buf: []byte) -> (int, Network_Error) {
  217. if len(buf) <= 0 {
  218. return 0, nil
  219. }
  220. bytes_read, errno := linux.recv(linux.Fd(tcp_sock), buf, {})
  221. if errno != .NONE {
  222. return 0, TCP_Recv_Error(errno)
  223. }
  224. return int(bytes_read), nil
  225. }
  226. @(private)
  227. _recv_udp :: proc(udp_sock: UDP_Socket, buf: []byte) -> (int, Endpoint, Network_Error) {
  228. if len(buf) <= 0 {
  229. // NOTE(flysand): It was returning no error, I didn't change anything
  230. return 0, {}, {}
  231. }
  232. // NOTE(tetra): On Linux, if the buffer is too small to fit the entire datagram payload, the rest is silently discarded,
  233. // and no error is returned.
  234. // However, if you pass MSG_TRUNC here, 'res' will be the size of the incoming message, rather than how much was read.
  235. // We can use this fact to detect this condition and return .Buffer_Too_Small.
  236. from_addr: linux.Sock_Addr_Any
  237. bytes_read, errno := linux.recvfrom(linux.Fd(udp_sock), buf, {.TRUNC}, &from_addr)
  238. if errno != .NONE {
  239. return 0, {}, UDP_Recv_Error(errno)
  240. }
  241. if bytes_read > len(buf) {
  242. // NOTE(tetra): The buffer has been filled, with a partial message.
  243. return len(buf), {}, .Buffer_Too_Small
  244. }
  245. return bytes_read, _wrap_os_addr(from_addr), nil
  246. }
  247. @(private)
  248. _send_tcp :: proc(tcp_sock: TCP_Socket, buf: []byte) -> (int, Network_Error) {
  249. total_written := 0
  250. for total_written < len(buf) {
  251. limit := min(int(max(i32)), len(buf) - total_written)
  252. remaining := buf[total_written:][:limit]
  253. res, errno := linux.send(linux.Fd(tcp_sock), remaining, {.NOSIGNAL})
  254. if errno == .EPIPE {
  255. // If the peer is disconnected when we are trying to send we will get an `EPIPE` error,
  256. // so we turn that into a clearer error
  257. return total_written, TCP_Send_Error.Connection_Closed
  258. } else if errno != .NONE {
  259. return total_written, TCP_Send_Error(errno)
  260. }
  261. total_written += int(res)
  262. }
  263. return total_written, nil
  264. }
  265. @(private)
  266. _send_udp :: proc(udp_sock: UDP_Socket, buf: []byte, to: Endpoint) -> (int, Network_Error) {
  267. to_addr := _unwrap_os_addr(to)
  268. bytes_written, errno := linux.sendto(linux.Fd(udp_sock), buf, {}, &to_addr)
  269. if errno != .NONE {
  270. return bytes_written, UDP_Send_Error(errno)
  271. }
  272. return int(bytes_written), nil
  273. }
  274. @(private)
  275. _shutdown :: proc(sock: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
  276. os_sock := _unwrap_os_socket(sock)
  277. errno := linux.shutdown(os_sock, cast(linux.Shutdown_How) manner)
  278. if errno != .NONE {
  279. return Shutdown_Error(errno)
  280. }
  281. return nil
  282. }
  283. // TODO(flysand): Figure out what we want to do with this on core:sys/ level.
  284. @(private)
  285. _set_option :: proc(sock: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
  286. level: int
  287. if option == .TCP_Nodelay {
  288. level = int(linux.SOL_TCP)
  289. } else {
  290. level = int(linux.SOL_SOCKET)
  291. }
  292. os_sock := _unwrap_os_socket(sock)
  293. // NOTE(tetra, 2022-02-15): On Linux, you cannot merely give a single byte for a bool;
  294. // it _has_ to be a b32.
  295. // I haven't tested if you can give more than that. <-- (flysand) probably not, posix explicitly specifies an int
  296. bool_value: b32
  297. int_value: i32
  298. timeval_value: linux.Time_Val
  299. errno: linux.Errno
  300. switch option {
  301. case
  302. .Reuse_Address,
  303. .Keep_Alive,
  304. .Out_Of_Bounds_Data_Inline,
  305. .TCP_Nodelay:
  306. // TODO: verify whether these are options or not on Linux
  307. // .Broadcast, <-- yes
  308. // .Conditional_Accept,
  309. // .Dont_Linger:
  310. switch x in value {
  311. case bool, b8:
  312. x2 := x
  313. bool_value = b32((^bool)(&x2)^)
  314. case b16:
  315. bool_value = b32(x)
  316. case b32:
  317. bool_value = b32(x)
  318. case b64:
  319. bool_value = b32(x)
  320. case:
  321. panic("set_option() value must be a boolean here", loc)
  322. }
  323. errno = linux.setsockopt(os_sock, level, int(option), &bool_value)
  324. case
  325. .Linger,
  326. .Send_Timeout,
  327. .Receive_Timeout:
  328. t, ok := value.(time.Duration)
  329. if !ok {
  330. panic("set_option() value must be a time.Duration here", loc)
  331. }
  332. micros := cast(i64) (time.duration_microseconds(t))
  333. timeval_value.microseconds = cast(int) (micros % 1e6)
  334. timeval_value.seconds = cast(int) ((micros - i64(timeval_value.microseconds)) / 1e6)
  335. errno = linux.setsockopt(os_sock, level, int(option), &timeval_value)
  336. case
  337. .Receive_Buffer_Size,
  338. .Send_Buffer_Size:
  339. // TODO: check for out of range values and return .Value_Out_Of_Range?
  340. switch i in value {
  341. case i8, u8: i2 := i; int_value = i32((^u8)(&i2)^)
  342. case i16, u16: i2 := i; int_value = i32((^u16)(&i2)^)
  343. case i32, u32: i2 := i; int_value = i32((^u32)(&i2)^)
  344. case i64, u64: i2 := i; int_value = i32((^u64)(&i2)^)
  345. case i128, u128: i2 := i; int_value = i32((^u128)(&i2)^)
  346. case int, uint: i2 := i; int_value = i32((^uint)(&i2)^)
  347. case:
  348. panic("set_option() value must be an integer here", loc)
  349. }
  350. errno = linux.setsockopt(os_sock, level, int(option), &int_value)
  351. }
  352. if errno != .NONE {
  353. return Socket_Option_Error(errno)
  354. }
  355. return nil
  356. }
  357. @(private)
  358. _set_blocking :: proc(sock: Any_Socket, should_block: bool) -> (err: Network_Error) {
  359. errno: linux.Errno
  360. flags: linux.Open_Flags
  361. os_sock := _unwrap_os_socket(sock)
  362. flags, errno = linux.fcntl(os_sock, linux.F_GETFL)
  363. if errno != .NONE {
  364. return Set_Blocking_Error(errno)
  365. }
  366. if should_block {
  367. flags -= {.NONBLOCK}
  368. } else {
  369. flags += {.NONBLOCK}
  370. }
  371. errno = linux.fcntl(os_sock, linux.F_SETFL, flags)
  372. if errno != .NONE {
  373. return Set_Blocking_Error(errno)
  374. }
  375. return nil
  376. }