]> git.saurik.com Git - apt.git/blame - methods/connect.cc
Updated authors
[apt.git] / methods / connect.cc
CommitLineData
0837bd25
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
28006885 3// $Id: connect.cc,v 1.6 2000/05/28 04:34:44 jgg Exp $
0837bd25
AL
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
28static string LastHost;
29static int LastPort = 0;
30static struct addrinfo *LastHostAddr = 0;
31static struct addrinfo *LastUsed = 0;
32
33// DoConnect - Attempt a connect operation /*{{{*/
34// ---------------------------------------------------------------------
35/* This helper function attempts a connection to a single address. */
36static 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];
28006885 41 char Service[NI_MAXSERV];
0837bd25 42 Name[0] = 0;
28006885 43 Service[0] = 0;
0837bd25 44 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
28006885
AL
45 Name,sizeof(Name),Service,sizeof(Service),
46 NI_NUMERICHOST|NI_NUMERICSERV);
0837bd25
AL
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 "
28006885 58 "to %s:%s (%s).",Host.c_str(),Service,Name);
0837bd25
AL
59
60 /* This implements a timeout for connect by opening the connection
61 nonblocking */
62 if (WaitFd(Fd,true,TimeOut) == false)
28006885
AL
63 return _error->Error("Could not connect to %s:%s (%s), "
64 "connection timed out",Host.c_str(),Service,Name);
0837bd25
AL
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)
28006885
AL
73 {
74 errno = Err;
75 return _error->Errno("connect","Could not connect to %s:%s (%s).",Host.c_str(),
76 Service,Name);
77 }
78
0837bd25
AL
79 return true;
80}
81 /*}}}*/
c141b9a9 82// Connect - Connect to a server /*{{{*/
0837bd25
AL
83// ---------------------------------------------------------------------
84/* Performs a connection to the server */
9505213b 85bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd,
0837bd25
AL
86 unsigned long TimeOut,pkgAcqMethod *Owner)
87{
88 if (_error->PendingError() == true)
89 return false;
28006885
AL
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);
0837bd25
AL
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
0837bd25
AL
105 // Free the old address structure
106 if (LastHostAddr != 0)
107 {
108 freeaddrinfo(LastHostAddr);
109 LastHostAddr = 0;
28006885 110 LastUsed = 0;
0837bd25
AL
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;
28006885 117 Hints.ai_protocol = 0;
0837bd25
AL
118
119 // Resolve both the host and service simultaneously
9505213b 120 while (1)
c141b9a9 121 {
9505213b 122 int Res;
28006885 123 if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
9505213b
AL
124 LastHostAddr == 0)
125 {
72472b95 126 if (Res == EAI_NONAME || Res == EAI_SERVICE)
9505213b
AL
127 {
128 if (DefPort != 0)
129 {
28006885 130 snprintf(ServStr,sizeof(ServStr),"%u",DefPort);
9505213b
AL
131 DefPort = 0;
132 continue;
133 }
134 return _error->Error("Could not resolve '%s'",Host.c_str());
135 }
136
28006885
AL
137 return _error->Error("Something wicked happend resolving '%s:%s'",
138 Host.c_str(),ServStr);
9505213b
AL
139 }
140 break;
c141b9a9
AL
141 }
142
0837bd25
AL
143 LastHost = Host;
144 LastPort = Port;
0837bd25
AL
145 }
146
28006885 147 // When we have an IP rotation stay with the last IP.
0837bd25
AL
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
28006885
AL
162 // Ignore UNIX domain sockets
163 do
164 {
165 CurHost = CurHost->ai_next;
166 }
167 while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
168
0837bd25
AL
169 LastUsed = 0;
170 if (CurHost != 0)
171 _error->Discard();
172 }
28006885 173
dd1fd92b
AL
174 if (_error->PendingError() == true)
175 return false;
28006885 176 return _error->Error("Unable to connect to %s:",Host.c_str(),ServStr);
0837bd25
AL
177}
178 /*}}}*/