socket.odin 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // +build windows, linux, darwin
  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-2023 Tetralux <[email protected]>
  9. Copyright 2022-2023 Colin Davidson <[email protected]>
  10. Copyright 2022-2023 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. any_socket_to_socket :: proc "contextless" (socket: Any_Socket) -> Socket {
  18. switch s in socket {
  19. case TCP_Socket: return Socket(s)
  20. case UDP_Socket: return Socket(s)
  21. case:
  22. // TODO(tetra): Bluetooth, Raw
  23. return Socket({})
  24. }
  25. }
  26. /*
  27. Expects both hostname and port to be present in the `hostname_and_port` parameter, either as:
  28. `a.host.name:9999`, or as `1.2.3.4:9999`, or IP6 equivalent.
  29. Calls `parse_hostname_or_endpoint` and `resolve`, then `dial_tcp_from_endpoint`.
  30. */
  31. dial_tcp_from_hostname_and_port_string :: proc(hostname_and_port: string, options := default_tcp_options) -> (socket: TCP_Socket, err: Network_Error) {
  32. target := parse_hostname_or_endpoint(hostname_and_port) or_return
  33. switch t in target {
  34. case Endpoint:
  35. return dial_tcp_from_endpoint(t, options)
  36. case Host:
  37. if t.port == 0 {
  38. return 0, .Port_Required
  39. }
  40. ep4, ep6 := resolve(t.hostname) or_return
  41. ep := ep4 if ep4.address != nil else ep6 // NOTE(tetra): We don't know what family the server uses, so we just default to IP4.
  42. ep.port = t.port
  43. return dial_tcp_from_endpoint(ep, options)
  44. }
  45. unreachable()
  46. }
  47. /*
  48. Expects the `hostname` as a string and `port` as a `int`.
  49. `parse_hostname_or_endpoint` is called and the `hostname` will be resolved into an IP.
  50. If a `hostname` of form `a.host.name:9999` is given, the port will be ignored in favor of the explicit `port` param.
  51. */
  52. dial_tcp_from_hostname_with_port_override :: proc(hostname: string, port: int, options := default_tcp_options) -> (socket: TCP_Socket, err: Network_Error) {
  53. target := parse_hostname_or_endpoint(hostname) or_return
  54. switch t in target {
  55. case Endpoint:
  56. return dial_tcp_from_endpoint({t.address, port}, options)
  57. case Host:
  58. if port == 0 {
  59. return 0, .Port_Required
  60. }
  61. ep4, ep6 := resolve(t.hostname) or_return
  62. ep := ep4 if ep4.address != nil else ep6 // NOTE(tetra): We don't know what family the server uses, so we just default to IP4.
  63. ep.port = port
  64. return dial_tcp_from_endpoint(ep, options)
  65. }
  66. unreachable()
  67. }
  68. // Dial from an Address
  69. dial_tcp_from_address_and_port :: proc(address: Address, port: int, options := default_tcp_options) -> (socket: TCP_Socket, err: Network_Error) {
  70. return dial_tcp_from_endpoint({address, port}, options)
  71. }
  72. dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (socket: TCP_Socket, err: Network_Error) {
  73. return _dial_tcp_from_endpoint(endpoint, options)
  74. }
  75. dial_tcp :: proc{
  76. dial_tcp_from_endpoint,
  77. dial_tcp_from_address_and_port,
  78. dial_tcp_from_hostname_and_port_string,
  79. dial_tcp_from_hostname_with_port_override,
  80. }
  81. create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
  82. return _create_socket(family, protocol)
  83. }
  84. bind :: proc(socket: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
  85. return _bind(socket, ep)
  86. }
  87. /*
  88. This type of socket becomes bound when you try to send data.
  89. It is likely what you want if you want to send data unsolicited.
  90. This is like a client TCP socket, except that it can send data to any remote endpoint without needing to establish a connection first.
  91. */
  92. make_unbound_udp_socket :: proc(family: Address_Family) -> (socket: UDP_Socket, err: Network_Error) {
  93. sock := create_socket(family, .UDP) or_return
  94. socket = sock.(UDP_Socket)
  95. return
  96. }
  97. /*
  98. This type of socket is bound immediately, which enables it to receive data on the port.
  99. Since it's UDP, it's also able to send data without receiving any first.
  100. This is like a listening TCP socket, except that data packets can be sent and received without needing to establish a connection first.
  101. The `bound_address` is the address of the network interface that you want to use, or a loopback address if you don't care which to use.
  102. */
  103. make_bound_udp_socket :: proc(bound_address: Address, port: int) -> (socket: UDP_Socket, err: Network_Error) {
  104. if bound_address == nil {
  105. return {}, .Bad_Address
  106. }
  107. socket = make_unbound_udp_socket(family_from_address(bound_address)) or_return
  108. bind(socket, {bound_address, port}) or_return
  109. return
  110. }
  111. listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (socket: TCP_Socket, err: Network_Error) {
  112. assert(backlog > 0 && backlog < int(max(i32)))
  113. return _listen_tcp(interface_endpoint, backlog)
  114. }
  115. accept_tcp :: proc(socket: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Network_Error) {
  116. return _accept_tcp(socket, options)
  117. }
  118. close :: proc(socket: Any_Socket) {
  119. _close(socket)
  120. }
  121. recv_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
  122. return _recv_tcp(socket, buf)
  123. }
  124. recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: Network_Error) {
  125. return _recv_udp(socket, buf)
  126. }
  127. /*
  128. Receive data from into a buffer from any socket.
  129. Note: `remote_endpoint` parameter is non-nil only if the socket type is UDP. On TCP sockets it
  130. will always return `nil`.
  131. */
  132. recv_any :: proc(socket: Any_Socket, buf: []byte) -> (
  133. bytes_read: int,
  134. remote_endpoint: Maybe(Endpoint),
  135. err: Network_Error,
  136. ) {
  137. switch socktype in socket {
  138. case TCP_Socket:
  139. bytes_read, err = recv_tcp(socktype, buf)
  140. return
  141. case UDP_Socket:
  142. return recv_udp(socktype, buf)
  143. case: panic("Not supported")
  144. }
  145. }
  146. recv :: proc{recv_tcp, recv_udp, recv_any}
  147. /*
  148. Repeatedly sends data until the entire buffer is sent.
  149. If a send fails before all data is sent, returns the amount sent up to that point.
  150. */
  151. send_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
  152. return _send_tcp(socket, buf)
  153. }
  154. /*
  155. Sends a single UDP datagram packet.
  156. Datagrams are limited in size; attempting to send more than this limit at once will result in a Message_Too_Long error.
  157. UDP packets are not guarenteed to be received in order.
  158. */
  159. send_udp :: proc(socket: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
  160. return _send_udp(socket, buf, to)
  161. }
  162. send_any :: proc(socket: Any_Socket, buf: []byte, to: Maybe(Endpoint) = nil) -> (
  163. bytes_written: int,
  164. err: Network_Error,
  165. ) {
  166. switch socktype in socket {
  167. case TCP_Socket:
  168. return send_tcp(socktype, buf)
  169. case UDP_Socket:
  170. return send_udp(socktype, buf, to.(Endpoint))
  171. case: panic("Not supported")
  172. }
  173. }
  174. send :: proc{send_tcp, send_udp, send_any}
  175. shutdown :: proc(socket: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
  176. return _shutdown(socket, manner)
  177. }
  178. set_option :: proc(socket: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
  179. return _set_option(socket, option, value, loc)
  180. }
  181. set_blocking :: proc(socket: Any_Socket, should_block: bool) -> (err: Network_Error) {
  182. return _set_blocking(socket, should_block)
  183. }