]>
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 /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/strutl.h>
19 #include <apt-pkg/acquire-method.h>
20 #include <apt-pkg/configuration.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
37 #include "rfc2553emu.h"
41 static std::string LastHost
;
42 static int LastPort
= 0;
43 static struct addrinfo
*LastHostAddr
= 0;
44 static struct addrinfo
*LastUsed
= 0;
46 // Set of IP/hostnames that we timed out before or couldn't resolve
47 static std::set
<std::string
> bad_addr
;
49 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
50 // ---------------------------------------------------------------------
51 /* This is called during certain errors in order to recover by selecting a
55 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
56 LastUsed
= LastUsed
->ai_next
;
58 LastUsed
= LastHostAddr
;
61 // DoConnect - Attempt a connect operation /*{{{*/
62 // ---------------------------------------------------------------------
63 /* This helper function attempts a connection to a single address. */
64 static bool DoConnect(struct addrinfo
*Addr
,std::string Host
,
65 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
67 // Show a status indicator
68 char Name
[NI_MAXHOST
];
69 char Service
[NI_MAXSERV
];
73 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
74 Name
,sizeof(Name
),Service
,sizeof(Service
),
75 NI_NUMERICHOST
|NI_NUMERICSERV
);
76 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
78 // if that addr did timeout before, we do not try it again
79 if(bad_addr
.find(std::string(Name
)) != bad_addr
.end())
82 /* If this is an IP rotation store the IP we are using.. If something goes
83 wrong this will get tacked onto the end of the error message */
84 if (LastHostAddr
->ai_next
!= 0)
87 ioprintf(ss
, _("[IP: %s %s]"),Name
,Service
);
88 Owner
->SetIP(ss
.str());
92 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
93 Addr
->ai_protocol
)) < 0)
94 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
95 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
98 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
100 return _error
->Errno("connect",_("Cannot initiate the connection "
101 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
103 /* This implements a timeout for connect by opening the connection
105 if (WaitFd(Fd
,true,TimeOut
) == false) {
106 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
107 Owner
->SetFailReason("Timeout");
108 return _error
->Error(_("Could not connect to %s:%s (%s), "
109 "connection timed out"),Host
.c_str(),Service
,Name
);
112 // Check the socket for an error condition
114 unsigned int Len
= sizeof(Err
);
115 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
116 return _error
->Errno("getsockopt",_("Failed"));
121 if(errno
== ECONNREFUSED
)
122 Owner
->SetFailReason("ConnectionRefused");
123 else if (errno
== ETIMEDOUT
)
124 Owner
->SetFailReason("ConnectionTimedOut");
125 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
126 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
133 // Connect - Connect to a server /*{{{*/
134 // ---------------------------------------------------------------------
135 /* Performs a connection to the server */
136 bool Connect(std::string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
137 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
139 if (_error
->PendingError() == true)
142 // Convert the port name/number
145 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
147 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
149 /* We used a cached address record.. Yes this is against the spec but
150 the way we have setup our rotating dns suggests that this is more
152 if (LastHost
!= Host
|| LastPort
!= Port
)
154 Owner
->Status(_("Connecting to %s"),Host
.c_str());
156 // Free the old address structure
157 if (LastHostAddr
!= 0)
159 freeaddrinfo(LastHostAddr
);
164 // We only understand SOCK_STREAM sockets.
165 struct addrinfo Hints
;
166 memset(&Hints
,0,sizeof(Hints
));
167 Hints
.ai_socktype
= SOCK_STREAM
;
168 Hints
.ai_flags
= AI_ADDRCONFIG
;
169 Hints
.ai_protocol
= 0;
171 if(_config
->FindB("Acquire::ForceIPv4", false) == true)
172 Hints
.ai_family
= AF_INET
;
173 else if(_config
->FindB("Acquire::ForceIPv6", false) == true)
174 Hints
.ai_family
= AF_INET6
;
176 Hints
.ai_family
= AF_UNSPEC
;
178 // if we couldn't resolve the host before, we don't try now
179 if(bad_addr
.find(Host
) != bad_addr
.end())
180 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
182 // Resolve both the host and service simultaneously
186 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
189 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
193 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
197 bad_addr
.insert(bad_addr
.begin(), Host
);
198 Owner
->SetFailReason("ResolveFailure");
199 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
202 if (Res
== EAI_AGAIN
)
204 Owner
->SetFailReason("TmpResolveFailure");
205 return _error
->Error(_("Temporary failure resolving '%s'"),
208 if (Res
== EAI_SYSTEM
)
209 return _error
->Errno("getaddrinfo", _("System error resolving '%s:%s'"),
210 Host
.c_str(),ServStr
);
211 return _error
->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
212 Host
.c_str(),ServStr
,Res
,gai_strerror(Res
));
221 // When we have an IP rotation stay with the last IP.
222 struct addrinfo
*CurHost
= LastHostAddr
;
228 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
236 // Ignore UNIX domain sockets
239 CurHost
= CurHost
->ai_next
;
241 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
243 /* If we reached the end of the search list then wrap around to the
245 if (CurHost
== 0 && LastUsed
!= 0)
246 CurHost
= LastHostAddr
;
248 // Reached the end of the search cycle
249 if (CurHost
== LastUsed
)
256 if (_error
->PendingError() == true)
258 return _error
->Error(_("Unable to connect to %s:%s:"),Host
.c_str(),ServStr
);