errors_linux.odin 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // The buffer is too small to fit the entire message, and the message was truncated.
  93. // When this happens, the rest of message is lost.
  94. Buffer_Too_Small = c.int(os.EMSGSIZE),
  95. Not_Socket = c.int(os.ENOTSOCK), // The so-called socket is not an open socket.
  96. Not_Descriptor = c.int(os.EBADF), // The so-called socket is, in fact, not even a valid descriptor.
  97. Bad_Buffer = c.int(os.EFAULT), // The buffer did not point to a valid location in memory.
  98. Interrupted = c.int(os.EINTR), // A signal occurred before any data was transmitted. See signal(7).
  99. // The send timeout duration passed before all data was received. See Socket_Option.Receive_Timeout.
  100. // NOTE: No, really. Presumably this means something different for nonblocking sockets...
  101. Timeout = c.int(os.EWOULDBLOCK),
  102. Socket_Not_Bound = c.int(os.EINVAL), // The socket must be bound for this operation, but isn't.
  103. }
  104. // TODO
  105. TCP_Send_Error :: enum c.int {
  106. None = 0,
  107. // TODO(tetra): merge with other errors?
  108. Aborted = c.int(os.ECONNABORTED),
  109. Connection_Closed = c.int(os.ECONNRESET),
  110. Not_Connected = c.int(os.ENOTCONN),
  111. Shutdown = c.int(os.ESHUTDOWN),
  112. // The send queue was full.
  113. // This is usually a transient issue.
  114. //
  115. // This also shouldn't normally happen on Linux, as data is dropped if it
  116. // doesn't fit in the send queue.
  117. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  118. Offline = c.int(os.ENETDOWN),
  119. Host_Unreachable = c.int(os.EHOSTUNREACH), // A signal occurred before any data was transmitted. See signal(7).
  120. Interrupted = c.int(os.EINTR), // The send timeout duration passed before all data was sent. See Socket_Option.Send_Timeout.
  121. Timeout = c.int(os.EWOULDBLOCK), // NOTE: No, really. Presumably this means something different for nonblocking sockets...
  122. Not_Socket = c.int(os.ENOTSOCK), // The so-called socket is not an open socket.
  123. }
  124. // TODO
  125. UDP_Send_Error :: enum c.int {
  126. None = 0,
  127. Message_Too_Long = c.int(os.EMSGSIZE), // The message is too big. No data was sent.
  128. // TODO: not sure what the exact circumstances for this is yet
  129. Network_Unreachable = c.int(os.ENETUNREACH),
  130. 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.
  131. // The send timeout duration passed before all data was sent. See Socket_Option.Send_Timeout.
  132. // NOTE: No, really. Presumably this means something different for nonblocking sockets...
  133. Timeout = c.int(os.EWOULDBLOCK),
  134. Not_Socket = c.int(os.ENOTSOCK), // The so-called socket is not an open socket.
  135. Not_Descriptor = c.int(os.EBADF), // The so-called socket is, in fact, not even a valid descriptor.
  136. Bad_Buffer = c.int(os.EFAULT), // The buffer did not point to a valid location in memory.
  137. Interrupted = c.int(os.EINTR), // A signal occurred before any data was transmitted. See signal(7).
  138. // The send queue was full.
  139. // This is usually a transient issue.
  140. //
  141. // This also shouldn't normally happen on Linux, as data is dropped if it
  142. // doesn't fit in the send queue.
  143. No_Buffer_Space_Available = c.int(os.ENOBUFS),
  144. No_Memory_Available = c.int(os.ENOMEM), // No memory was available to properly manage the send queue.
  145. }
  146. Shutdown_Manner :: enum c.int {
  147. Receive = c.int(os.SHUT_RD),
  148. Send = c.int(os.SHUT_WR),
  149. Both = c.int(os.SHUT_RDWR),
  150. }
  151. Shutdown_Error :: enum c.int {
  152. None = 0,
  153. Aborted = c.int(os.ECONNABORTED),
  154. Reset = c.int(os.ECONNRESET),
  155. Offline = c.int(os.ENETDOWN),
  156. Not_Connected = c.int(os.ENOTCONN),
  157. Not_Socket = c.int(os.ENOTSOCK),
  158. Invalid_Manner = c.int(os.EINVAL),
  159. }
  160. Socket_Option_Error :: enum c.int {
  161. None = 0,
  162. Offline = c.int(os.ENETDOWN),
  163. Timeout_When_Keepalive_Set = c.int(os.ENETRESET),
  164. Invalid_Option_For_Socket = c.int(os.ENOPROTOOPT),
  165. Reset_When_Keepalive_Set = c.int(os.ENOTCONN),
  166. Not_Socket = c.int(os.ENOTSOCK),
  167. }
  168. Set_Blocking_Error :: enum c.int {
  169. None = 0,
  170. // TODO: add errors occuring on followig calls:
  171. // flags, _ := os.fcntl(sd, os.F_GETFL, 0)
  172. // os.fcntl(sd, os.F_SETFL, flags | int(os.O_NONBLOCK))
  173. }