]> git.saurik.com Git - apt.git/blame - methods/connect.cc
Typo fixes.
[apt.git] / methods / connect.cc
CommitLineData
0837bd25
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7da2b375 3// $Id: connect.cc,v 1.9 2002/09/14 05:28:38 jgg Exp $
0837bd25
AL
4/* ######################################################################
5
6 Connect - Replacement connect call
7da2b375
AL
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
0837bd25
AL
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
b2e465d6
AL
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 /*}}}*/
0837bd25
AL
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];
28006885 56 char Service[NI_MAXSERV];
b2e465d6
AL
57
58 Name[0] = 0;
28006885 59 Service[0] = 0;
0837bd25 60 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
28006885
AL
61 Name,sizeof(Name),Service,sizeof(Service),
62 NI_NUMERICHOST|NI_NUMERICSERV);
0837bd25 63 Owner->Status("Connecting to %s (%s)",Host.c_str(),Name);
b2e465d6
AL
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
0837bd25
AL
76 // Get a socket
77 if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
78 Addr->ai_protocol)) < 0)
b2e465d6
AL
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);
0837bd25
AL
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 "
28006885 86 "to %s:%s (%s).",Host.c_str(),Service,Name);
0837bd25
AL
87
88 /* This implements a timeout for connect by opening the connection
89 nonblocking */
90 if (WaitFd(Fd,true,TimeOut) == false)
28006885
AL
91 return _error->Error("Could not connect to %s:%s (%s), "
92 "connection timed out",Host.c_str(),Service,Name);
b2e465d6 93
0837bd25
AL
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)
28006885
AL
101 {
102 errno = Err;
103 return _error->Errno("connect","Could not connect to %s:%s (%s).",Host.c_str(),
104 Service,Name);
105 }
106
0837bd25
AL
107 return true;
108}
109 /*}}}*/
c141b9a9 110// Connect - Connect to a server /*{{{*/
0837bd25
AL
111// ---------------------------------------------------------------------
112/* Performs a connection to the server */
9505213b 113bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd,
0837bd25
AL
114 unsigned long TimeOut,pkgAcqMethod *Owner)
115{
116 if (_error->PendingError() == true)
117 return false;
28006885
AL
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);
0837bd25
AL
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
0837bd25
AL
133 // Free the old address structure
134 if (LastHostAddr != 0)
135 {
136 freeaddrinfo(LastHostAddr);
137 LastHostAddr = 0;
28006885 138 LastUsed = 0;
0837bd25
AL
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;
28006885 145 Hints.ai_protocol = 0;
0837bd25
AL
146
147 // Resolve both the host and service simultaneously
9505213b 148 while (1)
c141b9a9 149 {
9505213b 150 int Res;
28006885 151 if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
9505213b
AL
152 LastHostAddr == 0)
153 {
72472b95 154 if (Res == EAI_NONAME || Res == EAI_SERVICE)
9505213b
AL
155 {
156 if (DefPort != 0)
157 {
28006885 158 snprintf(ServStr,sizeof(ServStr),"%u",DefPort);
9505213b
AL
159 DefPort = 0;
160 continue;
161 }
162 return _error->Error("Could not resolve '%s'",Host.c_str());
163 }
164
4fe6e0c2
AL
165 if (Res == EAI_AGAIN)
166 return _error->Error("Temporary failure resolving '%s'",
167 Host.c_str());
b2e465d6
AL
168 return _error->Error("Something wicked happened resolving '%s:%s' (%i)",
169 Host.c_str(),ServStr,Res);
9505213b
AL
170 }
171 break;
c141b9a9
AL
172 }
173
0837bd25
AL
174 LastHost = Host;
175 LastPort = Port;
0837bd25
AL
176 }
177
28006885 178 // When we have an IP rotation stay with the last IP.
0837bd25
AL
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
28006885
AL
193 // Ignore UNIX domain sockets
194 do
195 {
196 CurHost = CurHost->ai_next;
197 }
198 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
b2e465d6
AL
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
0837bd25
AL
209 if (CurHost != 0)
210 _error->Discard();
b2e465d6 211 }
28006885 212
dd1fd92b 213 if (_error->PendingError() == true)
b2e465d6
AL
214 return false;
215 return _error->Error("Unable to connect to %s %s:",Host.c_str(),ServStr);
0837bd25
AL
216}
217 /*}}}*/