rfc2553emu.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: rfc2553emu.h,v 1.4 2000/06/18 06:04:45 jgg Exp $
  4. /* ######################################################################
  5. RFC 2553 Emulation - Provides emulation for RFC 2553 getaddrinfo,
  6. freeaddrinfo and getnameinfo
  7. These functions are necessary to write portable protocol independent
  8. networking. They transparently support IPv4, IPv6 and probably many
  9. other protocols too. This implementation is needed when the host does
  10. not support these standards. It implements a simple wrapper that
  11. basically supports only IPv4.
  12. Perfect emulation is not provided, but it is passable..
  13. Originally written by Jason Gunthorpe <jgg@debian.org> and placed into
  14. the Public Domain, do with it what you will.
  15. ##################################################################### */
  16. /*}}}*/
  17. #ifndef RFC2553EMU_H
  18. #define RFC2553EMU_H
  19. #include <netdb.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. // Autosense getaddrinfo
  23. #if defined(AI_PASSIVE) && defined(EAI_NONAME)
  24. #define HAVE_GETADDRINFO
  25. #endif
  26. // Autosense getnameinfo
  27. #if defined(NI_NUMERICHOST)
  28. #define HAVE_GETNAMEINFO
  29. #endif
  30. // getaddrinfo support?
  31. #ifndef HAVE_GETADDRINFO
  32. // Renamed to advoid type clashing.. (for debugging)
  33. struct addrinfo_emu
  34. {
  35. int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
  36. int ai_family; /* PF_xxx */
  37. int ai_socktype; /* SOCK_xxx */
  38. int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
  39. size_t ai_addrlen; /* length of ai_addr */
  40. char *ai_canonname; /* canonical name for nodename */
  41. struct sockaddr *ai_addr; /* binary address */
  42. struct addrinfo_emu *ai_next; /* next structure in linked list */
  43. };
  44. #define addrinfo addrinfo_emu
  45. int getaddrinfo(const char *nodename, const char *servname,
  46. const struct addrinfo *hints,
  47. struct addrinfo **res);
  48. void freeaddrinfo(struct addrinfo *ai);
  49. #ifndef AI_PASSIVE
  50. #define AI_PASSIVE (1<<1)
  51. #endif
  52. #ifndef EAI_NONAME
  53. #define EAI_NONAME -1
  54. #define EAI_AGAIN -2
  55. #define EAI_FAIL -3
  56. #define EAI_NODATA -4
  57. #define EAI_FAMILY -5
  58. #define EAI_SOCKTYPE -6
  59. #define EAI_SERVICE -7
  60. #define EAI_ADDRFAMILY -8
  61. #define EAI_SYSTEM -10
  62. #define EAI_MEMORY -11
  63. #endif
  64. /* If we don't have getaddrinfo then we probably don't have
  65. sockaddr_storage either (same RFC) so we definitely will not be
  66. doing any IPv6 stuff. Do not use the members of this structure to
  67. retain portability, cast to a sockaddr. */
  68. #define sockaddr_storage sockaddr_in
  69. #endif
  70. // getnameinfo support (glibc2.0 has getaddrinfo only)
  71. #ifndef HAVE_GETNAMEINFO
  72. int getnameinfo(const struct sockaddr *sa, socklen_t salen,
  73. char *host, size_t hostlen,
  74. char *serv, size_t servlen,
  75. int flags);
  76. #ifndef NI_MAXHOST
  77. #define NI_MAXHOST 1025
  78. #define NI_MAXSERV 32
  79. #endif
  80. #ifndef NI_NUMERICHOST
  81. #define NI_NUMERICHOST (1<<0)
  82. #define NI_NUMERICSERV (1<<1)
  83. // #define NI_NOFQDN (1<<2)
  84. #define NI_NAMEREQD (1<<3)
  85. #define NI_DATAGRAM (1<<4)
  86. #endif
  87. #define sockaddr_storage sockaddr_in
  88. #endif
  89. // Glibc 2.0.7 misses this one
  90. #ifndef AI_NUMERICHOST
  91. #define AI_NUMERICHOST 0
  92. #endif
  93. #endif