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