rfc2553emu.cc 5.8 KB

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