Mercurial > gftp.yaz
annotate lib/protocols.c @ 76:fe308e435ed5
2002-12-4 Brian Masney <masneyb@gftp.org>
* lib/protocols.c (gftp_set_data_type) - return 0 if there is no
protocol specific set_data_type function
* src/gtk/transfer.c (gftp_gtk_transfer_files) - ascii/binary automatic
switching fixes
| author | masneyb |
|---|---|
| date | Thu, 05 Dec 2002 00:43:07 +0000 |
| parents | e2b30d0c97a4 |
| children | 7ef60ce2bdb2 |
| 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) | |
| 76 | 499 return (0); |
| 1 | 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 ret = def; | |
| 1592 stlen = strlen (filename); | |
| 1593 for (templist = registered_exts; templist != NULL; templist = templist->next) | |
| 1594 { | |
| 1595 tempext = templist->data; | |
| 1596 | |
| 1597 if (stlen >= tempext->stlen && | |
| 1598 strcmp (&filename[stlen - tempext->stlen], tempext->ext) == 0) | |
| 1599 { | |
| 1600 if (toupper (*tempext->ascii_binary == 'A')) | |
| 1601 ret = GFTP_TYPE_ASCII; | |
| 1602 else if (toupper (*tempext->ascii_binary == 'B')) | |
| 1603 ret = GFTP_TYPE_BINARY; | |
| 1604 break; | |
| 1605 } | |
| 1606 } | |
| 1607 | |
| 1608 return (ret); | |
| 1609 } | |
| 1610 | |
| 1611 | |
| 1612 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 1613 int | |
| 1614 get_port (struct addrinfo *addr) | |
| 1615 { | |
| 1616 struct sockaddr_in * saddr; | |
| 1617 int port; | |
| 1618 | |
| 1619 if (addr->ai_family == AF_INET) | |
| 1620 { | |
| 1621 saddr = (struct sockaddr_in *) addr->ai_addr; | |
| 1622 port = ntohs (saddr->sin_port); | |
| 1623 } | |
| 1624 else | |
| 1625 port = 0; | |
| 1626 | |
| 1627 return (port); | |
| 1628 } | |
| 1629 #endif | |
| 1630 | |
| 1631 | |
| 1632 int | |
| 1633 gftp_connect_server (gftp_request * request, char *service) | |
| 1634 { | |
| 1635 char *connect_host, *disphost; | |
| 1636 int port, sock; | |
| 1637 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 1638 struct addrinfo hints, *res; | |
| 1639 char serv[8]; | |
| 1640 int errnum; | |
| 1641 | |
| 1642 if ((request->use_proxy = gftp_need_proxy (request, service)) < 0) | |
| 1643 return (-1); | |
| 1644 else if (request->use_proxy == 1) | |
| 1645 request->hostp = NULL; | |
| 1646 | |
| 1647 memset (&hints, 0, sizeof (hints)); | |
| 1648 hints.ai_flags = AI_CANONNAME; | |
| 1649 hints.ai_family = AF_INET; | |
| 1650 hints.ai_socktype = SOCK_STREAM; | |
| 1651 | |
| 1652 connect_host = request->use_proxy ? request->proxy_hostname : | |
| 1653 request->hostname; | |
| 1654 port = request->use_proxy ? request->proxy_port : request->port; | |
| 1655 | |
| 1656 if (request->hostp == NULL) | |
| 1657 { | |
| 1658 if (port == 0) | |
| 1659 strcpy (serv, service); | |
| 1660 else | |
| 1661 snprintf (serv, sizeof (serv), "%d", port); | |
| 1662 | |
| 1663 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1664 _("Looking up %s\n"), connect_host); | |
| 1665 if ((errnum = getaddrinfo (connect_host, serv, &hints, | |
| 1666 &request->hostp)) != 0) | |
| 1667 { | |
| 1668 request->logging_function (gftp_logging_error, request->user_data, | |
| 1669 _("Cannot look up hostname %s: %s\n"), | |
| 1670 connect_host, gai_strerror (errnum)); | |
| 1671 return (-1); | |
| 1672 } | |
| 1673 } | |
| 1674 | |
| 1675 disphost = connect_host; | |
| 1676 for (res = request->hostp; res != NULL; res = res->ai_next) | |
| 1677 { | |
| 1678 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
| 1679 port = get_port (res); | |
| 56 | 1680 if (!request->use_proxy) |
| 1681 request->port = port; | |
| 1682 | |
| 1 | 1683 if ((sock = socket (res->ai_family, res->ai_socktype, |
| 1684 res->ai_protocol)) < 0) | |
| 1685 { | |
| 1686 request->logging_function (gftp_logging_error, request->user_data, | |
| 1687 _("Failed to create a socket: %s\n"), | |
| 1688 g_strerror (errno)); | |
| 1689 continue; | |
| 66 | 1690 } |
| 1 | 1691 |
| 1692 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1693 _("Trying %s:%d\n"), disphost, port); | |
| 1694 | |
| 1695 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
| 1696 { | |
| 1697 request->logging_function (gftp_logging_error, request->user_data, | |
| 1698 _("Cannot connect to %s: %s\n"), | |
| 1699 disphost, g_strerror (errno)); | |
| 1700 close (sock); | |
| 1701 continue; | |
| 1702 } | |
| 1703 break; | |
| 1704 } | |
| 1705 | |
| 1706 if (res == NULL) | |
| 1707 { | |
| 1708 if (request->hostp != NULL) | |
| 1709 { | |
| 1710 freeaddrinfo (request->hostp); | |
| 1711 request->hostp = NULL; | |
| 1712 } | |
| 1713 return (-1); | |
| 1714 } | |
| 1715 | |
| 1716 #else /* !HAVE_GETADDRINFO */ | |
| 1717 struct sockaddr_in remote_address; | |
| 1718 struct servent serv_struct; | |
| 1719 int curhost; | |
| 1720 | |
| 1721 if ((request->use_proxy = gftp_need_proxy (request, service)) < 0) | |
| 1722 return (-1); | |
| 1723 else if (request->use_proxy == 1) | |
| 1724 request->hostp = NULL; | |
| 1725 | |
| 1726 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | |
| 1727 { | |
| 1728 request->logging_function (gftp_logging_error, request->user_data, | |
| 1729 _("Failed to create a socket: %s\n"), | |
| 1730 g_strerror (errno)); | |
| 1731 return (-1); | |
| 1732 } | |
| 1733 | |
| 1734 memset (&remote_address, 0, sizeof (remote_address)); | |
| 1735 remote_address.sin_family = AF_INET; | |
| 1736 | |
| 1737 connect_host = request->use_proxy ? request->proxy_hostname : | |
| 1738 request->hostname; | |
| 1739 port = htons (request->use_proxy ? request->proxy_port : request->port); | |
| 1740 | |
| 1741 if (port == 0) | |
| 1742 { | |
| 1743 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
| 1744 { | |
| 1745 port = htons (21); | |
| 1746 request->port = 21; | |
| 1747 } | |
| 1748 else | |
| 1749 { | |
| 1750 port = serv_struct.s_port; | |
| 1751 request->port = ntohs (serv_struct.s_port); | |
| 1752 } | |
| 56 | 1753 |
| 1754 if (!request->use_proxy) | |
| 1755 request->port = ntohs (port); | |
| 1 | 1756 } |
| 1757 remote_address.sin_port = port; | |
| 1758 | |
| 1759 if (request->hostp == NULL) | |
| 1760 { | |
| 1761 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1762 _("Looking up %s\n"), connect_host); | |
| 1763 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, | |
| 1764 NULL))) | |
| 1765 { | |
| 1766 request->logging_function (gftp_logging_error, request->user_data, | |
| 1767 _("Cannot look up hostname %s: %s\n"), | |
| 1768 connect_host, g_strerror (errno)); | |
| 1769 close (sock); | |
| 1770 return (-1); | |
| 1771 } | |
| 1772 } | |
| 1773 | |
| 1774 disphost = NULL; | |
| 1775 for (curhost = 0; request->host.h_addr_list[curhost] != NULL; curhost++) | |
| 1776 { | |
| 1777 disphost = request->host.h_name; | |
| 1778 memcpy (&remote_address.sin_addr, request->host.h_addr_list[curhost], | |
| 1779 request->host.h_length); | |
| 1780 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1781 _("Trying %s:%d\n"), | |
| 1782 request->host.h_name, ntohs (port)); | |
| 1783 | |
| 1784 if (connect (sock, (struct sockaddr *) &remote_address, | |
| 1785 sizeof (remote_address)) == -1) | |
| 1786 { | |
| 1787 request->logging_function (gftp_logging_error, request->user_data, | |
| 1788 _("Cannot connect to %s: %s\n"), | |
| 1789 connect_host, g_strerror (errno)); | |
| 1790 } | |
| 1791 break; | |
| 1792 } | |
| 1793 | |
| 1794 if (request->host.h_addr_list[curhost] == NULL) | |
| 1795 { | |
| 1796 close (sock); | |
| 1797 return (-1); | |
| 1798 } | |
| 1799 port = ntohs (port); | |
| 1800 #endif /* HAVE_GETADDRINFO */ | |
| 1801 | |
| 1802 request->logging_function (gftp_logging_misc, request->user_data, | |
| 1803 _("Connected to %s:%d\n"), connect_host, port); | |
| 58 | 1804 |
| 1805 if (gftp_set_sockblocking (request, sock, 1) == -1) | |
| 1806 { | |
| 1807 close (sock); | |
| 1808 return (-1); | |
| 1809 } | |
| 1810 | |
| 1 | 1811 return (sock); |
| 1812 } | |
| 1813 | |
| 1814 | |
| 1815 void | |
| 1816 gftp_set_config_options (gftp_request * request) | |
| 1817 { | |
| 1818 request->network_timeout = network_timeout; | |
| 1819 request->retries = retries; | |
| 1820 request->sleep_time = sleep_time; | |
| 1821 request->maxkbs = maxkbs; | |
| 1822 | |
| 58 | 1823 if (request->set_config_options != NULL) |
| 1824 request->set_config_options (request); | |
| 1 | 1825 } |
| 1826 | |
| 1827 | |
| 1828 void | |
| 1829 gftp_set_sftpserv_path (gftp_request * request, char *path) | |
| 1830 { | |
| 1831 g_return_if_fail (request != NULL); | |
| 1832 | |
| 1833 if (request->sftpserv_path) | |
| 1834 g_free (request->sftpserv_path); | |
| 1835 | |
| 1836 if (path != NULL && *path != '\0') | |
| 1837 { | |
| 1838 request->sftpserv_path = g_malloc (strlen (path) + 1); | |
| 1839 strcpy (request->sftpserv_path, path); | |
| 1840 } | |
| 1841 else | |
| 1842 request->sftpserv_path = NULL; | |
| 1843 } | |
| 1844 | |
| 1845 | |
| 1846 void | |
| 1847 print_file_list (GList * list) | |
| 1848 { | |
| 1849 gftp_file * tempfle; | |
| 1850 GList * templist; | |
| 1851 | |
| 1852 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
| 1853 for (templist = list; ; templist = templist->next) | |
| 1854 { | |
| 1855 tempfle = templist->data; | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1856 #if defined (_LARGEFILE_SOURCE) |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1857 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
|
1858 #else |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1859 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
|
1860 #endif |
| 16 | 1861 tempfle->file, tempfle->destfile, |
| 1862 tempfle->size, tempfle->startsize, | |
| 1863 tempfle->user, tempfle->group, tempfle->attribs); | |
| 1 | 1864 if (templist->next == NULL) |
| 1865 break; | |
| 1866 } | |
| 1867 | |
| 1868 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
| 1869 for (; ; templist = templist->prev) | |
| 1870 { | |
| 1871 tempfle = templist->data; | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1872 #if defined (_LARGEFILE_SOURCE) |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1873 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
|
1874 #else |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1875 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
|
1876 #endif |
| 16 | 1877 tempfle->file, tempfle->destfile, |
| 1878 tempfle->size, tempfle->startsize, | |
| 1879 tempfle->user, tempfle->group, tempfle->attribs); | |
| 1 | 1880 if (templist == list) |
| 1881 break; | |
| 1882 } | |
| 1883 printf ("--END OF FILE LISTING--\n"); | |
| 1884 } | |
| 1885 | |
| 41 | 1886 |
| 58 | 1887 static void |
| 1888 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) | |
| 41 | 1889 { |
| 58 | 1890 g_free ((*rbuf)->buffer); |
| 1891 g_free (*rbuf); | |
| 1892 *rbuf = NULL; | |
| 41 | 1893 } |
| 1894 | |
| 1895 | |
| 58 | 1896 ssize_t |
| 1897 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
| 1898 char * str, size_t len, int fd) | |
| 41 | 1899 { |
| 58 | 1900 ssize_t ret, retval, rlen, copysize; |
| 1901 char *pos, *nextpos; | |
| 1902 | |
| 1903 if (*rbuf == NULL) | |
| 1904 { | |
| 1905 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
| 1906 (*rbuf)->max_bufsize = len; | |
| 60 | 1907 (*rbuf)->buffer = g_malloc ((*rbuf)->max_bufsize + 1); |
| 58 | 1908 if ((ret = gftp_read (request, (*rbuf)->buffer, (*rbuf)->max_bufsize, |
| 1909 fd)) <= 0) | |
| 1910 { | |
| 1911 gftp_free_getline_buffer (rbuf); | |
| 1912 return (ret); | |
| 1913 } | |
| 60 | 1914 (*rbuf)->buffer[ret] = '\0'; |
| 58 | 1915 (*rbuf)->cur_bufsize = ret; |
| 1916 (*rbuf)->curpos = (*rbuf)->buffer; | |
| 1917 } | |
| 1918 | |
| 1919 retval = -2; | |
| 1920 | |
| 1921 do | |
| 1922 { | |
| 1923 if ((*rbuf)->cur_bufsize > 0 && | |
| 1924 ((pos = strchr ((*rbuf)->curpos, '\n')) != NULL || | |
| 1925 ((*rbuf)->curpos == (*rbuf)->buffer && | |
| 1926 (*rbuf)->max_bufsize == (*rbuf)->cur_bufsize))) | |
| 1927 { | |
| 1928 if (pos != NULL) | |
| 1929 { | |
| 1930 nextpos = pos + 1; | |
| 1931 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
| 1932 pos--; | |
| 1933 *pos = '\0'; | |
| 1934 } | |
| 1935 else | |
| 60 | 1936 nextpos = NULL; |
| 58 | 1937 |
| 1938 strncpy (str, (*rbuf)->curpos, len); | |
| 60 | 1939 retval = pos - (*rbuf)->curpos > len ? len : pos - (*rbuf)->curpos; |
| 58 | 1940 |
| 1941 if (pos != NULL) | |
| 1942 { | |
| 1943 if (nextpos - (*rbuf)->buffer >= (*rbuf)->cur_bufsize) | |
| 1944 (*rbuf)->cur_bufsize = 0; | |
| 1945 else | |
| 1946 (*rbuf)->curpos = nextpos; | |
| 1947 } | |
| 1948 | |
| 1949 break; | |
| 1950 } | |
| 1951 else | |
| 1952 { | |
| 1953 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
| 1954 { | |
| 1955 rlen = (*rbuf)->max_bufsize; | |
| 1956 pos = (*rbuf)->buffer; | |
| 1957 copysize = 0; | |
| 1958 } | |
| 1959 else | |
| 1960 { | |
| 1961 copysize = (*rbuf)->cur_bufsize - ((*rbuf)->curpos - (*rbuf)->buffer); | |
| 1962 memmove ((*rbuf)->buffer, (*rbuf)->curpos, copysize); | |
| 1963 pos = (*rbuf)->buffer + copysize; | |
| 1964 rlen = (*rbuf)->max_bufsize - copysize; | |
| 1965 } | |
| 1966 (*rbuf)->curpos = (*rbuf)->buffer; | |
| 1967 | |
| 1968 if ((ret = gftp_read (request, pos, rlen, fd)) <= 0) | |
| 1969 { | |
| 1970 gftp_free_getline_buffer (rbuf); | |
| 1971 return (ret); | |
| 1972 } | |
| 60 | 1973 (*rbuf)->buffer[ret + copysize] = '\0'; |
| 58 | 1974 (*rbuf)->cur_bufsize = ret + copysize; |
| 1975 } | |
| 1976 } | |
| 1977 while (retval == -2); | |
| 1978 | |
| 1979 return (retval); | |
| 1980 } | |
| 1981 | |
| 1982 | |
| 1983 ssize_t | |
| 1984 gftp_read (gftp_request * request, void *ptr, size_t size, int fd) | |
| 1985 { | |
| 1986 struct timeval tv; | |
| 1987 fd_set fset; | |
| 1988 ssize_t ret; | |
| 41 | 1989 |
| 1990 errno = 0; | |
| 1991 ret = 0; | |
| 1992 do | |
| 1993 { | |
| 58 | 1994 FD_ZERO (&fset); |
| 1995 FD_SET (fd, &fset); | |
| 1996 if (request != NULL) | |
| 1997 tv.tv_sec = request->network_timeout; | |
| 1998 else | |
| 1999 tv.tv_sec = network_timeout; | |
| 2000 tv.tv_usec = 0; | |
| 2001 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
| 2002 if (ret == -1 && errno == EINTR) | |
| 41 | 2003 { |
| 58 | 2004 if (request && request->cancel) |
| 2005 break; | |
| 2006 else | |
| 2007 continue; | |
| 2008 } | |
| 2009 else if (ret <= 0) | |
| 2010 { | |
| 2011 if (request != NULL) | |
| 41 | 2012 { |
| 58 | 2013 request->logging_function (gftp_logging_error, request->user_data, |
| 2014 _("Connection to %s timed out\n"), | |
| 2015 request->hostname); | |
| 2016 gftp_disconnect (request); | |
| 2017 } | |
| 41 | 2018 return (-1); |
| 2019 } | |
| 2020 | |
| 58 | 2021 if ((ret = read (fd, ptr, size)) < 0) |
| 41 | 2022 { |
| 2023 if (errno == EINTR) | |
| 2024 { | |
| 58 | 2025 if (request != NULL && request->cancel) |
| 41 | 2026 break; |
| 2027 else | |
| 58 | 2028 continue; |
| 41 | 2029 } |
| 2030 | |
| 58 | 2031 if (request != NULL) |
| 2032 { | |
| 2033 request->logging_function (gftp_logging_error, request->user_data, | |
| 2034 _("Error: Could not read from socket: %s\n"), | |
| 41 | 2035 g_strerror (errno)); |
| 58 | 2036 gftp_disconnect (request); |
| 2037 } | |
| 41 | 2038 return (-1); |
| 2039 } | |
| 2040 } | |
| 58 | 2041 while (errno == EINTR && !(request != NULL && request->cancel)); |
| 41 | 2042 |
| 58 | 2043 if (errno == EINTR && request != NULL && request->cancel) |
| 41 | 2044 { |
| 2045 gftp_disconnect (request); | |
| 2046 return (-1); | |
| 2047 } | |
| 2048 | |
| 2049 return (ret); | |
| 2050 } | |
| 2051 | |
| 58 | 2052 |
| 2053 ssize_t | |
| 2054 gftp_write (gftp_request * request, const char *ptr, size_t size, int fd) | |
| 2055 { | |
| 2056 struct timeval tv; | |
| 2057 size_t ret, w_ret; | |
| 2058 fd_set fset; | |
| 2059 | |
| 2060 errno = 0; | |
| 2061 ret = 0; | |
| 2062 do | |
| 2063 { | |
| 2064 FD_ZERO (&fset); | |
| 2065 FD_SET (fd, &fset); | |
| 2066 if (request != NULL) | |
| 2067 tv.tv_sec = request->network_timeout; | |
| 2068 else | |
| 2069 tv.tv_sec = network_timeout; | |
| 2070 tv.tv_usec = 0; | |
| 2071 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
| 2072 if (ret == -1 && errno == EINTR) | |
| 2073 { | |
| 2074 if (request != NULL && request->cancel) | |
| 2075 break; | |
| 2076 else | |
| 2077 continue; | |
| 2078 } | |
| 2079 else if (ret <= 0) | |
| 2080 { | |
| 2081 if (request != NULL) | |
| 2082 { | |
| 2083 request->logging_function (gftp_logging_error, request->user_data, | |
| 2084 _("Connection to %s timed out\n"), | |
| 2085 request->hostname); | |
| 2086 gftp_disconnect (request); | |
| 2087 } | |
| 2088 return (-1); | |
| 2089 } | |
| 2090 | |
| 2091 if ((w_ret = write (fd, ptr, size)) < 0) | |
| 2092 { | |
| 2093 if (errno == EINTR) | |
| 2094 { | |
| 2095 if (request != NULL && request->cancel) | |
| 2096 break; | |
| 2097 else | |
| 2098 continue; | |
| 2099 } | |
| 2100 | |
| 2101 if (request != NULL) | |
| 2102 { | |
| 2103 request->logging_function (gftp_logging_error, request->user_data, | |
| 2104 _("Error: Could not write to socket: %s\n"), | |
| 2105 g_strerror (errno)); | |
| 2106 gftp_disconnect (request); | |
| 2107 } | |
| 2108 return (-1); | |
| 2109 } | |
| 2110 | |
| 2111 ptr += w_ret; | |
| 2112 size -= w_ret; | |
| 2113 ret += w_ret; | |
| 2114 } | |
| 2115 while (size > 0); | |
| 2116 | |
| 2117 if (errno == EINTR && request != NULL && request->cancel) | |
| 2118 { | |
| 2119 gftp_disconnect (request); | |
| 2120 return (-1); | |
| 2121 } | |
| 2122 | |
| 2123 return (ret); | |
| 2124 } | |
| 2125 | |
| 2126 | |
| 2127 ssize_t | |
| 2128 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
| 2129 { | |
| 2130 char *tempstr; | |
| 2131 va_list argp; | |
| 2132 ssize_t ret; | |
| 2133 | |
| 2134 va_start (argp, fmt); | |
| 2135 tempstr = g_strdup_vprintf (fmt, argp); | |
| 2136 va_end (argp); | |
| 2137 | |
| 2138 ret = gftp_write (request, tempstr, strlen (tempstr), fd); | |
| 2139 g_free (tempstr); | |
| 2140 return (ret); | |
| 2141 } | |
| 2142 | |
| 2143 | |
| 2144 int | |
| 2145 gftp_set_sockblocking (gftp_request * request, int fd, int non_blocking) | |
| 2146 { | |
| 2147 int flags; | |
| 2148 | |
| 2149 if ((flags = fcntl (fd, F_GETFL, 0)) == -1) | |
| 2150 { | |
| 2151 request->logging_function (gftp_logging_error, request->user_data, | |
| 2152 _("Cannot get socket flags: %s\n"), | |
| 2153 g_strerror (errno)); | |
| 2154 gftp_disconnect (request); | |
| 2155 return (-1); | |
| 2156 } | |
| 2157 | |
| 2158 if (non_blocking) | |
| 2159 flags |= O_NONBLOCK; | |
| 2160 else | |
| 2161 flags &= ~O_NONBLOCK; | |
| 2162 | |
| 2163 if (fcntl (fd, F_SETFL, flags) == -1) | |
| 2164 { | |
| 2165 request->logging_function (gftp_logging_error, request->user_data, | |
| 2166 _("Cannot set socket to non-blocking: %s\n"), | |
| 2167 g_strerror (errno)); | |
| 2168 gftp_disconnect (request); | |
| 2169 return (-1); | |
| 2170 } | |
| 2171 | |
| 2172 return (0); | |
| 2173 } | |
| 2174 | |
| 2175 | |
| 63 | 2176 void |
| 2177 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
| 2178 { | |
| 2179 g_return_if_fail (dest != NULL); | |
| 2180 g_return_if_fail (source != NULL); | |
| 2181 g_return_if_fail (dest->protonum == source->protonum); | |
| 2182 | |
| 2183 dest->sockfd = source->sockfd; | |
| 2184 dest->datafd = source->datafd; | |
| 2185 dest->cached = 0; | |
| 2186 if (!source->always_connected) | |
| 2187 { | |
| 2188 source->sockfd = -1; | |
| 2189 source->datafd = -1; | |
| 2190 source->cached = 1; | |
| 2191 } | |
| 2192 | |
| 2193 if (dest->swap_socks) | |
| 2194 dest->swap_socks (dest, source); | |
| 2195 } | |
| 2196 |
