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