2
0

socket.odin 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #+build windows, linux, darwin, freebsd
  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. 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. Feoramund: FreeBSD platform code
  18. */
  19. any_socket_to_socket :: proc "contextless" (socket: Any_Socket) -> Socket {
  20. switch s in socket {
  21. case TCP_Socket: return Socket(s)
  22. case UDP_Socket: return Socket(s)
  23. case:
  24. // TODO(tetra): Bluetooth, Raw
  25. return Socket({})
  26. }
  27. }
  28. /*
  29. Expects both hostname and port to be present in the `hostname_and_port` parameter, either as:
  30. `a.host.name:9999`, or as `1.2.3.4:9999`, or IP6 equivalent.
  31. Calls `parse_hostname_or_endpoint` and `dial_tcp_from_host_or_endpoint`.
  32. Errors that can be returned: `Parse_Endpoint_Error`, `Resolve_Error`, `DNS_Error`, `Create_Socket_Error`, or `Dial_Error`
  33. */
  34. dial_tcp_from_hostname_and_port_string :: proc(hostname_and_port: string, options := DEFAULT_TCP_OPTIONS) -> (socket: TCP_Socket, err: Network_Error) {
  35. target := parse_hostname_or_endpoint(hostname_and_port) or_return
  36. return dial_tcp_from_host_or_endpoint(target, options)
  37. }
  38. /*
  39. Expects the `hostname` as a string and `port` as a `int`.
  40. `parse_hostname_or_endpoint` is called and the `hostname` will be resolved into an IP.
  41. If a `hostname` of form `a.host.name:9999` is given, the port will be ignored in favor of the explicit `port` param.
  42. Errors that can be returned: `Parse_Endpoint_Error`, `Resolve_Error`, `DNS_Error`, `Create_Socket_Error`, or `Dial_Error`
  43. */
  44. dial_tcp_from_hostname_with_port_override :: proc(hostname: string, port: int, options := DEFAULT_TCP_OPTIONS) -> (socket: TCP_Socket, err: Network_Error) {
  45. target := parse_hostname_or_endpoint(hostname) or_return
  46. switch &t in target {
  47. case Endpoint:
  48. t.port = port
  49. case Host:
  50. t.port = port
  51. }
  52. return dial_tcp_from_host_or_endpoint(target, options)
  53. }
  54. /*
  55. Expects the `host` as Host.
  56. Errors that can be returned: `Resolve_Error`, `DNS_Error`, `Create_Socket_Error`, or `Dial_Error`
  57. */
  58. dial_tcp_from_host :: proc(host: Host, options := DEFAULT_TCP_OPTIONS) -> (socket: TCP_Socket, err: Network_Error) {
  59. if host.port == 0 {
  60. return 0, .Port_Required
  61. }
  62. ep4, ep6 := resolve(host.hostname) or_return
  63. 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.
  64. ep.port = host.port
  65. return dial_tcp_from_endpoint(ep, options)
  66. }
  67. /*
  68. Expects the `target` as a Host_OrEndpoint.
  69. Unwraps the underlying type and calls `dial_tcp_from_host` or `dial_tcp_from_endpoint`.
  70. Errors that can be returned: `Parse_Endpoint_Error`, `Resolve_Error`, `DNS_Error`, `Create_Socket_Error`, or `Dial_Error`
  71. */
  72. dial_tcp_from_host_or_endpoint :: proc(target: Host_Or_Endpoint, options := DEFAULT_TCP_OPTIONS) -> (socket: TCP_Socket, err: Network_Error) {
  73. switch t in target {
  74. case Endpoint:
  75. return dial_tcp_from_endpoint(t, options)
  76. case Host:
  77. return dial_tcp_from_host(t, options)
  78. }
  79. unreachable()
  80. }
  81. /*
  82. Dial from an Address.
  83. Errors that can be returned: `Create_Socket_Error`, or `Dial_Error`
  84. */
  85. dial_tcp_from_address_and_port :: proc(address: Address, port: int, options := DEFAULT_TCP_OPTIONS) -> (socket: TCP_Socket, err: Network_Error) {
  86. return dial_tcp_from_endpoint({address, port}, options)
  87. }
  88. /*
  89. Dial from an Endpoint.
  90. Errors that can be returned: `Create_Socket_Error`, or `Dial_Error`
  91. */
  92. dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := DEFAULT_TCP_OPTIONS) -> (socket: TCP_Socket, err: Network_Error) {
  93. return _dial_tcp_from_endpoint(endpoint, options)
  94. }
  95. dial_tcp :: proc{
  96. dial_tcp_from_endpoint,
  97. dial_tcp_from_address_and_port,
  98. dial_tcp_from_hostname_and_port_string,
  99. dial_tcp_from_hostname_with_port_override,
  100. dial_tcp_from_host,
  101. dial_tcp_from_host_or_endpoint,
  102. }
  103. create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Create_Socket_Error) {
  104. return _create_socket(family, protocol)
  105. }
  106. bind :: proc(socket: Any_Socket, ep: Endpoint) -> (err: Bind_Error) {
  107. return _bind(socket, ep)
  108. }
  109. /*
  110. This type of socket becomes bound when you try to send data.
  111. It is likely what you want if you want to send data unsolicited.
  112. This is like a client TCP socket, except that it can send data to any remote endpoint without needing to establish a connection first.
  113. */
  114. make_unbound_udp_socket :: proc(family: Address_Family) -> (socket: UDP_Socket, err: Create_Socket_Error) {
  115. sock := create_socket(family, .UDP) or_return
  116. socket = sock.(UDP_Socket)
  117. return
  118. }
  119. /*
  120. This type of socket is bound immediately, which enables it to receive data on the port.
  121. Since it's UDP, it's also able to send data without receiving any first.
  122. This is like a listening TCP socket, except that data packets can be sent and received without needing to establish a connection first.
  123. 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.
  124. Errors that can be returned: `Parse_Endpoint_Error`, `Create_Socket_Error`, or `Bind_Error`
  125. */
  126. make_bound_udp_socket :: proc(bound_address: Address, port: int) -> (socket: UDP_Socket, err: Network_Error) {
  127. if bound_address == nil {
  128. return {}, .Bad_Address
  129. }
  130. socket = make_unbound_udp_socket(family_from_address(bound_address)) or_return
  131. bind(socket, {bound_address, port}) or_return
  132. return
  133. }
  134. /*
  135. Creates a TCP socket and starts listening on the given endpoint.
  136. Errors that can be returned: `Create_Socket_Error`, `Bind_Error`, or `Listen_Error`
  137. */
  138. listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (socket: TCP_Socket, err: Network_Error) {
  139. assert(backlog > 0 && backlog < int(max(i32)))
  140. return _listen_tcp(interface_endpoint, backlog)
  141. }
  142. /*
  143. Returns the endpoint that the given socket is listening / bound on.
  144. */
  145. bound_endpoint :: proc(socket: Any_Socket) -> (endpoint: Endpoint, err: Socket_Info_Error) {
  146. return _bound_endpoint(socket)
  147. }
  148. /*
  149. Returns the endpoint that the given socket is connected to. (Peer's endpoint)
  150. */
  151. peer_endpoint :: proc(socket: Any_Socket) -> (endpoint: Endpoint, err: Socket_Info_Error) {
  152. return _peer_endpoint(socket)
  153. }
  154. accept_tcp :: proc(socket: TCP_Socket, options := DEFAULT_TCP_OPTIONS) -> (client: TCP_Socket, source: Endpoint, err: Accept_Error) {
  155. return _accept_tcp(socket, options)
  156. }
  157. close :: proc(socket: Any_Socket) {
  158. _close(socket)
  159. }
  160. recv_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_read: int, err: TCP_Recv_Error) {
  161. return _recv_tcp(socket, buf)
  162. }
  163. recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: UDP_Recv_Error) {
  164. return _recv_udp(socket, buf)
  165. }
  166. /*
  167. Receive data from into a buffer from any socket.
  168. Note: `remote_endpoint` parameter is non-nil only if the socket type is UDP. On TCP sockets it
  169. will always return `nil`.
  170. Errors that can be returned: `TCP_Recv_Error`, or `UDP_Recv_Error`
  171. */
  172. recv_any :: proc(socket: Any_Socket, buf: []byte) -> (
  173. bytes_read: int,
  174. remote_endpoint: Maybe(Endpoint),
  175. err: Network_Error,
  176. ) {
  177. switch socktype in socket {
  178. case TCP_Socket:
  179. bytes_read, err = recv_tcp(socktype, buf)
  180. return
  181. case UDP_Socket:
  182. return recv_udp(socktype, buf)
  183. case: panic("Not supported")
  184. }
  185. }
  186. recv :: proc{recv_tcp, recv_udp, recv_any}
  187. /*
  188. Repeatedly sends data until the entire buffer is sent.
  189. If a send fails before all data is sent, returns the amount sent up to that point.
  190. */
  191. send_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_written: int, err: TCP_Send_Error) {
  192. return _send_tcp(socket, buf)
  193. }
  194. /*
  195. Sends a single UDP datagram packet.
  196. Datagrams are limited in size; attempting to send more than this limit at once will result in a Message_Too_Long error.
  197. UDP packets are not guarenteed to be received in order.
  198. */
  199. send_udp :: proc(socket: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: UDP_Send_Error) {
  200. return _send_udp(socket, buf, to)
  201. }
  202. /*
  203. Sends data over the socket.
  204. Errors that can be returned: `TCP_Send_Error`, or `UDP_Send_Error`
  205. */
  206. send_any :: proc(socket: Any_Socket, buf: []byte, to: Maybe(Endpoint) = nil) -> (
  207. bytes_written: int,
  208. err: Network_Error,
  209. ) {
  210. switch socktype in socket {
  211. case TCP_Socket:
  212. return send_tcp(socktype, buf)
  213. case UDP_Socket:
  214. return send_udp(socktype, buf, to.(Endpoint))
  215. case: panic("Not supported")
  216. }
  217. }
  218. send :: proc{send_tcp, send_udp, send_any}
  219. shutdown :: proc(socket: Any_Socket, manner: Shutdown_Manner) -> (err: Shutdown_Error) {
  220. return _shutdown(socket, manner)
  221. }
  222. set_option :: proc(socket: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Socket_Option_Error {
  223. return _set_option(socket, option, value, loc)
  224. }
  225. set_blocking :: proc(socket: Any_Socket, should_block: bool) -> (err: Set_Blocking_Error) {
  226. return _set_blocking(socket, should_block)
  227. }