failure code if package files could not be downloaded
[ntk/apt.git] / methods / connect.cc
CommitLineData
0837bd25
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: connect.cc,v 1.1 1999/05/29 03:25:03 jgg Exp $
4/* ######################################################################
5
6 Connect - Replacement connect call
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
11#include "connect.h"
12#include <apt-pkg/error.h>
13#include <apt-pkg/fileutl.h>
14
15#include <stdio.h>
16#include <errno.h>
17#include <unistd.h>
18
19// Internet stuff
20#include <netinet/in.h>
21#include <sys/socket.h>
22#include <arpa/inet.h>
23#include <netdb.h>
24
25#include "rfc2553emu.h"
26 /*}}}*/
27
28static string LastHost;
29static int LastPort = 0;
30static struct addrinfo *LastHostAddr = 0;
31static struct addrinfo *LastUsed = 0;
32
33// DoConnect - Attempt a connect operation /*{{{*/
34// ---------------------------------------------------------------------
35/* This helper function attempts a connection to a single address. */
36static bool DoConnect(struct addrinfo *Addr,string Host,
37 unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner)
38{
39 // Show a status indicator
40 char Name[NI_MAXHOST];
41 Name[0] = 0;
42 getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
43 Name,sizeof(Name),0,0,NI_NUMERICHOST);
44 Owner->Status("Connecting to %s (%s)",Host.c_str(),Name);
45
46 // Get a socket
47 if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
48 Addr->ai_protocol)) < 0)
49 return _error->Errno("socket","Could not create a socket");
50
51 SetNonBlock(Fd,true);
52 if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 &&
53 errno != EINPROGRESS)
54 return _error->Errno("connect","Cannot initiate the connection "
55 "to %s (%s).",Host.c_str(),Name);
56
57 /* This implements a timeout for connect by opening the connection
58 nonblocking */
59 if (WaitFd(Fd,true,TimeOut) == false)
60 return _error->Error("Could not connect to %s (%s), "
61 "connection timed out",Host.c_str(),Name);
62
63 // Check the socket for an error condition
64 unsigned int Err;
65 unsigned int Len = sizeof(Err);
66 if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0)
67 return _error->Errno("getsockopt","Failed");
68
69 if (Err != 0)
70 return _error->Error("Could not connect to %s (%s).",Host.c_str(),Name);
71
72 return true;
73}
74 /*}}}*/
75// Connect - Connect to a server /*{{{*/
76// ---------------------------------------------------------------------
77/* Performs a connection to the server */
78bool Connect(string Host,int Port,const char *Service,int &Fd,
79 unsigned long TimeOut,pkgAcqMethod *Owner)
80{
81 if (_error->PendingError() == true)
82 return false;
83
84 /* We used a cached address record.. Yes this is against the spec but
85 the way we have setup our rotating dns suggests that this is more
86 sensible */
87 if (LastHost != Host || LastPort != Port)
88 {
89 Owner->Status("Connecting to %s",Host.c_str());
90
91 // Lookup the host
92 char S[300];
93 if (Port != 0)
94 snprintf(S,sizeof(S),"%u",Port);
95 else
96 snprintf(S,sizeof(S),"%s",Service);
97
98 // Free the old address structure
99 if (LastHostAddr != 0)
100 {
101 freeaddrinfo(LastHostAddr);
102 LastHostAddr = 0;
103 }
104
105 // We only understand SOCK_STREAM sockets.
106 struct addrinfo Hints;
107 memset(&Hints,0,sizeof(Hints));
108 Hints.ai_socktype = SOCK_STREAM;
109
110 // Resolve both the host and service simultaneously
111 if (getaddrinfo(Host.c_str(),S,&Hints,&LastHostAddr) != 0 ||
112 LastHostAddr == 0)
113 return _error->Error("Could not resolve '%s'",Host.c_str());
114
115 LastHost = Host;
116 LastPort = Port;
117 LastUsed = 0;
118 }
119
120 // Get the printable IP address
121 struct addrinfo *CurHost = LastHostAddr;
122 if (LastUsed != 0)
123 CurHost = LastUsed;
124
125 while (CurHost != 0)
126 {
127 if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
128 {
129 LastUsed = CurHost;
130 return true;
131 }
132 close(Fd);
133 Fd = -1;
134
135 CurHost = CurHost->ai_next;
136 LastUsed = 0;
137 if (CurHost != 0)
138 _error->Discard();
139 }
140
141 return false;
142}
143 /*}}}*/