2
0

socket_linux.odin 13 KB

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