rfc2553emu.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: rfc2553emu.cc,v 1.8 2001/02/20 07:03:18 jgg Exp $
  4. /* ######################################################################
  5. RFC 2553 Emulation - Provides emulation for RFC 2553 getaddrinfo,
  6. freeaddrinfo and getnameinfo
  7. This is really C code, it just has a .cc extensions to play nicer with
  8. the rest of APT.
  9. Originally written by Jason Gunthorpe <jgg@debian.org> and placed into
  10. the Public Domain, do with it what you will.
  11. ##################################################################### */
  12. /*}}}*/
  13. #include <config.h>
  14. #include <stdlib.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/in.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include "rfc2553emu.h"
  20. #ifndef HAVE_GETADDRINFO
  21. // getaddrinfo - Resolve a hostname /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* */
  24. int getaddrinfo(const char *nodename, const char *servname,
  25. const struct addrinfo *hints,
  26. struct addrinfo **res)
  27. {
  28. struct addrinfo **Result = res;
  29. hostent *Addr;
  30. unsigned int Port;
  31. int Proto;
  32. const char *End;
  33. char **CurAddr;
  34. // Try to convert the service as a number
  35. Port = htons(strtol(servname,(char **)&End,0));
  36. Proto = SOCK_STREAM;
  37. if (hints != 0 && hints->ai_socktype != 0)
  38. Proto = hints->ai_socktype;
  39. // Not a number, must be a name.
  40. if (End != servname + strlen(servname))
  41. {
  42. struct servent *Srv = 0;
  43. // Do a lookup in the service database
  44. if (hints == 0 || hints->ai_socktype == SOCK_STREAM)
  45. Srv = getservbyname(servname,"tcp");
  46. if (hints != 0 && hints->ai_socktype == SOCK_DGRAM)
  47. Srv = getservbyname(servname,"udp");
  48. if (Srv == 0)
  49. return EAI_NONAME;
  50. // Get the right protocol
  51. Port = Srv->s_port;
  52. if (strcmp(Srv->s_proto,"tcp") == 0)
  53. Proto = SOCK_STREAM;
  54. else
  55. {
  56. if (strcmp(Srv->s_proto,"udp") == 0)
  57. Proto = SOCK_DGRAM;
  58. else
  59. return EAI_NONAME;
  60. }
  61. if (hints != 0 && hints->ai_socktype != Proto &&
  62. hints->ai_socktype != 0)
  63. return EAI_SERVICE;
  64. }
  65. // Hostname lookup, only if this is not a listening socket
  66. if (hints != 0 && (hints->ai_flags & AI_PASSIVE) != AI_PASSIVE)
  67. {
  68. Addr = gethostbyname(nodename);
  69. if (Addr == 0)
  70. {
  71. if (h_errno == TRY_AGAIN)
  72. return EAI_AGAIN;
  73. if (h_errno == NO_RECOVERY)
  74. return EAI_FAIL;
  75. return EAI_NONAME;
  76. }
  77. // No A records
  78. if (Addr->h_addr_list[0] == 0)
  79. return EAI_NONAME;
  80. CurAddr = Addr->h_addr_list;
  81. }
  82. else
  83. CurAddr = (char **)&End; // Fake!
  84. // Start constructing the linked list
  85. *res = 0;
  86. for (; *CurAddr != 0; CurAddr++)
  87. {
  88. // New result structure
  89. *Result = (struct addrinfo *)calloc(sizeof(**Result),1);
  90. if (*Result == 0)
  91. {
  92. freeaddrinfo(*res);
  93. return EAI_MEMORY;
  94. }
  95. if (*res == 0)
  96. *res = *Result;
  97. (*Result)->ai_family = AF_INET;
  98. (*Result)->ai_socktype = Proto;
  99. // If we have the IPPROTO defines we can set the protocol field
  100. #ifdef IPPROTO_TCP
  101. if (Proto == SOCK_STREAM)
  102. (*Result)->ai_protocol = IPPROTO_TCP;
  103. if (Proto == SOCK_DGRAM)
  104. (*Result)->ai_protocol = IPPROTO_UDP;
  105. #endif
  106. // Allocate space for the address
  107. (*Result)->ai_addrlen = sizeof(struct sockaddr_in);
  108. (*Result)->ai_addr = (struct sockaddr *)calloc(sizeof(sockaddr_in),1);
  109. if ((*Result)->ai_addr == 0)
  110. {
  111. freeaddrinfo(*res);
  112. return EAI_MEMORY;
  113. }
  114. // Set the address
  115. ((struct sockaddr_in *)(*Result)->ai_addr)->sin_family = AF_INET;
  116. ((struct sockaddr_in *)(*Result)->ai_addr)->sin_port = Port;
  117. if (hints != 0 && (hints->ai_flags & AI_PASSIVE) != AI_PASSIVE)
  118. ((struct sockaddr_in *)(*Result)->ai_addr)->sin_addr = *(in_addr *)(*CurAddr);
  119. else
  120. {
  121. // Already zerod by calloc.
  122. break;
  123. }
  124. Result = &(*Result)->ai_next;
  125. }
  126. return 0;
  127. }
  128. /*}}}*/
  129. // freeaddrinfo - Free the result of getaddrinfo /*{{{*/
  130. // ---------------------------------------------------------------------
  131. /* */
  132. void freeaddrinfo(struct addrinfo *ai)
  133. {
  134. while (ai != 0)
  135. {
  136. free(ai->ai_addr);
  137. ai = ai->ai_next;
  138. free(ai);
  139. }
  140. }
  141. /*}}}*/
  142. #endif // HAVE_GETADDRINFO
  143. #ifndef HAVE_GETNAMEINFO
  144. // getnameinfo - Convert a sockaddr to a string /*{{{*/
  145. // ---------------------------------------------------------------------
  146. /* */
  147. int getnameinfo(const struct sockaddr *sa, socklen_t salen,
  148. char *host, size_t hostlen,
  149. char *serv, size_t servlen,
  150. int flags)
  151. {
  152. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  153. // This routine only supports internet addresses
  154. if (sa->sa_family != AF_INET)
  155. return EAI_ADDRFAMILY;
  156. if (host != 0)
  157. {
  158. // Try to resolve the hostname
  159. if ((flags & NI_NUMERICHOST) != NI_NUMERICHOST)
  160. {
  161. struct hostent *Ent = gethostbyaddr((char *)&sin->sin_addr,sizeof(sin->sin_addr),
  162. AF_INET);
  163. if (Ent != 0)
  164. strncpy(host,Ent->h_name,hostlen);
  165. else
  166. {
  167. if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
  168. {
  169. if (h_errno == TRY_AGAIN)
  170. return EAI_AGAIN;
  171. if (h_errno == NO_RECOVERY)
  172. return EAI_FAIL;
  173. return EAI_NONAME;
  174. }
  175. flags |= NI_NUMERICHOST;
  176. }
  177. }
  178. // Resolve as a plain numberic
  179. if ((flags & NI_NUMERICHOST) == NI_NUMERICHOST)
  180. {
  181. strncpy(host,inet_ntoa(sin->sin_addr),hostlen);
  182. }
  183. }
  184. if (serv != 0)
  185. {
  186. // Try to resolve the hostname
  187. if ((flags & NI_NUMERICSERV) != NI_NUMERICSERV)
  188. {
  189. struct servent *Ent;
  190. if ((flags & NI_DATAGRAM) == NI_DATAGRAM)
  191. Ent = getservbyport(ntohs(sin->sin_port),"udp");
  192. else
  193. Ent = getservbyport(ntohs(sin->sin_port),"tcp");
  194. if (Ent != 0)
  195. strncpy(serv,Ent->s_name,servlen);
  196. else
  197. {
  198. if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
  199. return EAI_NONAME;
  200. flags |= NI_NUMERICSERV;
  201. }
  202. }
  203. // Resolve as a plain numberic
  204. if ((flags & NI_NUMERICSERV) == NI_NUMERICSERV)
  205. {
  206. snprintf(serv,servlen,"%u",ntohs(sin->sin_port));
  207. }
  208. }
  209. return 0;
  210. }
  211. /*}}}*/
  212. #endif // HAVE_GETNAMEINFO