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