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