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