]>
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>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
36 #include "rfc2553emu.h"
40 static std::string LastHost
;
41 static int LastPort
= 0;
42 static struct addrinfo
*LastHostAddr
= 0;
43 static struct addrinfo
*LastUsed
= 0;
45 // Set of IP/hostnames that we timed out before or couldn't resolve
46 static std::set
<std::string
> bad_addr
;
48 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
49 // ---------------------------------------------------------------------
50 /* This is called during certain errors in order to recover by selecting a
54 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
55 LastUsed
= LastUsed
->ai_next
;
57 LastUsed
= LastHostAddr
;
60 // DoConnect - Attempt a connect operation /*{{{*/
61 // ---------------------------------------------------------------------
62 /* This helper function attempts a connection to a single address. */
63 static bool DoConnect(struct addrinfo
*Addr
,std::string Host
,
64 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
66 // Show a status indicator
67 char Name
[NI_MAXHOST
];
68 char Service
[NI_MAXSERV
];
72 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
73 Name
,sizeof(Name
),Service
,sizeof(Service
),
74 NI_NUMERICHOST
|NI_NUMERICSERV
);
75 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
77 // if that addr did timeout before, we do not try it again
78 if(bad_addr
.find(std::string(Name
)) != bad_addr
.end())
81 /* If this is an IP rotation store the IP we are using.. If something goes
82 wrong this will get tacked onto the end of the error message */
83 if (LastHostAddr
->ai_next
!= 0)
86 ioprintf(ss
, _("[IP: %s %s]"),Name
,Service
);
87 Owner
->SetIP(ss
.str());
91 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
92 Addr
->ai_protocol
)) < 0)
93 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
94 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
97 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
99 return _error
->Errno("connect",_("Cannot initiate the connection "
100 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
102 /* This implements a timeout for connect by opening the connection
104 if (WaitFd(Fd
,true,TimeOut
) == false) {
105 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
106 Owner
->SetFailReason("Timeout");
107 return _error
->Error(_("Could not connect to %s:%s (%s), "
108 "connection timed out"),Host
.c_str(),Service
,Name
);
111 // Check the socket for an error condition
113 unsigned int Len
= sizeof(Err
);
114 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
115 return _error
->Errno("getsockopt",_("Failed"));
120 if(errno
== ECONNREFUSED
)
121 Owner
->SetFailReason("ConnectionRefused");
122 else if (errno
== ETIMEDOUT
)
123 Owner
->SetFailReason("ConnectionTimedOut");
124 bad_addr
.insert(bad_addr
.begin(), std::string(Name
));
125 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
132 // Connect - Connect to a server /*{{{*/
133 // ---------------------------------------------------------------------
134 /* Performs a connection to the server */
135 bool Connect(std::string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
136 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
138 if (_error
->PendingError() == true)
141 // Convert the port name/number
144 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
146 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
148 /* We used a cached address record.. Yes this is against the spec but
149 the way we have setup our rotating dns suggests that this is more
151 if (LastHost
!= Host
|| LastPort
!= Port
)
153 Owner
->Status(_("Connecting to %s"),Host
.c_str());
155 // Free the old address structure
156 if (LastHostAddr
!= 0)
158 freeaddrinfo(LastHostAddr
);
163 // We only understand SOCK_STREAM sockets.
164 struct addrinfo Hints
;
165 memset(&Hints
,0,sizeof(Hints
));
166 Hints
.ai_socktype
= SOCK_STREAM
;
167 Hints
.ai_flags
= AI_ADDRCONFIG
;
168 Hints
.ai_protocol
= 0;
170 // if we couldn't resolve the host before, we don't try now
171 if(bad_addr
.find(Host
) != bad_addr
.end())
172 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
174 // Resolve both the host and service simultaneously
178 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
181 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
185 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
189 bad_addr
.insert(bad_addr
.begin(), Host
);
190 Owner
->SetFailReason("ResolveFailure");
191 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
194 if (Res
== EAI_AGAIN
)
196 Owner
->SetFailReason("TmpResolveFailure");
197 return _error
->Error(_("Temporary failure resolving '%s'"),
200 return _error
->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
201 Host
.c_str(),ServStr
,Res
,gai_strerror(Res
));
210 // When we have an IP rotation stay with the last IP.
211 struct addrinfo
*CurHost
= LastHostAddr
;
217 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
225 // Ignore UNIX domain sockets
228 CurHost
= CurHost
->ai_next
;
230 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
232 /* If we reached the end of the search list then wrap around to the
234 if (CurHost
== 0 && LastUsed
!= 0)
235 CurHost
= LastHostAddr
;
237 // Reached the end of the search cycle
238 if (CurHost
== LastUsed
)
245 if (_error
->PendingError() == true)
247 return _error
->Error(_("Unable to connect to %s:%s:"),Host
.c_str(),ServStr
);