forked from JasonAlt/UberFTP
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftp_a.c
314 lines (261 loc) · 6.62 KB
/
ftp_a.c
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
/*
* University of Illinois/NCSA Open Source License
*
* Copyright (c) 2003-2012 NCSA. All rights reserved.
*
* Developed by:
*
* Storage Enabling Technologies (SET)
*
* Nation Center for Supercomputing Applications (NCSA)
*
* http://dims.ncsa.uiuc.edu/set/uberftp
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the .Software.),
* to deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* + Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* + Neither the names of SET, NCSA
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED .AS IS., WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*/
#include <stdlib.h>
#include "settings.h"
#include "errcode.h"
#include "network.h"
#include "output.h"
#include "ftp_a.h"
#include "misc.h"
#include "gsi.h"
#include "ftp.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif /* DMALLOC */
#define DC_STATE_RD_WR 0x00
#define DC_STATE_ACCEPT 0x01
#define DC_STATE_CONNECT 0x02
#define DC_STATE_ACCEPT_AUTH 0x03
#define DC_STATE_CONNECT_AUTH 0x04
typedef struct _dc_ {
nh_t * nh;
gh_t * gh;
int state; /* 0 read/write, 1 listen, 2 connect */
globus_off_t off;
} dc_t;
static errcode_t
_f_a_poll(dch_t * dch)
{
errcode_t ec = EC_SUCCESS;
int done = 0;
dc_t * dc = (dc_t*)dch->privdata;
nh_t * nh = NULL;
switch (dc->state)
{
case DC_STATE_CONNECT:
dc->state = DC_STATE_CONNECT_AUTH;
case DC_STATE_CONNECT_AUTH:
ec = gsi_dc_auth(&dc->gh, dc->nh, dch->pbsz, dch->dcau, 0, &done);
if (!ec && done)
dc->state = DC_STATE_RD_WR;
break;
case DC_STATE_ACCEPT:
ec = net_accept(dc->nh, &nh);
if (ec || !nh)
break;
net_close(dc->nh);
net_destroy(dc->nh);
dc->nh = nh;
dc->state = DC_STATE_ACCEPT_AUTH;
break;
case DC_STATE_ACCEPT_AUTH:
ec = gsi_dc_auth(&dc->gh, dc->nh, dch->pbsz, dch->dcau, 1, &done);
if (!ec && done)
dc->state = DC_STATE_RD_WR;
break;
}
return ec;
}
static errcode_t
_f_a_active(dch_t * dch, struct sockaddr * sin, socklen_t sin_len, int scnt)
{
errcode_t ec = EC_SUCCESS;
dc_t * dc = NULL;
dch->privdata = dc = (dc_t*) malloc(sizeof(dc_t));
memset(dc, 0, sizeof(dc_t));
/* Non blocking connect. */
ec = net_connect(&dc->nh, sin, sin_len);
if (ec)
{
FREE(dch->privdata);
dch->privdata = NULL;
return ec;
}
dc->state = DC_STATE_CONNECT;
return ec;
}
static errcode_t
_f_a_passive(dch_t * dch, struct sockaddr * sin, socklen_t sin_len)
{
errcode_t ec = EC_SUCCESS;
dc_t * dc = NULL;
dch->privdata = dc = (dc_t*) malloc(sizeof(dc_t));
memset(dc, 0, sizeof(dc_t));
ec = net_listen(&dc->nh, sin, sin_len);
if (ec)
{
FREE(dch->privdata);
dch->privdata = NULL;
return ec;
}
dc->state = DC_STATE_ACCEPT;
return ec;
}
static errcode_t
_f_a_read_ready(dch_t * dch, int * ready)
{
errcode_t ec = EC_SUCCESS;
dc_t * dc = (dc_t*)dch->privdata;
*ready = 0;
ec = _f_a_poll(dch);
if (ec || dc->state != DC_STATE_RD_WR)
return ec;
if (!gsi_dc_ready(dc->gh, dc->nh, 1))
ec = gsi_dc_fl_read(dc->gh, dc->nh);
if (ec)
return ec;
if (gsi_dc_ready(dc->gh, dc->nh, 1))
*ready = 1;
return ec;
}
static errcode_t
_f_a_read(dch_t * dch,
char ** buf,
globus_off_t * off,
size_t * len,
int * eof)
{
errcode_t ec = EC_SUCCESS;
char * cptr = NULL;
dc_t * dc = (dc_t*)dch->privdata;
do {
ec = _f_a_poll(dch);
} while (!ec && dc->state != DC_STATE_RD_WR);
while (!ec && !gsi_dc_ready(dc->gh, dc->nh, 1))
{
ec = gsi_dc_fl_read(dc->gh, dc->nh);
}
if (ec)
return ec;
ec = gsi_dc_read(dc->gh, dc->nh, buf, len, eof);
for (cptr = *buf; cptr; )
{
cptr = Strnstr(cptr, "\r\n", *len - (cptr-*buf));
if (!cptr)
break;
memmove(cptr, cptr+1, *len - (cptr-*buf) - 1);
(*len)--;
}
*off = dc->off + dch->partial_off;
dc->off += *len;
return ec;
}
static errcode_t
_f_a_write_ready(dch_t * dch, int * ready)
{
errcode_t ec = EC_SUCCESS;
dc_t * dc = (dc_t*)dch->privdata;
*ready = 0;
ec = _f_a_poll(dch);
if (ec || dc->state != DC_STATE_RD_WR)
return ec;
if (!gsi_dc_ready(dc->gh, dc->nh, 0))
ec = gsi_dc_fl_write(dc->gh, dc->nh);
if (ec)
return ec;
if (gsi_dc_ready(dc->gh, dc->nh, 0))
*ready = 1;
return ec;
}
static errcode_t
_f_a_write(dch_t * dch,
char * buf,
globus_off_t off,
size_t len,
int eof)
{
errcode_t ec = EC_SUCCESS;
char * cptr = NULL;
dc_t * dc = (dc_t*)dch->privdata;
do {
ec = _f_a_poll(dch);
} while (!ec && dc->state != DC_STATE_RD_WR);
while (!ec && !gsi_dc_ready(dc->gh, dc->nh, 0))
{
ec = gsi_dc_fl_write(dc->gh, dc->nh);
}
if (ec)
return ec;
/* Convert "\n" to "\r\n" */
while (cptr)
{
cptr = Strnstr(cptr, "\n", len - (cptr-buf));
if (!cptr)
break;
buf = (char *) realloc(buf, len+1);
memmove(cptr+1, cptr, len - (cptr-buf));
*cptr = '\r';
cptr += 2;
len++;
}
return gsi_dc_write(dc->gh, dc->nh, buf, len, eof);
}
static void
_f_a_close(dch_t * dch)
{
errcode_t ec = EC_SUCCESS;
dc_t * dc = (dc_t*)dch->privdata;
int wr = 0;
int rd = 0;
if (!dch->privdata)
return;
if (dc->state == DC_STATE_CONNECT)
{
do {
rd = wr = 1;
ec = net_poll(dc->nh, &rd, &wr, -1);
} while (ec == EC_SUCCESS && !wr && !rd);
ec_destroy(ec);
}
net_destroy(dc->nh);
gsi_destroy(dc->gh);
FREE(dc);
dch->privdata = NULL;
}
const dci_t Ftp_a_dci = {
_f_a_active,
_f_a_passive,
_f_a_read_ready,
_f_a_read,
_f_a_write_ready,
_f_a_write,
_f_a_close,
};