]> git.saurik.com Git - apt.git/blob - methods/connect.cc
Corrections for object lifetime
[apt.git] / methods / connect.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: connect.cc,v 1.8 2002/02/28 03:51:55 jgg Exp $
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
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 /*}}}*/
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];
53 char Service[NI_MAXSERV];
54
55 Name[0] = 0;
56 Service[0] = 0;
57 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
58 Name,sizeof(Name),Service,sizeof(Service),
59 NI_NUMERICHOST|NI_NUMERICSERV);
60 Owner->Status("Connecting to %s (%s)",Host.c_str(),Name);
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
73 // Get a socket
74 if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
75 Addr->ai_protocol)) < 0)
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);
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 "
83 "to %s:%s (%s).",Host.c_str(),Service,Name);
84
85 /* This implements a timeout for connect by opening the connection
86 nonblocking */
87 if (WaitFd(Fd,true,TimeOut) == false)
88 return _error->Error("Could not connect to %s:%s (%s), "
89 "connection timed out",Host.c_str(),Service,Name);
90
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)
98 {
99 errno = Err;
100 return _error->Errno("connect","Could not connect to %s:%s (%s).",Host.c_str(),
101 Service,Name);
102 }
103
104 return true;
105 }
106 /*}}}*/
107 // Connect - Connect to a server /*{{{*/
108 // ---------------------------------------------------------------------
109 /* Performs a connection to the server */
110 bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd,
111 unsigned long TimeOut,pkgAcqMethod *Owner)
112 {
113 if (_error->PendingError() == true)
114 return false;
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);
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
130 // Free the old address structure
131 if (LastHostAddr != 0)
132 {
133 freeaddrinfo(LastHostAddr);
134 LastHostAddr = 0;
135 LastUsed = 0;
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;
142 Hints.ai_protocol = 0;
143
144 // Resolve both the host and service simultaneously
145 while (1)
146 {
147 int Res;
148 if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
149 LastHostAddr == 0)
150 {
151 if (Res == EAI_NONAME || Res == EAI_SERVICE)
152 {
153 if (DefPort != 0)
154 {
155 snprintf(ServStr,sizeof(ServStr),"%u",DefPort);
156 DefPort = 0;
157 continue;
158 }
159 return _error->Error("Could not resolve '%s'",Host.c_str());
160 }
161
162 if (Res == EAI_AGAIN)
163 return _error->Error("Temporary failure resolving '%s'",
164 Host.c_str());
165 return _error->Error("Something wicked happened resolving '%s:%s' (%i)",
166 Host.c_str(),ServStr,Res);
167 }
168 break;
169 }
170
171 LastHost = Host;
172 LastPort = Port;
173 }
174
175 // When we have an IP rotation stay with the last IP.
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
190 // Ignore UNIX domain sockets
191 do
192 {
193 CurHost = CurHost->ai_next;
194 }
195 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
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
206 if (CurHost != 0)
207 _error->Discard();
208 }
209
210 if (_error->PendingError() == true)
211 return false;
212 return _error->Error("Unable to connect to %s %s:",Host.c_str(),ServStr);
213 }
214 /*}}}*/