apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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