]>
git.saurik.com Git - apt.git/blob - methods/connect.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: connect.cc,v 1.10.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 Connect - Replacement connect call
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.
11 ##################################################################### */
13 // Include Files /*{{{*/
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
32 #include "rfc2553emu.h"
36 static string LastHost
;
37 static int LastPort
= 0;
38 static struct addrinfo
*LastHostAddr
= 0;
39 static struct addrinfo
*LastUsed
= 0;
41 // Set of IP/hostnames that we timed out before or couldn't resolve
42 static std::set
<string
> bad_addr
;
44 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
45 // ---------------------------------------------------------------------
46 /* This is called during certain errors in order to recover by selecting a
50 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
51 LastUsed
= LastUsed
->ai_next
;
53 LastUsed
= LastHostAddr
;
56 // DoConnect - Attempt a connect operation /*{{{*/
57 // ---------------------------------------------------------------------
58 /* This helper function attempts a connection to a single address. */
59 static bool DoConnect(struct addrinfo
*Addr
,string Host
,
60 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
62 // Show a status indicator
63 char Name
[NI_MAXHOST
];
64 char Service
[NI_MAXSERV
];
68 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
69 Name
,sizeof(Name
),Service
,sizeof(Service
),
70 NI_NUMERICHOST
|NI_NUMERICSERV
);
71 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
73 // if that addr did timeout before, we do not try it again
74 if(bad_addr
.find(string(Name
)) != bad_addr
.end())
77 /* If this is an IP rotation store the IP we are using.. If something goes
78 wrong this will get tacked onto the end of the error message */
79 if (LastHostAddr
->ai_next
!= 0)
82 ioprintf(ss
, _("[IP: %s %s]"),Name
,Service
);
83 Owner
->SetIP(ss
.str());
87 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
88 Addr
->ai_protocol
)) < 0)
89 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
90 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
93 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
95 return _error
->Errno("connect",_("Cannot initiate the connection "
96 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
98 /* This implements a timeout for connect by opening the connection
100 if (WaitFd(Fd
,true,TimeOut
) == false) {
101 bad_addr
.insert(bad_addr
.begin(), string(Name
));
102 Owner
->SetFailReason("Timeout");
103 return _error
->Error(_("Could not connect to %s:%s (%s), "
104 "connection timed out"),Host
.c_str(),Service
,Name
);
107 // Check the socket for an error condition
109 unsigned int Len
= sizeof(Err
);
110 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
111 return _error
->Errno("getsockopt",_("Failed"));
116 if(errno
== ECONNREFUSED
)
117 Owner
->SetFailReason("ConnectionRefused");
118 else if (errno
== ETIMEDOUT
)
119 Owner
->SetFailReason("ConnectionTimedOut");
120 bad_addr
.insert(bad_addr
.begin(), string(Name
));
121 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
128 // Connect - Connect to a server /*{{{*/
129 // ---------------------------------------------------------------------
130 /* Performs a connection to the server */
131 bool Connect(string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
132 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
134 if (_error
->PendingError() == true)
137 // Convert the port name/number
140 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
142 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
144 /* We used a cached address record.. Yes this is against the spec but
145 the way we have setup our rotating dns suggests that this is more
147 if (LastHost
!= Host
|| LastPort
!= Port
)
149 Owner
->Status(_("Connecting to %s"),Host
.c_str());
151 // Free the old address structure
152 if (LastHostAddr
!= 0)
154 freeaddrinfo(LastHostAddr
);
159 // We only understand SOCK_STREAM sockets.
160 struct addrinfo Hints
;
161 memset(&Hints
,0,sizeof(Hints
));
162 Hints
.ai_socktype
= SOCK_STREAM
;
163 Hints
.ai_flags
= AI_ADDRCONFIG
;
164 Hints
.ai_protocol
= 0;
166 // if we couldn't resolve the host before, we don't try now
167 if(bad_addr
.find(Host
) != bad_addr
.end())
168 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
170 // Resolve both the host and service simultaneously
174 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
177 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
181 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
185 bad_addr
.insert(bad_addr
.begin(), Host
);
186 Owner
->SetFailReason("ResolveFailure");
187 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
190 if (Res
== EAI_AGAIN
)
192 Owner
->SetFailReason("TmpResolveFailure");
193 return _error
->Error(_("Temporary failure resolving '%s'"),
196 return _error
->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
197 Host
.c_str(),ServStr
,Res
,gai_strerror(Res
));
206 // When we have an IP rotation stay with the last IP.
207 struct addrinfo
*CurHost
= LastHostAddr
;
213 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
221 // Ignore UNIX domain sockets
224 CurHost
= CurHost
->ai_next
;
226 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
228 /* If we reached the end of the search list then wrap around to the
230 if (CurHost
== 0 && LastUsed
!= 0)
231 CurHost
= LastHostAddr
;
233 // Reached the end of the search cycle
234 if (CurHost
== LastUsed
)
241 if (_error
->PendingError() == true)
243 return _error
->Error(_("Unable to connect to %s:%s:"),Host
.c_str(),ServStr
);