]> git.saurik.com Git - apt.git/blame - methods/connect.cc
Merge branch 'debian/experimental' into feature/srv-records
[apt.git] / methods / connect.cc
CommitLineData
0837bd25
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: connect.cc,v 1.10.2.1 2004/01/16 18:58:50 mdz Exp $
0837bd25
AL
4/* ######################################################################
5
6 Connect - Replacement connect call
7da2b375
AL
7
8 This was originally authored by Jason Gunthorpe <jgg@debian.org>
9 and is placed in the Public Domain, do with it what you will.
10
0837bd25
AL
11 ##################################################################### */
12 /*}}}*/
13// Include Files /*{{{*/
ea542140
DK
14#include <config.h>
15
0837bd25
AL
16#include <apt-pkg/error.h>
17#include <apt-pkg/fileutl.h>
472ff00e
DK
18#include <apt-pkg/strutl.h>
19#include <apt-pkg/acquire-method.h>
45d02095 20#include <apt-pkg/configuration.h>
a01695e8 21#include <apt-pkg/srvrec.h>
0837bd25
AL
22
23#include <stdio.h>
24#include <errno.h>
25#include <unistd.h>
36280399 26#include <sstream>
453b82a3 27#include <string.h>
654881fb
MV
28#include<set>
29#include<string>
30
0837bd25
AL
31// Internet stuff
32#include <netinet/in.h>
33#include <sys/socket.h>
34#include <arpa/inet.h>
35#include <netdb.h>
36
ea542140 37#include "connect.h"
0837bd25 38#include "rfc2553emu.h"
d77559ac 39#include <apti18n.h>
0837bd25
AL
40 /*}}}*/
41
8f3ba4e8 42static std::string LastHost;
0837bd25
AL
43static int LastPort = 0;
44static struct addrinfo *LastHostAddr = 0;
45static struct addrinfo *LastUsed = 0;
46
a01695e8
MV
47static std::vector<SrvRec> SrvRecords;
48static int LastSrvRecord = 0;
49
654881fb 50// Set of IP/hostnames that we timed out before or couldn't resolve
8f3ba4e8 51static std::set<std::string> bad_addr;
654881fb 52
b2e465d6
AL
53// RotateDNS - Select a new server from a DNS rotation /*{{{*/
54// ---------------------------------------------------------------------
55/* This is called during certain errors in order to recover by selecting a
56 new server */
57void RotateDNS()
58{
59 if (LastUsed != 0 && LastUsed->ai_next != 0)
60 LastUsed = LastUsed->ai_next;
61 else
62 LastUsed = LastHostAddr;
63}
64 /*}}}*/
0837bd25
AL
65// DoConnect - Attempt a connect operation /*{{{*/
66// ---------------------------------------------------------------------
67/* This helper function attempts a connection to a single address. */
8f3ba4e8 68static bool DoConnect(struct addrinfo *Addr,std::string Host,
0837bd25
AL
69 unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner)
70{
71 // Show a status indicator
72 char Name[NI_MAXHOST];
28006885 73 char Service[NI_MAXSERV];
b2e465d6
AL
74
75 Name[0] = 0;
28006885 76 Service[0] = 0;
0837bd25 77 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
28006885
AL
78 Name,sizeof(Name),Service,sizeof(Service),
79 NI_NUMERICHOST|NI_NUMERICSERV);
dc738e7a 80 Owner->Status(_("Connecting to %s (%s)"),Host.c_str(),Name);
b2e465d6 81
654881fb 82 // if that addr did timeout before, we do not try it again
8f3ba4e8 83 if(bad_addr.find(std::string(Name)) != bad_addr.end())
654881fb
MV
84 return false;
85
b2e465d6
AL
86 /* If this is an IP rotation store the IP we are using.. If something goes
87 wrong this will get tacked onto the end of the error message */
88 if (LastHostAddr->ai_next != 0)
89 {
36280399
MV
90 std::stringstream ss;
91 ioprintf(ss, _("[IP: %s %s]"),Name,Service);
92 Owner->SetIP(ss.str());
93 }
b2e465d6 94
0837bd25
AL
95 // Get a socket
96 if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
97 Addr->ai_protocol)) < 0)
dc738e7a 98 return _error->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
b2e465d6 99 Name,Addr->ai_family,Addr->ai_socktype,Addr->ai_protocol);
0837bd25
AL
100
101 SetNonBlock(Fd,true);
102 if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 &&
103 errno != EINPROGRESS)
dc738e7a
AL
104 return _error->Errno("connect",_("Cannot initiate the connection "
105 "to %s:%s (%s)."),Host.c_str(),Service,Name);
0837bd25
AL
106
107 /* This implements a timeout for connect by opening the connection
108 nonblocking */
24057ad6 109 if (WaitFd(Fd,true,TimeOut) == false) {
8f3ba4e8 110 bad_addr.insert(bad_addr.begin(), std::string(Name));
36280399 111 Owner->SetFailReason("Timeout");
dc738e7a
AL
112 return _error->Error(_("Could not connect to %s:%s (%s), "
113 "connection timed out"),Host.c_str(),Service,Name);
24057ad6 114 }
b2e465d6 115
0837bd25
AL
116 // Check the socket for an error condition
117 unsigned int Err;
118 unsigned int Len = sizeof(Err);
119 if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0)
dc738e7a 120 return _error->Errno("getsockopt",_("Failed"));
0837bd25
AL
121
122 if (Err != 0)
28006885
AL
123 {
124 errno = Err;
75dd8af1 125 if(errno == ECONNREFUSED)
36280399 126 Owner->SetFailReason("ConnectionRefused");
785b920b 127 else if (errno == ETIMEDOUT)
df3226c1 128 Owner->SetFailReason("ConnectionTimedOut");
8f3ba4e8 129 bad_addr.insert(bad_addr.begin(), std::string(Name));
dc738e7a 130 return _error->Errno("connect",_("Could not connect to %s:%s (%s)."),Host.c_str(),
28006885
AL
131 Service,Name);
132 }
133
0837bd25
AL
134 return true;
135}
136 /*}}}*/
28006885 137
cc480014
MV
138// Connect to a given Hostname
139bool ConnectAfterSrvRecords(std::string Host,int Port,const char *Service,
140 int DefPort,int &Fd,
141 unsigned long TimeOut,pkgAcqMethod *Owner)
142{
28006885
AL
143 // Convert the port name/number
144 char ServStr[300];
145 if (Port != 0)
9ce3cfc9 146 snprintf(ServStr,sizeof(ServStr),"%i", Port);
28006885 147 else
9ce3cfc9 148 snprintf(ServStr,sizeof(ServStr),"%s", Service);
0837bd25
AL
149
150 /* We used a cached address record.. Yes this is against the spec but
151 the way we have setup our rotating dns suggests that this is more
152 sensible */
153 if (LastHost != Host || LastPort != Port)
154 {
dc738e7a 155 Owner->Status(_("Connecting to %s"),Host.c_str());
0837bd25 156
0837bd25
AL
157 // Free the old address structure
158 if (LastHostAddr != 0)
159 {
160 freeaddrinfo(LastHostAddr);
161 LastHostAddr = 0;
28006885 162 LastUsed = 0;
0837bd25
AL
163 }
164
165 // We only understand SOCK_STREAM sockets.
166 struct addrinfo Hints;
167 memset(&Hints,0,sizeof(Hints));
168 Hints.ai_socktype = SOCK_STREAM;
d746ad6e 169 Hints.ai_flags = AI_ADDRCONFIG;
28006885 170 Hints.ai_protocol = 0;
0837bd25 171
45d02095
MV
172 if(_config->FindB("Acquire::ForceIPv4", false) == true)
173 Hints.ai_family = AF_INET;
174 else if(_config->FindB("Acquire::ForceIPv6", false) == true)
175 Hints.ai_family = AF_INET6;
176 else
177 Hints.ai_family = AF_UNSPEC;
178
654881fb
MV
179 // if we couldn't resolve the host before, we don't try now
180 if(bad_addr.find(Host) != bad_addr.end())
181 return _error->Error(_("Could not resolve '%s'"),Host.c_str());
182
0837bd25 183 // Resolve both the host and service simultaneously
9505213b 184 while (1)
c141b9a9 185 {
9505213b 186 int Res;
28006885 187 if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
9505213b
AL
188 LastHostAddr == 0)
189 {
72472b95 190 if (Res == EAI_NONAME || Res == EAI_SERVICE)
9505213b
AL
191 {
192 if (DefPort != 0)
193 {
9ce3cfc9 194 snprintf(ServStr, sizeof(ServStr), "%i", DefPort);
9505213b
AL
195 DefPort = 0;
196 continue;
197 }
654881fb 198 bad_addr.insert(bad_addr.begin(), Host);
59271f62 199 Owner->SetFailReason("ResolveFailure");
dc738e7a 200 return _error->Error(_("Could not resolve '%s'"),Host.c_str());
9505213b
AL
201 }
202
4fe6e0c2 203 if (Res == EAI_AGAIN)
25182152 204 {
36280399 205 Owner->SetFailReason("TmpResolveFailure");
dc738e7a 206 return _error->Error(_("Temporary failure resolving '%s'"),
4fe6e0c2 207 Host.c_str());
25182152 208 }
945d2a8a 209 if (Res == EAI_SYSTEM)
5cf466f4
MV
210 return _error->Errno("getaddrinfo", _("System error resolving '%s:%s'"),
211 Host.c_str(),ServStr);
ce26dee7
DK
212 return _error->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
213 Host.c_str(),ServStr,Res,gai_strerror(Res));
9505213b
AL
214 }
215 break;
c141b9a9
AL
216 }
217
0837bd25
AL
218 LastHost = Host;
219 LastPort = Port;
0837bd25
AL
220 }
221
28006885 222 // When we have an IP rotation stay with the last IP.
0837bd25
AL
223 struct addrinfo *CurHost = LastHostAddr;
224 if (LastUsed != 0)
225 CurHost = LastUsed;
226
227 while (CurHost != 0)
228 {
229 if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
230 {
231 LastUsed = CurHost;
232 return true;
233 }
234 close(Fd);
235 Fd = -1;
236
28006885
AL
237 // Ignore UNIX domain sockets
238 do
239 {
240 CurHost = CurHost->ai_next;
241 }
242 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
b2e465d6
AL
243
244 /* If we reached the end of the search list then wrap around to the
245 start */
246 if (CurHost == 0 && LastUsed != 0)
247 CurHost = LastHostAddr;
248
249 // Reached the end of the search cycle
250 if (CurHost == LastUsed)
251 break;
252
0837bd25
AL
253 if (CurHost != 0)
254 _error->Discard();
b2e465d6 255 }
28006885 256
dd1fd92b 257 if (_error->PendingError() == true)
b2e465d6 258 return false;
cdd5a135 259 return _error->Error(_("Unable to connect to %s:%s:"),Host.c_str(),ServStr);
0837bd25
AL
260}
261 /*}}}*/
cc480014
MV
262// Connect - Connect to a server /*{{{*/
263// ---------------------------------------------------------------------
264/* Performs a connection to the server */
265bool Connect(std::string Host,int Port,const char *Service,
266 int DefPort,int &Fd,
267 unsigned long TimeOut,pkgAcqMethod *Owner)
268{
269#if 0
270 if (_error->PendingError() == true)
271 return false;
272#endif
273
274 if(LastHost != Host || LastPort != Port)
275 {
276 SrvRecords.clear();
277 bool res = GetSrvRecords(Host, DefPort, SrvRecords);
278 }
279 if(SrvRecords.size() == 0)
280 return ConnectAfterSrvRecords(Host, Port, Service, DefPort, Fd,
281 TimeOut, Owner);
282
283 bool connected = false;
284 while(SrvRecords.size() > 0)
285 {
286 Host = SrvRecords[0].target;
287 connected = ConnectAfterSrvRecords(Host, Port, Service, DefPort, Fd,
288 TimeOut, Owner);
289 if(connected == true)
290 return true;
291
292 // we couldn't connect to this one, use the next
293 SrvRecords.erase(SrvRecords.begin());
294 }
295 return false;
296}