]> git.saurik.com Git - apt.git/blob - methods/connect.cc
Fixed passive connect waiting bug
[apt.git] / methods / connect.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: connect.cc,v 1.6 2000/05/28 04:34:44 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
28 static string LastHost;
29 static int LastPort = 0;
30 static struct addrinfo *LastHostAddr = 0;
31 static struct addrinfo *LastUsed = 0;
32
33 // DoConnect - Attempt a connect operation /*{{{*/
34 // ---------------------------------------------------------------------
35 /* This helper function attempts a connection to a single address. */
36 static bool DoConnect(struct addrinfo *Addr,string Host,
37 unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner)
38 {
39 // Show a status indicator
40 char Name[NI_MAXHOST];
41 char Service[NI_MAXSERV];
42 Name[0] = 0;
43 Service[0] = 0;
44 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
45 Name,sizeof(Name),Service,sizeof(Service),
46 NI_NUMERICHOST|NI_NUMERICSERV);
47 Owner->Status("Connecting to %s (%s)",Host.c_str(),Name);
48
49 // Get a socket
50 if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
51 Addr->ai_protocol)) < 0)
52 return _error->Errno("socket","Could not create a socket");
53
54 SetNonBlock(Fd,true);
55 if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 &&
56 errno != EINPROGRESS)
57 return _error->Errno("connect","Cannot initiate the connection "
58 "to %s:%s (%s).",Host.c_str(),Service,Name);
59
60 /* This implements a timeout for connect by opening the connection
61 nonblocking */
62 if (WaitFd(Fd,true,TimeOut) == false)
63 return _error->Error("Could not connect to %s:%s (%s), "
64 "connection timed out",Host.c_str(),Service,Name);
65
66 // Check the socket for an error condition
67 unsigned int Err;
68 unsigned int Len = sizeof(Err);
69 if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0)
70 return _error->Errno("getsockopt","Failed");
71
72 if (Err != 0)
73 {
74 errno = Err;
75 return _error->Errno("connect","Could not connect to %s:%s (%s).",Host.c_str(),
76 Service,Name);
77 }
78
79 return true;
80 }
81 /*}}}*/
82 // Connect - Connect to a server /*{{{*/
83 // ---------------------------------------------------------------------
84 /* Performs a connection to the server */
85 bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd,
86 unsigned long TimeOut,pkgAcqMethod *Owner)
87 {
88 if (_error->PendingError() == true)
89 return false;
90
91 // Convert the port name/number
92 char ServStr[300];
93 if (Port != 0)
94 snprintf(ServStr,sizeof(ServStr),"%u",Port);
95 else
96 snprintf(ServStr,sizeof(ServStr),"%s",Service);
97
98 /* We used a cached address record.. Yes this is against the spec but
99 the way we have setup our rotating dns suggests that this is more
100 sensible */
101 if (LastHost != Host || LastPort != Port)
102 {
103 Owner->Status("Connecting to %s",Host.c_str());
104
105 // Free the old address structure
106 if (LastHostAddr != 0)
107 {
108 freeaddrinfo(LastHostAddr);
109 LastHostAddr = 0;
110 LastUsed = 0;
111 }
112
113 // We only understand SOCK_STREAM sockets.
114 struct addrinfo Hints;
115 memset(&Hints,0,sizeof(Hints));
116 Hints.ai_socktype = SOCK_STREAM;
117 Hints.ai_protocol = 0;
118
119 // Resolve both the host and service simultaneously
120 while (1)
121 {
122 int Res;
123 if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
124 LastHostAddr == 0)
125 {
126 if (Res == EAI_NONAME || Res == EAI_SERVICE)
127 {
128 if (DefPort != 0)
129 {
130 snprintf(ServStr,sizeof(ServStr),"%u",DefPort);
131 DefPort = 0;
132 continue;
133 }
134 return _error->Error("Could not resolve '%s'",Host.c_str());
135 }
136
137 return _error->Error("Something wicked happend resolving '%s:%s'",
138 Host.c_str(),ServStr);
139 }
140 break;
141 }
142
143 LastHost = Host;
144 LastPort = Port;
145 }
146
147 // When we have an IP rotation stay with the last IP.
148 struct addrinfo *CurHost = LastHostAddr;
149 if (LastUsed != 0)
150 CurHost = LastUsed;
151
152 while (CurHost != 0)
153 {
154 if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
155 {
156 LastUsed = CurHost;
157 return true;
158 }
159 close(Fd);
160 Fd = -1;
161
162 // Ignore UNIX domain sockets
163 do
164 {
165 CurHost = CurHost->ai_next;
166 }
167 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
168
169 LastUsed = 0;
170 if (CurHost != 0)
171 _error->Discard();
172 }
173
174 if (_error->PendingError() == true)
175 return false;
176 return _error->Error("Unable to connect to %s:",Host.c_str(),ServStr);
177 }
178 /*}}}*/