]> git.saurik.com Git - apt.git/blame_incremental - methods/connect.cc
LD_LIBRARY_PATH fix
[apt.git] / methods / connect.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: connect.cc,v 1.7 2001/02/20 07:03:18 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
28static string LastHost;
29static int LastPort = 0;
30static struct addrinfo *LastHostAddr = 0;
31static 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 */
37void 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. */
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];
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 */
110bool 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 return _error->Error("Something wicked happened resolving '%s:%s' (%i)",
163 Host.c_str(),ServStr,Res);
164 }
165 break;
166 }
167
168 LastHost = Host;
169 LastPort = Port;
170 }
171
172 // When we have an IP rotation stay with the last IP.
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
187 // Ignore UNIX domain sockets
188 do
189 {
190 CurHost = CurHost->ai_next;
191 }
192 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
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
203 if (CurHost != 0)
204 _error->Discard();
205 }
206
207 if (_error->PendingError() == true)
208 return false;
209 return _error->Error("Unable to connect to %s %s:",Host.c_str(),ServStr);
210}
211 /*}}}*/