]>
Commit | Line | Data |
---|---|---|
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 | 42 | static std::string LastHost; |
0837bd25 AL |
43 | static int LastPort = 0; |
44 | static struct addrinfo *LastHostAddr = 0; | |
45 | static struct addrinfo *LastUsed = 0; | |
46 | ||
a01695e8 | 47 | static std::vector<SrvRec> SrvRecords; |
a01695e8 | 48 | |
654881fb | 49 | // Set of IP/hostnames that we timed out before or couldn't resolve |
8f3ba4e8 | 50 | static std::set<std::string> bad_addr; |
654881fb | 51 | |
b2e465d6 AL |
52 | // RotateDNS - Select a new server from a DNS rotation /*{{{*/ |
53 | // --------------------------------------------------------------------- | |
54 | /* This is called during certain errors in order to recover by selecting a | |
55 | new server */ | |
56 | void RotateDNS() | |
57 | { | |
58 | if (LastUsed != 0 && LastUsed->ai_next != 0) | |
59 | LastUsed = LastUsed->ai_next; | |
60 | else | |
61 | LastUsed = LastHostAddr; | |
62 | } | |
63 | /*}}}*/ | |
0837bd25 AL |
64 | // DoConnect - Attempt a connect operation /*{{{*/ |
65 | // --------------------------------------------------------------------- | |
66 | /* This helper function attempts a connection to a single address. */ | |
8f3ba4e8 | 67 | static bool DoConnect(struct addrinfo *Addr,std::string Host, |
0837bd25 AL |
68 | unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner) |
69 | { | |
70 | // Show a status indicator | |
71 | char Name[NI_MAXHOST]; | |
28006885 | 72 | char Service[NI_MAXSERV]; |
b2e465d6 AL |
73 | |
74 | Name[0] = 0; | |
28006885 | 75 | Service[0] = 0; |
0837bd25 | 76 | getnameinfo(Addr->ai_addr,Addr->ai_addrlen, |
28006885 AL |
77 | Name,sizeof(Name),Service,sizeof(Service), |
78 | NI_NUMERICHOST|NI_NUMERICSERV); | |
dc738e7a | 79 | Owner->Status(_("Connecting to %s (%s)"),Host.c_str(),Name); |
b2e465d6 | 80 | |
654881fb | 81 | // if that addr did timeout before, we do not try it again |
8f3ba4e8 | 82 | if(bad_addr.find(std::string(Name)) != bad_addr.end()) |
654881fb MV |
83 | return false; |
84 | ||
b2e465d6 AL |
85 | /* If this is an IP rotation store the IP we are using.. If something goes |
86 | wrong this will get tacked onto the end of the error message */ | |
87 | if (LastHostAddr->ai_next != 0) | |
88 | { | |
36280399 MV |
89 | std::stringstream ss; |
90 | ioprintf(ss, _("[IP: %s %s]"),Name,Service); | |
91 | Owner->SetIP(ss.str()); | |
92 | } | |
b2e465d6 | 93 | |
0837bd25 AL |
94 | // Get a socket |
95 | if ((Fd = socket(Addr->ai_family,Addr->ai_socktype, | |
96 | Addr->ai_protocol)) < 0) | |
dc738e7a | 97 | return _error->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"), |
b2e465d6 | 98 | Name,Addr->ai_family,Addr->ai_socktype,Addr->ai_protocol); |
0837bd25 AL |
99 | |
100 | SetNonBlock(Fd,true); | |
101 | if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 && | |
102 | errno != EINPROGRESS) | |
dc738e7a AL |
103 | return _error->Errno("connect",_("Cannot initiate the connection " |
104 | "to %s:%s (%s)."),Host.c_str(),Service,Name); | |
0837bd25 AL |
105 | |
106 | /* This implements a timeout for connect by opening the connection | |
107 | nonblocking */ | |
24057ad6 | 108 | if (WaitFd(Fd,true,TimeOut) == false) { |
8f3ba4e8 | 109 | bad_addr.insert(bad_addr.begin(), std::string(Name)); |
36280399 | 110 | Owner->SetFailReason("Timeout"); |
dc738e7a AL |
111 | return _error->Error(_("Could not connect to %s:%s (%s), " |
112 | "connection timed out"),Host.c_str(),Service,Name); | |
24057ad6 | 113 | } |
b2e465d6 | 114 | |
0837bd25 AL |
115 | // Check the socket for an error condition |
116 | unsigned int Err; | |
117 | unsigned int Len = sizeof(Err); | |
118 | if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0) | |
dc738e7a | 119 | return _error->Errno("getsockopt",_("Failed")); |
0837bd25 AL |
120 | |
121 | if (Err != 0) | |
28006885 AL |
122 | { |
123 | errno = Err; | |
75dd8af1 | 124 | if(errno == ECONNREFUSED) |
36280399 | 125 | Owner->SetFailReason("ConnectionRefused"); |
785b920b | 126 | else if (errno == ETIMEDOUT) |
df3226c1 | 127 | Owner->SetFailReason("ConnectionTimedOut"); |
8f3ba4e8 | 128 | bad_addr.insert(bad_addr.begin(), std::string(Name)); |
dc738e7a | 129 | return _error->Errno("connect",_("Could not connect to %s:%s (%s)."),Host.c_str(), |
28006885 AL |
130 | Service,Name); |
131 | } | |
132 | ||
0837bd25 AL |
133 | return true; |
134 | } | |
135 | /*}}}*/ | |
b830f576 DK |
136 | // Connect to a given Hostname /*{{{*/ |
137 | static bool ConnectToHostname(std::string const &Host, int const Port, | |
138 | const char * const Service, int DefPort, int &Fd, | |
139 | unsigned long const TimeOut, pkgAcqMethod * const Owner) | |
cc480014 | 140 | { |
28006885 AL |
141 | // Convert the port name/number |
142 | char ServStr[300]; | |
143 | if (Port != 0) | |
9ce3cfc9 | 144 | snprintf(ServStr,sizeof(ServStr),"%i", Port); |
28006885 | 145 | else |
9ce3cfc9 | 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; | |
23d35ec1 | 167 | Hints.ai_flags = 0; |
920c3672 DK |
168 | if (_config->FindB("Acquire::Connect::IDN", true) == true) |
169 | Hints.ai_flags |= AI_IDN; | |
23d35ec1 DK |
170 | // see getaddrinfo(3): only return address if system has such a address configured |
171 | // useful if system is ipv4 only, to not get ipv6, but that fails if the system has | |
172 | // no address configured: e.g. offline and trying to connect to localhost. | |
173 | if (_config->FindB("Acquire::Connect::AddrConfig", true) == true) | |
174 | Hints.ai_flags |= AI_ADDRCONFIG; | |
28006885 | 175 | Hints.ai_protocol = 0; |
0837bd25 | 176 | |
45d02095 MV |
177 | if(_config->FindB("Acquire::ForceIPv4", false) == true) |
178 | Hints.ai_family = AF_INET; | |
179 | else if(_config->FindB("Acquire::ForceIPv6", false) == true) | |
180 | Hints.ai_family = AF_INET6; | |
181 | else | |
182 | Hints.ai_family = AF_UNSPEC; | |
183 | ||
654881fb MV |
184 | // if we couldn't resolve the host before, we don't try now |
185 | if(bad_addr.find(Host) != bad_addr.end()) | |
186 | return _error->Error(_("Could not resolve '%s'"),Host.c_str()); | |
187 | ||
0837bd25 | 188 | // Resolve both the host and service simultaneously |
9505213b | 189 | while (1) |
c141b9a9 | 190 | { |
9505213b | 191 | int Res; |
28006885 | 192 | if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 || |
9505213b AL |
193 | LastHostAddr == 0) |
194 | { | |
72472b95 | 195 | if (Res == EAI_NONAME || Res == EAI_SERVICE) |
9505213b AL |
196 | { |
197 | if (DefPort != 0) | |
198 | { | |
9ce3cfc9 | 199 | snprintf(ServStr, sizeof(ServStr), "%i", DefPort); |
9505213b AL |
200 | DefPort = 0; |
201 | continue; | |
202 | } | |
654881fb | 203 | bad_addr.insert(bad_addr.begin(), Host); |
59271f62 | 204 | Owner->SetFailReason("ResolveFailure"); |
dc738e7a | 205 | return _error->Error(_("Could not resolve '%s'"),Host.c_str()); |
9505213b AL |
206 | } |
207 | ||
4fe6e0c2 | 208 | if (Res == EAI_AGAIN) |
25182152 | 209 | { |
36280399 | 210 | Owner->SetFailReason("TmpResolveFailure"); |
dc738e7a | 211 | return _error->Error(_("Temporary failure resolving '%s'"), |
4fe6e0c2 | 212 | Host.c_str()); |
25182152 | 213 | } |
945d2a8a | 214 | if (Res == EAI_SYSTEM) |
5cf466f4 MV |
215 | return _error->Errno("getaddrinfo", _("System error resolving '%s:%s'"), |
216 | Host.c_str(),ServStr); | |
ce26dee7 DK |
217 | return _error->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"), |
218 | Host.c_str(),ServStr,Res,gai_strerror(Res)); | |
9505213b AL |
219 | } |
220 | break; | |
c141b9a9 AL |
221 | } |
222 | ||
0837bd25 AL |
223 | LastHost = Host; |
224 | LastPort = Port; | |
0837bd25 AL |
225 | } |
226 | ||
28006885 | 227 | // When we have an IP rotation stay with the last IP. |
0837bd25 AL |
228 | struct addrinfo *CurHost = LastHostAddr; |
229 | if (LastUsed != 0) | |
230 | CurHost = LastUsed; | |
231 | ||
232 | while (CurHost != 0) | |
233 | { | |
234 | if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true) | |
235 | { | |
236 | LastUsed = CurHost; | |
237 | return true; | |
238 | } | |
239 | close(Fd); | |
240 | Fd = -1; | |
241 | ||
28006885 AL |
242 | // Ignore UNIX domain sockets |
243 | do | |
244 | { | |
245 | CurHost = CurHost->ai_next; | |
246 | } | |
247 | while (CurHost != 0 && CurHost->ai_family == AF_UNIX); | |
b2e465d6 AL |
248 | |
249 | /* If we reached the end of the search list then wrap around to the | |
250 | start */ | |
251 | if (CurHost == 0 && LastUsed != 0) | |
252 | CurHost = LastHostAddr; | |
253 | ||
254 | // Reached the end of the search cycle | |
255 | if (CurHost == LastUsed) | |
256 | break; | |
257 | ||
0837bd25 AL |
258 | if (CurHost != 0) |
259 | _error->Discard(); | |
b2e465d6 | 260 | } |
28006885 | 261 | |
dd1fd92b | 262 | if (_error->PendingError() == true) |
b2e465d6 | 263 | return false; |
cdd5a135 | 264 | return _error->Error(_("Unable to connect to %s:%s:"),Host.c_str(),ServStr); |
0837bd25 AL |
265 | } |
266 | /*}}}*/ | |
cc480014 MV |
267 | // Connect - Connect to a server /*{{{*/ |
268 | // --------------------------------------------------------------------- | |
cdeb54d4 | 269 | /* Performs a connection to the server (including SRV record lookup) */ |
cc480014 MV |
270 | bool Connect(std::string Host,int Port,const char *Service, |
271 | int DefPort,int &Fd, | |
272 | unsigned long TimeOut,pkgAcqMethod *Owner) | |
273 | { | |
cc480014 MV |
274 | if (_error->PendingError() == true) |
275 | return false; | |
cc480014 MV |
276 | |
277 | if(LastHost != Host || LastPort != Port) | |
278 | { | |
279 | SrvRecords.clear(); | |
c8ec5ab7 | 280 | if (_config->FindB("Acquire::EnableSrvRecords", true) == true) |
c29dbdff | 281 | GetSrvRecords(Host, DefPort, SrvRecords); |
cc480014 | 282 | } |
cc480014 | 283 | |
3af3ac2f | 284 | size_t stackSize = 0; |
cdeb54d4 | 285 | // try to connect in the priority order of the srv records |
3af3ac2f DK |
286 | std::string initialHost{std::move(Host)}; |
287 | while(SrvRecords.empty() == false) | |
cc480014 | 288 | { |
3af3ac2f DK |
289 | _error->PushToStack(); |
290 | ++stackSize; | |
0b7d34ee | 291 | // PopFromSrvRecs will also remove the server |
c29dbdff | 292 | Host = PopFromSrvRecs(SrvRecords).target; |
3af3ac2f DK |
293 | auto const ret = ConnectToHostname(Host, Port, Service, DefPort, Fd, TimeOut, Owner); |
294 | if (ret) | |
295 | { | |
296 | while(stackSize--) | |
297 | _error->RevertToStack(); | |
cc480014 | 298 | return true; |
3af3ac2f | 299 | } |
cdeb54d4 | 300 | } |
3af3ac2f | 301 | Host = std::move(initialHost); |
cdeb54d4 | 302 | |
3af3ac2f DK |
303 | // we have no (good) SrvRecords for this host, connect right away |
304 | _error->PushToStack(); | |
305 | ++stackSize; | |
306 | auto const ret = ConnectToHostname(Host, Port, Service, DefPort, Fd, | |
307 | TimeOut, Owner); | |
308 | while(stackSize--) | |
309 | if (ret) | |
310 | _error->RevertToStack(); | |
311 | else | |
312 | _error->MergeWithStack(); | |
313 | return ret; | |
cc480014 | 314 | } |