]> cygwin.com Git - cygwin-apps/setup.git/blob - nio-ftp.cc
2002-04-26 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 =
21 "\n%%% $Id$\n";
22 #endif
23
24 #include "win32.h"
25 #include "winsock.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29
30 #include "resource.h"
31 #include "state.h"
32 #include "simpsock.h"
33 #include "log.h"
34
35 #include "netio.h"
36 #include "nio-ftp.h"
37
38 static SimpleSocket *cmd = 0;
39 static char *cmd_host = 0;
40 static int cmd_port = 0;
41
42 static char *last_line;
43
44 static int
45 ftp_line (SimpleSocket * s)
46 {
47 do
48 {
49 last_line = s->gets ();
50 log (LOG_BABBLE, String ("ftp > ") + (last_line ? last_line : "error"));
51 }
52 while (last_line && (!isdigit (last_line[0]) || last_line[3] != ' '));
53 return atoi (last_line ? : "0");
54 }
55
56 NetIO_FTP::NetIO_FTP (char const *Purl, BOOL allow_ftp_auth):NetIO (Purl, allow_ftp_auth)
57 {
58 s = 0;
59 int
60 code;
61
62 if (port == 0)
63 port = 21;
64
65 if (cmd_host && strcmp (host, cmd_host) != 0 || port != cmd_port)
66 {
67 if (cmd)
68 cmd->printf ("QUIT\r\n");
69 delete cmd;
70 delete [] cmd_host;
71 cmd = 0;
72 cmd_host = 0;
73 }
74
75 if (cmd == 0)
76 {
77 SimpleSocket *
78 c = new SimpleSocket (host, port);
79 code = ftp_line (c);
80
81 auth_retry:
82 if (net_ftp_user)
83 c->printf ("USER %s\r\n", net_ftp_user);
84 else
85 c->printf ("USER anonymous\r\n");
86 code = ftp_line (c);
87 if (code == 331)
88 {
89 if (net_ftp_passwd)
90 c->printf ("PASS %s\r\n", net_ftp_passwd);
91 else
92 c->printf ("PASS cygwin-setup@\r\n");
93 code = ftp_line (c);
94 }
95 if (code == 530) /* Authentication failed, retry */
96 {
97 get_ftp_auth (NULL);
98 if (net_ftp_user && net_ftp_passwd)
99 goto auth_retry;
100 }
101
102 if (code < 200 || code >= 300)
103 {
104 delete
105 c;
106 return;
107 }
108
109 cmd = c;
110 cmd_host = new char [strlen (host) + 1];
111 strcpy (cmd_host, host);
112 cmd_port = port;
113
114 cmd->printf ("TYPE I\r\n");
115 code = ftp_line (cmd);
116 }
117
118 cmd->printf ("PASV\r\n");
119 do
120 {
121 code = ftp_line (cmd);
122 }
123 while (code == 226); /* previous RETR */
124 if (code != 227)
125 return;
126
127 char *
128 paren = strchr (last_line, '(');
129 if (!paren)
130 return;
131
132 int
133 i1, i2, i3, i4, p1, p2;
134 sscanf (paren + 1, "%d,%d,%d,%d,%d,%d", &i1, &i2, &i3, &i4, &p1, &p2);
135 char
136 tmp[20];
137 sprintf (tmp, "%d.%d.%d.%d", i1, i2, i3, i4);
138 s = new SimpleSocket (tmp, p1 * 256 + p2);
139
140 cmd->printf ("RETR %s\r\n", path);
141 code = ftp_line (cmd);
142 if (code != 150 && code != 125)
143 {
144 delete
145 s;
146 s = 0;
147 return;
148 }
149 }
150
151 NetIO_FTP::~NetIO_FTP ()
152 {
153 if (s)
154 delete s;
155 }
156
157 int
158 NetIO_FTP::ok ()
159 {
160 if (s && s->ok ())
161 return 1;
162 return 0;
163 }
164
165 int
166 NetIO_FTP::read (char *buf, int nbytes)
167 {
168 if (!ok ())
169 return 0;
170 return s->read (buf, nbytes);
171 }
This page took 0.040611 seconds and 5 git commands to generate.