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