]>
git.saurik.com Git - apt.git/blob - methods/connect.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: connect.cc,v 1.10.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 Connect - Replacement connect call
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.
11 ##################################################################### */
13 // Include Files /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/strutl.h>
19 #include <apt-pkg/acquire-method.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/srvrec.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
38 #include "rfc2553emu.h"
42 static std::string LastHost
;
43 static int LastPort
= 0;
44 static struct addrinfo
*LastHostAddr
= 0;
45 static struct addrinfo
*LastUsed
= 0;
47 static std::vector
<SrvRec
> SrvRecords
;
48 static int LastSrvRecord
= 0;
50 // Set of IP/hostnames that we timed out before or couldn't resolve
51 static std::set
<std::string
> bad_addr
;
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
59 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
60 LastUsed
= LastUsed
->ai_next
;
62 LastUsed
= LastHostAddr
;
65 // DoConnect - Attempt a connect operation /*{{{*/
66 // ---------------------------------------------------------------------
67 /* This helper function attempts a connection to a single address. */
68 static bool DoConnect(struct addrinfo
*Addr
,std::string Host
,
69 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
71 // Show a status indicator
72 char Name
[NI_MAXHOST
];
73 char Service
[NI_MAXSERV
];
77 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
78 Name
,sizeof(Name
),Service
,sizeof(Service
),
79 NI_NUMERICHOST
|NI_NUMERICSERV
);
80 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
82 // if that addr did timeout before, we do not try it again
83 if(bad_addr
.find(std::string(Name
)) != bad_addr
.end())
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)
91 ioprintf(ss
, _("[IP: %s %s]"),Name
,Service
);
92 Owner
->SetIP(ss
.str());
96 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
97 Addr
->ai_protocol
)) < 0)
98 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
99 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
101 SetNonBlock(Fd
,true);
102 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
103 errno
!= EINPROGRESS
)
104 return _error
->Errno("connect",_("Cannot initiate the connection "
105 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
107 /* This implements a timeout for connect by opening the connection
109 if (WaitFd(Fd
,true,TimeOut
) == false) {
110 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
111 Owner
->SetFailReason("Timeout");
112 return _error
->Error(_("Could not connect to %s:%s (%s), "
113 "connection timed out"),Host
.c_str(),Service
,Name
);
116 // Check the socket for an error condition
118 unsigned int Len
= sizeof(Err
);
119 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
120 return _error
->Errno("getsockopt",_("Failed"));
125 if(errno
== ECONNREFUSED
)
126 Owner
->SetFailReason("ConnectionRefused");
127 else if (errno
== ETIMEDOUT
)
128 Owner
->SetFailReason("ConnectionTimedOut");
129 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
130 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
138 // Connect to a given Hostname
139 bool ConnectToHostname(std::string Host
,int Port
,const char *Service
,
141 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
143 // Convert the port name/number
146 snprintf(ServStr
,sizeof(ServStr
),"%i", Port
);
148 snprintf(ServStr
,sizeof(ServStr
),"%s", Service
);
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
153 if (LastHost
!= Host
|| LastPort
!= Port
)
155 Owner
->Status(_("Connecting to %s"),Host
.c_str());
157 // Free the old address structure
158 if (LastHostAddr
!= 0)
160 freeaddrinfo(LastHostAddr
);
165 // We only understand SOCK_STREAM sockets.
166 struct addrinfo Hints
;
167 memset(&Hints
,0,sizeof(Hints
));
168 Hints
.ai_socktype
= SOCK_STREAM
;
169 Hints
.ai_flags
= AI_ADDRCONFIG
;
170 Hints
.ai_protocol
= 0;
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
;
177 Hints
.ai_family
= AF_UNSPEC
;
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());
183 // Resolve both the host and service simultaneously
187 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
190 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
194 snprintf(ServStr
, sizeof(ServStr
), "%i", DefPort
);
198 bad_addr
.insert(bad_addr
.begin(), Host
);
199 Owner
->SetFailReason("ResolveFailure");
200 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
203 if (Res
== EAI_AGAIN
)
205 Owner
->SetFailReason("TmpResolveFailure");
206 return _error
->Error(_("Temporary failure resolving '%s'"),
209 if (Res
== EAI_SYSTEM
)
210 return _error
->Errno("getaddrinfo", _("System error resolving '%s:%s'"),
211 Host
.c_str(),ServStr
);
212 return _error
->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
213 Host
.c_str(),ServStr
,Res
,gai_strerror(Res
));
222 // When we have an IP rotation stay with the last IP.
223 struct addrinfo
*CurHost
= LastHostAddr
;
229 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
237 // Ignore UNIX domain sockets
240 CurHost
= CurHost
->ai_next
;
242 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
244 /* If we reached the end of the search list then wrap around to the
246 if (CurHost
== 0 && LastUsed
!= 0)
247 CurHost
= LastHostAddr
;
249 // Reached the end of the search cycle
250 if (CurHost
== LastUsed
)
257 if (_error
->PendingError() == true)
259 return _error
->Error(_("Unable to connect to %s:%s:"),Host
.c_str(),ServStr
);
262 // Connect - Connect to a server /*{{{*/
263 // ---------------------------------------------------------------------
264 /* Performs a connection to the server (including SRV record lookup) */
265 bool Connect(std::string Host
,int Port
,const char *Service
,
267 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
269 if (_error
->PendingError() == true)
272 if(LastHost
!= Host
|| LastPort
!= Port
)
275 if (_config
->FindB("Acquire::EnableSrvRecords", true) == true)
276 GetSrvRecords(Host
, DefPort
, SrvRecords
);
278 // we have no SrvRecords for this host, connect right away
279 if(SrvRecords
.size() == 0)
280 return ConnectToHostname(Host
, Port
, Service
, DefPort
, Fd
,
283 // try to connect in the priority order of the srv records
284 while(SrvRecords
.size() > 0)
286 Host
= PopFromSrvRecs(SrvRecords
).target
;
287 if(ConnectToHostname(Host
, Port
, Service
, DefPort
, Fd
, TimeOut
, Owner
))
290 // we couldn't connect to this one, use the next
291 SrvRecords
.erase(SrvRecords
.begin());