rfc2553emu.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. struct addrinfo *Tmp;
  135. while (ai != 0)
  136. {
  137. free(ai->ai_addr);
  138. Tmp = ai;
  139. ai = ai->ai_next;
  140. free(ai);
  141. }
  142. }
  143. /*}}}*/
  144. #endif // HAVE_GETADDRINFO
  145. #ifndef HAVE_GETNAMEINFO
  146. // getnameinfo - Convert a sockaddr to a string /*{{{*/
  147. // ---------------------------------------------------------------------
  148. /* */
  149. int getnameinfo(const struct sockaddr *sa, socklen_t salen,
  150. char *host, size_t hostlen,
  151. char *serv, size_t servlen,
  152. int flags)
  153. {
  154. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  155. // This routine only supports internet addresses
  156. if (sa->sa_family != AF_INET)
  157. return EAI_ADDRFAMILY;
  158. if (host != 0)
  159. {
  160. // Try to resolve the hostname
  161. if ((flags & NI_NUMERICHOST) != NI_NUMERICHOST)
  162. {
  163. struct hostent *Ent = gethostbyaddr((char *)&sin->sin_addr,sizeof(sin->sin_addr),
  164. AF_INET);
  165. if (Ent != 0)
  166. strncpy(host,Ent->h_name,hostlen);
  167. else
  168. {
  169. if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
  170. {
  171. if (h_errno == TRY_AGAIN)
  172. return EAI_AGAIN;
  173. if (h_errno == NO_RECOVERY)
  174. return EAI_FAIL;
  175. return EAI_NONAME;
  176. }
  177. flags |= NI_NUMERICHOST;
  178. }
  179. }
  180. // Resolve as a plain numberic
  181. if ((flags & NI_NUMERICHOST) == NI_NUMERICHOST)
  182. {
  183. strncpy(host,inet_ntoa(sin->sin_addr),hostlen);
  184. }
  185. }
  186. if (serv != 0)
  187. {
  188. // Try to resolve the hostname
  189. if ((flags & NI_NUMERICSERV) != NI_NUMERICSERV)
  190. {
  191. struct servent *Ent;
  192. if ((flags & NI_DATAGRAM) == NI_DATAGRAM)
  193. Ent = getservbyport(ntohs(sin->sin_port),"udp");
  194. else
  195. Ent = getservbyport(ntohs(sin->sin_port),"tcp");
  196. if (Ent != 0)
  197. strncpy(serv,Ent->s_name,servlen);
  198. else
  199. {
  200. if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
  201. return EAI_NONAME;
  202. flags |= NI_NUMERICSERV;
  203. }
  204. }
  205. // Resolve as a plain numberic
  206. if ((flags & NI_NUMERICSERV) == NI_NUMERICSERV)
  207. {
  208. snprintf(serv,servlen,"%u",ntohs(sin->sin_port));
  209. }
  210. }
  211. return 0;
  212. }
  213. /*}}}*/
  214. #endif // HAVE_GETNAMEINFO