More updates
[ntk/apt.git] / methods / connect.cc
CommitLineData
0837bd25
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
4fe6e0c2 3// $Id: connect.cc,v 1.8 2002/02/28 03:51:55 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
28static string LastHost;
29static int LastPort = 0;
30static struct addrinfo *LastHostAddr = 0;
31static 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 */
37void 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. */
48static 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 110bool 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
4fe6e0c2
AL
162 if (Res == EAI_AGAIN)
163 return _error->Error("Temporary failure resolving '%s'",
164 Host.c_str());
b2e465d6
AL
165 return _error->Error("Something wicked happened resolving '%s:%s' (%i)",
166 Host.c_str(),ServStr,Res);
9505213b
AL
167 }
168 break;
c141b9a9
AL
169 }
170
0837bd25
AL
171 LastHost = Host;
172 LastPort = Port;
0837bd25
AL
173 }
174
28006885 175 // When we have an IP rotation stay with the last IP.
0837bd25
AL
176 struct addrinfo *CurHost = LastHostAddr;
177 if (LastUsed != 0)
178 CurHost = LastUsed;
179
180 while (CurHost != 0)
181 {
182 if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
183 {
184 LastUsed = CurHost;
185 return true;
186 }
187 close(Fd);
188 Fd = -1;
189
28006885
AL
190 // Ignore UNIX domain sockets
191 do
192 {
193 CurHost = CurHost->ai_next;
194 }
195 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
b2e465d6
AL
196
197 /* If we reached the end of the search list then wrap around to the
198 start */
199 if (CurHost == 0 && LastUsed != 0)
200 CurHost = LastHostAddr;
201
202 // Reached the end of the search cycle
203 if (CurHost == LastUsed)
204 break;
205
0837bd25
AL
206 if (CurHost != 0)
207 _error->Discard();
b2e465d6 208 }
28006885 209
dd1fd92b 210 if (_error->PendingError() == true)
b2e465d6
AL
211 return false;
212 return _error->Error("Unable to connect to %s %s:",Host.c_str(),ServStr);
0837bd25
AL
213}
214 /*}}}*/