1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/*
* nvidia-installer: A tool for installing NVIDIA software packages on
* Unix and Linux systems.
*
* Copyright (C) 2003 NVIDIA Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>.
*
*
* snarf.c - this source file contains functions for downloading
* files. Based on snarf (http://www.xach.com/snarf/) by Zachary
* Beane <xach@xach.com>, released under the GPL.
*/
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include "nvidia-installer.h"
#include "snarf.h"
#include "snarf-internal.h"
#include "user-interface.h"
#include "misc.h"
static const char *get_url_service_type(const char *string, Url *u);
static const char *get_url_username (const char *string, Url *u);
static const char *get_url_password (const char *string, Url *u);
static const char *get_url_hostname (const char *url, Url *u);
static const char *get_url_port (const char *url, Url *u);
static const char *get_url_path (const char *url, Url *u);
static const char *get_url_file (const char *string, Url *u);
static UrlResource *url_resource_new(void);
static void url_resource_destroy(UrlResource *rsrc);
static const char *local_hstrerror(int n);
/*
* snarf() - main entry point
*/
int snarf(Options *op, const char *url, int out_fd, uint32 flags)
{
UrlResource *rsrc = NULL;
int ret;
if (op->no_network) {
ui_error(op, "Unable to access file '%s', because the '--no-network' "
"commandline option was specified.", url);
return FALSE;
}
rsrc = url_resource_new();
rsrc->url = url_new();
rsrc->op = op;
if (url_init(rsrc->url, url) == NULL) {
ui_error(op, "'%s' is not a valid URL.", url);
return FALSE;
}
rsrc->out_fd = out_fd;
rsrc->flags = flags;
ret = transfer(rsrc);
url_resource_destroy(rsrc);
return ret;
} /* snarf() */
Url *url_new(void)
{
Url *new_url;
new_url = nvalloc(sizeof(Url));
new_url->full_url = NULL;
new_url->service_type = 0;
new_url->username = NULL;
new_url->password = NULL;
new_url->host = NULL;
new_url->port = 0;
new_url->path = NULL;
new_url->file = NULL;
return new_url;
} /* url_new() */
Url *url_init(Url *u, const char *string)
{
const char *sp = string;
u->full_url = nvstrdup(string);
if (!(sp = get_url_service_type(sp, u))) return NULL;
/*
* only get username/password if they are not null,
* allows us to handle redirects properly
*/
if (!u->username)
sp = get_url_username(sp, u);
if (!u->password)
sp = get_url_password(sp, u);
sp = get_url_hostname(sp, u);
if (!(u->host && *(u->host))) return NULL;
sp = get_url_port(sp, u);
sp = get_url_path(sp, u);
sp = get_url_file(sp, u);
return u;
} /* url_init() */
void url_destroy(Url *u)
{
if (!u) return;
nvfree(u->full_url);
nvfree(u->username);
nvfree(u->password);
nvfree(u->host);
nvfree(u->path);
nvfree(u->file);
} /* url_destroy() */
char *get_proxy(const char *firstchoice)
{
char *proxy;
char *help;
if ((proxy = getenv(firstchoice))) return proxy;
help = nvstrdup(firstchoice);
nvstrtolower(help);
proxy = getenv(help);
nvfree(help);
if (proxy) return proxy;
if ((proxy = getenv("SNARF_PROXY"))) return proxy;
if ((proxy = getenv("PROXY"))) return proxy;
return NULL;
} /* get_proxy() */
int tcp_connect(Options *op, char *remote_host, int port)
{
struct hostent *host;
struct sockaddr_in sa;
int sock_fd;
if ((host = (struct hostent *)gethostbyname(remote_host)) == NULL) {
ui_error(op, "Unable to connect to %s (%s)",
remote_host, local_hstrerror(h_errno));
return FALSE;
}
/* get the socket */
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
ui_error(op, "Unable to create socket (%s)", strerror(errno));
return FALSE;
}
/* connect the socket, filling in the important stuff */
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
memcpy(&sa.sin_addr, host->h_addr,host->h_length);
if(connect(sock_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0){
ui_error(op, "Unable to connect to remote host %s (%s)",
remote_host, strerror(errno));
return FALSE;
}
return sock_fd;
} /* tcp_connect() */
int dump_data(UrlResource *rsrc, int sock)
{
int bytes_read = 0;
int total_bytes_read = 0;
ssize_t written = 0;
char buf[SNARF_BUFSIZE];
char *msg;
float percent;
msg = nvstrcat("Downloading: ", rsrc->url->full_url, NULL);
if (rsrc->flags & SNARF_FLAGS_STATUS_BAR) {
ui_status_begin(rsrc->op, msg, "Downloading");
}
while ((bytes_read = read(sock, buf, SNARF_BUFSIZE)) > 0) {
if (rsrc->flags & SNARF_FLAGS_STATUS_BAR) {
total_bytes_read += bytes_read;
percent = (float) total_bytes_read / (float) rsrc->outfile_size;
ui_status_update(rsrc->op, percent, NULL);
}
written = write(rsrc->out_fd, buf, bytes_read);
if (written == -1) {
ui_error(rsrc->op, "Error while writing output file (%s)",
strerror(errno));
close(sock);
return FALSE;
}
}
close(sock);
if (rsrc->flags & SNARF_FLAGS_STATUS_BAR) {
ui_status_end(rsrc->op, "done.");
}
free(msg);
return 1;
} /* dump_data() */
int transfer(UrlResource *rsrc)
{
int ret = FALSE;
switch (rsrc->url->service_type) {
case SERVICE_HTTP:
ret = http_transfer(rsrc);
break;
case SERVICE_FTP:
ret = ftp_transfer(rsrc);
break;
default:
ret = FALSE;
break;
}
return ret;
}
/*************************************************************************/
/* static functions */
static const char *get_url_service_type(const char *string, Url *u)
{
/*
* fixme: what if the string isn't at the beginning of the string?
*/
if (strstr(string, "http://")) {
string += 7;
u->service_type = SERVICE_HTTP;
return string;
}
if (strstr(string, "ftp://")) {
string += 6;
u->service_type = SERVICE_FTP;
return string;
}
if (strncasecmp(string, "www", 3) == 0) {
u->service_type = SERVICE_HTTP;
u->full_url = nvstrcat("http://", u->full_url, NULL);
return string;
}
if (strncasecmp(string, "ftp", 3) == 0) {
u->service_type = SERVICE_FTP;
u->full_url = nvstrcat("ftp://", u->full_url, NULL);
return string;
}
/* default to browser-style serviceless http URL */
u->full_url = nvstrcat("http://", u->full_url, NULL);
u->service_type = SERVICE_HTTP;
return string;
} /* get_url_service_type() */
static const char *get_url_username(const char *string, Url *u)
{
int i;
char *username;
char *at;
char *slash;
at = strchr(string, '@');
slash = strchr(string, '/');
if ((!at) || (slash && (at >= slash))) return string;
for (i = 0; string[i] && string[i] != ':' && string[i] != '@' &&
string[i] != '/'; i++);
if (string[i] != '@' && string[i] != ':') return string;
username = nvalloc(i);
memcpy(username, string, i + 1);
username[i] = '\0';
string += i + 1;
u->username = username;
return string;
} /* get_url_username() */
static const char *get_url_password(const char *string, Url *u)
{
int i;
char *password;
char *at;
char *slash;
at = strchr(string, '@');
slash = strchr(string, '/');
if ((!at) || (slash && (at >= slash))) return string;
/*
* skipping to the end of the host portion. this is kinda messy
* for the (rare) cases where someone has a slash and/or at in
* their password. It's not perfect; but it catches easy cases.
*
* If you know of a better way to do this, be my guest. I do not
* feel a particular paternal instinct towards my ugly code.
*
* I guess that applies to this whole program.
*/
for (i = 0 ; string[i] != '@'; i++);
password = nvalloc(i);
/* and finally, get the password portion */
memcpy(password, string, i);
password[i] = '\0';
string += i + 1;
u->password = password;
return string;
} /* get_url_password() */
static const char *get_url_hostname(const char *url, Url *u)
{
char *hostname;
int i;
/* skip to end, slash, or port colon */
for (i = 0; url[i] && url[i] != '/' && url[i] != ':'; i++);
hostname = nvalloc(i + 1);
memcpy(hostname, url, i);
hostname[i] = '\0';
/* if there's a port */
if(url[i] == ':')
url += i + 1;
else
url += i;
u->host = hostname;
return url;
} /* get_url_hostname() */
static const char *get_url_port(const char *url, Url *u)
{
char *port_string;
int i;
for (i = 0; isdigit(url[i]); i++);
if (i == 0) return url;
port_string = nvalloc(i + 1);
memcpy(port_string, url, i + 1);
port_string[i] = '\0';
url += i;
u->port = atoi(port_string);
return url;
} /* get_url_port() */
static const char *get_url_path(const char *url, Url *u)
{
int i;
char *path;
/* find where the last slash is */
for (i = strlen(url); i > 0 && url[i] != '/'; i--);
if (url[i] != '/') return url;
path = nvalloc(i + 2);
memcpy(path, url, i + 1);
path[i] = '/';
path[i + 1] = '\0';
url += i + 1;
u->path = path;
return url;
} /* get_url_path() */
static const char *get_url_file(const char *string, Url *u)
{
char *file;
if (!string[0]) return NULL;
file = nvalloc(strlen(string) + 1);
memcpy(file, string, strlen(string) + 1);
u->file = file;
return string;
} /* get_url_file() */
static UrlResource *url_resource_new(void)
{
UrlResource *new_resource;
new_resource = nvalloc(sizeof(UrlResource));
new_resource->url = NULL;
new_resource->out_fd = 0;
new_resource->proxy = NULL;
new_resource->proxy_username = NULL;
new_resource->proxy_password = NULL;
new_resource->op = NULL;
new_resource->outfile_size = 0;
new_resource->outfile_offset = 0;
return new_resource;
} /* url_resource_new() */
static void url_resource_destroy(UrlResource *rsrc)
{
if (!rsrc) return;
if(rsrc->url) url_destroy(rsrc->url);
free(rsrc);
} /* url_resource_destroy() */
static const char *local_hstrerror(int n)
{
switch (n) {
case HOST_NOT_FOUND: return "unknown host";
case NO_ADDRESS: return "no IP address associated with host";
case NO_RECOVERY: return "fatal DNS error";
case TRY_AGAIN: return "temporary DNS error (try again later)";
default: return "unknown error";
}
} /* local_hstrerror() */
|