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