summaryrefslogtreecommitdiff
path: root/daemon/gvfsftptask.c
blob: 15de4ce468624af0491f42eeb347d66fe72339de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2009 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Benjamin Otte <otte@gnome.org>
 */

#include <config.h>

#include <stdio.h> /* for sscanf() */
#include <stdlib.h> /* for exit() */

#include <glib/gi18n.h>

#include "gvfsftptask.h"

/*** DOCS ***/

/**
 * GVfsFtpResponseFlags:
 * @G_VFS_FTP_PASS_100: Don't treat 1XX responses, but return them
 * @G_VFS_FTP_PASS_300: Don't treat 3XX responses, but return them
 * @G_VFS_FTP_PASS_400: Don't treat 4XX responses, but return them
 * @G_VFS_FTP_PASS_500: Don't treat 5XX responses, but return them
 * @G_VFS_FTP_PASS_550: Don't treat 550 responses, but return them
 * @G_VFS_FTP_FAIL_200: Fail on a 2XX response
 *
 * These flags can be passed to gvfs_ftp_task_receive() (and in
 * turn gvfs_ftp_task_send()) to influence the behavior of the functions.
 */

/**
 * G_VFS_FTP_G_VFS_FTP_GROUP:
 * @response: a valid ftp response
 *
 * Determines the group the given @response belongs to. The group is the first
 * digit of the reply.
 *
 * Returns: The group the response code belonged to from 1-5
 */

/**
 * G_VFS_FTP_TASK_INIT:
 * @backend: the backend used by this task
 * @job: the job that initiated the task or %NULL if none
 *
 * Initializes a new task structure for the given backend and job.
 */

/**
 * GVfsFtpErrorFunc:
 * @task: task to handle
 * @data: data argument provided to g_vfs_ftp_task_send_and_check()
 *
 * Function prototype for error checking functions used by
 * g_vfs_ftp_task_send_and_check(). When called, these functions are supposed
 * to check a specific error condition and if met, set an error on the passed
 * @task.
 */

/*** CODE ***/

gboolean
g_vfs_ftp_task_login (GVfsFtpTask *task,
                      const char * username,
                      const char * password)
{
  guint status;

  g_return_val_if_fail (task != NULL, FALSE);
  g_return_val_if_fail (username != NULL, FALSE);
  g_return_val_if_fail (password != NULL, FALSE);

  if (g_vfs_ftp_task_is_in_error (task))
    return FALSE;

  status = g_vfs_ftp_task_send (task, G_VFS_FTP_PASS_300,
                                "USER %s", username);
 
  if (G_VFS_FTP_RESPONSE_GROUP (status) == 3)
    {
      /* rationale for choosing the default password:
       * - some ftp servers expect something that looks like an email address
       * - we don't want to send the user's name or address, as that would be
       *   a privacy problem
       * - we want to give ftp server administrators a chance to notify us of
       *   problems with our client.
       * - we don't want to drown in spam.
       */
      if (password == NULL || password[0] == 0)
        password = "gvfsd-ftp-" VERSION "@example.com";
      status = g_vfs_ftp_task_send (task, 0,
        			    "PASS %s", password);
    }

  return status;
}

/**
 * g_vfs_ftp_task_setup_connection:
 * @task: the task
 *
 * Sends all commands necessary to put the connection into a usable state,
 * like setting the transfer mode to binary. Note that passive mode will
 * will be set on a case-by-case basis when opening a data connection.
 **/
void
g_vfs_ftp_task_setup_connection (GVfsFtpTask *task)
{
  g_return_if_fail (task != NULL);

  /* only binary transfers please */
  g_vfs_ftp_task_send (task, 0, "TYPE I");
  if (g_vfs_ftp_task_is_in_error (task))
    return;

#if 0
  /* RFC 2428 suggests to send this to make NAT routers happy */
  /* XXX: Disabled for the following reasons:
   * - most ftp clients don't use it
   * - lots of broken ftp servers can't see the difference between
   *   "EPSV" and "EPSV ALL"
   * - impossible to dynamically fall back to regular PASV in case
   *   EPSV doesn't work for some reason.
   * If this makes your ftp connection fail, please file a bug and we will
   * try to invent a way to make this all work. Until then, we'll just
   * ignore the RFC.
   */
  if (g_vfs_backend_ftp_has_feature (task->backend, g_VFS_FTP_FEATURE_EPSV))
    g_vfs_ftp_task_send (task, 0, "EPSV ALL");
  g_vfs_ftp_task_clear_error (task);
#endif

  /* instruct server that we'll give and assume we get utf8 */
  if (g_vfs_backend_ftp_has_feature (task->backend, G_VFS_FTP_FEATURE_UTF8))
    {
      if (!g_vfs_ftp_task_send (task, 0, "OPTS UTF8 ON"))
        g_vfs_ftp_task_clear_error (task);
    }
}


static void
do_broadcast (GCancellable *cancellable, GCond *cond)
{
  g_cond_broadcast (cond);
}

/**
 * g_vfs_ftp_task_acquire_connection:
 * @task: a task without an associated connection
 *
 * Acquires a new connection for use by this @task. This uses the connection
 * pool of @task's backend, so it reuses previously opened connections and
 * does not reopen new connections unnecessarily. If all connections are busy,
 * it waits %G_VFS_FTP_TIMEOUT_IN_SECONDS seconds for a new connection to
 * become available. Keep in mind that a newly acquired connection might have
 * timed out and therefore closed by the FTP server. You must account for
 * this when sending the first command to the server.
 *
 * Returns: %TRUE if a connection could be acquired, %FALSE if an error
 *          occured
 **/
static gboolean
g_vfs_ftp_task_acquire_connection (GVfsFtpTask *task)
{
  GVfsBackendFtp *ftp;
  gint64 end_time;
  gulong id;

  g_return_val_if_fail (task != NULL, FALSE);
  g_return_val_if_fail (task->conn == NULL, FALSE);

  if (g_vfs_ftp_task_is_in_error (task))
    return FALSE;

  ftp = task->backend;
  g_mutex_lock (&ftp->mutex);
  id = g_cancellable_connect (task->cancellable,
        		      G_CALLBACK (do_broadcast),
        		      &ftp->cond, NULL);
  while (task->conn == NULL && ftp->queue != NULL)
    {
      if (g_cancellable_is_cancelled (task->cancellable))
        {
          task->error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED,
        		                     _("Operation was cancelled"));
          break;
        }

      task->conn = g_queue_pop_head (ftp->queue);
      if (task->conn != NULL)
        break;

      if (ftp->connections < ftp->max_connections)
        {
          static GThread *last_thread = NULL;
          /* Save current number of connections here, so we can limit maximum
           * connections later.
           * This is necessary for threading reasons (connections can be
           * opened or closed while we are still in the opening process. */
          guint maybe_max_connections = ftp->connections;

          ftp->connections++;
          last_thread = g_thread_self ();
          g_mutex_unlock (&ftp->mutex);
          task->conn = g_vfs_ftp_connection_new (ftp->addr, task->cancellable, &task->error);
          if (G_LIKELY (task->conn != NULL))
            {
              g_vfs_ftp_task_receive (task, 0, NULL);
              g_vfs_ftp_task_login (task, ftp->user, ftp->password);
              g_vfs_ftp_task_setup_connection (task);
              if (G_LIKELY (!g_vfs_ftp_task_is_in_error (task)))
                break;
            }

          g_vfs_ftp_connection_free (task->conn);
          task->conn = NULL;
          g_mutex_lock (&ftp->mutex);
          ftp->connections--;
          /* If this value is still equal to our thread it means there were no races 
           * trying to open connections and the maybe_max_connections value is 
           * reliable. */
          if (last_thread == g_thread_self () && 
              !g_vfs_ftp_task_error_matches (task, G_IO_ERROR, G_IO_ERROR_CANCELLED))
            {
              g_print ("maybe: %u, max %u (due to %s)\n", maybe_max_connections, ftp->max_connections, task->error->message);
              ftp->max_connections = MIN (ftp->max_connections, maybe_max_connections);
              if (ftp->max_connections == 0)
                {
                  g_debug ("no more connections left, exiting...\n");
                  /* FIXME: shut down properly */
                  exit (0);
                }
            }

          g_vfs_ftp_task_clear_error (task);
          continue;
        }

      end_time = g_get_monotonic_time () + G_VFS_FTP_TIMEOUT_IN_SECONDS * G_TIME_SPAN_SECOND;
      if (ftp->busy_connections >= ftp->connections ||
          !g_cond_wait_until (&ftp->cond, &ftp->mutex, end_time))
        {
          task->error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_BUSY,
        		                     _("The FTP server is busy. Try again later"));
          break;
        }
    }
  g_cancellable_disconnect (task->cancellable, id);
  g_mutex_unlock (&ftp->mutex);

  return task->conn != NULL;
}

/**
 * g_vfs_ftp_task_release_connection:
 * @task: a task
 *
 * Releases the connection in use by @task to the backend's connection pool,
 * or frees it if it is in an error state. You must use this function to free
 * a @task's connection, never use g_vfs_ftp_connection_free() directly. If
 * the task does not have a current connection, this function just returns.
 **/
static void
g_vfs_ftp_task_release_connection (GVfsFtpTask *task)
{
  g_return_if_fail (task != NULL);

  /* we allow task->conn == NULL to ease error cases */
  if (task->conn == NULL)
    return;

  g_mutex_lock (&task->backend->mutex);
  if (task->backend->queue && g_vfs_ftp_connection_is_usable (task->conn))
    {
      g_queue_push_tail (task->backend->queue, task->conn);
      g_cond_signal (&task->backend->cond);
    }
  else
    {
      task->backend->connections--;
      g_vfs_ftp_connection_free (task->conn);
    }
  g_mutex_unlock (&task->backend->mutex);
  task->conn = NULL;
}

/**
 * g_vfs_ftp_task_done:
 * @task: the task to finalize
 *
 * Finalizes the given task and clears all memory in use. It also marks the
 * associated job as success or failure depending on the error state of the
 * task.
 **/
void
g_vfs_ftp_task_done (GVfsFtpTask *task)
{
  g_return_if_fail (task != NULL);

  g_vfs_ftp_task_release_connection (task);

  if (task->job)
    {
      if (g_vfs_ftp_task_is_in_error (task))
        g_vfs_job_failed_from_error (task->job, task->error);
      else
        g_vfs_job_succeeded (task->job);
    }

  g_vfs_ftp_task_clear_error (task);
}

/**
 * g_vfs_ftp_task_set_error_from_response:
 * @task: the task
 * @response: the response code
 *
 * Sets the @task into an error state. The exact error is determined from the
 * @response code.
 **/
void
g_vfs_ftp_task_set_error_from_response (GVfsFtpTask *task, guint response)
{
  const char *msg;
  int code;

  g_return_if_fail (task != NULL);
  g_return_if_fail (task->error == NULL);

  /* Please keep this list ordered by response code,
   * but group responses with the same message. */
  switch (response)
    {
      case 332: /* Need account for login. */
      case 532: /* Need account for storing files. */
        /* FIXME: implement a sane way to handle accounts. */
        code = G_IO_ERROR_NOT_SUPPORTED;
        msg = _("Accounts are unsupported");
        break;
      case 421: /* Service not available, closing control connection. */
        code = G_IO_ERROR_FAILED;
        msg = _("Host closed connection");
        break;
      case 425: /* Can't open data connection. */
        code = G_IO_ERROR_CLOSED;
        msg = _("Cannot open data connection. Maybe your firewall prevents this?");
        break;
      case 426: /* Connection closed; transfer aborted. */
        code = G_IO_ERROR_CLOSED;
        msg = _("Data connection closed");
        break;
      case 450: /* Requested file action not taken. File unavailable (e.g., file busy). */
      case 550: /* Requested action not taken. File unavailable (e.g., file not found, no access). */
        /* FIXME: This is a lot of different errors. So we have to pretend to
         * be smart here. */
        code = G_IO_ERROR_FAILED;
        msg = _("Operation failed");
        break;
      case 451: /* Requested action aborted: local error in processing. */
        code = G_IO_ERROR_FAILED;
        msg = _("Operation failed");
        break;
      case 452: /* Requested action not taken. Insufficient storage space in system. */
      case 552:
        code = G_IO_ERROR_NO_SPACE;
        msg = _("No space left on server");
        break;
      case 500: /* Syntax error, command unrecognized. */
      case 501: /* Syntax error in parameters or arguments. */
      case 502: /* Command not implemented. */
      case 503: /* Bad sequence of commands. */
      case 504: /* Command not implemented for that parameter. */
        code = G_IO_ERROR_NOT_SUPPORTED;
        msg = _("Operation unsupported");
        break;
      case 522: /* EPRT: unsupported network protocol */
        code = G_IO_ERROR_NOT_SUPPORTED;
        msg = _("Unsupported network protocol");
        break;
      case 530: /* Not logged in. */
        code = G_IO_ERROR_PERMISSION_DENIED;
        msg = _("Permission denied");
        break;
      case 551: /* Requested action aborted: page type unknown. */
        code = G_IO_ERROR_FAILED;
        msg = _("Page type unknown");
        break;
      case 553: /* Requested action not taken. File name not allowed. */
        code = G_IO_ERROR_INVALID_FILENAME;
        msg = _("Invalid filename");
        break;
      default:
        code = G_IO_ERROR_FAILED;
        msg = _("Invalid reply");
        break;
    }

  g_set_error_literal (&task->error, G_IO_ERROR, code, msg);
}

/**
 * g_vfs_ftp_task_give_connection:
 * @task: the task
 * @conn: the connection that the @task should use
 *
 * Forces a given @task to do I/O using the given connection. The @task must
 * not have a connection associated with itself. The @task will take
 * ownership of @conn.
 **/
void
g_vfs_ftp_task_give_connection (GVfsFtpTask *      task,
                                GVfsFtpConnection *conn)
{
  g_return_if_fail (task != NULL);
  g_return_if_fail (task->conn == NULL);

  task->conn = conn;
  /* this connection is not busy anymore */
  g_mutex_lock (&task->backend->mutex);
  g_assert (task->backend->busy_connections > 0);
  task->backend->busy_connections--;
  g_mutex_unlock (&task->backend->mutex);
}

/**
 * g_vfs_ftp_task_take_connection:
 * @task: the task
 *
 * Acquires the connection in use by the @task, so it can later be used with
 * g_vfs_ftp_task_give_connection(). This or any other task will not use the
 * connection anymore. The @task must have a connection in use.
 *
 * Returns: The connection that @task was using. You acquire ownership of
 *          the connection.
 **/
GVfsFtpConnection *
g_vfs_ftp_task_take_connection (GVfsFtpTask *task)
{
  GVfsFtpConnection *conn;
  GVfsBackendFtp *ftp;

  g_return_val_if_fail (task != NULL, NULL);
  g_return_val_if_fail (task->conn != NULL, NULL);

  conn = task->conn;
  task->conn = NULL;

  ftp = task->backend;
  /* mark this connection as busy */
  g_mutex_lock (&ftp->mutex);
  ftp->busy_connections++;
  /* if all connections are busy, signal all waiting threads, 
   * so they stop waiting and return BUSY earlier */
  if (ftp->busy_connections >= ftp->connections)
    g_cond_broadcast (&ftp->cond);
  g_mutex_unlock (&ftp->mutex);

  return conn;
}

/**
 * g_vfs_ftp_task_send:
 * @task: the sending task
 * @flags: response flags to use when sending
 * @format: format string to construct command from
 *          (without trailing \r\n)
 * @...: arguments to format string
 *
 * Shortcut to calling g_vfs_ftp_task_send_and_check() with the reply, funcs
 * and data arguments set to %NULL. See that function for details.
 *
 * Returns: 0 on error or the received FTP code otherwise.
 **/
guint
g_vfs_ftp_task_send (GVfsFtpTask *        task,
                     GVfsFtpResponseFlags flags,
                     const char *         format,
                     ...)
{
  va_list varargs;
  guint response;

  g_return_val_if_fail (task != NULL, 0);
  g_return_val_if_fail (format != NULL, 0);

  va_start (varargs, format);
  response = g_vfs_ftp_task_sendv (task,
        			   flags,
                                   NULL,
        			   format,
        			   varargs);
  va_end (varargs);
  return response;
}

/**
 * g_vfs_ftp_task_send_and_check:
 * @task: the sending task
 * @flags: response flags to use when sending
 * @funcs: %NULL or %NULL-terminated array of functions used to determine the
 *         exact failure case upon a "550 Operation Failed" reply. This is
 *         often necessary
 * @data: data to pass to @funcs.
 * @reply: %NULL or pointer to take a char array containing the full reply of
 *         the ftp server upon successful reply. Use g_strfreev() to free
 *         after use.
 * @format: format string to construct command from
 *          (without trailing \r\n)
 * @...: arguments to format string
 *
 * Takes an ftp command in printf-style @format, potentially acquires a
 * connection automatically, sends the command and waits for an answer from
 * the ftp server. Without any @flags, FTP response codes other than 2xx cause
 * an error. If @reply is not %NULL, the full reply will be put into a
 * %NULL-terminated string array that must be freed with g_strfreev() after
 * use.
 * If @funcs is set, the 550 response code will cause all of these functions to
 * be called in order passing them the @task and @data arguments given to this
 * function until one of them sets an error on @task. This error will then be
 * returned from this function. If none of those functions sets an error, the
 * generic error for the 550 response will be used.
 * If an error has been set on @task previously, this function will do nothing.
 *
 * Returns: 0 on error or the received FTP code otherwise.
 **/
guint
g_vfs_ftp_task_send_and_check (GVfsFtpTask *           task,
                               GVfsFtpResponseFlags    flags,
                               const GVfsFtpErrorFunc *funcs,
                               gpointer                data,
                               char ***                reply,
                               const char *            format,
                               ...)
{
  va_list varargs;
  guint response;

  g_return_val_if_fail (task != NULL, 0);
  g_return_val_if_fail (format != NULL, 0);
  g_return_val_if_fail (funcs == NULL || funcs[0] != NULL, 0);

  if (funcs)
    {
      g_return_val_if_fail ((flags & G_VFS_FTP_PASS_550) == 0, 0);
      flags |= G_VFS_FTP_PASS_550;
    }

  va_start (varargs, format);
  response = g_vfs_ftp_task_sendv (task,
        			   flags,
                                   reply,
        			   format,
        			   varargs);
  va_end (varargs);

  if (response == 550 && funcs)
    {
      /* close a potentially open data connection, the error handlers
       * might try to open new ones and that would cause assertions */
      g_vfs_ftp_task_close_data_connection (task);

      while (*funcs && !g_vfs_ftp_task_is_in_error (task))
        {
          (*funcs) (task, data);
          funcs++;
        }
      if (!g_vfs_ftp_task_is_in_error (task))
          g_vfs_ftp_task_set_error_from_response (task, response);
      response = 0;
    }

  return response;
}

/**
 * g_vfs_ftp_task_sendv:
 * @task: the sending task
 * @flags: response flags to use when receiving the reply
 * @reply: %NULL or pointer to char array that takes the full reply from the
 *         server
 * @format: format string to construct command from
 *          (without trailing \r\n)
 * @varargs: arguments to format string
 *
 * This is the varargs version of g_vfs_ftp_task_send(). See that function
 * for details.
 *
 * Returns: the received FTP code or 0 on error.
 **/
guint
g_vfs_ftp_task_sendv (GVfsFtpTask *          task,
                      GVfsFtpResponseFlags   flags,
                      char ***               reply,
                      const char *           format,
                      va_list                varargs)
{
  GString *command;
  gboolean retry_on_timeout = FALSE;
  guint response;

  if (g_vfs_ftp_task_is_in_error (task))
    return 0;

  command = g_string_new ("");
  g_string_append_vprintf (command, format, varargs);
  g_string_append (command, "\r\n");

retry:
  if (task->conn == NULL)
    {
      if (!g_vfs_ftp_task_acquire_connection (task))
        {
          g_string_free (command, TRUE);
          return 0;
        }
      retry_on_timeout = TRUE;
    }

  g_vfs_ftp_connection_send (task->conn,
                             command->str,
                             command->len,
                             task->cancellable,
                             &task->error);

  response = g_vfs_ftp_task_receive (task, flags, reply);
 
  /* NB: requires adaption if we allow passing 4xx responses */
  if (retry_on_timeout &&
      g_vfs_ftp_task_is_in_error (task) &&
      !g_vfs_ftp_connection_is_usable (task->conn))
    {
      g_vfs_ftp_task_clear_error (task);
      g_vfs_ftp_task_release_connection (task);
      goto retry;
    }

  g_string_free (command, TRUE);
  return response;
}

/**
 * g_vfs_ftp_task_receive:
 * @task: the receiving task
 * @flags: response flags to use
 * @reply: %NULL or pointer to char array that takes the full reply from the
 *         server
 *
 * Unless @task is in an error state, this function receives a reply from
 * the @task's connection. The @task must have a connection set, which will
 * happen when either g_vfs_ftp_task_send() or
 * g_vfs_ftp_task_give_connection() have been called on the @task before.
 * Unless @flags are given, all reply codes not in the 200s cause an error.
 * If @task is in an error state when calling this function, nothing will
 * happen and the function will just return.
 *
 * Returns: the received FTP code or 0 on error.
 **/
guint
g_vfs_ftp_task_receive (GVfsFtpTask *        task,
                        GVfsFtpResponseFlags flags,
                        char ***             reply)
{
  guint response;

  g_return_val_if_fail (task != NULL, 0);
  if (g_vfs_ftp_task_is_in_error (task))
    return 0;
  g_return_val_if_fail (task->conn != NULL, 0);

  response = g_vfs_ftp_connection_receive (task->conn,
                                           reply,
                                           task->cancellable,
                                           &task->error);

  switch (G_VFS_FTP_RESPONSE_GROUP (response))
    {
      case 0:
        return 0;
      case 1:
        if (flags & G_VFS_FTP_PASS_100)
          break;
        g_vfs_ftp_task_set_error_from_response (task, response);
        break;
      case 2:
        if (flags & G_VFS_FTP_FAIL_200)
          g_vfs_ftp_task_set_error_from_response (task, response);
        break;
      case 3:
        if (flags & G_VFS_FTP_PASS_300)
          break;
        g_vfs_ftp_task_set_error_from_response (task, response);
        break;
      case 4:
        g_vfs_ftp_task_set_error_from_response (task, response);
        break;
      case 5:
        if ((flags & G_VFS_FTP_PASS_500) ||
            (response == 550 && (flags & G_VFS_FTP_PASS_550)))
          break;
        g_vfs_ftp_task_set_error_from_response (task, response);
        break;
      default:
        g_assert_not_reached ();
        break;
    }

  if (g_vfs_ftp_task_is_in_error (task))
    {
      if (response != 0 && reply)
        {
          g_strfreev (*reply);
          *reply = NULL;
        }
      response = 0;
    }

  return response;
}

/**
 * g_vfs_ftp_task_close_data_connection:
 * @task: a task potentially having an open data connection
 *
 * Closes any data connection @task might have opened.
 */
void
g_vfs_ftp_task_close_data_connection (GVfsFtpTask *task)
{
  g_return_if_fail (task != NULL);

  if (task->conn == NULL)
    return;

  g_vfs_ftp_connection_close_data_connection (task->conn);
}

static GSocketAddress *
g_vfs_ftp_task_create_remote_address (GVfsFtpTask *task, guint port)
{
  GSocketAddress *old, *new;

  old = g_vfs_ftp_connection_get_address (task->conn, &task->error);
  if (old == NULL)
    return NULL;
  g_assert (G_IS_INET_SOCKET_ADDRESS (old));
  new = g_inet_socket_address_new (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (old)), port);

  return new;
}

static GVfsFtpMethod
g_vfs_ftp_task_setup_data_connection_epsv (GVfsFtpTask *task, GVfsFtpMethod method)
{
  const char *s;
  char **reply;
  guint port;
  GSocketAddress *addr;
  guint status;
  gboolean success;

  g_assert (task->error == NULL);

  status = g_vfs_ftp_task_send_and_check (task, G_VFS_FTP_PASS_500, NULL, NULL, &reply, "EPSV");
  if (G_VFS_FTP_RESPONSE_GROUP (status) != 2)
    return G_VFS_FTP_METHOD_ANY;

  /* FIXME: parse multiple lines? */
  s = strrchr (reply[0], '(');
  if (!s)
    goto fail;

  s += 4;
  port = strtoul (s, NULL, 10);
  if (port == 0)
    goto fail;

  g_strfreev (reply);
  addr = g_vfs_ftp_task_create_remote_address (task, port);
  if (addr == NULL)
    return G_VFS_FTP_METHOD_ANY;

  success = g_vfs_ftp_connection_open_data_connection (task->conn,
                                                       addr,
                                                       task->cancellable,
                                                       &task->error);
  g_object_unref (addr);
  return success ? G_VFS_FTP_METHOD_EPSV : G_VFS_FTP_METHOD_ANY;

fail:
  g_strfreev (reply);
  return G_VFS_FTP_METHOD_ANY;
}

static GVfsFtpMethod
g_vfs_ftp_task_setup_data_connection_pasv (GVfsFtpTask *task, GVfsFtpMethod method)
{
  guint ip1, ip2, ip3, ip4, port1, port2;
  char **reply;
  const char *s;
  GSocketAddress *addr;
  guint status;
  gboolean success;

  status = g_vfs_ftp_task_send_and_check (task, 0, NULL, NULL, &reply, "PASV");
  if (status == 0)
    return G_VFS_FTP_METHOD_ANY;

  /* parse response and try to find the address to connect to.
   * This code does the same as curl.
   */
  for (s = reply[0]; *s; s++)
    {
      if (sscanf (s, "%u,%u,%u,%u,%u,%u",
        	 &ip1, &ip2, &ip3, &ip4,
        	 &port1, &port2) == 6)
       break;
    }
  if (*s == 0)
    {
      g_strfreev (reply);
      g_set_error_literal (&task->error, G_IO_ERROR, G_IO_ERROR_FAILED,
        		   _("Invalid reply"));
      return G_VFS_FTP_METHOD_ANY;
    }
  g_strfreev (reply);

  if (method == G_VFS_FTP_METHOD_PASV || method == G_VFS_FTP_METHOD_ANY)
    {
      guint8 ip[4];
      GInetAddress *inet_addr;

      ip[0] = ip1;
      ip[1] = ip2;
      ip[2] = ip3;
      ip[3] = ip4;
      inet_addr = g_inet_address_new_from_bytes (ip, G_SOCKET_FAMILY_IPV4);
      addr = g_inet_socket_address_new (inet_addr, port1 << 8 | port2);
      g_object_unref (inet_addr);

      success = g_vfs_ftp_connection_open_data_connection (task->conn,
                                                           addr,
                                                           task->cancellable,
                                                           &task->error);
      g_object_unref (addr);
      if (success)
        return G_VFS_FTP_METHOD_PASV;
      if (g_vfs_ftp_task_is_in_error (task) && method != G_VFS_FTP_METHOD_ANY)
        return G_VFS_FTP_METHOD_ANY;

      g_vfs_ftp_task_clear_error (task);
    }

  if (method == G_VFS_FTP_METHOD_PASV_ADDR || method == G_VFS_FTP_METHOD_ANY)
    {
      /* Workaround code:
       * Various ftp servers aren't setup correctly when behind a NAT. They report
       * their own IP address (like 10.0.0.4) and not the address in front of the
       * NAT. But this is likely the same address that we connected to with our
       * command connetion. So if the address given by PASV fails, we fall back
       * to the address of the command stream.
       */
      addr = g_vfs_ftp_task_create_remote_address (task, port1 << 8 | port2);
      if (addr == NULL)
        return G_VFS_FTP_METHOD_ANY;
      success = g_vfs_ftp_connection_open_data_connection (task->conn,
                                                           addr,
                                                           task->cancellable,
                                                           &task->error);
      g_object_unref (addr);
      if (success)
        return G_VFS_FTP_METHOD_PASV_ADDR;
    }

  return G_VFS_FTP_METHOD_ANY;
}

static GVfsFtpMethod
g_vfs_ftp_task_setup_data_connection_eprt (GVfsFtpTask *task, GVfsFtpMethod unused)
{
  GSocketAddress *addr;
  guint status, port, family;
  char *ip_string;

  /* workaround for the task not having a connection yet */
  if (task->conn == NULL &&
      g_vfs_ftp_task_send (task, 0, "NOOP") == 0)
    return G_VFS_FTP_METHOD_ANY;

  addr = g_vfs_ftp_connection_listen_data_connection (task->conn, &task->error);
  if (addr == NULL)
    return G_VFS_FTP_METHOD_ANY;
  switch (g_socket_address_get_family (addr))
    {
      case G_SOCKET_FAMILY_IPV4:
        family = 1;
        break;
      case G_SOCKET_FAMILY_IPV6:
        family = 2;
        break;
      default:
        g_object_unref (addr);
        return G_VFS_FTP_METHOD_ANY;
    }

  ip_string = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (addr)));
  /* if this ever happens (and it must not for IP4 and IP6 addresses), 
   * we need to add support for using a different separator */
  g_assert (strchr (ip_string, '|') == NULL);
  port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));

  /* we could handle the 522 response here, (unsupported network family),
   * but I don't think that will buy us anything */
  status = g_vfs_ftp_task_send (task, 0, "EPRT |%u|%s|%u|", family, ip_string, port);
  g_free (ip_string);
  g_object_unref (addr);
  if (status == 0)
    return G_VFS_FTP_METHOD_ANY;
  
  return G_VFS_FTP_METHOD_EPRT;
}

static GVfsFtpMethod
g_vfs_ftp_task_setup_data_connection_port (GVfsFtpTask *task, GVfsFtpMethod unused)
{
  GSocketAddress *addr;
  guint status, i, port;
  char *ip_string;

  /* workaround for the task not having a connection yet */
  if (task->conn == NULL &&
      g_vfs_ftp_task_send (task, 0, "NOOP") == 0)
    return G_VFS_FTP_METHOD_ANY;

  addr = g_vfs_ftp_connection_listen_data_connection (task->conn, &task->error);
  if (addr == NULL)
    return G_VFS_FTP_METHOD_ANY;
  /* the PORT command only supports IPv4 */
  if (g_socket_address_get_family (addr) != G_SOCKET_FAMILY_IPV4)
    {
      g_object_unref (addr);
      return G_VFS_FTP_METHOD_ANY;
    }

  ip_string = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (addr)));
  for (i = 0; ip_string[i]; i++)
    {
      if (ip_string[i] == '.')
        ip_string[i] = ',';
    }
  port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));

  status = g_vfs_ftp_task_send (task, 0, "PORT %s,%u,%u", ip_string, port >> 8, port & 0xFF);
  g_free (ip_string);
  g_object_unref (addr);
  if (status == 0)
    return G_VFS_FTP_METHOD_ANY;
  
  return G_VFS_FTP_METHOD_PORT;
}

typedef GVfsFtpMethod (* GVfsFtpOpenDataConnectionFunc) (GVfsFtpTask *task, GVfsFtpMethod method);
typedef struct _GVfsFtpOpenDataConnectionMethod GVfsFtpOpenDataConnectionMethod;
struct _GVfsFtpOpenDataConnectionMethod {
    GVfsFtpFeature                required_feature;
    GSocketFamily                 required_family;
    GVfsFtpOpenDataConnectionFunc func;
};

static gboolean
g_vfs_ftp_task_open_data_connection_method_is_supported (const GVfsFtpOpenDataConnectionMethod *method,
                                                         GVfsFtpTask *                          task,
                                                         GSocketFamily                          family)
{
  if (method->required_feature &&
      !g_vfs_backend_ftp_has_feature (task->backend, method->required_feature))
    return FALSE;

  if (method->required_family != G_SOCKET_FAMILY_INVALID &&
      method->required_family != family)
    return FALSE;

  return TRUE;
}

static GSocketFamily
g_vfs_ftp_task_get_socket_family (GVfsFtpTask *task)
{
  GSocketAddress *addr;
  GSocketFamily family;

  /* workaround for the task not having a connection yet */
  if (task->conn == NULL &&
      g_vfs_ftp_task_send (task, 0, "NOOP") == 0)
    {
      g_vfs_ftp_task_clear_error (task);
      return G_SOCKET_FAMILY_INVALID;
    }

  addr = g_vfs_ftp_connection_get_address (task->conn, NULL);
  if (addr == NULL)
    return G_SOCKET_FAMILY_INVALID;

  family = g_socket_address_get_family (addr);
  g_object_unref (addr);
  return family;
}

static GVfsFtpMethod
g_vfs_ftp_task_setup_data_connection_any (GVfsFtpTask *task, GVfsFtpMethod unused)
{
  static const GVfsFtpOpenDataConnectionMethod funcs_ordered[] = {
    { 0,                      G_SOCKET_FAMILY_IPV4,    g_vfs_ftp_task_setup_data_connection_pasv },
    { G_VFS_FTP_FEATURE_EPSV, G_SOCKET_FAMILY_INVALID, g_vfs_ftp_task_setup_data_connection_epsv },
    { 0,                      G_SOCKET_FAMILY_IPV4,    g_vfs_ftp_task_setup_data_connection_port },
    { G_VFS_FTP_FEATURE_EPRT, G_SOCKET_FAMILY_INVALID, g_vfs_ftp_task_setup_data_connection_eprt }
  };
  GVfsFtpMethod method;
  GSocketFamily family;
  guint i;

  family = g_vfs_ftp_task_get_socket_family (task);

  /* first try all advertised features */
  for (i = 0; i < G_N_ELEMENTS (funcs_ordered); i++)
    {
      if (!g_vfs_ftp_task_open_data_connection_method_is_supported (&funcs_ordered[i], task, family))
        continue;
      method = funcs_ordered[i].func (task, G_VFS_FTP_METHOD_ANY);
      if (method != G_VFS_FTP_METHOD_ANY)
        return method;
      
      g_vfs_ftp_task_clear_error (task);
    }

  /* then try if the non-advertised features work */
  for (i = 0; i < G_N_ELEMENTS (funcs_ordered); i++)
    {
      if (g_vfs_ftp_task_open_data_connection_method_is_supported (&funcs_ordered[i], task, family))
        continue;
      method = funcs_ordered[i].func (task, G_VFS_FTP_METHOD_ANY);
      if (method != G_VFS_FTP_METHOD_ANY)
        return method;
      
      g_vfs_ftp_task_clear_error (task);
    }

  /* finally, just give up */
  return G_VFS_FTP_METHOD_ANY;
}

/**
 * g_vfs_ftp_task_setup_data_connection:
 * @task: a task not having an open data connection
 *
 * Sets up a data connection to the ftp server with using the best method for 
 * this task. If the operation fails, @task will be set into an error state.
 * You must call g_vfs_ftp_task_open_data_connection() to finish setup and 
 * ensure the data connection actually gets opened. Usually, this requires 
 * sending an FTP command down the stream.
 **/
void
g_vfs_ftp_task_setup_data_connection (GVfsFtpTask *task)
{
  static const GVfsFtpOpenDataConnectionFunc connect_funcs[] = {
    [G_VFS_FTP_METHOD_ANY]       = g_vfs_ftp_task_setup_data_connection_any,
    [G_VFS_FTP_METHOD_EPSV]      = g_vfs_ftp_task_setup_data_connection_epsv,
    [G_VFS_FTP_METHOD_PASV]      = g_vfs_ftp_task_setup_data_connection_pasv,
    [G_VFS_FTP_METHOD_PASV_ADDR] = g_vfs_ftp_task_setup_data_connection_pasv,
    [G_VFS_FTP_METHOD_EPRT]      = g_vfs_ftp_task_setup_data_connection_eprt,
    [G_VFS_FTP_METHOD_PORT]      = g_vfs_ftp_task_setup_data_connection_port
  };
  GVfsFtpMethod method, result;

  g_return_if_fail (task != NULL);

  task->method = G_VFS_FTP_METHOD_ANY;

  method = g_atomic_int_get (&task->backend->method);
  g_assert (method < G_N_ELEMENTS (connect_funcs) && connect_funcs[method]);

  if (g_vfs_ftp_task_is_in_error (task))
    return;

  result = connect_funcs[method] (task, method);

  /* be sure to try all possibilities if one failed */
  if (result == G_VFS_FTP_METHOD_ANY &&
      method != G_VFS_FTP_METHOD_ANY &&
      !g_vfs_ftp_task_is_in_error (task))
    result = g_vfs_ftp_task_setup_data_connection_any (task, G_VFS_FTP_METHOD_ANY);

  g_assert (result < G_N_ELEMENTS (connect_funcs) && connect_funcs[result]);
  if (result != method)
    {
      static const char *methods[] = {
        [G_VFS_FTP_METHOD_ANY] = "any",
        [G_VFS_FTP_METHOD_EPSV] = "EPSV",
        [G_VFS_FTP_METHOD_PASV] = "PASV",
        [G_VFS_FTP_METHOD_PASV_ADDR] = "PASV with workaround",
        [G_VFS_FTP_METHOD_EPRT] = "EPRT",
        [G_VFS_FTP_METHOD_PORT] = "PORT"
      };
      g_atomic_int_set (&task->backend->method, result);
      g_debug ("# set default data connection method from %s to %s\n",
               methods[method], methods[result]);
    }
  task->method = result;
}

/**
 * g_vfs_ftp_task_open_data_connection:
 * @task: a task
 *
 * Tries to open a data connection to the ftp server. If the operation fails,
 * @task will be set into an error state.
 **/
void
g_vfs_ftp_task_open_data_connection (GVfsFtpTask *task)
{
  g_return_if_fail (task != NULL);

  if (g_vfs_ftp_task_is_in_error (task))
    return;

  if (task->method == G_VFS_FTP_METHOD_EPRT ||
      task->method == G_VFS_FTP_METHOD_PORT)
    g_vfs_ftp_connection_accept_data_connection (task->conn, 
                                                 task->cancellable,
                                                 &task->error);
}