socket_linux.odin 13 KB

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