errors_linux.odin 9.8 KB

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