]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: connect.cc,v 1.10.2.1 2004/01/16 18:58:50 mdz Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Connect - Replacement connect call | |
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 | ||
11 | ##################################################################### */ | |
12 | /*}}}*/ | |
13 | // Include Files /*{{{*/ | |
14 | #include <config.h> | |
15 | ||
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> | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <errno.h> | |
25 | #include <unistd.h> | |
26 | #include <sstream> | |
27 | #include <string.h> | |
28 | #include<set> | |
29 | #include<string> | |
30 | ||
31 | // Internet stuff | |
32 | #include <netinet/in.h> | |
33 | #include <sys/socket.h> | |
34 | #include <arpa/inet.h> | |
35 | #include <netdb.h> | |
36 | ||
37 | #include "connect.h" | |
38 | #include "rfc2553emu.h" | |
39 | #include <apti18n.h> | |
40 | /*}}}*/ | |
41 | ||
42 | static std::string LastHost; | |
43 | static int LastPort = 0; | |
44 | static struct addrinfo *LastHostAddr = 0; | |
45 | static struct addrinfo *LastUsed = 0; | |
46 | ||
47 | static std::vector<SrvRec> SrvRecords; | |
48 | static int LastSrvRecord = 0; | |
49 | ||
50 | // Set of IP/hostnames that we timed out before or couldn't resolve | |
51 | static std::set<std::string> bad_addr; | |
52 | ||
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 */ | |
57 | void RotateDNS() | |
58 | { | |
59 | if (LastUsed != 0 && LastUsed->ai_next != 0) | |
60 | LastUsed = LastUsed->ai_next; | |
61 | else | |
62 | LastUsed = LastHostAddr; | |
63 | } | |
64 | /*}}}*/ | |
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) | |
70 | { | |
71 | // Show a status indicator | |
72 | char Name[NI_MAXHOST]; | |
73 | char Service[NI_MAXSERV]; | |
74 | ||
75 | Name[0] = 0; | |
76 | Service[0] = 0; | |
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); | |
81 | ||
82 | // if that addr did timeout before, we do not try it again | |
83 | if(bad_addr.find(std::string(Name)) != bad_addr.end()) | |
84 | return false; | |
85 | ||
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 | { | |
90 | std::stringstream ss; | |
91 | ioprintf(ss, _("[IP: %s %s]"),Name,Service); | |
92 | Owner->SetIP(ss.str()); | |
93 | } | |
94 | ||
95 | // Get a socket | |
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); | |
100 | ||
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); | |
106 | ||
107 | /* This implements a timeout for connect by opening the connection | |
108 | nonblocking */ | |
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); | |
114 | } | |
115 | ||
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) | |
120 | return _error->Errno("getsockopt",_("Failed")); | |
121 | ||
122 | if (Err != 0) | |
123 | { | |
124 | errno = Err; | |
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(), | |
131 | Service,Name); | |
132 | } | |
133 | ||
134 | return true; | |
135 | } | |
136 | /*}}}*/ | |
137 | ||
138 | // Connect to a given Hostname | |
139 | bool ConnectToHostname(std::string Host,int Port,const char *Service, | |
140 | int DefPort,int &Fd, | |
141 | unsigned long TimeOut,pkgAcqMethod *Owner) | |
142 | { | |
143 | // Convert the port name/number | |
144 | char ServStr[300]; | |
145 | if (Port != 0) | |
146 | snprintf(ServStr,sizeof(ServStr),"%i", Port); | |
147 | else | |
148 | snprintf(ServStr,sizeof(ServStr),"%s", Service); | |
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 | { | |
155 | Owner->Status(_("Connecting to %s"),Host.c_str()); | |
156 | ||
157 | // Free the old address structure | |
158 | if (LastHostAddr != 0) | |
159 | { | |
160 | freeaddrinfo(LastHostAddr); | |
161 | LastHostAddr = 0; | |
162 | LastUsed = 0; | |
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; | |
169 | Hints.ai_flags = AI_ADDRCONFIG; | |
170 | Hints.ai_protocol = 0; | |
171 | ||
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 | ||
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 | ||
183 | // Resolve both the host and service simultaneously | |
184 | while (1) | |
185 | { | |
186 | int Res; | |
187 | if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 || | |
188 | LastHostAddr == 0) | |
189 | { | |
190 | if (Res == EAI_NONAME || Res == EAI_SERVICE) | |
191 | { | |
192 | if (DefPort != 0) | |
193 | { | |
194 | snprintf(ServStr, sizeof(ServStr), "%i", DefPort); | |
195 | DefPort = 0; | |
196 | continue; | |
197 | } | |
198 | bad_addr.insert(bad_addr.begin(), Host); | |
199 | Owner->SetFailReason("ResolveFailure"); | |
200 | return _error->Error(_("Could not resolve '%s'"),Host.c_str()); | |
201 | } | |
202 | ||
203 | if (Res == EAI_AGAIN) | |
204 | { | |
205 | Owner->SetFailReason("TmpResolveFailure"); | |
206 | return _error->Error(_("Temporary failure resolving '%s'"), | |
207 | Host.c_str()); | |
208 | } | |
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)); | |
214 | } | |
215 | break; | |
216 | } | |
217 | ||
218 | LastHost = Host; | |
219 | LastPort = Port; | |
220 | } | |
221 | ||
222 | // When we have an IP rotation stay with the last IP. | |
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 | ||
237 | // Ignore UNIX domain sockets | |
238 | do | |
239 | { | |
240 | CurHost = CurHost->ai_next; | |
241 | } | |
242 | while (CurHost != 0 && CurHost->ai_family == AF_UNIX); | |
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 | ||
253 | if (CurHost != 0) | |
254 | _error->Discard(); | |
255 | } | |
256 | ||
257 | if (_error->PendingError() == true) | |
258 | return false; | |
259 | return _error->Error(_("Unable to connect to %s:%s:"),Host.c_str(),ServStr); | |
260 | } | |
261 | /*}}}*/ | |
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, | |
266 | int DefPort,int &Fd, | |
267 | unsigned long TimeOut,pkgAcqMethod *Owner) | |
268 | { | |
269 | if (_error->PendingError() == true) | |
270 | return false; | |
271 | ||
272 | if(LastHost != Host || LastPort != Port) | |
273 | { | |
274 | SrvRecords.clear(); | |
275 | if (_config->FindB("Acquire::EnableSrvRecords", true) == true) | |
276 | GetSrvRecords(Host, DefPort, SrvRecords); | |
277 | } | |
278 | // we have no SrvRecords for this host, connect right away | |
279 | if(SrvRecords.size() == 0) | |
280 | return ConnectToHostname(Host, Port, Service, DefPort, Fd, | |
281 | TimeOut, Owner); | |
282 | ||
283 | // try to connect in the priority order of the srv records | |
284 | while(SrvRecords.size() > 0) | |
285 | { | |
286 | Host = PopFromSrvRecs(SrvRecords).target; | |
287 | if(ConnectToHostname(Host, Port, Service, DefPort, Fd, TimeOut, Owner)) | |
288 | return true; | |
289 | ||
290 | // we couldn't connect to this one, use the next | |
291 | SrvRecords.erase(SrvRecords.begin()); | |
292 | } | |
293 | ||
294 | return false; | |
295 | } |