]>
git.saurik.com Git - apt.git/blob - methods/connect.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: connect.cc,v 1.9 2002/09/14 05:28:38 jgg 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"
31 static string LastHost
;
32 static int LastPort
= 0;
33 static struct addrinfo
*LastHostAddr
= 0;
34 static struct addrinfo
*LastUsed
= 0;
36 // RotateDNS - Select a new server from a DNS rotation /*{{{*/
37 // ---------------------------------------------------------------------
38 /* This is called during certain errors in order to recover by selecting a
42 if (LastUsed
!= 0 && LastUsed
->ai_next
!= 0)
43 LastUsed
= LastUsed
->ai_next
;
45 LastUsed
= LastHostAddr
;
48 // DoConnect - Attempt a connect operation /*{{{*/
49 // ---------------------------------------------------------------------
50 /* This helper function attempts a connection to a single address. */
51 static bool DoConnect(struct addrinfo
*Addr
,string Host
,
52 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
54 // Show a status indicator
55 char Name
[NI_MAXHOST
];
56 char Service
[NI_MAXSERV
];
60 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
61 Name
,sizeof(Name
),Service
,sizeof(Service
),
62 NI_NUMERICHOST
|NI_NUMERICSERV
);
63 Owner
->Status("Connecting to %s (%s)",Host
.c_str(),Name
);
65 /* If this is an IP rotation store the IP we are using.. If something goes
66 wrong this will get tacked onto the end of the error message */
67 if (LastHostAddr
->ai_next
!= 0)
69 char Name2
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
70 snprintf(Name2
,sizeof(Name2
),"[IP: %s %s]",Name
,Service
);
71 Owner
->SetFailExtraMsg(string(Name2
));
74 Owner
->SetFailExtraMsg("");
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 return _error
->Error("Could not connect to %s:%s (%s), "
92 "connection timed out",Host
.c_str(),Service
,Name
);
94 // Check the socket for an error condition
96 unsigned int Len
= sizeof(Err
);
97 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
98 return _error
->Errno("getsockopt","Failed");
103 return _error
->Errno("connect","Could not connect to %s:%s (%s).",Host
.c_str(),
110 // Connect - Connect to a server /*{{{*/
111 // ---------------------------------------------------------------------
112 /* Performs a connection to the server */
113 bool Connect(string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
114 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
116 if (_error
->PendingError() == true)
119 // Convert the port name/number
122 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
124 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
126 /* We used a cached address record.. Yes this is against the spec but
127 the way we have setup our rotating dns suggests that this is more
129 if (LastHost
!= Host
|| LastPort
!= Port
)
131 Owner
->Status("Connecting to %s",Host
.c_str());
133 // Free the old address structure
134 if (LastHostAddr
!= 0)
136 freeaddrinfo(LastHostAddr
);
141 // We only understand SOCK_STREAM sockets.
142 struct addrinfo Hints
;
143 memset(&Hints
,0,sizeof(Hints
));
144 Hints
.ai_socktype
= SOCK_STREAM
;
145 Hints
.ai_protocol
= 0;
147 // Resolve both the host and service simultaneously
151 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
154 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
158 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
162 return _error
->Error("Could not resolve '%s'",Host
.c_str());
165 if (Res
== EAI_AGAIN
)
166 return _error
->Error("Temporary failure resolving '%s'",
168 return _error
->Error("Something wicked happened resolving '%s:%s' (%i)",
169 Host
.c_str(),ServStr
,Res
);
178 // When we have an IP rotation stay with the last IP.
179 struct addrinfo
*CurHost
= LastHostAddr
;
185 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
193 // Ignore UNIX domain sockets
196 CurHost
= CurHost
->ai_next
;
198 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
200 /* If we reached the end of the search list then wrap around to the
202 if (CurHost
== 0 && LastUsed
!= 0)
203 CurHost
= LastHostAddr
;
205 // Reached the end of the search cycle
206 if (CurHost
== LastUsed
)
213 if (_error
->PendingError() == true)
215 return _error
->Error("Unable to connect to %s %s:",Host
.c_str(),ServStr
);