]>
git.saurik.com Git - apt.git/blob - methods/connect.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: connect.cc,v 1.6 2000/05/28 04:34:44 jgg Exp $
4 /* ######################################################################
6 Connect - Replacement connect call
8 ##################################################################### */
10 // Include Files /*{{{*/
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/fileutl.h>
20 #include <netinet/in.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
25 #include "rfc2553emu.h"
28 static string LastHost
;
29 static int LastPort
= 0;
30 static struct addrinfo
*LastHostAddr
= 0;
31 static struct addrinfo
*LastUsed
= 0;
33 // DoConnect - Attempt a connect operation /*{{{*/
34 // ---------------------------------------------------------------------
35 /* This helper function attempts a connection to a single address. */
36 static bool DoConnect(struct addrinfo
*Addr
,string Host
,
37 unsigned long TimeOut
,int &Fd
,pkgAcqMethod
*Owner
)
39 // Show a status indicator
40 char Name
[NI_MAXHOST
];
41 char Service
[NI_MAXSERV
];
44 getnameinfo(Addr
->ai_addr
,Addr
->ai_addrlen
,
45 Name
,sizeof(Name
),Service
,sizeof(Service
),
46 NI_NUMERICHOST
|NI_NUMERICSERV
);
47 Owner
->Status("Connecting to %s (%s)",Host
.c_str(),Name
);
50 if ((Fd
= socket(Addr
->ai_family
,Addr
->ai_socktype
,
51 Addr
->ai_protocol
)) < 0)
52 return _error
->Errno("socket","Could not create a socket");
55 if (connect(Fd
,Addr
->ai_addr
,Addr
->ai_addrlen
) < 0 &&
57 return _error
->Errno("connect","Cannot initiate the connection "
58 "to %s:%s (%s).",Host
.c_str(),Service
,Name
);
60 /* This implements a timeout for connect by opening the connection
62 if (WaitFd(Fd
,true,TimeOut
) == false)
63 return _error
->Error("Could not connect to %s:%s (%s), "
64 "connection timed out",Host
.c_str(),Service
,Name
);
66 // Check the socket for an error condition
68 unsigned int Len
= sizeof(Err
);
69 if (getsockopt(Fd
,SOL_SOCKET
,SO_ERROR
,&Err
,&Len
) != 0)
70 return _error
->Errno("getsockopt","Failed");
75 return _error
->Errno("connect","Could not connect to %s:%s (%s).",Host
.c_str(),
82 // Connect - Connect to a server /*{{{*/
83 // ---------------------------------------------------------------------
84 /* Performs a connection to the server */
85 bool Connect(string Host
,int Port
,const char *Service
,int DefPort
,int &Fd
,
86 unsigned long TimeOut
,pkgAcqMethod
*Owner
)
88 if (_error
->PendingError() == true)
91 // Convert the port name/number
94 snprintf(ServStr
,sizeof(ServStr
),"%u",Port
);
96 snprintf(ServStr
,sizeof(ServStr
),"%s",Service
);
98 /* We used a cached address record.. Yes this is against the spec but
99 the way we have setup our rotating dns suggests that this is more
101 if (LastHost
!= Host
|| LastPort
!= Port
)
103 Owner
->Status("Connecting to %s",Host
.c_str());
105 // Free the old address structure
106 if (LastHostAddr
!= 0)
108 freeaddrinfo(LastHostAddr
);
113 // We only understand SOCK_STREAM sockets.
114 struct addrinfo Hints
;
115 memset(&Hints
,0,sizeof(Hints
));
116 Hints
.ai_socktype
= SOCK_STREAM
;
117 Hints
.ai_protocol
= 0;
119 // Resolve both the host and service simultaneously
123 if ((Res
= getaddrinfo(Host
.c_str(),ServStr
,&Hints
,&LastHostAddr
)) != 0 ||
126 if (Res
== EAI_NONAME
|| Res
== EAI_SERVICE
)
130 snprintf(ServStr
,sizeof(ServStr
),"%u",DefPort
);
134 return _error
->Error("Could not resolve '%s'",Host
.c_str());
137 return _error
->Error("Something wicked happend resolving '%s:%s'",
138 Host
.c_str(),ServStr
);
147 // When we have an IP rotation stay with the last IP.
148 struct addrinfo
*CurHost
= LastHostAddr
;
154 if (DoConnect(CurHost
,Host
,TimeOut
,Fd
,Owner
) == true)
162 // Ignore UNIX domain sockets
165 CurHost
= CurHost
->ai_next
;
167 while (CurHost
!= 0 && CurHost
->ai_family
== AF_UNIX
);
174 if (_error
->PendingError() == true)
176 return _error
->Error("Unable to connect to %s:",Host
.c_str(),ServStr
);