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