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