File: rfc2553emu.cc

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (245 lines) | stat: -rw-r--r-- 6,363 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: rfc2553emu.cc,v 1.8 2001/02/20 07:03:18 jgg Exp $
/* ######################################################################

   RFC 2553 Emulation - Provides emulation for RFC 2553 getaddrinfo,
                        freeaddrinfo and getnameinfo

   This is really C code, it just has a .cc extensions to play nicer with
   the rest of APT.
   
   Originally written by Jason Gunthorpe <jgg@debian.org> and placed into
   the Public Domain, do with it what you will.

   ##################################################################### */
									/*}}}*/
#include "rfc2553emu.h"
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>

#ifndef HAVE_GETADDRINFO
// getaddrinfo - Resolve a hostname					/*{{{*/
// ---------------------------------------------------------------------
/* */
int getaddrinfo(const char *nodename, const char *servname,
		const struct addrinfo *hints,
		struct addrinfo **res)
{
   struct addrinfo **Result = res;
   hostent *Addr;
   unsigned int Port;
   int Proto;
   const char *End;
   char **CurAddr;
   
   // Try to convert the service as a number
   Port = htons(strtol(servname,(char **)&End,0));
   Proto = SOCK_STREAM;
   
   if (hints != 0 && hints->ai_socktype != 0)
      Proto = hints->ai_socktype;
   
   // Not a number, must be a name.
   if (End != servname + strlen(servname))
   {
      struct servent *Srv = 0;
      
      // Do a lookup in the service database
      if (hints == 0 || hints->ai_socktype == SOCK_STREAM)
	 Srv = getservbyname(servname,"tcp");
      if (hints != 0 && hints->ai_socktype == SOCK_DGRAM)
	 Srv = getservbyname(servname,"udp");
      if (Srv == 0)
	 return EAI_NONAME;  
      
      // Get the right protocol
      Port = Srv->s_port;
      if (strcmp(Srv->s_proto,"tcp") == 0)
	 Proto = SOCK_STREAM;
      else
      {
	 if (strcmp(Srv->s_proto,"udp") == 0)
	    Proto = SOCK_DGRAM;
         else
	    return EAI_NONAME;
      }      
      
      if (hints != 0 && hints->ai_socktype != Proto && 
	  hints->ai_socktype != 0)
	 return EAI_SERVICE;
   }
      
   // Hostname lookup, only if this is not a listening socket
   if (hints != 0 && (hints->ai_flags & AI_PASSIVE) != AI_PASSIVE)
   {
      Addr = gethostbyname(nodename);
      if (Addr == 0)
      {
	 if (h_errno == TRY_AGAIN)
	    return EAI_AGAIN;
	 if (h_errno == NO_RECOVERY)
	    return EAI_FAIL;
	 return EAI_NONAME;
      }
   
      // No A records 
      if (Addr->h_addr_list[0] == 0)
	 return EAI_NONAME;
      
      CurAddr = Addr->h_addr_list;
   }
   else
      CurAddr = (char **)&End;    // Fake!
   
   // Start constructing the linked list
   *res = 0;
   for (; *CurAddr != 0; CurAddr++)
   {
      // New result structure
      *Result = (struct addrinfo *)calloc(sizeof(**Result),1);
      if (*Result == 0)
      {
	 freeaddrinfo(*res);
	 return EAI_MEMORY;
      }
      if (*res == 0)
	 *res = *Result;
      
      (*Result)->ai_family = AF_INET;
      (*Result)->ai_socktype = Proto;

      // If we have the IPPROTO defines we can set the protocol field
      #ifdef IPPROTO_TCP
      if (Proto == SOCK_STREAM)
	 (*Result)->ai_protocol = IPPROTO_TCP;
      if (Proto == SOCK_DGRAM)
	 (*Result)->ai_protocol = IPPROTO_UDP;
      #endif

      // Allocate space for the address
      (*Result)->ai_addrlen = sizeof(struct sockaddr_in);
      (*Result)->ai_addr = (struct sockaddr *)calloc(sizeof(sockaddr_in),1);
      if ((*Result)->ai_addr == 0)
      {
	 freeaddrinfo(*res);
	 return EAI_MEMORY;
      }
      
      // Set the address
      ((struct sockaddr_in *)(*Result)->ai_addr)->sin_family = AF_INET;
      ((struct sockaddr_in *)(*Result)->ai_addr)->sin_port = Port;
      
      if (hints != 0 && (hints->ai_flags & AI_PASSIVE) != AI_PASSIVE)
	 ((struct sockaddr_in *)(*Result)->ai_addr)->sin_addr = *(in_addr *)(*CurAddr);
      else
      {
         // Already zerod by calloc.
	 break;
      }
      
      Result = &(*Result)->ai_next;
   }
   
   return 0;
}
									/*}}}*/
// freeaddrinfo - Free the result of getaddrinfo			/*{{{*/
// ---------------------------------------------------------------------
/* */
void freeaddrinfo(struct addrinfo *ai)
{
   struct addrinfo *Tmp;
   while (ai != 0)
   {
      free(ai->ai_addr);
      Tmp = ai;
      ai = ai->ai_next;
      free(ai);
   }
}
									/*}}}*/
#endif // HAVE_GETADDRINFO

#ifndef HAVE_GETNAMEINFO
// getnameinfo - Convert a sockaddr to a string 			/*{{{*/
// ---------------------------------------------------------------------
/* */
int getnameinfo(const struct sockaddr *sa, socklen_t salen,
		char *host, size_t hostlen,
		char *serv, size_t servlen,
		int flags)
{
   struct sockaddr_in *sin = (struct sockaddr_in *)sa;
   
   // This routine only supports internet addresses
   if (sa->sa_family != AF_INET)
      return EAI_ADDRFAMILY;
   
   if (host != 0)
   {
      // Try to resolve the hostname
      if ((flags & NI_NUMERICHOST) != NI_NUMERICHOST)
      {
	 struct hostent *Ent = gethostbyaddr((char *)&sin->sin_addr,sizeof(sin->sin_addr),
					     AF_INET);
	 if (Ent != 0)
	    strncpy(host,Ent->h_name,hostlen);
	 else
	 {
	    if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
	    {
	       if (h_errno == TRY_AGAIN)
		  return EAI_AGAIN;
	       if (h_errno == NO_RECOVERY)
		  return EAI_FAIL;
	       return EAI_NONAME;
	    }

	    flags |= NI_NUMERICHOST;
	 }
      }
      
      // Resolve as a plain numberic
      if ((flags & NI_NUMERICHOST) == NI_NUMERICHOST)
      {
	 strncpy(host,inet_ntoa(sin->sin_addr),hostlen);
      }
   }
   
   if (serv != 0)
   {
      // Try to resolve the hostname
      if ((flags & NI_NUMERICSERV) != NI_NUMERICSERV)
      {
	 struct servent *Ent;
	 if ((flags & NI_DATAGRAM) == NI_DATAGRAM)
	    Ent = getservbyport(ntohs(sin->sin_port),"udp");
	 else
	    Ent = getservbyport(ntohs(sin->sin_port),"tcp");
	 
	 if (Ent != 0)
	    strncpy(serv,Ent->s_name,servlen);
	 else
	 {
	    if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
	       return EAI_NONAME;

	    flags |= NI_NUMERICSERV;
	 }
      }
      
      // Resolve as a plain numberic
      if ((flags & NI_NUMERICSERV) == NI_NUMERICSERV)
      {
	 snprintf(serv,servlen,"%u",ntohs(sin->sin_port));
      }
   }
   
   return 0;
}
									/*}}}*/
#endif // HAVE_GETNAMEINFO