]>
git.saurik.com Git - apt.git/blob - methods/connect.cc
ba2264faabfb017d05b6de5d8b4e932eeb68a673
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 /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
34 #include "rfc2553emu.h"
38 static std::string LastHost
;
39 static int LastPort
= 0;
40 static struct addrinfo
*LastHostAddr
= 0;
41 static struct addrinfo
*LastUsed
= 0;
43 // Set of IP/hostnames that we timed out before or couldn't resolve
44 static std::set
<std::string
> bad_addr
;
46 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
47 // ---------------------------------------------------------------------
48 /* This is called during certain errors in order to recover by selecting a
52 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
53 LastUsed
= LastUsed
->ai_next
;
55 LastUsed
= LastHostAddr
;
58 // DoConnect - Attempt a connect operation /*{{{*/
59 // ---------------------------------------------------------------------
60 /* This helper function attempts a connection to a single address. */
61 static bool DoConnect(struct addrinfo
*Addr
,std::string Host
,
62 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
64 // Show a status indicator
65 char Name
[NI_MAXHOST
];
66 char Service
[NI_MAXSERV
];
70 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
71 Name
,sizeof(Name
),Service
,sizeof(Service
),
72 NI_NUMERICHOST
|NI_NUMERICSERV
);
73 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
75 // if that addr did timeout before, we do not try it again
76 if(bad_addr
.find(std::string(Name
)) != bad_addr
.end())
79 /* If this is an IP rotation store the IP we are using.. If something goes
80 wrong this will get tacked onto the end of the error message */
81 if (LastHostAddr
->ai_next
!= 0)
84 ioprintf(ss
, _("[IP: %s %s]"),Name
,Service
);
85 Owner
->SetIP(ss
.str());
89 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
90 Addr
->ai_protocol
)) < 0)
91 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
92 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
95 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
97 return _error
->Errno("connect",_("Cannot initiate the connection "
98 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
100 /* This implements a timeout for connect by opening the connection
102 if (WaitFd(Fd
,true,TimeOut
) == false) {
103 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
104 Owner
->SetFailReason("Timeout");
105 return _error
->Error(_("Could not connect to %s:%s (%s), "
106 "connection timed out"),Host
.c_str(),Service
,Name
);
109 // Check the socket for an error condition
111 unsigned int Len
= sizeof(Err
);
112 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
113 return _error
->Errno("getsockopt",_("Failed"));
118 if(errno
== ECONNREFUSED
)
119 Owner
->SetFailReason("ConnectionRefused");
120 else if (errno
== ETIMEDOUT
)
121 Owner
->SetFailReason("ConnectionTimedOut");
122 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
123 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
130 // Connect - Connect to a server /*{{{*/
131 // ---------------------------------------------------------------------
132 /* Performs a connection to the server */
133 bool Connect(std::string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
134 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
136 if (_error
->PendingError() == true)
139 // Convert the port name/number
142 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
144 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
146 /* We used a cached address record.. Yes this is against the spec but
147 the way we have setup our rotating dns suggests that this is more
149 if (LastHost
!= Host
|| LastPort
!= Port
)
151 Owner
->Status(_("Connecting to %s"),Host
.c_str());
153 // Free the old address structure
154 if (LastHostAddr
!= 0)
156 freeaddrinfo(LastHostAddr
);
161 // We only understand SOCK_STREAM sockets.
162 struct addrinfo Hints
;
163 memset(&Hints
,0,sizeof(Hints
));
164 Hints
.ai_socktype
= SOCK_STREAM
;
165 Hints
.ai_flags
= AI_ADDRCONFIG
;
166 Hints
.ai_protocol
= 0;
168 // if we couldn't resolve the host before, we don't try now
169 if(bad_addr
.find(Host
) != bad_addr
.end())
170 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
172 // Resolve both the host and service simultaneously
176 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
179 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
183 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
187 bad_addr
.insert(bad_addr
.begin(), Host
);
188 Owner
->SetFailReason("ResolveFailure");
189 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
192 if (Res
== EAI_AGAIN
)
194 Owner
->SetFailReason("TmpResolveFailure");
195 return _error
->Error(_("Temporary failure resolving '%s'"),
198 return _error
->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
199 Host
.c_str(),ServStr
,Res
,gai_strerror(Res
));
208 // When we have an IP rotation stay with the last IP.
209 struct addrinfo
*CurHost
= LastHostAddr
;
215 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
223 // Ignore UNIX domain sockets
226 CurHost
= CurHost
->ai_next
;
228 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
230 /* If we reached the end of the search list then wrap around to the
232 if (CurHost
== 0 && LastUsed
!= 0)
233 CurHost
= LastHostAddr
;
235 // Reached the end of the search cycle
236 if (CurHost
== LastUsed
)
243 if (_error
->PendingError() == true)
245 return _error
->Error(_("Unable to connect to %s:%s:"),Host
.c_str(),ServStr
);