rfc2553emu.cc 5.8 KB

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