]>
git.saurik.com Git - apt.git/blob - methods/connect.cc
8c2ac6d56dab4451b41ba0208297cad60c40fbe1
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>
23 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
28 #include "rfc2553emu.h"
32 static string LastHost
;
33 static int LastPort
= 0;
34 static struct addrinfo
*LastHostAddr
= 0;
35 static struct addrinfo
*LastUsed
= 0;
37 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
38 // ---------------------------------------------------------------------
39 /* This is called during certain errors in order to recover by selecting a
43 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
44 LastUsed
= LastUsed
->ai_next
;
46 LastUsed
= LastHostAddr
;
49 // DoConnect - Attempt a connect operation /*{{{*/
50 // ---------------------------------------------------------------------
51 /* This helper function attempts a connection to a single address. */
52 static bool DoConnect(struct addrinfo
*Addr
,string Host
,
53 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
55 // Show a status indicator
56 char Name
[NI_MAXHOST
];
57 char Service
[NI_MAXSERV
];
61 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
62 Name
,sizeof(Name
),Service
,sizeof(Service
),
63 NI_NUMERICHOST
|NI_NUMERICSERV
);
64 Owner
->Status(_("Connecting to %s (%s)"),Host
.c_str(),Name
);
66 /* If this is an IP rotation store the IP we are using.. If something goes
67 wrong this will get tacked onto the end of the error message */
68 if (LastHostAddr
->ai_next
!= 0)
70 char Name2
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
71 snprintf(Name2
,sizeof(Name2
),_("[IP: %s %s]"),Name
,Service
);
72 Owner
->SetFailExtraMsg(string(Name2
));
75 Owner
->SetFailExtraMsg("");
78 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
79 Addr
->ai_protocol
)) < 0)
80 return _error
->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
81 Name
,Addr
->ai_family
,Addr
->ai_socktype
,Addr
->ai_protocol
);
84 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
86 return _error
->Errno("connect",_("Cannot initiate the connection "
87 "to %s:%s (%s)."),Host
.c_str(),Service
,Name
);
89 /* This implements a timeout for connect by opening the connection
91 if (WaitFd(Fd
,true,TimeOut
) == false) {
92 Owner
->SetFailExtraMsg("\nFailReason: Timeout");
93 return _error
->Error(_("Could not connect to %s:%s (%s), "
94 "connection timed out"),Host
.c_str(),Service
,Name
);
97 // Check the socket for an error condition
99 unsigned int Len
= sizeof(Err
);
100 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
101 return _error
->Errno("getsockopt",_("Failed"));
106 if(errno
== ECONNREFUSED
)
107 Owner
->SetFailExtraMsg("\nFailReason: ConnectionRefused");
108 return _error
->Errno("connect",_("Could not connect to %s:%s (%s)."),Host
.c_str(),
115 // Connect - Connect to a server /*{{{*/
116 // ---------------------------------------------------------------------
117 /* Performs a connection to the server */
118 bool Connect(string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
119 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
121 if (_error
->PendingError() == true)
124 // Convert the port name/number
127 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
129 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
131 /* We used a cached address record.. Yes this is against the spec but
132 the way we have setup our rotating dns suggests that this is more
134 if (LastHost
!= Host
|| LastPort
!= Port
)
136 Owner
->Status(_("Connecting to %s"),Host
.c_str());
138 // Free the old address structure
139 if (LastHostAddr
!= 0)
141 freeaddrinfo(LastHostAddr
);
146 // We only understand SOCK_STREAM sockets.
147 struct addrinfo Hints
;
148 memset(&Hints
,0,sizeof(Hints
));
149 Hints
.ai_socktype
= SOCK_STREAM
;
150 Hints
.ai_protocol
= 0;
152 // Resolve both the host and service simultaneously
156 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
159 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
163 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
167 return _error
->Error(_("Could not resolve '%s'"),Host
.c_str());
170 if (Res
== EAI_AGAIN
)
172 Owner
->SetFailExtraMsg("\nFailReason: 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
);