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