]> git.saurik.com Git - apt.git/blame - methods/connect.cc
* Give a warning if an illegal type abbreviation is use...
[apt.git] / methods / connect.cc
CommitLineData
0837bd25
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
dc738e7a 3// $Id: connect.cc,v 1.10 2003/02/10 07:34:41 doogie 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 /*{{{*/
dc738e7a 14#include <apti18n.h>
0837bd25
AL
15#include "connect.h"
16#include <apt-pkg/error.h>
17#include <apt-pkg/fileutl.h>
18
19#include <stdio.h>
20#include <errno.h>
21#include <unistd.h>
22
23// Internet stuff
24#include <netinet/in.h>
25#include <sys/socket.h>
26#include <arpa/inet.h>
27#include <netdb.h>
28
29#include "rfc2553emu.h"
30 /*}}}*/
31
32static string LastHost;
33static int LastPort = 0;
34static struct addrinfo *LastHostAddr = 0;
35static struct addrinfo *LastUsed = 0;
36
b2e465d6
AL
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 /*}}}*/
0837bd25
AL
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];
28006885 57 char Service[NI_MAXSERV];
b2e465d6
AL
58
59 Name[0] = 0;
28006885 60 Service[0] = 0;
0837bd25 61 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
28006885
AL
62 Name,sizeof(Name),Service,sizeof(Service),
63 NI_NUMERICHOST|NI_NUMERICSERV);
dc738e7a 64 Owner->Status(_("Connecting to %s (%s)"),Host.c_str(),Name);
b2e465d6
AL
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];
dc738e7a 71 snprintf(Name2,sizeof(Name2),_("[IP: %s %s]"),Name,Service);
b2e465d6
AL
72 Owner->SetFailExtraMsg(string(Name2));
73 }
74 else
75 Owner->SetFailExtraMsg("");
76
0837bd25
AL
77 // Get a socket
78 if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
79 Addr->ai_protocol)) < 0)
dc738e7a 80 return _error->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
b2e465d6 81 Name,Addr->ai_family,Addr->ai_socktype,Addr->ai_protocol);
0837bd25
AL
82
83 SetNonBlock(Fd,true);
84 if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 &&
85 errno != EINPROGRESS)
dc738e7a
AL
86 return _error->Errno("connect",_("Cannot initiate the connection "
87 "to %s:%s (%s)."),Host.c_str(),Service,Name);
0837bd25
AL
88
89 /* This implements a timeout for connect by opening the connection
90 nonblocking */
91 if (WaitFd(Fd,true,TimeOut) == false)
dc738e7a
AL
92 return _error->Error(_("Could not connect to %s:%s (%s), "
93 "connection timed out"),Host.c_str(),Service,Name);
b2e465d6 94
0837bd25
AL
95 // Check the socket for an error condition
96 unsigned int Err;
97 unsigned int Len = sizeof(Err);
98 if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0)
dc738e7a 99 return _error->Errno("getsockopt",_("Failed"));
0837bd25
AL
100
101 if (Err != 0)
28006885
AL
102 {
103 errno = Err;
dc738e7a 104 return _error->Errno("connect",_("Could not connect to %s:%s (%s)."),Host.c_str(),
28006885
AL
105 Service,Name);
106 }
107
0837bd25
AL
108 return true;
109}
110 /*}}}*/
c141b9a9 111// Connect - Connect to a server /*{{{*/
0837bd25
AL
112// ---------------------------------------------------------------------
113/* Performs a connection to the server */
9505213b 114bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd,
0837bd25
AL
115 unsigned long TimeOut,pkgAcqMethod *Owner)
116{
117 if (_error->PendingError() == true)
118 return false;
28006885
AL
119
120 // Convert the port name/number
121 char ServStr[300];
122 if (Port != 0)
123 snprintf(ServStr,sizeof(ServStr),"%u",Port);
124 else
125 snprintf(ServStr,sizeof(ServStr),"%s",Service);
0837bd25
AL
126
127 /* We used a cached address record.. Yes this is against the spec but
128 the way we have setup our rotating dns suggests that this is more
129 sensible */
130 if (LastHost != Host || LastPort != Port)
131 {
dc738e7a 132 Owner->Status(_("Connecting to %s"),Host.c_str());
0837bd25 133
0837bd25
AL
134 // Free the old address structure
135 if (LastHostAddr != 0)
136 {
137 freeaddrinfo(LastHostAddr);
138 LastHostAddr = 0;
28006885 139 LastUsed = 0;
0837bd25
AL
140 }
141
142 // We only understand SOCK_STREAM sockets.
143 struct addrinfo Hints;
144 memset(&Hints,0,sizeof(Hints));
145 Hints.ai_socktype = SOCK_STREAM;
28006885 146 Hints.ai_protocol = 0;
0837bd25
AL
147
148 // Resolve both the host and service simultaneously
9505213b 149 while (1)
c141b9a9 150 {
9505213b 151 int Res;
28006885 152 if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
9505213b
AL
153 LastHostAddr == 0)
154 {
72472b95 155 if (Res == EAI_NONAME || Res == EAI_SERVICE)
9505213b
AL
156 {
157 if (DefPort != 0)
158 {
28006885 159 snprintf(ServStr,sizeof(ServStr),"%u",DefPort);
9505213b
AL
160 DefPort = 0;
161 continue;
162 }
dc738e7a 163 return _error->Error(_("Could not resolve '%s'"),Host.c_str());
9505213b
AL
164 }
165
4fe6e0c2 166 if (Res == EAI_AGAIN)
dc738e7a 167 return _error->Error(_("Temporary failure resolving '%s'"),
4fe6e0c2 168 Host.c_str());
dc738e7a 169 return _error->Error(_("Something wicked happened resolving '%s:%s' (%i)"),
b2e465d6 170 Host.c_str(),ServStr,Res);
9505213b
AL
171 }
172 break;
c141b9a9
AL
173 }
174
0837bd25
AL
175 LastHost = Host;
176 LastPort = Port;
0837bd25
AL
177 }
178
28006885 179 // When we have an IP rotation stay with the last IP.
0837bd25
AL
180 struct addrinfo *CurHost = LastHostAddr;
181 if (LastUsed != 0)
182 CurHost = LastUsed;
183
184 while (CurHost != 0)
185 {
186 if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
187 {
188 LastUsed = CurHost;
189 return true;
190 }
191 close(Fd);
192 Fd = -1;
193
28006885
AL
194 // Ignore UNIX domain sockets
195 do
196 {
197 CurHost = CurHost->ai_next;
198 }
199 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
b2e465d6
AL
200
201 /* If we reached the end of the search list then wrap around to the
202 start */
203 if (CurHost == 0 && LastUsed != 0)
204 CurHost = LastHostAddr;
205
206 // Reached the end of the search cycle
207 if (CurHost == LastUsed)
208 break;
209
0837bd25
AL
210 if (CurHost != 0)
211 _error->Discard();
b2e465d6 212 }
28006885 213
dd1fd92b 214 if (_error->PendingError() == true)
b2e465d6 215 return false;
dc738e7a 216 return _error->Error(_("Unable to connect to %s %s:"),Host.c_str(),ServStr);
0837bd25
AL
217}
218 /*}}}*/