Debian Codegraph @

src/resolver.c

raw
1 /*
2 resolver.c: TCP network stuff, for IPv4 and IPv6
3 Oh, and also some URL parsing... extracting host name and such.
4
5 copyright 2008-2023 by the mpg123 project - free software under the terms of the LGPL 2.1
6 see COPYING and AUTHORS files in distribution or http://mpg123.org
7 initially written Thomas Orgis (based on httpget.c)
8
9 The idea is to have everything involving the game between URLs and IPs/connections separated here.
10 I begin with the outsourcing of IPv4 stuff, then make the stuff generic.
11 */
12
13 /* Newer glibc is strict about this: getaddrinfo() stuff needs POSIX2K. */
14 #define _POSIX_C_SOURCE 200112L
15
16 #include "mpg123app.h"
17
18 #ifdef NETWORK
19 #include "common/true.h"
20 #include "resolver.h"
21 #if !defined (WANT_WIN32_SOCKETS)
22 #include <netdb.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <errno.h>
28 #if defined (IPV6) && defined (__OS2__)
29 #include <libcx/net.h>
30 #endif
31 #endif
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
34 #endif
35 #ifdef HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38 #ifdef HAVE_SYS_TYPES_H
39 #include <sys/types.h>
40 #endif
41 #include <unistd.h>
42 #include "common/debug.h"
43
44 int split_url(mpg123_string *url, mpg123_string *auth, mpg123_string *host, mpg123_string *port, mpg123_string *path)
45 {
46 size_t pos = 0; /* current position in input URL */
47 size_t pos2 = 0; /* another position in input URL */
48 #ifdef IPV6
49 size_t pos3 = 0; /* yet another (for IPv6 port) */
50 #endif
51 char *part = NULL; /* a part of url we work on */
52 int ret = TRUE; /* return code */
53 /* Zeroing the output strings; not freeing to avoid unnecessary mallocs. */
54 if(auth) auth->fill = 0;
55 if(host) host->fill = 0;
56 if(port) port->fill = 0;
57 if(path) path->fill = 0;
58
59 if(!url || !url->fill || url->p[url->fill-1] != 0)
60 {
61 error("URL string is not good! (Programmer's fault!?)");
62 return FALSE;
63 }
64 if (!(strncmp(url->p+pos, "http://", 7)))
65 pos += 7; /* Drop protocol. */
66
67 /* Extract user[:passwd]@... */
68 if( (part = strchr(url->p+pos,'@')) )
69 {
70 size_t i;
71 size_t partlen = part - url->p - pos;
72 int have_auth = TRUE;
73 /* User names or passwords don't have "/" in them (?), the "@" wasn't for real if we find such. */
74 for(i=0;i<partlen;i++)
75 {
76 if(url->p[pos+i] == '/' )
77 {
78 have_auth = FALSE;
79 break;
80 }
81 }
82 if(have_auth)
83 {
84 if(auth != NULL && !mpg123_set_substring(auth, url->p, pos, partlen))
85 {
86 error("Cannot set auth string (out of mem?).");
87 return FALSE;
88 }
89 pos += partlen+1; /* Continuing after the "@". */
90 }
91 }
92
93 /* Extract host name or IP. */
94 #ifdef IPV6
95 if(url->p[pos] == '[')
96 { /* It's possibly an IPv6 url in [ ] */
97 ++pos;
98 if( (part = strchr(url->p+pos,']')) != NULL)
99 {
100 pos2 = part-url->p;
101 pos3 = pos2+1; /* : after ] */
102 }
103 else { error("Malformed IPv6 URL!"); return FALSE; }
104 }
105 else
106 {
107 #endif
108 for(pos2=pos; pos2 < url->fill-1; ++pos2)
109 {
110 char a = url->p[pos2];
111 if( a == ':' || a == '/') break;
112 }
113 #ifdef IPV6
114 pos3 = pos2;
115 }
116 #endif
117 /* At pos2 there is now either a delimiter or the end. */
118 debug4("hostname between %lu and %lu, %lu chars of %s", (unsigned long)pos, (unsigned long)pos2, (unsigned long)(pos2-pos), url->p + pos);
119 if(host != NULL && !mpg123_set_substring(host, url->p, pos, pos2-pos))
120 {
121 error("Cannot set host string (out of mem?).");
122 return FALSE;
123 }
124 #ifdef IPV6
125 pos = pos3; /* Look after ], if present. */
126 #else
127 pos = pos2;
128 #endif
129
130 /* Now for the port... */
131 if(url->p[pos] == ':')
132 {
133 pos += 1; /* We begin _after_ the ":". */
134 for(pos2=pos; pos2 < url->fill-1; ++pos2)
135 if( url->p[pos2] == '/' ) break;
136
137 /* Check for port being numbers? Not sure if that's needed. */
138 if(port) ret = mpg123_set_substring(port, url->p, pos, pos2-pos);
139 pos = pos2;
140 }
141 else if(port) ret = mpg123_set_string(port, "80");
142
143 if(!ret)
144 {
145 error("Cannot set port string (out of mem?).");
146 return FALSE;
147 }
148
149 /* Now only the path is left.
150 If there is no path at all, assume "/" */
151 if(path) ret = url->p[pos] == 0
152 ? mpg123_set_string(path, "/")
153 : mpg123_set_substring(path, url->p, pos, url->fill-1-pos);
154
155 if(!ret) error("Cannot set path string (out of mem?)");
156
157 return ret;
158 }
159
160 /* Switch between blocking and non-blocking mode. */
161 #if !defined (WANT_WIN32_SOCKETS)
162 static void nonblock(int sock)
163 {
164 int flags = fcntl(sock, F_GETFL);
165 flags |= O_NONBLOCK;
166 fcntl(sock, F_SETFL, flags);
167 }
168
169 static void block(int sock)
170 {
171 int flags = fcntl(sock, F_GETFL);
172 flags &= ~O_NONBLOCK;
173 fcntl(sock, F_SETFL, flags);
174 }
175
176 /* If we want a timeout, connect non-blocking and wait for that long... */
177 static int timeout_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
178 {
179 if(param.timeout > 0)
180 {
181 int err;
182 nonblock(sockfd);
183 err = connect(sockfd, serv_addr, addrlen);
184 if(err == 0)
185 {
186 debug("immediately successful");
187 block(sockfd);
188 return 0;
189 }
190 else if(errno == EINPROGRESS)
191 {
192 struct timeval tv;
193 fd_set fds;
194 tv.tv_sec = param.timeout;
195 tv.tv_usec = 0;
196
197 debug("in progress, waiting...");
198
199 FD_ZERO(&fds);
200 FD_SET(sockfd, &fds);
201 err = select(sockfd+1, NULL, &fds, NULL, &tv);
202 if(err > 0)
203 {
204 socklen_t len = sizeof(err);
205 if( (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
206 && (err == 0) )
207 {
208 debug("non-blocking connect has been successful");
209 block(sockfd);
210 return 0;
211 }
212 else
213 {
214 error1("connection error: %s", strerror(err));
215 return -1;
216 }
217 }
218 else if(err == 0)
219 {
220 error("connection timed out");
221 return -1;
222 }
223 else
224 {
225 error1("error from select(): %s", INT123_strerror(errno));
226 return -1;
227 }
228 }
229 else
230 {
231 error1("connection failed: %s", INT123_strerror(errno));
232 return err;
233 }
234 }
235 else
236 {
237 if(connect(sockfd, serv_addr, addrlen))
238 {
239 error1("connection failed: %s", INT123_strerror(errno));
240 return -1;
241 }
242 else return 0; /* _good_ */
243 }
244 }
245
246
247 /* So, this then is the only routine that should know about IPv4 or v6 in future. */
248 int open_connection(mpg123_string *host, mpg123_string *port)
249 {
250 #ifndef IPV6 /* The legacy code for IPv4. No real change to keep all compatibility. */
251 #ifndef INADDR_NONE
252 #define INADDR_NONE 0xffffffff
253 #endif
254 struct sockaddr_in server;
255 struct hostent *myhostent;
256 struct in_addr myaddr;
257 int isip = 1;
258 char *cptr = host->p;
259 int sock = -1;
260 if(param.verbose>1) fprintf(stderr, "Note: Attempting old-style connection to %s\n", host->p);
261 /* Resolve to IP; parse port number. */
262 while(*cptr) /* Iterate over characters of hostname, check if it's an IP or name. */
263 {
264 if ((*cptr < '0' || *cptr > '9') && *cptr != '.')
265 {
266 isip = 0;
267 break;
268 }
269 cptr++;
270 }
271 if(!isip)
272 { /* Name lookup. */
273 if (!(myhostent = gethostbyname(host->p))) return -1;
274
275 memcpy (&myaddr, myhostent->h_addr_list[0], sizeof(myaddr));
276 server.sin_addr.s_addr = myaddr.s_addr;
277 }
278 else /* Just use the IP. */
279 if((server.sin_addr.s_addr = inet_addr(host->p)) == INADDR_NONE)
280 return -1;
281
282 server.sin_port = htons(atoi(port->p));
283 server.sin_family = AF_INET;
284
285 if((sock = socket(PF_INET, SOCK_STREAM, 6)) < 0)
286 {
287 error1("Cannot create socket: %s", INT123_strerror(errno));
288 return -1;
289 }
290 if(timeout_connect(sock, (struct sockaddr *)&server, sizeof(server)))
291 return -1;
292 #else /* Host lookup and connection in a protocol independent manner. */
293 struct addrinfo hints;
294 struct addrinfo *addr, *addrlist;
295 int ret, sock = -1;
296
297 if(param.verbose>1) fprintf(stderr, "Note: Attempting new-style connection to %s\n", host->p);
298 memset(&hints, 0, sizeof(struct addrinfo));
299 hints.ai_family = AF_UNSPEC; /* We accept both IPv4 and IPv6 ... and perhaps IPv8;-) */
300 hints.ai_socktype = SOCK_STREAM;
301 hints.ai_flags = 0;
302 #ifdef HAVE_GAI_ADDRCONFIG
303 hints.ai_flags |= AI_ADDRCONFIG; /* Only ask for addresses that we have configured interfaces for. */
304 #endif
305 ret = getaddrinfo(host->p, port->p, &hints, &addrlist);
306 if(ret != 0)
307 {
308 error3("Resolving %s:%s: %s", host->p, port->p, gai_strerror(ret));
309 return -1;
310 }
311
312 addr = addrlist;
313 while(addr != NULL)
314 {
315 sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
316 if(sock >= 0)
317 {
318 if(timeout_connect(sock, addr->ai_addr, addr->ai_addrlen) == 0)
319 break;
320
321 close(sock);
322 sock=-1;
323 }
324 addr=addr->ai_next;
325 }
326 if(sock < 0) error2("Cannot resolve/connect to %s:%s!", host->p, port->p);
327
328 freeaddrinfo(addrlist);
329 #endif
330 return sock; /* Hopefully, that's an open socket to talk with. */
331 }
332 #endif /* !defined (WANT_WIN32_SOCKETS) */
333 #else /* NETWORK */
334
335 void resolver_dummy_without_sense()
336 {
337 /* Some compilers don't like empty source files. */
338 }
339
340 #endif