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