Better handling of missing services
[ntk/apt.git] / methods / connect.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: connect.cc,v 1.3 1999/07/18 23:06:56 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
28 static string LastHost;
29 static int LastPort = 0;
30 static struct addrinfo *LastHostAddr = 0;
31 static struct addrinfo *LastUsed = 0;
32
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)
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 */
78 bool Connect(string Host,int Port,const char *Service,int DefPort,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 Hints.ai_protocol = IPPROTO_TCP; // Right?
110
111 // Resolve both the host and service simultaneously
112 while (1)
113 {
114 int Res;
115 if ((Res = getaddrinfo(Host.c_str(),S,&Hints,&LastHostAddr)) != 0 ||
116 LastHostAddr == 0)
117 {
118 if (Res == EAI_SERVICE)
119 return _error->Error("Could not resolve service '%s'",S);
120
121 if (Res == EAI_NONAME)
122 {
123 if (DefPort != 0)
124 {
125 snprintf(S,sizeof(S),"%u",DefPort);
126 DefPort = 0;
127 continue;
128 }
129 return _error->Error("Could not resolve '%s'",Host.c_str());
130 }
131
132 return _error->Error("Something wicked happend resolving '%s/%s'",
133 Host.c_str(),S);
134 }
135 break;
136 }
137
138 if (LastHostAddr->ai_family == AF_UNIX)
139 return _error->Error("getaddrinfo returned a unix domain socket\n");
140
141 LastHost = Host;
142 LastPort = Port;
143 LastUsed = 0;
144 }
145
146 // Get the printable IP address
147 struct addrinfo *CurHost = LastHostAddr;
148 if (LastUsed != 0)
149 CurHost = LastUsed;
150
151 while (CurHost != 0)
152 {
153 if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
154 {
155 LastUsed = CurHost;
156 return true;
157 }
158 close(Fd);
159 Fd = -1;
160
161 CurHost = CurHost->ai_next;
162 LastUsed = 0;
163 if (CurHost != 0)
164 _error->Discard();
165 }
166
167 return false;
168 }
169 /*}}}*/