]>
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>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
29 #include "rfc2553emu.h"
33 static string LastHost
;
34 static int LastPort
= 0;
35 static struct addrinfo
*LastHostAddr
= 0;
36 static struct addrinfo
*LastUsed
= 0;
38 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
39 // ---------------------------------------------------------------------
40 /* This is called during certain errors in order to recover by selecting a
44 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
45 LastUsed
= LastUsed
->ai_next
;
47 LastUsed
= LastHostAddr
;
50 // DoConnect - Attempt a connect operation /*{{{*/
51 // ---------------------------------------------------------------------
52 /* This helper function attempts a connection to a single address. */
53 static bool DoConnect(struct addrinfo
*Addr
,string Host
,
54 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
56 // Show a status indicator
57 char Name
[NI_MAXHOST
];
58 char Service
[NI_MAXSERV
];
62 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
63 Name
,sizeof(Name
),Service
,sizeof(Service
),
64 NI_NUMERICHOST
|NI_NUMERICSERV
);
65 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
67 /* If this is an IP rotation store the IP we are using.. If something goes
68 wrong this will get tacked onto the end of the error message */
69 if (LastHostAddr
->ai_next
!= 0)
72 ioprintf(ss
, _("[IP: %s %s]"),Name
,Service
);
73 Owner
->SetIP(ss
.str());
77 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
78 Addr
->ai_protocol
)) < 0)
79 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
80 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
83 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
85 return _error
->Errno("connect",_("Cannot initiate the connection "
86 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
88 /* This implements a timeout for connect by opening the connection
90 if (WaitFd(Fd
,true,TimeOut
) == false) {
91 Owner
->SetFailReason("Timeout");
92 return _error
->Error(_("Could not connect to %s:%s (%s), "
93 "connection timed out"),Host
.c_str(),Service
,Name
);
96 // Check the socket for an error condition
98 unsigned int Len
= sizeof(Err
);
99 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
100 return _error
->Errno("getsockopt",_("Failed"));
105 if(errno
== ECONNREFUSED
)
106 Owner
->SetFailReason("ConnectionRefused");
107 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
114 // Connect - Connect to a server /*{{{*/
115 // ---------------------------------------------------------------------
116 /* Performs a connection to the server */
117 bool Connect(string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
118 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
120 if (_error
->PendingError() == true)
123 // Convert the port name/number
126 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
128 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
130 /* We used a cached address record.. Yes this is against the spec but
131 the way we have setup our rotating dns suggests that this is more
133 if (LastHost
!= Host
|| LastPort
!= Port
)
135 Owner
->Status(_("Connecting to %s"),Host
.c_str());
137 // Free the old address structure
138 if (LastHostAddr
!= 0)
140 freeaddrinfo(LastHostAddr
);
145 // We only understand SOCK_STREAM sockets.
146 struct addrinfo Hints
;
147 memset(&Hints
,0,sizeof(Hints
));
148 Hints
.ai_socktype
= SOCK_STREAM
;
149 Hints
.ai_protocol
= 0;
151 // Resolve both the host and service simultaneously
155 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
158 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
162 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
166 Owner
->SetFailReason("ResolveFailure");
167 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
170 if (Res
== EAI_AGAIN
)
172 Owner
->SetFailReason("TmpResolveFailure");
173 return _error
->Error(_("Temporary failure resolving '%s'"),
176 return _error
->Error(_("Something wicked happened resolving '%s:%s' (%i)"),
177 Host
.c_str(),ServStr
,Res
);
186 // When we have an IP rotation stay with the last IP.
187 struct addrinfo
*CurHost
= LastHostAddr
;
193 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
201 // Ignore UNIX domain sockets
204 CurHost
= CurHost
->ai_next
;
206 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
208 /* If we reached the end of the search list then wrap around to the
210 if (CurHost
== 0 && LastUsed
!= 0)
211 CurHost
= LastHostAddr
;
213 // Reached the end of the search cycle
214 if (CurHost
== LastUsed
)
221 if (_error
->PendingError() == true)
223 return _error
->Error(_("Unable to connect to %s %s:"),Host
.c_str(),ServStr
);