arpa_inet.odin 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #+build darwin, linux, freebsd, openbsd, netbsd, haiku
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System"
  6. } else when ODIN_OS == .Haiku {
  7. foreign import lib "system:network"
  8. } else {
  9. foreign import lib "system:c"
  10. }
  11. // arpa/inet.h - definitions for internet operations
  12. foreign lib {
  13. // Use Odin's native big endian types `u32be` and `u16be` instead.
  14. // htonl :: proc(c.uint32_t) -> c.uint32_t ---
  15. // htons :: proc(c.uint16_t) -> c.uint16_t ---
  16. // ntohl :: proc(c.uint32_t) -> c.uint32_t ---
  17. // ntohs :: proc(c.uint16_t) -> c.uint16_t ---
  18. // Use of this function is problematic because -1 is a valid address (255.255.255.255).
  19. // Avoid its use in favor of inet_aton(), inet_pton(3), or getaddrinfo(3) which provide a cleaner way to indicate error return.
  20. // inet_addr :: proc(cstring) -> in_addr_t ---
  21. // Convert the Internet host address specified by in to a string in the Internet standard dot notation.
  22. //
  23. // NOTE: returns a static string overwritten by further calls.
  24. //
  25. // [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_ntoa.html ]]
  26. inet_ntoa :: proc(in_addr) -> cstring ---
  27. // Convert a numeric address into a text string suitable for presentation.
  28. //
  29. // Returns `nil` and sets `errno` on failure.
  30. //
  31. // [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_ntop.html ]]
  32. inet_ntop :: proc(
  33. af: AF, // INET or INET6
  34. src: rawptr, // either ^in_addr or ^in_addr6
  35. dst: [^]byte, // use `INET_ADDRSTRLEN` or `INET6_ADDRSTRLEN` for minimum lengths
  36. size: socklen_t,
  37. ) -> cstring ---
  38. // Convert an address in its standard text presentation form into its numeric binary form.
  39. //
  40. // [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_ntop.html ]]
  41. inet_pton :: proc(
  42. af: AF, // INET or INET6
  43. src: cstring,
  44. dst: rawptr, // either ^in_addr or ^in_addr6
  45. ) -> pton_result ---
  46. }
  47. pton_result :: enum c.int {
  48. AFNOSUPPORT = -1,
  49. INVALID = 0,
  50. SUCCESS = 1,
  51. }