]> cygwin.com Git - cygwin-apps/setup.git/blob - nio-ftp.cc
2001-11-09 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / nio-ftp.cc
1 /*
2 * Copyright (c) 2000, 2001, Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16 /* This file is responsible for implementing all direct FTP protocol
17 channels. It is intentionally simplistic. */
18
19 #if 0
20 static const char *cvsid = "\n%%% $Id$\n";
21 #endif
22
23 #include "win32.h"
24 #include "winsock.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #include "resource.h"
30 #include "state.h"
31 #include "simpsock.h"
32 #include "log.h"
33
34 #include "netio.h"
35 #include "nio-ftp.h"
36
37 static SimpleSocket *cmd = 0;
38 static char *cmd_host = 0;
39 static int cmd_port = 0;
40
41 static char *last_line;
42
43 static int
44 ftp_line (SimpleSocket *s)
45 {
46 do {
47 last_line = s->gets ();
48 log (LOG_BABBLE, "ftp > %s", last_line);
49 } while (last_line && (!isdigit (last_line[0]) || last_line[3] != ' '));
50 return atoi (last_line ?: "0");
51 }
52
53 NetIO_FTP::NetIO_FTP (char *Purl, BOOL allow_ftp_auth)
54 : NetIO (Purl, allow_ftp_auth)
55 {
56 s = 0;
57 int code;
58
59 if (port == 0)
60 port = 21;
61
62 if (cmd_host && strcmp (host, cmd_host) != 0 || port != cmd_port)
63 {
64 if (cmd)
65 cmd->printf ("QUIT\r\n");
66 delete cmd;
67 free (cmd_host);
68 cmd = 0;
69 cmd_host = 0;
70 }
71
72 if (cmd == 0)
73 {
74 SimpleSocket *c = new SimpleSocket (host, port);
75 code = ftp_line (c);
76
77 auth_retry:
78 if (net_ftp_user)
79 c->printf ("USER %s\r\n", net_ftp_user);
80 else
81 c->printf ("USER anonymous\r\n");
82 code = ftp_line (c);
83 if (code == 331)
84 {
85 if (net_ftp_passwd)
86 c->printf ("PASS %s\r\n", net_ftp_passwd);
87 else
88 c->printf ("PASS cygwin-setup@\r\n");
89 code = ftp_line (c);
90 }
91 if (code == 530) /* Authentication failed, retry */
92 {
93 get_ftp_auth ();
94 if (net_ftp_user && net_ftp_passwd)
95 goto auth_retry;
96 }
97
98 if (code < 200 || code >= 300)
99 {
100 delete c;
101 return;
102 }
103
104 cmd = c;
105 cmd_host = _strdup (host);
106 cmd_port = port;
107
108 cmd->printf ("TYPE I\r\n");
109 code = ftp_line (cmd);
110 }
111
112 cmd->printf ("PASV\r\n");
113 do {
114 code = ftp_line (cmd);
115 } while (code == 226); /* previous RETR */
116 if (code != 227)
117 return;
118
119 char *paren = strchr (last_line, '(');
120 if (!paren)
121 return;
122
123 int i1, i2, i3, i4, p1, p2;
124 sscanf (paren+1, "%d,%d,%d,%d,%d,%d", &i1, &i2, &i3, &i4, &p1, &p2);
125 char tmp[20];
126 sprintf (tmp, "%d.%d.%d.%d", i1, i2, i3, i4);
127 s = new SimpleSocket (tmp, p1*256 + p2);
128
129 cmd->printf ("RETR %s\r\n", path);
130 code = ftp_line (cmd);
131 if (code != 150)
132 {
133 delete s;
134 s = 0;
135 return;
136 }
137 }
138
139 NetIO_FTP::~NetIO_FTP ()
140 {
141 if (s)
142 delete s;
143 }
144
145 int
146 NetIO_FTP::ok ()
147 {
148 if (s)
149 return 1;
150 return 0;
151 }
152
153 int
154 NetIO_FTP::read (char *buf, int nbytes)
155 {
156 if (!s)
157 return 0;
158 return s->read (buf, nbytes);
159 }
This page took 0.043109 seconds and 5 git commands to generate.