]>
Commit | Line | Data |
---|---|---|
0837bd25 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
72472b95 | 3 | // $Id: connect.cc,v 1.4 1999/11/19 05:01:54 jgg Exp $ |
0837bd25 AL |
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 | /*}}}*/ | |
c141b9a9 | 75 | // Connect - Connect to a server /*{{{*/ |
0837bd25 AL |
76 | // --------------------------------------------------------------------- |
77 | /* Performs a connection to the server */ | |
9505213b | 78 | bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd, |
0837bd25 AL |
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; | |
9505213b | 109 | Hints.ai_protocol = IPPROTO_TCP; // Right? |
0837bd25 AL |
110 | |
111 | // Resolve both the host and service simultaneously | |
9505213b | 112 | while (1) |
c141b9a9 | 113 | { |
9505213b AL |
114 | int Res; |
115 | if ((Res = getaddrinfo(Host.c_str(),S,&Hints,&LastHostAddr)) != 0 || | |
116 | LastHostAddr == 0) | |
117 | { | |
72472b95 | 118 | if (Res == EAI_NONAME || Res == EAI_SERVICE) |
9505213b AL |
119 | { |
120 | if (DefPort != 0) | |
121 | { | |
122 | snprintf(S,sizeof(S),"%u",DefPort); | |
123 | DefPort = 0; | |
124 | continue; | |
125 | } | |
126 | return _error->Error("Could not resolve '%s'",Host.c_str()); | |
127 | } | |
128 | ||
129 | return _error->Error("Something wicked happend resolving '%s/%s'", | |
130 | Host.c_str(),S); | |
131 | } | |
132 | break; | |
c141b9a9 AL |
133 | } |
134 | ||
9505213b AL |
135 | if (LastHostAddr->ai_family == AF_UNIX) |
136 | return _error->Error("getaddrinfo returned a unix domain socket\n"); | |
137 | ||
0837bd25 AL |
138 | LastHost = Host; |
139 | LastPort = Port; | |
140 | LastUsed = 0; | |
141 | } | |
142 | ||
143 | // Get the printable IP address | |
144 | struct addrinfo *CurHost = LastHostAddr; | |
145 | if (LastUsed != 0) | |
146 | CurHost = LastUsed; | |
147 | ||
148 | while (CurHost != 0) | |
149 | { | |
150 | if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true) | |
151 | { | |
152 | LastUsed = CurHost; | |
153 | return true; | |
154 | } | |
155 | close(Fd); | |
156 | Fd = -1; | |
157 | ||
158 | CurHost = CurHost->ai_next; | |
159 | LastUsed = 0; | |
160 | if (CurHost != 0) | |
161 | _error->Discard(); | |
162 | } | |
163 | ||
164 | return false; | |
165 | } | |
166 | /*}}}*/ |