errors_linux.odin 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package net
  2. // +build linux
  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. 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. import "core:c"
  18. import "core:os"
  19. Create_Socket_Error :: enum c.int {
  20. None = 0,
  21. Family_Not_Supported_For_This_Socket = c.int(os.EAFNOSUPPORT),
  22. No_Socket_Descriptors_Available = c.int(os.EMFILE),
  23. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  24. No_Memory_Available_Available = c.int(os.ENOMEM),
  25. Protocol_Unsupported_By_System = c.int(os.EPROTONOSUPPORT),
  26. Wrong_Protocol_For_Socket = c.int(os.EPROTONOSUPPORT),
  27. Family_And_Socket_Type_Mismatch = c.int(os.EPROTONOSUPPORT),
  28. }
  29. Dial_Error :: enum c.int {
  30. None = 0,
  31. Port_Required = -1,
  32. Address_In_Use = c.int(os.EADDRINUSE),
  33. In_Progress = c.int(os.EINPROGRESS),
  34. Cannot_Use_Any_Address = c.int(os.EADDRNOTAVAIL),
  35. Wrong_Family_For_Socket = c.int(os.EAFNOSUPPORT),
  36. Refused = c.int(os.ECONNREFUSED),
  37. Is_Listening_Socket = c.int(os.EACCES),
  38. Already_Connected = c.int(os.EISCONN),
  39. Network_Unreachable = c.int(os.ENETUNREACH), // Device is offline
  40. Host_Unreachable = c.int(os.EHOSTUNREACH), // Remote host cannot be reached
  41. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  42. Not_Socket = c.int(os.ENOTSOCK),
  43. Timeout = c.int(os.ETIMEDOUT),
  44. // TODO: we may need special handling for this; maybe make a socket a struct with metadata?
  45. Would_Block = c.int(os.EWOULDBLOCK),
  46. }
  47. Bind_Error :: enum c.int {
  48. None = 0,
  49. Address_In_Use = c.int(os.EADDRINUSE), // Another application is currently bound to this endpoint.
  50. Given_Nonlocal_Address = c.int(os.EADDRNOTAVAIL), // The address is not a local address on this machine.
  51. Broadcast_Disabled = c.int(os.EACCES), // To bind a UDP socket to the broadcast address, the appropriate socket option must be set.
  52. Address_Family_Mismatch = c.int(os.EFAULT), // The address family of the address does not match that of the socket.
  53. Already_Bound = c.int(os.EINVAL), // The socket is already bound to an address.
  54. No_Ports_Available = c.int(os.ENOBUFS), // There are not enough ephemeral ports available.
  55. }
  56. Listen_Error :: enum c.int {
  57. None = 0,
  58. Address_In_Use = c.int(os.EADDRINUSE),
  59. Already_Connected = c.int(os.EISCONN),
  60. No_Socket_Descriptors_Available = c.int(os.EMFILE),
  61. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  62. Nonlocal_Address = c.int(os.EADDRNOTAVAIL),
  63. Not_Socket = c.int(os.ENOTSOCK),
  64. Listening_Not_Supported_For_This_Socket = c.int(os.EOPNOTSUPP),
  65. }
  66. Accept_Error :: enum c.int {
  67. None = 0,
  68. Not_Listening = c.int(os.EINVAL),
  69. No_Socket_Descriptors_Available_For_Client_Socket = c.int(os.EMFILE),
  70. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  71. Not_Socket = c.int(os.ENOTSOCK),
  72. Not_Connection_Oriented_Socket = c.int(os.EOPNOTSUPP),
  73. // TODO: we may need special handling for this; maybe make a socket a struct with metadata?
  74. Would_Block = c.int(os.EWOULDBLOCK),
  75. }
  76. TCP_Recv_Error :: enum c.int {
  77. None = 0,
  78. Shutdown = c.int(os.ESHUTDOWN),
  79. Not_Connected = c.int(os.ENOTCONN),
  80. Connection_Broken = c.int(os.ENETRESET),
  81. Not_Socket = c.int(os.ENOTSOCK),
  82. Aborted = c.int(os.ECONNABORTED),
  83. // TODO(tetra): Determine when this is different from the syscall returning n=0 and maybe normalize them?
  84. Connection_Closed = c.int(os.ECONNRESET),
  85. Offline = c.int(os.ENETDOWN),
  86. Host_Unreachable = c.int(os.EHOSTUNREACH),
  87. Interrupted = c.int(os.EINTR),
  88. Timeout = c.int(os.EWOULDBLOCK), // NOTE: No, really. Presumably this means something different for nonblocking sockets...
  89. }
  90. UDP_Recv_Error :: enum c.int {
  91. None = 0,
  92. Buffer_Too_Small = c.int(os.EMSGSIZE), // The buffer is too small to fit the entire message, and the message was truncated. When this happens, the rest of message is lost.
  93. Not_Socket = c.int(os.ENOTSOCK), // The so-called socket is not an open socket.
  94. Not_Descriptor = c.int(os.EBADF), // The so-called socket is, in fact, not even a valid descriptor.
  95. Bad_Buffer = c.int(os.EFAULT), // The buffer did not point to a valid location in memory.
  96. Interrupted = c.int(os.EINTR), // A signal occurred before any data was transmitted. See signal(7).
  97. // The send timeout duration passed before all data was received. See Socket_Option.Receive_Timeout.
  98. // NOTE: No, really. Presumably this means something different for nonblocking sockets...
  99. Timeout = c.int(os.EWOULDBLOCK),
  100. Socket_Not_Bound = c.int(os.EINVAL), // The socket must be bound for this operation, but isn't.
  101. }
  102. TCP_Send_Error :: enum c.int {
  103. None = 0,
  104. Aborted = c.int(os.ECONNABORTED),
  105. Connection_Closed = c.int(os.ECONNRESET),
  106. Not_Connected = c.int(os.ENOTCONN),
  107. Shutdown = c.int(os.ESHUTDOWN),
  108. // The send queue was full.
  109. // This is usually a transient issue.
  110. //
  111. // This also shouldn't normally happen on Linux, as data is dropped if it
  112. // doesn't fit in the send queue.
  113. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  114. Offline = c.int(os.ENETDOWN),
  115. Host_Unreachable = c.int(os.EHOSTUNREACH),
  116. Interrupted = c.int(os.EINTR), // A signal occurred before any data was transmitted. See signal(7).
  117. Timeout = c.int(os.EWOULDBLOCK), // The send timeout duration passed before all data was sent. See Socket_Option.Send_Timeout.
  118. Not_Socket = c.int(os.ENOTSOCK), // The so-called socket is not an open socket.
  119. }
  120. // TODO
  121. UDP_Send_Error :: enum c.int {
  122. None = 0,
  123. Message_Too_Long = c.int(os.EMSGSIZE), // The message is larger than the maximum UDP packet size. No data was sent.
  124. // TODO: not sure what the exact circumstances for this is yet
  125. Network_Unreachable = c.int(os.ENETUNREACH),
  126. No_Outbound_Ports_Available = c.int(os.EAGAIN), // There are no more emphemeral outbound ports available to bind the socket to, in order to send.
  127. // The send timeout duration passed before all data was sent. See Socket_Option.Send_Timeout.
  128. // NOTE: No, really. Presumably this means something different for nonblocking sockets...
  129. Timeout = c.int(os.EWOULDBLOCK),
  130. Not_Socket = c.int(os.ENOTSOCK), // The so-called socket is not an open socket.
  131. Not_Descriptor = c.int(os.EBADF), // The so-called socket is, in fact, not even a valid descriptor.
  132. Bad_Buffer = c.int(os.EFAULT), // The buffer did not point to a valid location in memory.
  133. Interrupted = c.int(os.EINTR), // A signal occurred before any data was transmitted. See signal(7).
  134. // The send queue was full.
  135. // This is usually a transient issue.
  136. //
  137. // This also shouldn't normally happen on Linux, as data is dropped if it
  138. // doesn't fit in the send queue.
  139. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  140. No_Memory_Available = c.int(os.ENOMEM), // No memory was available to properly manage the send queue.
  141. }
  142. Shutdown_Manner :: enum c.int {
  143. Receive = c.int(os.SHUT_RD),
  144. Send = c.int(os.SHUT_WR),
  145. Both = c.int(os.SHUT_RDWR),
  146. }
  147. Shutdown_Error :: enum c.int {
  148. None = 0,
  149. Aborted = c.int(os.ECONNABORTED),
  150. Reset = c.int(os.ECONNRESET),
  151. Offline = c.int(os.ENETDOWN),
  152. Not_Connected = c.int(os.ENOTCONN),
  153. Not_Socket = c.int(os.ENOTSOCK),
  154. Invalid_Manner = c.int(os.EINVAL),
  155. }
  156. Socket_Option_Error :: enum c.int {
  157. None = 0,
  158. Offline = c.int(os.ENETDOWN),
  159. Timeout_When_Keepalive_Set = c.int(os.ENETRESET),
  160. Invalid_Option_For_Socket = c.int(os.ENOPROTOOPT),
  161. Reset_When_Keepalive_Set = c.int(os.ENOTCONN),
  162. Not_Socket = c.int(os.ENOTSOCK),
  163. }
  164. Set_Blocking_Error :: enum c.int {
  165. None = 0,
  166. // TODO: add errors occuring on followig calls:
  167. // flags, _ := os.fcntl(sd, os.F_GETFL, 0)
  168. // os.fcntl(sd, os.F_SETFL, flags | int(os.O_NONBLOCK))
  169. }