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
|
/* Start/stop data channel transfer
Copyright (C) 1997 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <ftpconn.h>
#include "priv.h"
/* Open an active data connection, returning the file descriptor in DATA. */
static error_t
ftp_conn_start_open_actv_data (struct ftp_conn *conn, int *data)
{
error_t err = 0;
if (conn->actv_data_conn_queue < 0)
{
/* DCQ is a socket on which we listen for data connections from the
server. */
int dcq;
struct sockaddr *addr = conn->actv_data_addr;
size_t addr_len = sizeof *addr;
if (! addr)
/* Generate an address for the data connection (which we must know,
so we can tell the server). */
{
addr = conn->actv_data_addr = malloc (sizeof (struct sockaddr_in));
if (! addr)
return ENOMEM;
/* Get the local address chosen by the system. */
if (conn->control < 0)
err = EBADF;
else if (getsockname (conn->control, addr, &addr_len) < 0)
err = errno;
if (err == EBADF || err == EPIPE)
/* Control connection has closed; reopen it and try again. */
{
err = ftp_conn_open (conn);
if (!err && getsockname (conn->control, addr, &addr_len) < 0)
err = errno;
}
if (err)
{
free (addr);
conn->actv_data_addr = 0;
return err;
}
}
dcq = socket (AF_INET, SOCK_STREAM, 0);
if (dcq < 0)
return errno;
#if 0
if (setsockopt (dcq, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on) < 0)
err = errno;
#endif
/* Let the system choose a port for us. */
((struct sockaddr_in *)addr)->sin_port = 0;
/* Use ADDR as the data socket's local address. */
if (!err && bind (dcq, addr, addr_len) < 0)
err = errno;
/* See what port was chosen by the system. */
if (!err && getsockname (dcq, addr, &addr_len) < 0)
err = errno;
/* Set the incoming connection queue length. */
if (!err && listen (dcq, 1) < 0)
err = errno;
if (err)
close (dcq);
else
conn->actv_data_conn_queue = dcq;
}
if (! err)
/* By my reading of rfc959, we shouldn't have to re-send the data
connection address to the server after the first time (the servers
should always use the last-specified address), but bsd-derived ftp
servers don't work unless this is done. Aren't standards wonderful? */
err = ftp_conn_send_actv_addr (conn, conn->actv_data_addr);
if (! err)
*data = conn->actv_data_conn_queue;
return err;
}
/* Finish opening the active data connection *DATA opened with
ftp_conn_start_open_actv_data, following the sending of the command that
uses the connection to the server. This function closes the file
descriptor in *DATA, and returns a new file descriptor for the actual data
connection. */
static error_t
ftp_conn_finish_open_actv_data (struct ftp_conn *conn, int *data)
{
struct sockaddr_in rmt_addr;
size_t rmt_addr_len = sizeof rmt_addr;
int real = accept (*data, &rmt_addr, &rmt_addr_len);
if (real < 0)
return errno;
*data = real;
return 0;
}
/* Return a data connection, which may not be in a completely open state;
this call should be followed by the command that uses the connection, and
a call to ftp_conn_finish_open_data, if that succeeds. */
static error_t
ftp_conn_start_open_data (struct ftp_conn *conn, int *data)
{
error_t err;
if (conn->use_passive)
/* First try a passive connection. */
{
struct sockaddr *addr;
/* Tell the server we wan't to use passive mode, for which it should
give us an address to connect to. */
err = ftp_conn_get_pasv_addr (conn, &addr);
if (! err)
{
int dsock = socket (PF_INET, SOCK_STREAM, 0);
if (dsock < 0)
err = errno;
else if (connect (dsock, addr, addr->sa_len) < 0)
{
err = errno;
close (dsock);
}
else
*data = dsock;
free (addr);
}
}
else
err = EAGAIN;
if (err)
/* Using a passive connection didn't work, try an active one. */
{
conn->use_passive = 0; /* Don't try again. */
err = ftp_conn_start_open_actv_data (conn, data);
}
return err;
}
/* Finish opening the data connection *DATA opened with
ftp_conn_start_open_data, following the sending of the command that uses
the connection to the server. This function may change *DATA, in which
case the old file descriptor is closed. */
static error_t
ftp_conn_finish_open_data (struct ftp_conn *conn, int *data)
{
if (conn->use_passive)
/* Passive connections should already have been completely opened. */
return 0;
else
return ftp_conn_finish_open_actv_data (conn, data);
}
/* Start a transfer command CMD/ARG, returning a file descriptor in DATA.
POSS_ERRS is a list of errnos to try matching against any resulting error
text. */
error_t
ftp_conn_start_transfer (struct ftp_conn *conn,
const char *cmd, const char *arg,
const error_t *poss_errs,
int *data)
{
error_t err = ftp_conn_start_open_data (conn, data);
if (! err)
{
int reply;
const char *txt;
err = ftp_conn_cmd (conn, cmd, arg, &reply, &txt);
if (!err && !REPLY_IS_PRELIM (reply))
err = unexpected_reply (conn, reply, txt, poss_errs);
if (! err)
err = ftp_conn_finish_open_data (conn, data);
if (err)
close (*data);
}
return err;
}
/* Wait for the reply signalling the end of a data transfer. */
error_t
ftp_conn_finish_transfer (struct ftp_conn *conn)
{
int reply;
error_t err = ftp_conn_get_reply (conn, &reply, 0);
if (!err && reply != REPLY_TRANS_OK && reply != REPLY_FCMD_OK)
err = unexpected_reply (conn, reply, 0, 0);
return err;
}
/* Start retreiving file NAME over CONN, returning a file descriptor in DATA
over which the data can be read. */
error_t
ftp_conn_start_retrieve (struct ftp_conn *conn, const char *name, int *data)
{
if (! name)
return EINVAL;
return
ftp_conn_start_transfer (conn, "retr", name, ftp_conn_poss_file_errs, data);
}
/* Start retreiving a list of files in NAME over CONN, returning a file
descriptor in DATA over which the data can be read. */
error_t
ftp_conn_start_list (struct ftp_conn *conn, const char *name, int *data)
{
return
ftp_conn_start_transfer (conn, "nlst", name, ftp_conn_poss_file_errs, data);
}
/* Start retreiving a directory listing of NAME over CONN, returning a file
descriptor in DATA over which the data can be read. */
error_t
ftp_conn_start_dir (struct ftp_conn *conn, const char *name, int *data)
{
return
ftp_conn_start_transfer (conn, "list", name, ftp_conn_poss_file_errs, data);
}
/* Start storing into file NAME over CONN, returning a file descriptor in DATA
into which the data can be written. */
error_t
ftp_conn_start_store (struct ftp_conn *conn, const char *name, int *data)
{
if (! name)
return EINVAL;
return
ftp_conn_start_transfer (conn, "stor", name, ftp_conn_poss_file_errs, data);
}
|