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