Mercurial > gftp.yaz
annotate lib/protocols.c @ 67:aa971a4bb16f
2002-11-27 Brian Masney <masneyb@gftp.org>
* Officially release 2.0.14rc1
* lib/cache.c - take out a warning message
* lib/misc.c lib/protocols.c lib/gftp.h lib/gtk/dnd.c lib/transfer.c -
add second argument (free_request) to gftp_request_destroy
* lib/protocols.c (gftp_parse_url) - make sure the request structure
is cleared before we start to modify it
* src/gtk/gftp-text.c - fixed crash if you didn't enter a username
| author | masneyb |
|---|---|
| date | Wed, 27 Nov 2002 14:29:57 +0000 |
| parents | cd3e457cbc85 |
| children | e2b30d0c97a4 |
| rev | line source |
|---|---|
| 1 | 1 /*****************************************************************************/ |
| 2 /* protocols.c - Skeleton functions for the protocols gftp supports */ | |
| 3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */ | |
| 4 /* */ | |
| 5 /* This program is free software; you can redistribute it and/or modify */ | |
| 6 /* it under the terms of the GNU General Public License as published by */ | |
| 7 /* the Free Software Foundation; either version 2 of the License, or */ | |
| 8 /* (at your option) any later version. */ | |
| 9 /* */ | |
| 10 /* This program is distributed in the hope that it will be useful, */ | |
| 11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
| 12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
| 13 /* GNU General Public License for more details. */ | |
| 14 /* */ | |
| 15 /* You should have received a copy of the GNU General Public License */ | |
| 16 /* along with this program; if not, write to the Free Software */ | |
| 17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
| 18 /*****************************************************************************/ | |
| 19 | |
| 20 #include "gftp.h" | |
| 33 | 21 static const char cvsid[] = "$Id$"; |
| 1 | 22 |
| 23 gftp_request * | |
| 24 gftp_request_new (void) | |
| 25 { | |
| 26 gftp_request *request; | |
| 27 | |
| 28 request = g_malloc0 (sizeof (*request)); | |
| 58 | 29 request->sockfd = -1; |
| 66 | 30 request->datafd = -1; |
| 31 request->cachefd = -1; | |
| 1 | 32 request->data_type = GFTP_TYPE_BINARY; |
| 33 return (request); | |
| 34 } | |
| 35 | |
| 36 | |
| 37 void | |
| 67 | 38 gftp_request_destroy (gftp_request * request, int free_request) |
| 1 | 39 { |
| 40 g_return_if_fail (request != NULL); | |
| 41 | |
| 42 gftp_disconnect (request); | |
| 43 | |
| 44 if (request->destroy != NULL) | |
| 45 request->destroy (request); | |
| 46 | |
| 47 if (request->hostname) | |
| 48 g_free (request->hostname); | |
| 49 if (request->username) | |
| 50 g_free (request->username); | |
| 51 if (request->password) | |
| 52 { | |
| 53 memset (request->password, 0, strlen (request->password)); | |
| 54 g_free (request->password); | |
| 55 } | |
| 56 if (request->account) | |
| 57 { | |
| 58 memset (request->account, 0, strlen (request->account)); | |
| 59 g_free (request->account); | |
| 60 } | |
| 61 if (request->directory) | |
| 62 g_free (request->directory); | |
| 63 if (request->proxy_config) | |
| 64 g_free (request->proxy_config); | |
| 65 if (request->proxy_hostname) | |
| 66 g_free (request->proxy_hostname); | |
| 67 if (request->proxy_username) | |
| 68 g_free (request->proxy_username); | |
| 69 if (request->proxy_password) | |
| 70 g_free (request->proxy_password); | |
| 71 if (request->proxy_account) | |
| 72 g_free (request->proxy_account); | |
| 73 if (request->last_ftp_response) | |
| 74 g_free (request->last_ftp_response); | |
| 75 if (request->protocol_data) | |
| 76 g_free (request->protocol_data); | |
| 77 if (request->sftpserv_path) | |
| 78 g_free (request->sftpserv_path); | |
| 67 | 79 |
| 1 | 80 memset (request, 0, sizeof (*request)); |
| 67 | 81 |
| 82 if (free_request) | |
| 83 g_free (request); | |
| 84 else | |
| 85 { | |
| 86 request->sockfd = -1; | |
| 87 request->datafd = -1; | |
| 88 request->cachefd = -1; | |
| 89 request->data_type = GFTP_TYPE_BINARY; | |
| 90 } | |
| 1 | 91 } |
| 92 | |
| 93 | |
| 94 void | |
| 95 gftp_file_destroy (gftp_file * file) | |
| 96 { | |
| 97 g_return_if_fail (file != NULL); | |
| 98 | |
| 99 if (file->file) | |
| 100 g_free (file->file); | |
| 101 if (file->user) | |
| 102 g_free (file->user); | |
| 103 if (file->group) | |
| 104 g_free (file->group); | |
| 105 if (file->attribs) | |
| 106 g_free (file->attribs); | |
| 107 if (file->destfile) | |
| 108 g_free (file->destfile); | |
| 109 memset (file, 0, sizeof (*file)); | |
| 110 } | |
| 111 | |
| 112 | |
| 113 int | |
| 114 gftp_connect (gftp_request * request) | |
| 115 { | |
| 116 g_return_val_if_fail (request != NULL, -2); | |
| 117 | |
| 118 if (request->connect == NULL) | |
| 119 return (-2); | |
| 120 | |
| 121 gftp_set_config_options (request); | |
| 122 | |
| 123 return (request->connect (request)); | |
| 124 } | |
| 125 | |
| 126 | |
| 127 void | |
| 128 gftp_disconnect (gftp_request * request) | |
| 129 { | |
| 130 g_return_if_fail (request != NULL); | |
| 131 | |
| 132 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 133 if (request->hostp) | |
| 134 freeaddrinfo (request->hostp); | |
| 135 #endif | |
| 136 request->hostp = NULL; | |
| 137 | |
| 138 if (request->sftpserv_path != NULL) | |
| 139 { | |
| 140 g_free (request->sftpserv_path); | |
| 141 request->sftpserv_path = NULL; | |
| 142 } | |
| 143 | |
| 144 request->cached = 0; | |
| 145 if (request->disconnect == NULL) | |
| 146 return; | |
| 147 request->disconnect (request); | |
| 148 } | |
| 149 | |
| 150 | |
| 58 | 151 off_t |
| 152 gftp_get_file (gftp_request * request, const char *filename, int fd, | |
| 1 | 153 size_t startsize) |
| 154 { | |
| 155 g_return_val_if_fail (request != NULL, -2); | |
| 156 | |
| 157 request->cached = 0; | |
| 158 if (request->get_file == NULL) | |
| 159 return (-2); | |
| 160 return (request->get_file (request, filename, fd, startsize)); | |
| 161 } | |
| 162 | |
| 163 | |
| 164 int | |
| 58 | 165 gftp_put_file (gftp_request * request, const char *filename, int fd, |
| 1 | 166 size_t startsize, size_t totalsize) |
| 167 { | |
| 168 g_return_val_if_fail (request != NULL, -2); | |
| 169 | |
| 170 request->cached = 0; | |
| 171 if (request->put_file == NULL) | |
| 172 return (-2); | |
| 173 return (request->put_file (request, filename, fd, startsize, totalsize)); | |
| 174 } | |
| 175 | |
| 176 | |
| 177 long | |
| 178 gftp_transfer_file (gftp_request * fromreq, const char *fromfile, | |
| 58 | 179 int fromfd, size_t fromsize, |
| 1 | 180 gftp_request * toreq, const char *tofile, |
| 58 | 181 int tofd, size_t tosize) |
| 1 | 182 { |
| 183 long size; | |
| 184 | |
| 185 g_return_val_if_fail (fromreq != NULL, -2); | |
| 186 g_return_val_if_fail (fromfile != NULL, -2); | |
| 187 g_return_val_if_fail (toreq != NULL, -2); | |
| 188 g_return_val_if_fail (tofile != NULL, -2); | |
| 189 | |
| 190 if (strcmp (fromreq->protocol_name, toreq->protocol_name) == 0) | |
| 191 { | |
| 192 if (fromreq->transfer_file == NULL) | |
| 193 return (-2); | |
| 194 return (fromreq->transfer_file (fromreq, fromfile, fromsize, toreq, | |
| 195 tofile, tosize)); | |
| 196 } | |
| 197 | |
| 198 fromreq->cached = 0; | |
| 199 toreq->cached = 0; | |
| 200 if ((size = gftp_get_file (fromreq, fromfile, fromfd, tosize)) < 0) | |
| 201 return (-2); | |
| 202 | |
| 203 if (gftp_put_file (toreq, tofile, tofd, tosize, size) != 0) | |
| 204 { | |
| 40 | 205 if (gftp_abort_transfer (fromreq) != 0) |
| 206 gftp_end_transfer (fromreq); | |
| 207 | |
| 1 | 208 return (-2); |
| 209 } | |
| 210 | |
| 211 return (size); | |
| 212 } | |
| 213 | |
| 214 | |
| 58 | 215 ssize_t |
| 1 | 216 gftp_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
| 217 { | |
| 218 g_return_val_if_fail (request != NULL, -2); | |
| 219 g_return_val_if_fail (buf != NULL, -2); | |
| 220 | |
| 221 if (request->get_next_file_chunk != NULL) | |
| 222 return (request->get_next_file_chunk (request, buf, size)); | |
| 223 | |
| 58 | 224 return (gftp_read (request, buf, size, request->datafd)); |
| 1 | 225 } |
| 226 | |
| 227 | |
| 58 | 228 ssize_t |
| 1 | 229 gftp_put_next_file_chunk (gftp_request * request, char *buf, size_t size) |
| 230 { | |
| 231 g_return_val_if_fail (request != NULL, -2); | |
| 58 | 232 g_return_val_if_fail (buf != NULL, -2); |
| 1 | 233 |
| 234 if (request->put_next_file_chunk != NULL) | |
| 235 return (request->put_next_file_chunk (request, buf, size)); | |
| 236 | |
| 237 if (size == 0) | |
| 238 return (0); | |
| 239 | |
| 58 | 240 return (gftp_write (request, buf, size, request->datafd)); |
| 1 | 241 } |
| 242 | |
| 243 | |
| 244 int | |
| 245 gftp_end_transfer (gftp_request * request) | |
| 246 { | |
| 247 int ret; | |
| 248 | |
| 249 g_return_val_if_fail (request != NULL, -2); | |
| 250 | |
| 40 | 251 if (!request->cached && |
| 252 request->end_transfer != NULL) | |
| 253 ret = request->end_transfer (request); | |
| 254 else | |
| 255 ret = 0; | |
| 1 | 256 |
| 58 | 257 if (request->cachefd > 0) |
| 1 | 258 { |
| 58 | 259 close (request->cachefd); |
| 260 request->cachefd = -1; | |
| 1 | 261 } |
| 262 | |
| 263 if (request->last_dir_entry) | |
| 264 { | |
| 265 g_free (request->last_dir_entry); | |
| 266 request->last_dir_entry = NULL; | |
| 267 request->last_dir_entry_len = 0; | |
| 268 } | |
| 269 | |
| 270 return (ret); | |
| 271 } | |
| 272 | |
| 273 | |
| 274 int | |
| 40 | 275 gftp_abort_transfer (gftp_request * request) |
| 276 { | |
| 277 g_return_val_if_fail (request != NULL, -2); | |
| 278 | |
| 279 if (request->abort_transfer == NULL) | |
| 280 return (-2); | |
| 281 | |
| 282 return (request->abort_transfer (request)); | |
| 283 } | |
| 284 | |
| 285 | |
| 286 int | |
| 1 | 287 gftp_list_files (gftp_request * request) |
| 288 { | |
| 58 | 289 int fd; |
| 1 | 290 |
| 291 g_return_val_if_fail (request != NULL, -2); | |
| 292 | |
| 293 request->cached = 0; | |
| 58 | 294 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
| 1 | 295 { |
| 296 request->logging_function (gftp_logging_misc, request->user_data, | |
| 297 _("Loading directory listing %s from cache\n"), | |
| 298 request->directory); | |
| 299 | |
| 300 request->cachefd = fd; | |
| 301 request->cached = 1; | |
| 302 return (0); | |
| 303 } | |
| 304 else if (request->use_cache) | |
| 305 { | |
| 306 request->cachefd = gftp_new_cache_entry (request); | |
| 307 request->cached = 0; | |
| 308 } | |
| 309 | |
| 310 if (request->list_files == NULL) | |
| 311 return (-2); | |
| 312 return (request->list_files (request)); | |
| 313 } | |
| 314 | |
| 315 | |
| 316 int | |
| 317 gftp_get_next_file (gftp_request * request, char *filespec, gftp_file * fle) | |
| 318 { | |
| 58 | 319 int fd, ret; |
| 45 | 320 #if GLIB_MAJOR_VERSION > 1 |
| 321 gsize bread, bwrite; | |
| 322 char *tempstr; | |
| 46 | 323 GError * error; |
| 45 | 324 #endif |
| 1 | 325 |
| 326 g_return_val_if_fail (request != NULL, -2); | |
| 327 | |
| 328 if (request->get_next_file == NULL) | |
| 329 return (-2); | |
| 330 | |
| 58 | 331 if (request->cached && request->cachefd > 0) |
| 1 | 332 fd = request->cachefd; |
| 333 else | |
| 334 fd = request->datafd; | |
| 335 | |
| 336 memset (fle, 0, sizeof (*fle)); | |
| 337 do | |
| 338 { | |
| 339 gftp_file_destroy (fle); | |
| 340 ret = request->get_next_file (request, fle, fd); | |
| 341 | |
| 45 | 342 #if GLIB_MAJOR_VERSION > 1 |
| 343 if (fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL)) | |
| 344 { | |
| 46 | 345 error = NULL; |
| 45 | 346 if ((tempstr = g_locale_to_utf8 (fle->file, -1, &bread, |
| 46 | 347 &bwrite, &error)) != NULL) |
| 45 | 348 { |
| 349 g_free (fle->file); | |
| 350 fle->file = tempstr; | |
| 351 } | |
| 46 | 352 else |
| 353 g_warning ("Error when converting %s to UTF-8: %s\n", fle->file, | |
| 354 error->message); | |
| 45 | 355 } |
| 356 #endif | |
| 357 | |
| 60 | 358 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
| 1 | 359 request->last_dir_entry != NULL) |
| 360 { | |
| 60 | 361 if (gftp_write (request, request->last_dir_entry, |
| 362 request->last_dir_entry_len, request->cachefd) < 0) | |
| 1 | 363 { |
| 364 request->logging_function (gftp_logging_error, request->user_data, | |
| 365 _("Error: Cannot write to cache: %s\n"), | |
| 366 g_strerror (errno)); | |
| 60 | 367 close (request->cachefd); |
| 368 request->cachefd = -1; | |
| 1 | 369 } |
| 370 } | |
| 371 } while (ret > 0 && !gftp_match_filespec (fle->file, filespec)); | |
| 372 | |
| 373 return (ret); | |
| 374 } | |
| 375 | |
| 376 | |
| 377 int | |
| 378 gftp_parse_url (gftp_request * request, const char *url) | |
| 379 { | |
| 380 char *pos, *endpos, *endhostpos, *str, tempchar; | |
| 67 | 381 gftp_logging_func logging_function; |
| 1 | 382 const char *stpos; |
| 383 int len, i; | |
| 384 | |
| 385 g_return_val_if_fail (request != NULL, -2); | |
| 386 g_return_val_if_fail (url != NULL, -2); | |
| 387 | |
| 67 | 388 logging_function = request->logging_function; |
| 389 gftp_request_destroy (request, 0); | |
| 390 request->logging_function = logging_function; | |
| 391 | |
| 1 | 392 for (stpos = url; *stpos == ' '; stpos++); |
| 393 | |
| 394 if ((pos = strstr (stpos, "://")) != NULL) | |
| 395 { | |
| 396 *pos = '\0'; | |
| 397 | |
| 398 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
| 399 { | |
| 400 if (strcmp (gftp_protocols[i].url_prefix, stpos) == 0) | |
| 401 break; | |
| 402 } | |
| 403 | |
| 404 if (gftp_protocols[i].url_prefix == NULL) | |
| 405 return (-2); | |
| 406 *pos = ':'; | |
| 407 } | |
| 408 else | |
| 409 { | |
| 410 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
| 411 { | |
| 412 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
| 413 break; | |
| 414 } | |
| 415 | |
| 416 if (gftp_protocols[i].url_prefix == NULL) | |
| 417 i = GFTP_FTP_NUM; | |
| 418 } | |
| 419 | |
| 420 gftp_protocols[i].init (request); | |
| 421 | |
| 422 if (request->parse_url != NULL) | |
| 423 return (request->parse_url (request, url)); | |
| 424 | |
| 425 if (i == GFTP_LOCAL_NUM) | |
| 426 { | |
| 427 request->directory = g_malloc (strlen (stpos + 6) + 1); | |
| 65 | 428 strcpy (request->directory, stpos + 7); |
| 1 | 429 return (0); |
| 430 } | |
| 431 | |
| 432 for (; *stpos == ' '; stpos++); | |
| 433 str = g_malloc (strlen (stpos) + 1); | |
| 434 strcpy (str, stpos); | |
| 435 for (pos = str + strlen (str) - 1; *pos == ' '; pos--) | |
| 436 *pos = '\0'; | |
| 437 | |
| 438 pos = str; | |
| 439 len = strlen (request->url_prefix); | |
| 440 if (strncmp (pos, request->url_prefix, len) == 0 | |
| 441 && strncmp (pos + len, "://", 3) == 0) | |
| 442 pos += len + 3; | |
| 443 | |
| 444 if ((endhostpos = strchr (pos, '@')) != NULL) | |
| 445 { | |
| 446 /* A user/password was entered */ | |
| 447 if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos) | |
| 448 { | |
| 449 /* No password was entered */ | |
| 450 gftp_set_password (request, ""); | |
| 451 endpos = endhostpos; | |
| 452 } | |
| 453 | |
| 454 *endpos = '\0'; | |
| 455 gftp_set_username (request, pos); | |
| 456 | |
| 457 pos = endpos + 1; | |
| 458 if ((endpos = strchr (pos, '@')) != NULL) | |
| 459 { | |
| 460 if (strchr (endpos + 1, '@') != NULL) | |
| 461 endpos = strchr (endpos + 1, '@'); | |
| 462 *endpos = '\0'; | |
| 463 gftp_set_password (request, pos); | |
| 464 | |
| 465 pos = endpos + 1; | |
| 466 } | |
| 467 } | |
| 468 | |
| 469 /* Now get the hostname and an optional port and optional directory */ | |
| 470 endhostpos = pos + strlen (pos); | |
| 471 if ((endpos = strchr (pos, ':')) != NULL) | |
| 472 endhostpos = endpos; | |
| 473 else if ((endpos = strchr (pos, '/')) != NULL) | |
| 474 endhostpos = endpos; | |
| 475 tempchar = *endhostpos; | |
| 476 *endhostpos = '\0'; | |
| 477 gftp_set_hostname (request, pos); | |
| 478 *endhostpos = tempchar; | |
| 479 | |
| 480 if ((endpos = strchr (pos, ':')) != NULL) | |
| 481 { | |
| 482 /* A port was entered */ | |
| 483 pos = endpos + 1; | |
| 484 gftp_set_port (request, strtol (pos, NULL, 10)); | |
| 485 } | |
| 486 if ((endpos = strchr (pos, '/')) != NULL) | |
| 487 gftp_set_directory (request, endpos); | |
| 488 g_free (str); | |
| 489 return (0); | |
| 490 } | |
| 491 | |
| 492 | |
| 493 int | |
| 494 gftp_set_data_type (gftp_request * request, int data_type) | |
| 495 { | |
| 496 g_return_val_if_fail (request != NULL, -2); | |
| 497 | |
| 498 if (request->set_data_type == NULL) | |
| 499 return (-2); | |
| 500 return (request->set_data_type (request, data_type)); | |
| 501 } | |
| 502 | |
| 503 | |
| 504 void | |
| 505 gftp_set_hostname (gftp_request * request, const char *hostname) | |
| 506 { | |
| 507 g_return_if_fail (request != NULL); | |
| 508 g_return_if_fail (hostname != NULL); | |
| 509 | |
| 510 if (request->hostname) | |
| 511 g_free (request->hostname); | |
| 512 request->hostname = g_malloc (strlen (hostname) + 1); | |
| 513 strcpy (request->hostname, hostname); | |
| 514 } | |
| 515 | |
| 516 | |
| 517 void | |
| 518 gftp_set_username (gftp_request * request, const char *username) | |
| 519 { | |
| 520 g_return_if_fail (request != NULL); | |
| 521 g_return_if_fail (username != NULL); | |
| 522 | |
| 523 if (request->username) | |
| 524 g_free (request->username); | |
| 525 request->username = g_malloc (strlen (username) + 1); | |
| 526 strcpy (request->username, username); | |
| 527 } | |
| 528 | |
| 529 | |
| 530 void | |
| 531 gftp_set_password (gftp_request * request, const char *password) | |
| 532 { | |
| 533 g_return_if_fail (request != NULL); | |
| 534 g_return_if_fail (password != NULL); | |
| 535 | |
| 536 if (request->password) | |
| 537 g_free (request->password); | |
| 538 request->password = g_malloc (strlen (password) + 1); | |
| 539 strcpy (request->password, password); | |
| 540 } | |
| 541 | |
| 542 | |
| 543 void | |
| 544 gftp_set_account (gftp_request * request, const char *account) | |
| 545 { | |
| 546 g_return_if_fail (request != NULL); | |
| 547 g_return_if_fail (account != NULL); | |
| 548 | |
| 549 if (request->account) | |
| 550 g_free (request->account); | |
| 551 request->account = g_malloc (strlen (account) + 1); | |
| 552 strcpy (request->account, account); | |
| 553 } | |
| 554 | |
| 555 | |
| 556 int | |
| 557 gftp_set_directory (gftp_request * request, const char *directory) | |
| 558 { | |
| 559 g_return_val_if_fail (request != NULL, -2); | |
| 560 g_return_val_if_fail (directory != NULL, -2); | |
| 561 | |
| 562 | |
| 58 | 563 if (request->sockfd <= 0 && !request->always_connected) |
| 1 | 564 { |
| 565 if (directory != request->directory) | |
| 566 { | |
| 567 if (request->directory) | |
| 568 g_free (request->directory); | |
| 569 request->directory = g_malloc (strlen (directory) + 1); | |
| 570 strcpy (request->directory, directory); | |
| 571 } | |
| 572 return (0); | |
| 573 } | |
| 574 else if (request->chdir == NULL) | |
| 575 return (-2); | |
| 576 return (request->chdir (request, directory)); | |
| 577 } | |
| 578 | |
| 579 | |
| 580 void | |
| 581 gftp_set_port (gftp_request * request, unsigned int port) | |
| 582 { | |
| 583 g_return_if_fail (request != NULL); | |
| 584 | |
| 585 request->port = port; | |
| 586 } | |
| 587 | |
| 588 | |
| 589 void | |
| 590 gftp_set_proxy_hostname (gftp_request * request, const char *hostname) | |
| 591 { | |
| 592 g_return_if_fail (request != NULL); | |
| 593 g_return_if_fail (hostname != NULL); | |
| 594 | |
| 595 if (request->proxy_hostname) | |
| 596 g_free (request->proxy_hostname); | |
| 597 request->proxy_hostname = g_malloc (strlen (hostname) + 1); | |
| 598 strcpy (request->proxy_hostname, hostname); | |
| 599 } | |
| 600 | |
| 601 | |
| 602 void | |
| 603 gftp_set_proxy_username (gftp_request * request, const char *username) | |
| 604 { | |
| 605 g_return_if_fail (request != NULL); | |
| 606 g_return_if_fail (username != NULL); | |
| 607 | |
| 608 if (request->proxy_username) | |
| 609 g_free (request->proxy_username); | |
| 610 request->proxy_username = g_malloc (strlen (username) + 1); | |
| 611 strcpy (request->proxy_username, username); | |
| 612 } | |
| 613 | |
| 614 | |
| 615 void | |
| 616 gftp_set_proxy_password (gftp_request * request, const char *password) | |
| 617 { | |
| 618 g_return_if_fail (request != NULL); | |
| 619 g_return_if_fail (password != NULL); | |
| 620 | |
| 621 if (request->proxy_password) | |
| 622 g_free (request->proxy_password); | |
| 623 request->proxy_password = g_malloc (strlen (password) + 1); | |
| 624 strcpy (request->proxy_password, password); | |
| 625 } | |
| 626 | |
| 627 | |
| 628 void | |
| 629 gftp_set_proxy_account (gftp_request * request, const char *account) | |
| 630 { | |
| 631 g_return_if_fail (request != NULL); | |
| 632 g_return_if_fail (account != NULL); | |
| 633 | |
| 634 if (request->proxy_account) | |
| 635 g_free (request->proxy_account); | |
| 636 request->proxy_account = g_malloc (strlen (account) + 1); | |
| 637 strcpy (request->proxy_account, account); | |
| 638 } | |
| 639 | |
| 640 | |
| 641 void | |
| 642 gftp_set_proxy_port (gftp_request * request, unsigned int port) | |
| 643 { | |
| 644 g_return_if_fail (request != NULL); | |
| 645 | |
| 646 request->proxy_port = port; | |
| 647 } | |
| 648 | |
| 649 | |
| 650 int | |
| 651 gftp_remove_directory (gftp_request * request, const char *directory) | |
| 652 { | |
| 653 g_return_val_if_fail (request != NULL, -2); | |
| 654 | |
| 655 if (request->rmdir == NULL) | |
| 656 return (-2); | |
| 657 return (request->rmdir (request, directory)); | |
| 658 } | |
| 659 | |
| 660 | |
| 661 int | |
| 662 gftp_remove_file (gftp_request * request, const char *file) | |
| 663 { | |
| 664 g_return_val_if_fail (request != NULL, -2); | |
| 665 | |
| 666 if (request->rmfile == NULL) | |
| 667 return (-2); | |
| 668 return (request->rmfile (request, file)); | |
| 669 } | |
| 670 | |
| 671 | |
| 672 int | |
| 673 gftp_make_directory (gftp_request * request, const char *directory) | |
| 674 { | |
| 675 g_return_val_if_fail (request != NULL, -2); | |
| 676 | |
| 677 if (request->mkdir == NULL) | |
| 678 return (-2); | |
| 679 return (request->mkdir (request, directory)); | |
| 680 } | |
| 681 | |
| 682 | |
| 683 int | |
| 684 gftp_rename_file (gftp_request * request, const char *oldname, | |
| 685 const char *newname) | |
| 686 { | |
| 687 g_return_val_if_fail (request != NULL, -2); | |
| 688 | |
| 689 if (request->rename == NULL) | |
| 690 return (-2); | |
| 691 return (request->rename (request, oldname, newname)); | |
| 692 } | |
| 693 | |
| 694 | |
| 695 int | |
| 696 gftp_chmod (gftp_request * request, const char *file, int mode) | |
| 697 { | |
| 698 g_return_val_if_fail (request != NULL, -2); | |
| 699 | |
| 700 if (request->chmod == NULL) | |
| 701 return (-2); | |
| 702 return (request->chmod (request, file, mode)); | |
| 703 } | |
| 704 | |
| 705 | |
| 706 int | |
| 707 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
| 708 { | |
| 709 g_return_val_if_fail (request != NULL, -2); | |
| 710 | |
| 711 if (request->set_file_time == NULL) | |
| 712 return (-2); | |
| 713 return (request->set_file_time (request, file, datetime)); | |
| 714 } | |
| 715 | |
| 716 | |
| 717 char | |
| 718 gftp_site_cmd (gftp_request * request, const char *command) | |
| 719 { | |
| 720 g_return_val_if_fail (request != NULL, -2); | |
| 721 | |
| 722 if (request->site == NULL) | |
| 723 return (-2); | |
| 724 return (request->site (request, command)); | |
| 725 } | |
| 726 | |
| 727 | |
| 728 void | |
| 729 gftp_set_proxy_config (gftp_request * request, const char *proxy_config) | |
| 730 { | |
| 731 int len; | |
| 732 | |
| 733 g_return_if_fail (request != NULL); | |
| 734 | |
| 735 if (request->proxy_config != NULL) | |
| 736 g_free (request->proxy_config); | |
| 737 | |
| 66 | 738 if (proxy_config == NULL) |
| 739 { | |
| 740 request->proxy_config = NULL; | |
| 741 return; | |
| 742 } | |
| 743 | |
| 1 | 744 len = strlen (proxy_config); |
| 745 | |
| 746 if (len > 0 && (proxy_config[len - 1] != 'n' || | |
| 747 proxy_config[len - 2] != '%')) | |
| 748 len += 2; | |
| 749 | |
| 750 request->proxy_config = g_malloc (len + 1); | |
| 751 strcpy (request->proxy_config, proxy_config); | |
| 752 if (len != strlen (proxy_config)) | |
| 753 { | |
| 754 request->proxy_config[len - 2] = '%'; | |
| 755 request->proxy_config[len - 1] = 'n'; | |
| 756 request->proxy_config[len] = '\0'; | |
| 757 } | |
| 758 } | |
| 759 | |
| 760 | |
| 58 | 761 off_t |
| 1 | 762 gftp_get_file_size (gftp_request * request, const char *filename) |
| 763 { | |
| 764 g_return_val_if_fail (request != NULL, 0); | |
| 765 | |
| 766 if (request->get_file_size == NULL) | |
| 767 return (0); | |
| 768 return (request->get_file_size (request, filename)); | |
| 769 } | |
| 770 | |
| 771 | |
| 772 int | |
| 773 gftp_need_proxy (gftp_request * request, char *service) | |
| 774 { | |
| 775 gftp_proxy_hosts * hostname; | |
| 776 unsigned char addy[4]; | |
| 777 struct sockaddr *addr; | |
| 778 GList * templist; | |
| 779 gint32 netaddr; | |
| 780 char *pos; | |
| 781 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 782 struct addrinfo hints; | |
| 783 int port, errnum; | |
| 784 char serv[8]; | |
| 785 | |
| 786 request->hostp = NULL; | |
| 787 if (proxy_hosts == NULL) | |
| 788 return (request->proxy_hostname != NULL | |
| 789 && *request->proxy_hostname != '\0' | |
| 790 && request->proxy_config != NULL | |
| 791 && *request->proxy_config != '\0'); | |
| 792 | |
| 793 memset (&hints, 0, sizeof (hints)); | |
| 794 hints.ai_flags = AI_CANONNAME; | |
| 795 hints.ai_family = AF_INET; | |
| 796 hints.ai_socktype = SOCK_STREAM; | |
| 797 | |
| 798 port = request->use_proxy ? request->proxy_port : request->port; | |
| 799 if (port == 0) | |
| 800 strcpy (serv, service); | |
| 801 else | |
| 802 snprintf (serv, sizeof (serv), "%d", port); | |
| 803 | |
| 804 request->logging_function (gftp_logging_misc, request->user_data, | |
| 805 _("Looking up %s\n"), request->hostname); | |
| 806 | |
| 807 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
| 808 &request->hostp)) != 0) | |
| 809 { | |
| 810 request->logging_function (gftp_logging_error, request->user_data, | |
| 811 _("Cannot look up hostname %s: %s\n"), | |
| 812 request->hostname, gai_strerror (errnum)); | |
| 813 return (-1); | |
| 814 } | |
| 815 | |
| 816 addr = request->hostp->ai_addr; | |
| 817 | |
| 818 #else /* !HAVE_GETADDRINFO */ | |
| 819 | |
| 820 request->hostp = NULL; | |
| 821 if (proxy_hosts == NULL) | |
| 822 return (request->proxy_hostname != NULL | |
| 823 && *request->proxy_hostname != '\0' | |
| 824 && request->proxy_config != NULL | |
| 825 && *request->proxy_config != '\0'); | |
| 826 | |
| 827 request->logging_function (gftp_logging_misc, request->user_data, | |
| 828 _("Looking up %s\n"), request->hostname); | |
| 829 | |
| 830 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
| 831 NULL))) | |
| 832 { | |
| 833 request->logging_function (gftp_logging_error, request->user_data, | |
| 834 _("Cannot look up hostname %s: %s\n"), | |
| 835 request->hostname, g_strerror (errno)); | |
| 836 return (-1); | |
| 837 } | |
| 838 | |
| 839 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
| 840 | |
| 841 #endif /* HAVE_GETADDRINFO */ | |
| 842 | |
| 843 templist = proxy_hosts; | |
| 844 while (templist != NULL) | |
| 845 { | |
| 846 hostname = templist->data; | |
| 847 if (hostname->domain && | |
| 848 strlen (request->hostname) > strlen (hostname->domain)) | |
| 849 { | |
| 850 pos = request->hostname + strlen (request->hostname) - | |
| 851 strlen (hostname->domain); | |
| 852 if (strcmp (hostname->domain, pos) == 0) | |
| 853 return (0); | |
| 854 } | |
| 855 | |
| 856 if (hostname->ipv4_network_address != 0) | |
| 857 { | |
| 858 memcpy (addy, addr, sizeof (addy)); | |
| 859 netaddr = | |
| 860 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
| 861 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
| 862 hostname->ipv4_netmask; | |
| 863 if (netaddr == hostname->ipv4_network_address) | |
| 864 return (0); | |
| 865 } | |
| 866 templist = templist->next; | |
| 867 } | |
| 868 return (request->proxy_hostname != NULL && *request->proxy_hostname != '\0' | |
| 869 && request->proxy_config != NULL && *request->proxy_config != '\0'); | |
| 870 } | |
| 871 | |
| 872 | |
| 873 char * | |
| 874 gftp_convert_ascii (char *buf, ssize_t *len, int direction) | |
| 875 { | |
| 876 ssize_t i, j, newsize; | |
| 877 char *tempstr; | |
| 878 | |
| 879 if (direction == GFTP_DIRECTION_DOWNLOAD) | |
| 880 { | |
| 881 for (i = 0, j = 0; i < *len; i++) | |
| 882 { | |
| 883 if (buf[i] != '\r') | |
| 884 buf[j++] = buf[i]; | |
| 885 else | |
| 886 --*len; | |
| 887 } | |
| 888 tempstr = buf; | |
| 889 } | |
| 890 else | |
| 891 { | |
| 892 newsize = 0; | |
| 893 for (i = 0; i < *len; i++) | |
| 894 { | |
| 895 newsize++; | |
| 896 if (i > 0 && buf[i] == '\n' && buf[i - 1] != '\r') | |
| 897 newsize++; | |
| 898 } | |
| 899 tempstr = g_malloc (newsize); | |
| 900 | |
| 901 for (i = 0, j = 0; i < *len; i++) | |
| 902 { | |
| 903 if (i > 0 && buf[i] == '\n' && buf[i - 1] != '\r') | |
| 904 tempstr[j++] = '\r'; | |
| 905 tempstr[j++] = buf[i]; | |
| 906 } | |
| 907 *len = newsize; | |
| 908 } | |
| 909 return (tempstr); | |
| 910 } | |
| 911 | |
| 912 | |
| 48 | 913 static char * |
| 914 copy_token (char **dest, char *source) | |
| 1 | 915 { |
| 48 | 916 /* This function is used internally by gftp_parse_ls () */ |
| 917 char *endpos, savepos; | |
| 1 | 918 |
| 48 | 919 endpos = source; |
| 920 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
| 921 endpos++; | |
| 922 if (*endpos == '\0') | |
| 923 return (NULL); | |
| 1 | 924 |
| 48 | 925 savepos = *endpos; |
| 926 *endpos = '\0'; | |
| 927 *dest = g_malloc (endpos - source + 1); | |
| 928 strcpy (*dest, source); | |
| 929 *endpos = savepos; | |
| 1 | 930 |
| 48 | 931 /* Skip the blanks till we get to the next entry */ |
| 932 source = endpos + 1; | |
| 933 while ((*source == ' ' || *source == '\t') && *source != '\0') | |
| 934 source++; | |
| 935 return (source); | |
| 936 } | |
| 1 | 937 |
| 938 | |
| 48 | 939 static char * |
| 940 goto_next_token (char *pos) | |
| 941 { | |
| 942 while (*pos != ' ' && *pos != '\t' && *pos != '\0') | |
| 943 pos++; | |
| 1 | 944 |
| 48 | 945 if (pos == '\0') |
| 946 return (pos); | |
| 1 | 947 |
| 48 | 948 while ((*pos == ' ' || *pos == '\t') && *pos != '\0') |
| 949 pos++; | |
| 950 | |
| 951 return (pos); | |
| 1 | 952 } |
| 953 | |
| 954 | |
| 955 static time_t | |
| 956 parse_time (char **str) | |
| 957 { | |
| 958 const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
959 "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 1 | 960 char *startpos, *endpos, *datepos; |
| 961 struct tm curtime, tt; | |
| 962 time_t t; | |
| 963 int i; | |
| 964 | |
| 965 startpos = *str; | |
| 966 memset (&tt, 0, sizeof (tt)); | |
| 967 tt.tm_isdst = -1; | |
| 968 if (isdigit ((int) startpos[0]) && startpos[2] == '-') | |
| 969 { | |
| 970 /* This is how DOS will return the date/time */ | |
| 971 /* 07-06-99 12:57PM */ | |
| 972 if ((endpos = strchr (startpos, '-')) == NULL) | |
| 973 { | |
| 974 g_free (str); | |
| 975 return (0); | |
| 976 } | |
| 977 tt.tm_mon = strtol (startpos, NULL, 10) - 1; | |
| 978 | |
| 979 startpos = endpos + 1; | |
| 980 if ((endpos = strchr (startpos, '-')) == NULL) | |
| 981 { | |
| 982 g_free (str); | |
| 983 return (0); | |
| 984 } | |
| 985 tt.tm_mday = strtol (startpos, NULL, 10); | |
| 986 | |
| 987 startpos = endpos + 1; | |
| 988 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 989 { | |
| 990 g_free (str); | |
| 991 return (0); | |
| 992 } | |
| 993 tt.tm_year = strtol (startpos, NULL, 10); | |
| 994 | |
| 995 while (*endpos == ' ') | |
| 996 endpos++; | |
| 997 | |
| 998 startpos = endpos + 1; | |
| 999 if ((endpos = strchr (startpos, ':')) == NULL) | |
| 1000 { | |
| 1001 g_free (str); | |
| 1002 return (0); | |
| 1003 } | |
| 1004 tt.tm_hour = strtol (startpos, NULL, 10); | |
| 1005 | |
| 1006 startpos = endpos + 1; | |
| 1007 while ((*endpos != 'A') && (*endpos != 'P')) | |
| 1008 endpos++; | |
| 1009 if (*endpos == 'P') | |
| 1010 { | |
| 1011 if (tt.tm_hour == 12) | |
| 1012 tt.tm_hour = 0; | |
| 1013 else | |
| 1014 tt.tm_hour += 12; | |
| 1015 } | |
| 1016 tt.tm_min = strtol (startpos, NULL, 10); | |
| 1017 } | |
| 1018 else | |
| 1019 { | |
| 1020 /* This is how most UNIX, Novell, and MacOS ftp servers send their time */ | |
| 1021 /* Jul 06 12:57 */ | |
| 1022 t = time (NULL); | |
| 1023 curtime = *localtime (&t); | |
| 1024 | |
| 1025 /* Get the month */ | |
| 1026 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 1027 return (0); | |
| 1028 for (i = 0; i < 12; i++) | |
| 1029 { | |
| 1030 if (strncmp (months[i], startpos, 3) == 0) | |
| 1031 { | |
| 1032 tt.tm_mon = i; | |
| 1033 break; | |
| 1034 } | |
| 1035 } | |
| 1036 | |
| 1037 /* Skip the blanks till we get to the next entry */ | |
| 1038 startpos = endpos + 1; | |
| 1039 while (*startpos == ' ') | |
| 1040 startpos++; | |
| 1041 | |
| 1042 /* Get the day */ | |
| 1043 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 1044 return (0); | |
| 1045 tt.tm_mday = strtol (startpos, NULL, 10); | |
| 1046 | |
| 1047 /* Skip the blanks till we get to the next entry */ | |
| 1048 startpos = endpos + 1; | |
| 1049 while (*startpos == ' ') | |
| 1050 startpos++; | |
| 1051 | |
| 1052 if ((datepos = strchr (startpos, ':')) != NULL) | |
| 1053 { | |
| 1054 /* No year was specified. We will use the current year */ | |
| 1055 tt.tm_year = curtime.tm_year; | |
| 1056 | |
| 1057 /* If the date is in the future, than the year is from last year */ | |
| 1058 if ((tt.tm_mon > curtime.tm_mon) || | |
| 1059 ((tt.tm_mon == curtime.tm_mon) && tt.tm_mday > curtime.tm_mday)) | |
| 1060 tt.tm_year--; | |
| 1061 | |
| 1062 /* Get the hours and the minutes */ | |
| 1063 tt.tm_hour = strtol (startpos, NULL, 10); | |
| 1064 tt.tm_min = strtol (datepos + 1, NULL, 10); | |
| 1065 } | |
| 1066 else | |
| 1067 { | |
| 1068 /* Get the year. The hours and minutes will be assumed to be 0 */ | |
| 1069 tt.tm_year = strtol (startpos, NULL, 10) - 1900; | |
| 1070 } | |
| 1071 } | |
| 1072 *str = startpos; | |
| 1073 return (mktime (&tt)); | |
| 1074 } | |
| 1075 | |
| 1076 | |
| 1077 static int | |
| 1078 gftp_parse_ls_eplf (char *str, gftp_file * fle) | |
| 1079 { | |
| 1080 char *startpos; | |
| 1081 | |
| 1082 startpos = str; | |
| 1083 fle->attribs = g_malloc (11); | |
| 1084 strcpy (fle->attribs, "----------"); | |
| 1085 while (startpos) | |
| 1086 { | |
| 1087 startpos++; | |
| 1088 switch (*startpos) | |
| 1089 { | |
| 1090 case '/': | |
| 1091 *fle->attribs = 'd'; | |
| 1092 break; | |
| 1093 case 's': | |
| 1094 fle->size = strtol (startpos + 1, NULL, 10); | |
| 1095 break; | |
| 1096 case 'm': | |
| 1097 fle->datetime = strtol (startpos + 1, NULL, 10); | |
| 1098 break; | |
| 1099 } | |
| 1100 startpos = strchr (startpos, ','); | |
| 1101 } | |
| 1102 if ((startpos = strchr (str, 9)) == NULL) | |
| 1103 return (-2); | |
| 1104 fle->file = g_malloc (strlen (startpos)); | |
| 1105 strcpy (fle->file, startpos + 1); | |
| 1106 fle->user = g_malloc (8); | |
| 1107 strcpy (fle->user, _("unknown")); | |
| 1108 fle->group = g_malloc (8); | |
| 1109 strcpy (fle->group, _("unknown")); | |
| 1110 return (0); | |
| 1111 } | |
| 1112 | |
| 1113 | |
| 1114 static int | |
| 1115 gftp_parse_ls_unix (char *str, int cols, gftp_file * fle) | |
| 1116 { | |
| 1117 char *endpos, *startpos; | |
| 1118 | |
| 1119 startpos = str; | |
| 1120 /* Copy file attributes */ | |
| 1121 if ((startpos = copy_token (&fle->attribs, startpos)) == NULL) | |
| 1122 return (-2); | |
| 1123 | |
| 1124 if (cols >= 9) | |
| 1125 { | |
| 1126 /* Skip the number of links */ | |
| 1127 startpos = goto_next_token (startpos); | |
| 1128 | |
| 1129 /* Copy the user that owns this file */ | |
| 1130 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 1131 return (-2); | |
| 1132 | |
| 1133 /* Copy the group that owns this file */ | |
| 1134 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
| 1135 return (-2); | |
| 1136 } | |
| 1137 else | |
| 1138 { | |
| 1139 fle->group = g_malloc (8); | |
| 1140 strcpy (fle->group, _("unknown")); | |
| 1141 if (cols == 8) | |
| 1142 { | |
| 1143 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 1144 return (-2); | |
| 1145 } | |
| 1146 else | |
| 1147 { | |
| 1148 fle->user = g_malloc (8); | |
| 1149 strcpy (fle->user, _("unknown")); | |
| 1150 } | |
| 1151 startpos = goto_next_token (startpos); | |
| 1152 } | |
| 1153 | |
| 39 | 1154 /* FIXME - this is a bug if there is some extra spaces in the file */ |
| 1 | 1155 /* See if this is a Cray directory listing. It has the following format: |
| 1156 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
| 1157 if (cols == 11 && strstr (str, "->") == NULL) | |
| 1158 { | |
| 1159 startpos = goto_next_token (startpos); | |
| 1160 startpos = goto_next_token (startpos); | |
| 1161 } | |
| 1162 | |
| 1163 /* See if this is a block or character device. We will store the major number | |
| 1164 in the high word and the minor number in the low word. */ | |
| 1165 if ((fle->attribs[0] == 'b' || fle->attribs[0] == 'u' || | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1166 fle->attribs[0] == 'c') && |
| 1 | 1167 ((endpos = strchr (startpos, ',')) != NULL)) |
| 1168 { | |
| 1169 fle->size = strtol (startpos, NULL, 10) << 16; | |
| 1170 | |
| 1171 startpos = endpos + 1; | |
| 1172 while (*startpos == ' ') | |
| 1173 startpos++; | |
| 1174 | |
| 1175 /* Get the minor number */ | |
| 1176 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 1177 return (-2); | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1178 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
| 1 | 1179 } |
| 1180 else | |
| 1181 { | |
| 1182 /* This is a regular file */ | |
| 1183 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 1184 return (-2); | |
| 1185 fle->size = strtol (startpos, NULL, 10); | |
| 1186 } | |
| 1187 | |
| 1188 /* Skip the blanks till we get to the next entry */ | |
| 1189 startpos = endpos + 1; | |
| 1190 while (*startpos == ' ') | |
| 1191 startpos++; | |
| 1192 | |
| 1193 if ((fle->datetime = parse_time (&startpos)) == 0) | |
| 1194 return (-2); | |
| 1195 | |
| 1196 /* Skip the blanks till we get to the next entry */ | |
| 1197 startpos = goto_next_token (startpos); | |
| 1198 | |
| 1199 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
| 1200 if (fle->attribs[0] == 'l' && ((endpos = strstr (startpos, "->")) != NULL)) | |
| 1201 *(endpos - 1) = '\0'; | |
| 1202 fle->file = g_malloc (strlen (startpos) + 1); | |
| 1203 | |
| 1204 strcpy (fle->file, startpos); | |
| 1205 | |
| 1206 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
| 1207 I don't want to do this by default since there are valid filenames with | |
| 1208 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
| 1209 DSLAMS append a bunch of spaces at the end of the file. | |
| 1210 for (endpos = fle->file + strlen (fle->file) - 1; | |
| 1211 *endpos == ' '; | |
| 1212 *endpos-- = '\0'); | |
| 1213 */ | |
| 1214 | |
| 1215 return (0); | |
| 1216 } | |
| 1217 | |
| 1218 | |
| 1219 static int | |
| 1220 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
| 1221 { | |
| 1222 char *startpos; | |
| 1223 | |
| 1224 startpos = str; | |
| 1225 if ((fle->datetime = parse_time (&startpos)) == 0) | |
| 1226 return (-2); | |
| 1227 | |
| 1228 /* No such thing on Windoze.. */ | |
| 1229 fle->user = g_malloc (8); | |
| 1230 strcpy (fle->user, _("unknown")); | |
| 1231 fle->group = g_malloc (8); | |
| 1232 strcpy (fle->group, _("unknown")); | |
| 1233 | |
| 1234 startpos = goto_next_token (startpos); | |
| 1235 fle->attribs = g_malloc (11); | |
| 1236 if (startpos[0] == '<') | |
| 1237 strcpy (fle->attribs, "drwxrwxrwx"); | |
| 1238 else | |
| 1239 { | |
| 1240 strcpy (fle->attribs, "-rw-rw-rw-"); | |
| 1241 fle->size = strtol (startpos, NULL, 10); | |
| 1242 } | |
| 1243 startpos = goto_next_token (startpos); | |
| 1244 fle->file = g_malloc (strlen (startpos) + 1); | |
| 1245 strcpy (fle->file, startpos); | |
| 1246 return (0); | |
| 1247 } | |
| 1248 | |
| 1249 | |
| 1250 static int | |
| 1251 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
| 1252 { | |
| 1253 char *startpos; | |
| 1254 | |
| 1255 if (str[12] != ' ') | |
| 1256 return (-2); | |
| 1257 str[12] = '\0'; | |
| 1258 fle->attribs = g_malloc (13); | |
| 1259 strcpy (fle->attribs, str); | |
| 1260 startpos = str + 13; | |
| 1261 | |
| 1262 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
| 1263 startpos++; | |
| 1264 | |
| 1265 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 1266 return (-2); | |
| 1267 | |
| 1268 fle->group = g_malloc (8); | |
| 1269 strcpy (fle->group, _("unknown")); | |
| 1270 | |
| 1271 fle->size = strtol (startpos, NULL, 10); | |
| 1272 | |
| 1273 startpos = goto_next_token (startpos); | |
| 1274 if ((fle->datetime = parse_time (&startpos)) == 0) | |
| 1275 return (-2); | |
| 1276 | |
| 1277 startpos = goto_next_token (startpos); | |
| 1278 fle->file = g_malloc (strlen (startpos) + 1); | |
| 1279 strcpy (fle->file, startpos); | |
| 1280 return (0); | |
| 1281 } | |
| 1282 | |
| 1283 | |
| 48 | 1284 int |
| 1285 gftp_parse_ls (const char *lsoutput, gftp_file * fle) | |
| 1 | 1286 { |
| 48 | 1287 int result, cols; |
| 1288 char *str, *pos; | |
| 1289 | |
| 1290 g_return_val_if_fail (lsoutput != NULL, -2); | |
| 1291 g_return_val_if_fail (fle != NULL, -2); | |
| 1292 | |
| 1293 str = g_malloc (strlen (lsoutput) + 1); | |
| 1294 strcpy (str, lsoutput); | |
| 1295 memset (fle, 0, sizeof (*fle)); | |
| 1 | 1296 |
| 48 | 1297 if (str[strlen (str) - 1] == '\n') |
| 1298 str[strlen (str) - 1] = '\0'; | |
| 1299 if (str[strlen (str) - 1] == '\r') | |
| 1300 str[strlen (str) - 1] = '\0'; | |
| 1301 if (*lsoutput == '+') /* EPLF format */ | |
| 1302 result = gftp_parse_ls_eplf (str, fle); | |
| 1303 else if (isdigit ((int) str[0]) && str[2] == '-') /* DOS/WinNT format */ | |
| 1304 result = gftp_parse_ls_nt (str, fle); | |
| 1305 else if (str[1] == ' ' && str[2] == '[') /* Novell format */ | |
| 1306 result = gftp_parse_ls_novell (str, fle); | |
| 1307 else | |
| 1308 { | |
| 1309 /* UNIX/MacOS format */ | |
| 1310 | |
| 1311 /* If there is no space between the attribs and links field, just make one */ | |
| 1312 if (strlen (str) > 10) | |
| 1313 str[10] = ' '; | |
| 39 | 1314 |
| 48 | 1315 /* Determine the number of columns */ |
| 1316 cols = 0; | |
| 1317 pos = str; | |
| 1318 while (*pos != '\0') | |
| 1319 { | |
| 1320 while (*pos != '\0' && *pos != ' ' && *pos != '\t') | |
| 1321 { | |
| 1322 if (*pos == ':') | |
| 1323 break; | |
| 1324 pos++; | |
| 1325 } | |
| 1326 | |
| 1327 cols++; | |
| 1328 | |
| 1329 if (*pos == ':') | |
| 1330 { | |
| 1331 cols++; | |
| 1332 break; | |
| 1333 } | |
| 1334 | |
| 1335 while (*pos == ' ' || *pos == '\t') | |
| 1336 pos++; | |
| 1337 } | |
| 1 | 1338 |
| 48 | 1339 if (cols > 6) |
| 1340 result = gftp_parse_ls_unix (str, cols, fle); | |
| 1341 else | |
| 1342 result = -2; | |
| 1343 } | |
| 1344 g_free (str); | |
| 1345 | |
| 1346 if (fle->attribs == NULL) | |
| 1347 return (result); | |
| 1348 | |
| 1349 if (*fle->attribs == 'd') | |
| 1350 fle->isdir = 1; | |
| 1351 if (*fle->attribs == 'l') | |
| 1352 fle->islink = 1; | |
| 1353 if (strchr (fle->attribs, 'x') != NULL && !fle->isdir && !fle->islink) | |
| 1354 fle->isexe = 1; | |
| 1355 if (*fle->attribs == 'b') | |
| 1356 fle->isblock = 1; | |
| 1357 if (*fle->attribs == 'c') | |
| 1358 fle->ischar = 1; | |
| 1359 if (*fle->attribs == 's') | |
| 1360 fle->issocket = 1; | |
| 1361 if (*fle->attribs == 'p') | |
| 1362 fle->isfifo = 1; | |
| 1363 | |
| 1364 return (result); | |
| 1 | 1365 } |
| 1366 | |
| 1367 | |
| 48 | 1368 static GHashTable * |
| 1369 gftp_gen_dir_hash (gftp_request * request) | |
| 1 | 1370 { |
| 48 | 1371 unsigned long *newsize; |
| 1372 GHashTable * dirhash; | |
| 1373 gftp_file * fle; | |
| 1374 char * newname; | |
| 1375 | |
| 39 | 1376 |
| 48 | 1377 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); |
| 1378 if (gftp_list_files (request) == 0) | |
| 1379 { | |
| 1380 fle = g_malloc0 (sizeof (*fle)); | |
| 1381 while (gftp_get_next_file (request, NULL, fle) > 0) | |
| 1382 { | |
| 1383 newname = fle->file; | |
| 1384 newsize = g_malloc (sizeof (unsigned long)); | |
| 1385 *newsize = fle->size; | |
| 1386 g_hash_table_insert (dirhash, newname, newsize); | |
| 1387 fle->file = NULL; | |
| 1388 gftp_file_destroy (fle); | |
| 1389 } | |
| 1390 gftp_end_transfer (request); | |
| 1391 g_free (fle); | |
| 1392 } | |
| 1393 else | |
| 1394 { | |
| 1395 g_hash_table_destroy (dirhash); | |
| 1396 dirhash = NULL; | |
| 1397 } | |
| 1398 return (dirhash); | |
| 1399 } | |
| 39 | 1400 |
| 48 | 1401 |
| 1402 static void | |
| 1403 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
| 1404 { | |
| 39 | 1405 |
| 48 | 1406 g_free (key); |
| 1407 g_free (value); | |
| 1408 } | |
| 1409 | |
| 1410 | |
| 1411 static void | |
| 1412 gftp_destroy_dir_hash (GHashTable * dirhash) | |
| 1413 { | |
| 1414 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); | |
| 1415 g_hash_table_destroy (dirhash); | |
| 1 | 1416 } |
| 1417 | |
| 1418 | |
| 1419 static GList * | |
| 1420 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir) | |
| 1421 { | |
| 1422 unsigned long *newsize; | |
| 1423 GHashTable * dirhash; | |
| 1424 GList * templist; | |
| 1425 gftp_file * fle; | |
| 1426 char *newname; | |
| 1427 | |
| 1428 if (getothdir && transfer->toreq) | |
| 1429 dirhash = gftp_gen_dir_hash (transfer->toreq); | |
| 1430 else | |
| 1431 dirhash = NULL; | |
| 1432 | |
| 1433 if (gftp_list_files (transfer->fromreq) != 0) | |
| 1434 return (NULL); | |
| 1435 | |
| 1436 fle = g_malloc (sizeof (*fle)); | |
| 1437 templist = NULL; | |
| 1438 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
| 1439 { | |
| 1440 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
| 1441 { | |
| 1442 gftp_file_destroy (fle); | |
| 1443 continue; | |
| 1444 } | |
| 1445 | |
| 1446 if (dirhash && | |
| 1447 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
| 1448 fle->startsize = *newsize; | |
| 1449 | |
| 1450 if (transfer->toreq) | |
| 1451 fle->destfile = g_strconcat (transfer->toreq->directory, "/", | |
| 1452 fle->file, NULL); | |
| 1453 newname = g_strconcat (transfer->fromreq->directory, "/", fle->file, NULL); | |
| 1454 g_free (fle->file); | |
| 1455 fle->file = newname; | |
| 1456 | |
| 1457 templist = g_list_append (templist, fle); | |
| 1458 | |
| 1459 fle = g_malloc (sizeof (*fle)); | |
| 1460 } | |
| 1461 gftp_end_transfer (transfer->fromreq); | |
| 1462 | |
| 1463 gftp_file_destroy (fle); | |
| 1464 g_free (fle); | |
| 1465 | |
| 1466 if (dirhash) | |
| 1467 gftp_destroy_dir_hash (dirhash); | |
| 1468 | |
| 1469 | |
| 1470 return (templist); | |
| 1471 } | |
| 1472 | |
| 1473 | |
| 1474 int | |
| 1475 gftp_get_all_subdirs (gftp_transfer * transfer, | |
| 1476 void (*update_func) (gftp_transfer * transfer)) | |
| 1477 { | |
| 1478 char *oldfromdir, *oldtodir, *newname; | |
| 1479 GList * templist, * lastlist; | |
| 1480 int forcecd, remotechanged; | |
| 1481 unsigned long *newsize; | |
| 1482 GHashTable * dirhash; | |
| 1483 gftp_file * curfle; | |
| 1484 | |
| 1485 g_return_val_if_fail (transfer != NULL, -1); | |
| 1486 g_return_val_if_fail (transfer->fromreq != NULL, -1); | |
| 1487 g_return_val_if_fail (transfer->files != NULL, -1); | |
| 1488 | |
| 1489 if (transfer->toreq != NULL) | |
| 1490 dirhash = gftp_gen_dir_hash (transfer->toreq); | |
| 1491 else | |
| 1492 dirhash = NULL; | |
| 1493 | |
| 1494 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
| 1495 { | |
| 1496 curfle = lastlist->data; | |
| 1497 if (dirhash && | |
| 1498 (newsize = g_hash_table_lookup (dirhash, curfle->file)) != NULL) | |
| 1499 curfle->startsize = *newsize; | |
| 1500 | |
| 1501 if (transfer->toreq) | |
| 1502 curfle->destfile = g_strconcat (transfer->toreq->directory, "/", | |
| 1503 curfle->file, NULL); | |
| 1504 newname = g_strconcat (transfer->fromreq->directory, transfer->fromreq->directory[strlen (transfer->fromreq->directory) - 1] == '/' ? "" : "/", curfle->file, NULL); | |
| 1505 g_free (curfle->file); | |
| 1506 curfle->file = newname; | |
| 1507 | |
| 1508 if (lastlist->next == NULL) | |
| 1509 break; | |
| 1510 } | |
| 1511 | |
| 1512 if (dirhash) | |
| 1513 gftp_destroy_dir_hash (dirhash); | |
| 1514 | |
| 1515 oldfromdir = oldtodir = NULL; | |
| 1516 remotechanged = 0; | |
| 1517 forcecd = 0; | |
| 1518 for (templist = transfer->files; templist != NULL; templist = templist->next) | |
| 1519 { | |
| 1520 curfle = templist->data; | |
| 1521 | |
| 1522 if (curfle->isdir) | |
| 1523 { | |
| 1524 oldfromdir = transfer->fromreq->directory; | |
| 1525 transfer->fromreq->directory = curfle->file; | |
| 1526 | |
| 1527 if (transfer->toreq) | |
| 1528 { | |
| 1529 oldtodir = transfer->toreq->directory; | |
| 1530 transfer->toreq->directory = curfle->destfile; | |
| 1531 } | |
| 1532 forcecd = 1; | |
| 1533 if (gftp_set_directory (transfer->fromreq, | |
| 1534 transfer->fromreq->directory) == 0) | |
| 1535 { | |
| 1536 if (curfle->startsize > 0 && transfer->toreq) | |
| 1537 { | |
| 1538 remotechanged = 1; | |
| 1539 if (gftp_set_directory (transfer->toreq, | |
| 1540 transfer->toreq->directory) != 0) | |
| 1541 curfle->startsize = 0; | |
| 1542 } | |
| 1543 | |
| 1544 lastlist->next = gftp_get_dir_listing (transfer, | |
| 1545 curfle->startsize > 0); | |
| 1546 if (lastlist->next != NULL) | |
| 1547 { | |
| 1548 lastlist->next->prev = lastlist; | |
| 1549 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
| 1550 } | |
| 1551 transfer->numdirs++; | |
| 1552 if (update_func) | |
| 1553 update_func (transfer); | |
| 1554 } | |
| 1555 else | |
| 1556 curfle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
| 1557 | |
| 1558 transfer->fromreq->directory = oldfromdir; | |
| 1559 if (transfer->toreq) | |
| 1560 transfer->toreq->directory = oldtodir; | |
| 1561 } | |
| 1562 else | |
| 1563 { | |
| 1564 curfle->ascii = gftp_get_file_transfer_mode (curfle->file, | |
| 1565 transfer->fromreq->data_type) == GFTP_TYPE_ASCII; | |
| 1566 transfer->numfiles++; | |
| 1567 } | |
| 1568 } | |
| 1569 | |
| 1570 if (forcecd) | |
| 1571 gftp_set_directory (transfer->fromreq, transfer->fromreq->directory); | |
| 1572 if (remotechanged && transfer->toreq) | |
| 1573 gftp_set_directory (transfer->toreq, transfer->toreq->directory); | |
| 1574 | |
| 1575 if (update_func) | |
| 1576 { | |
| 1577 transfer->numfiles = transfer->numdirs = -1; | |
| 1578 update_func (transfer); | |
| 1579 } | |
| 1580 return (0); | |
| 1581 } | |
| 1582 | |
| 1583 | |
| 1584 int | |
| 1585 gftp_get_file_transfer_mode (char *filename, int def) | |
| 1586 { | |
| 1587 gftp_file_extensions * tempext; | |
| 1588 GList * templist; | |
| 1589 int stlen, ret; | |
| 1590 | |
| 1591 if (!use_default_dl_types) | |
| 1592 return (def); | |
| 1593 | |
| 1594 ret = def; | |
| 1595 stlen = strlen (filename); | |
| 1596 for (templist = registered_exts; templist != NULL; templist = templist->next) | |
| 1597 { | |
| 1598 tempext = templist->data; | |
| 1599 | |
| 1600 if (stlen >= tempext->stlen && | |
| 1601 strcmp (&filename[stlen - tempext->stlen], tempext->ext) == 0) | |
| 1602 { | |
| 1603 if (toupper (*tempext->ascii_binary == 'A')) | |
| 1604 ret = GFTP_TYPE_ASCII; | |
| 1605 else if (toupper (*tempext->ascii_binary == 'B')) | |
| 1606 ret = GFTP_TYPE_BINARY; | |
| 1607 break; | |
| 1608 } | |
| 1609 } | |
| 1610 | |
| 1611 return (ret); | |
| 1612 } | |
| 1613 | |
| 1614 | |
| 1615 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 1616 int | |
| 1617 get_port (struct addrinfo *addr) | |
| 1618 { | |
| 1619 struct sockaddr_in * saddr; | |
| 1620 int port; | |
| 1621 | |
| 1622 if (addr->ai_family == AF_INET) | |
| 1623 { | |
| 1624 saddr = (struct sockaddr_in *) addr->ai_addr; | |
| 1625 port = ntohs (saddr->sin_port); | |
| 1626 } | |
| 1627 else | |
| 1628 port = 0; | |
| 1629 | |
| 1630 return (port); | |
| 1631 } | |
| 1632 #endif | |
| 1633 | |
| 1634 | |
| 1635 int | |
| 1636 gftp_connect_server (gftp_request * request, char *service) | |
| 1637 { | |
| 1638 char *connect_host, *disphost; | |
| 1639 int port, sock; | |
| 1640 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 1641 struct addrinfo hints, *res; | |
| 1642 char serv[8]; | |
| 1643 int errnum; | |
| 1644 | |
| 1645 if ((request->use_proxy = gftp_need_proxy (request, service)) < 0) | |
| 1646 return (-1); | |
| 1647 else if (request->use_proxy == 1) | |
| 1648 request->hostp = NULL; | |
| 1649 | |
| 1650 memset (&hints, 0, sizeof (hints)); | |
| 1651 hints.ai_flags = AI_CANONNAME; | |
| 1652 hints.ai_family = AF_INET; | |
| 1653 hints.ai_socktype = SOCK_STREAM; | |
| 1654 | |
| 1655 connect_host = request->use_proxy ? request->proxy_hostname : | |
| 1656 request->hostname; | |
| 1657 port = request->use_proxy ? request->proxy_port : request->port; | |
| 1658 | |
| 1659 if (request->hostp == NULL) | |
| 1660 { | |
| 1661 if (port == 0) | |
| 1662 strcpy (serv, service); | |
| 1663 else | |
| 1664 snprintf (serv, sizeof (serv), "%d", port); | |
| 1665 | |
| 1666 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1667 _("Looking up %s\n"), connect_host); | |
| 1668 if ((errnum = getaddrinfo (connect_host, serv, &hints, | |
| 1669 &request->hostp)) != 0) | |
| 1670 { | |
| 1671 request->logging_function (gftp_logging_error, request->user_data, | |
| 1672 _("Cannot look up hostname %s: %s\n"), | |
| 1673 connect_host, gai_strerror (errnum)); | |
| 1674 return (-1); | |
| 1675 } | |
| 1676 } | |
| 1677 | |
| 1678 disphost = connect_host; | |
| 1679 for (res = request->hostp; res != NULL; res = res->ai_next) | |
| 1680 { | |
| 1681 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
| 1682 port = get_port (res); | |
| 56 | 1683 if (!request->use_proxy) |
| 1684 request->port = port; | |
| 1685 | |
| 1 | 1686 if ((sock = socket (res->ai_family, res->ai_socktype, |
| 1687 res->ai_protocol)) < 0) | |
| 1688 { | |
| 1689 request->logging_function (gftp_logging_error, request->user_data, | |
| 1690 _("Failed to create a socket: %s\n"), | |
| 1691 g_strerror (errno)); | |
| 1692 continue; | |
| 66 | 1693 } |
| 1 | 1694 |
| 1695 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1696 _("Trying %s:%d\n"), disphost, port); | |
| 1697 | |
| 1698 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
| 1699 { | |
| 1700 request->logging_function (gftp_logging_error, request->user_data, | |
| 1701 _("Cannot connect to %s: %s\n"), | |
| 1702 disphost, g_strerror (errno)); | |
| 1703 close (sock); | |
| 1704 continue; | |
| 1705 } | |
| 1706 break; | |
| 1707 } | |
| 1708 | |
| 1709 if (res == NULL) | |
| 1710 { | |
| 1711 if (request->hostp != NULL) | |
| 1712 { | |
| 1713 freeaddrinfo (request->hostp); | |
| 1714 request->hostp = NULL; | |
| 1715 } | |
| 1716 return (-1); | |
| 1717 } | |
| 1718 | |
| 1719 #else /* !HAVE_GETADDRINFO */ | |
| 1720 struct sockaddr_in remote_address; | |
| 1721 struct servent serv_struct; | |
| 1722 int curhost; | |
| 1723 | |
| 1724 if ((request->use_proxy = gftp_need_proxy (request, service)) < 0) | |
| 1725 return (-1); | |
| 1726 else if (request->use_proxy == 1) | |
| 1727 request->hostp = NULL; | |
| 1728 | |
| 1729 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | |
| 1730 { | |
| 1731 request->logging_function (gftp_logging_error, request->user_data, | |
| 1732 _("Failed to create a socket: %s\n"), | |
| 1733 g_strerror (errno)); | |
| 1734 return (-1); | |
| 1735 } | |
| 1736 | |
| 1737 memset (&remote_address, 0, sizeof (remote_address)); | |
| 1738 remote_address.sin_family = AF_INET; | |
| 1739 | |
| 1740 connect_host = request->use_proxy ? request->proxy_hostname : | |
| 1741 request->hostname; | |
| 1742 port = htons (request->use_proxy ? request->proxy_port : request->port); | |
| 1743 | |
| 1744 if (port == 0) | |
| 1745 { | |
| 1746 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
| 1747 { | |
| 1748 port = htons (21); | |
| 1749 request->port = 21; | |
| 1750 } | |
| 1751 else | |
| 1752 { | |
| 1753 port = serv_struct.s_port; | |
| 1754 request->port = ntohs (serv_struct.s_port); | |
| 1755 } | |
| 56 | 1756 |
| 1757 if (!request->use_proxy) | |
| 1758 request->port = ntohs (port); | |
| 1 | 1759 } |
| 1760 remote_address.sin_port = port; | |
| 1761 | |
| 1762 if (request->hostp == NULL) | |
| 1763 { | |
| 1764 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1765 _("Looking up %s\n"), connect_host); | |
| 1766 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, | |
| 1767 NULL))) | |
| 1768 { | |
| 1769 request->logging_function (gftp_logging_error, request->user_data, | |
| 1770 _("Cannot look up hostname %s: %s\n"), | |
| 1771 connect_host, g_strerror (errno)); | |
| 1772 close (sock); | |
| 1773 return (-1); | |
| 1774 } | |
| 1775 } | |
| 1776 | |
| 1777 disphost = NULL; | |
| 1778 for (curhost = 0; request->host.h_addr_list[curhost] != NULL; curhost++) | |
| 1779 { | |
| 1780 disphost = request->host.h_name; | |
| 1781 memcpy (&remote_address.sin_addr, request->host.h_addr_list[curhost], | |
| 1782 request->host.h_length); | |
| 1783 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1784 _("Trying %s:%d\n"), | |
| 1785 request->host.h_name, ntohs (port)); | |
| 1786 | |
| 1787 if (connect (sock, (struct sockaddr *) &remote_address, | |
| 1788 sizeof (remote_address)) == -1) | |
| 1789 { | |
| 1790 request->logging_function (gftp_logging_error, request->user_data, | |
| 1791 _("Cannot connect to %s: %s\n"), | |
| 1792 connect_host, g_strerror (errno)); | |
| 1793 } | |
| 1794 break; | |
| 1795 } | |
| 1796 | |
| 1797 if (request->host.h_addr_list[curhost] == NULL) | |
| 1798 { | |
| 1799 close (sock); | |
| 1800 return (-1); | |
| 1801 } | |
| 1802 port = ntohs (port); | |
| 1803 #endif /* HAVE_GETADDRINFO */ | |
| 1804 | |
| 1805 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1806 _("Connected to %s:%d\n"), connect_host, port); | |
| 58 | 1807 |
| 1808 if (gftp_set_sockblocking (request, sock, 1) == -1) | |
| 1809 { | |
| 1810 close (sock); | |
| 1811 return (-1); | |
| 1812 } | |
| 1813 | |
| 1 | 1814 return (sock); |
| 1815 } | |
| 1816 | |
| 1817 | |
| 1818 void | |
| 1819 gftp_set_config_options (gftp_request * request) | |
| 1820 { | |
| 1821 request->network_timeout = network_timeout; | |
| 1822 request->retries = retries; | |
| 1823 request->sleep_time = sleep_time; | |
| 1824 request->maxkbs = maxkbs; | |
| 1825 | |
| 58 | 1826 if (request->set_config_options != NULL) |
| 1827 request->set_config_options (request); | |
| 1 | 1828 } |
| 1829 | |
| 1830 | |
| 1831 void | |
| 1832 gftp_set_sftpserv_path (gftp_request * request, char *path) | |
| 1833 { | |
| 1834 g_return_if_fail (request != NULL); | |
| 1835 | |
| 1836 if (request->sftpserv_path) | |
| 1837 g_free (request->sftpserv_path); | |
| 1838 | |
| 1839 if (path != NULL && *path != '\0') | |
| 1840 { | |
| 1841 request->sftpserv_path = g_malloc (strlen (path) + 1); | |
| 1842 strcpy (request->sftpserv_path, path); | |
| 1843 } | |
| 1844 else | |
| 1845 request->sftpserv_path = NULL; | |
| 1846 } | |
| 1847 | |
| 1848 | |
| 1849 void | |
| 1850 print_file_list (GList * list) | |
| 1851 { | |
| 1852 gftp_file * tempfle; | |
| 1853 GList * templist; | |
| 1854 | |
| 1855 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
| 1856 for (templist = list; ; templist = templist->next) | |
| 1857 { | |
| 1858 tempfle = templist->data; | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1859 #if defined (_LARGEFILE_SOURCE) |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1860 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1861 #else |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1862 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1863 #endif |
| 16 | 1864 tempfle->file, tempfle->destfile, |
| 1865 tempfle->size, tempfle->startsize, | |
| 1866 tempfle->user, tempfle->group, tempfle->attribs); | |
| 1 | 1867 if (templist->next == NULL) |
| 1868 break; | |
| 1869 } | |
| 1870 | |
| 1871 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
| 1872 for (; ; templist = templist->prev) | |
| 1873 { | |
| 1874 tempfle = templist->data; | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1875 #if defined (_LARGEFILE_SOURCE) |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1876 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1877 #else |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1878 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1879 #endif |
| 16 | 1880 tempfle->file, tempfle->destfile, |
| 1881 tempfle->size, tempfle->startsize, | |
| 1882 tempfle->user, tempfle->group, tempfle->attribs); | |
| 1 | 1883 if (templist == list) |
| 1884 break; | |
| 1885 } | |
| 1886 printf ("--END OF FILE LISTING--\n"); | |
| 1887 } | |
| 1888 | |
| 41 | 1889 |
| 58 | 1890 static void |
| 1891 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) | |
| 41 | 1892 { |
| 58 | 1893 g_free ((*rbuf)->buffer); |
| 1894 g_free (*rbuf); | |
| 1895 *rbuf = NULL; | |
| 41 | 1896 } |
| 1897 | |
| 1898 | |
| 58 | 1899 ssize_t |
| 1900 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
| 1901 char * str, size_t len, int fd) | |
| 41 | 1902 { |
| 58 | 1903 ssize_t ret, retval, rlen, copysize; |
| 1904 char *pos, *nextpos; | |
| 1905 | |
| 1906 if (*rbuf == NULL) | |
| 1907 { | |
| 1908 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
| 1909 (*rbuf)->max_bufsize = len; | |
| 60 | 1910 (*rbuf)->buffer = g_malloc ((*rbuf)->max_bufsize + 1); |
| 58 | 1911 if ((ret = gftp_read (request, (*rbuf)->buffer, (*rbuf)->max_bufsize, |
| 1912 fd)) <= 0) | |
| 1913 { | |
| 1914 gftp_free_getline_buffer (rbuf); | |
| 1915 return (ret); | |
| 1916 } | |
| 60 | 1917 (*rbuf)->buffer[ret] = '\0'; |
| 58 | 1918 (*rbuf)->cur_bufsize = ret; |
| 1919 (*rbuf)->curpos = (*rbuf)->buffer; | |
| 1920 } | |
| 1921 | |
| 1922 retval = -2; | |
| 1923 | |
| 1924 do | |
| 1925 { | |
| 1926 if ((*rbuf)->cur_bufsize > 0 && | |
| 1927 ((pos = strchr ((*rbuf)->curpos, '\n')) != NULL || | |
| 1928 ((*rbuf)->curpos == (*rbuf)->buffer && | |
| 1929 (*rbuf)->max_bufsize == (*rbuf)->cur_bufsize))) | |
| 1930 { | |
| 1931 if (pos != NULL) | |
| 1932 { | |
| 1933 nextpos = pos + 1; | |
| 1934 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
| 1935 pos--; | |
| 1936 *pos = '\0'; | |
| 1937 } | |
| 1938 else | |
| 60 | 1939 nextpos = NULL; |
| 58 | 1940 |
| 1941 strncpy (str, (*rbuf)->curpos, len); | |
| 60 | 1942 retval = pos - (*rbuf)->curpos > len ? len : pos - (*rbuf)->curpos; |
| 58 | 1943 |
| 1944 if (pos != NULL) | |
| 1945 { | |
| 1946 if (nextpos - (*rbuf)->buffer >= (*rbuf)->cur_bufsize) | |
| 1947 (*rbuf)->cur_bufsize = 0; | |
| 1948 else | |
| 1949 (*rbuf)->curpos = nextpos; | |
| 1950 } | |
| 1951 | |
| 1952 break; | |
| 1953 } | |
| 1954 else | |
| 1955 { | |
| 1956 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
| 1957 { | |
| 1958 rlen = (*rbuf)->max_bufsize; | |
| 1959 pos = (*rbuf)->buffer; | |
| 1960 copysize = 0; | |
| 1961 } | |
| 1962 else | |
| 1963 { | |
| 1964 copysize = (*rbuf)->cur_bufsize - ((*rbuf)->curpos - (*rbuf)->buffer); | |
| 1965 memmove ((*rbuf)->buffer, (*rbuf)->curpos, copysize); | |
| 1966 pos = (*rbuf)->buffer + copysize; | |
| 1967 rlen = (*rbuf)->max_bufsize - copysize; | |
| 1968 } | |
| 1969 (*rbuf)->curpos = (*rbuf)->buffer; | |
| 1970 | |
| 1971 if ((ret = gftp_read (request, pos, rlen, fd)) <= 0) | |
| 1972 { | |
| 1973 gftp_free_getline_buffer (rbuf); | |
| 1974 return (ret); | |
| 1975 } | |
| 60 | 1976 (*rbuf)->buffer[ret + copysize] = '\0'; |
| 58 | 1977 (*rbuf)->cur_bufsize = ret + copysize; |
| 1978 } | |
| 1979 } | |
| 1980 while (retval == -2); | |
| 1981 | |
| 1982 return (retval); | |
| 1983 } | |
| 1984 | |
| 1985 | |
| 1986 ssize_t | |
| 1987 gftp_read (gftp_request * request, void *ptr, size_t size, int fd) | |
| 1988 { | |
| 1989 struct timeval tv; | |
| 1990 fd_set fset; | |
| 1991 ssize_t ret; | |
| 41 | 1992 |
| 1993 errno = 0; | |
| 1994 ret = 0; | |
| 1995 do | |
| 1996 { | |
| 58 | 1997 FD_ZERO (&fset); |
| 1998 FD_SET (fd, &fset); | |
| 1999 if (request != NULL) | |
| 2000 tv.tv_sec = request->network_timeout; | |
| 2001 else | |
| 2002 tv.tv_sec = network_timeout; | |
| 2003 tv.tv_usec = 0; | |
| 2004 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
| 2005 if (ret == -1 && errno == EINTR) | |
| 41 | 2006 { |
| 58 | 2007 if (request && request->cancel) |
| 2008 break; | |
| 2009 else | |
| 2010 continue; | |
| 2011 } | |
| 2012 else if (ret <= 0) | |
| 2013 { | |
| 2014 if (request != NULL) | |
| 41 | 2015 { |
| 58 | 2016 request->logging_function (gftp_logging_error, request->user_data, |
| 2017 _("Connection to %s timed out\n"), | |
| 2018 request->hostname); | |
| 2019 gftp_disconnect (request); | |
| 2020 } | |
| 41 | 2021 return (-1); |
| 2022 } | |
| 2023 | |
| 58 | 2024 if ((ret = read (fd, ptr, size)) < 0) |
| 41 | 2025 { |
| 2026 if (errno == EINTR) | |
| 2027 { | |
| 58 | 2028 if (request != NULL && request->cancel) |
| 41 | 2029 break; |
| 2030 else | |
| 58 | 2031 continue; |
| 41 | 2032 } |
| 2033 | |
| 58 | 2034 if (request != NULL) |
| 2035 { | |
| 2036 request->logging_function (gftp_logging_error, request->user_data, | |
| 2037 _("Error: Could not read from socket: %s\n"), | |
| 41 | 2038 g_strerror (errno)); |
| 58 | 2039 gftp_disconnect (request); |
| 2040 } | |
| 41 | 2041 return (-1); |
| 2042 } | |
| 2043 } | |
| 58 | 2044 while (errno == EINTR && !(request != NULL && request->cancel)); |
| 41 | 2045 |
| 58 | 2046 if (errno == EINTR && request != NULL && request->cancel) |
| 41 | 2047 { |
| 2048 gftp_disconnect (request); | |
| 2049 return (-1); | |
| 2050 } | |
| 2051 | |
| 2052 return (ret); | |
| 2053 } | |
| 2054 | |
| 58 | 2055 |
| 2056 ssize_t | |
| 2057 gftp_write (gftp_request * request, const char *ptr, size_t size, int fd) | |
| 2058 { | |
| 2059 struct timeval tv; | |
| 2060 size_t ret, w_ret; | |
| 2061 fd_set fset; | |
| 2062 | |
| 2063 errno = 0; | |
| 2064 ret = 0; | |
| 2065 do | |
| 2066 { | |
| 2067 FD_ZERO (&fset); | |
| 2068 FD_SET (fd, &fset); | |
| 2069 if (request != NULL) | |
| 2070 tv.tv_sec = request->network_timeout; | |
| 2071 else | |
| 2072 tv.tv_sec = network_timeout; | |
| 2073 tv.tv_usec = 0; | |
| 2074 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
| 2075 if (ret == -1 && errno == EINTR) | |
| 2076 { | |
| 2077 if (request != NULL && request->cancel) | |
| 2078 break; | |
| 2079 else | |
| 2080 continue; | |
| 2081 } | |
| 2082 else if (ret <= 0) | |
| 2083 { | |
| 2084 if (request != NULL) | |
| 2085 { | |
| 2086 request->logging_function (gftp_logging_error, request->user_data, | |
| 2087 _("Connection to %s timed out\n"), | |
| 2088 request->hostname); | |
| 2089 gftp_disconnect (request); | |
| 2090 } | |
| 2091 return (-1); | |
| 2092 } | |
| 2093 | |
| 2094 if ((w_ret = write (fd, ptr, size)) < 0) | |
| 2095 { | |
| 2096 if (errno == EINTR) | |
| 2097 { | |
| 2098 if (request != NULL && request->cancel) | |
| 2099 break; | |
| 2100 else | |
| 2101 continue; | |
| 2102 } | |
| 2103 | |
| 2104 if (request != NULL) | |
| 2105 { | |
| 2106 request->logging_function (gftp_logging_error, request->user_data, | |
| 2107 _("Error: Could not write to socket: %s\n"), | |
| 2108 g_strerror (errno)); | |
| 2109 gftp_disconnect (request); | |
| 2110 } | |
| 2111 return (-1); | |
| 2112 } | |
| 2113 | |
| 2114 ptr += w_ret; | |
| 2115 size -= w_ret; | |
| 2116 ret += w_ret; | |
| 2117 } | |
| 2118 while (size > 0); | |
| 2119 | |
| 2120 if (errno == EINTR && request != NULL && request->cancel) | |
| 2121 { | |
| 2122 gftp_disconnect (request); | |
| 2123 return (-1); | |
| 2124 } | |
| 2125 | |
| 2126 return (ret); | |
| 2127 } | |
| 2128 | |
| 2129 | |
| 2130 ssize_t | |
| 2131 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
| 2132 { | |
| 2133 char *tempstr; | |
| 2134 va_list argp; | |
| 2135 ssize_t ret; | |
| 2136 | |
| 2137 va_start (argp, fmt); | |
| 2138 tempstr = g_strdup_vprintf (fmt, argp); | |
| 2139 va_end (argp); | |
| 2140 | |
| 2141 ret = gftp_write (request, tempstr, strlen (tempstr), fd); | |
| 2142 g_free (tempstr); | |
| 2143 return (ret); | |
| 2144 } | |
| 2145 | |
| 2146 | |
| 2147 int | |
| 2148 gftp_set_sockblocking (gftp_request * request, int fd, int non_blocking) | |
| 2149 { | |
| 2150 int flags; | |
| 2151 | |
| 2152 if ((flags = fcntl (fd, F_GETFL, 0)) == -1) | |
| 2153 { | |
| 2154 request->logging_function (gftp_logging_error, request->user_data, | |
| 2155 _("Cannot get socket flags: %s\n"), | |
| 2156 g_strerror (errno)); | |
| 2157 gftp_disconnect (request); | |
| 2158 return (-1); | |
| 2159 } | |
| 2160 | |
| 2161 if (non_blocking) | |
| 2162 flags |= O_NONBLOCK; | |
| 2163 else | |
| 2164 flags &= ~O_NONBLOCK; | |
| 2165 | |
| 2166 if (fcntl (fd, F_SETFL, flags) == -1) | |
| 2167 { | |
| 2168 request->logging_function (gftp_logging_error, request->user_data, | |
| 2169 _("Cannot set socket to non-blocking: %s\n"), | |
| 2170 g_strerror (errno)); | |
| 2171 gftp_disconnect (request); | |
| 2172 return (-1); | |
| 2173 } | |
| 2174 | |
| 2175 return (0); | |
| 2176 } | |
| 2177 | |
| 2178 | |
| 63 | 2179 void |
| 2180 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
| 2181 { | |
| 2182 g_return_if_fail (dest != NULL); | |
| 2183 g_return_if_fail (source != NULL); | |
| 2184 g_return_if_fail (dest->protonum == source->protonum); | |
| 2185 | |
| 2186 dest->sockfd = source->sockfd; | |
| 2187 dest->datafd = source->datafd; | |
| 2188 dest->cached = 0; | |
| 2189 if (!source->always_connected) | |
| 2190 { | |
| 2191 source->sockfd = -1; | |
| 2192 source->datafd = -1; | |
| 2193 source->cached = 1; | |
| 2194 } | |
| 2195 | |
| 2196 if (dest->swap_socks) | |
| 2197 dest->swap_socks (dest, source); | |
| 2198 } | |
| 2199 |
