File: ftp.cpp

package info (click to toggle)
socket%2B%2B 1.12.13-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 716 kB
  • ctags: 1,096
  • sloc: cpp: 3,988; makefile: 115; sh: 22; ansic: 14
file content (332 lines) | stat: -rw-r--r-- 6,969 bytes parent folder | download | duplicates (5)
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
// ftp.h 
// Copyright (C) 1992-1996 Gnanasekaran Swaminathan <gs4t@virginia.edu>
//
// Permission is granted to use at your own risk and distribute this software
// in source and  binary forms provided  the above copyright notice and  this
// paragraph are  preserved on all copies.  This software is provided "as is"
// with no express or implied warranty.
//
// Version: 12Jan97 1.11

#include <config.h>

#include <socket++/ftp.h>
#include <fstream>
#include <socket++/fork.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h> // for sprintf
#include <errno.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

using namespace std;

#if defined (__osf__) && defined (__DECCXX)
   extern "C" {
#    include <netdb.h>
   }
#else 
#  include <netdb.h>
#endif

char reptype [][8] = {
  "A N",
  "A T",
  "A C",
  "E N",
  "E T",
  "E C",
  "I",
  "L "
};

char filestru [][8] = {
  "F",
  "R",
  "P"
};

char transmode [][8] = {
  "S",
  "B",
  "C"
};

// ftpdata waits on a port at the local machine.
// When a connection is made, it receives a file from remote
// host if the ostream o is set, or it sends a file to the remote
// host if the istream i is set.
ftp::replycodea ftp::ftpbuf::ftpdata (int portno, istream* i, ostream* o,
                                      const char* cmd, const char* arg)
{
  sockinetbuf sb (sockbuf::sock_stream, 0);

  sb.bind_until_success (portno);
  useraddr (sb.localaddr ());
  
  sb.listen (1);

  if (send_cmd (cmd, arg) >= ftp::rca_error)
    return ftp::rca_error;
  
  if (o) {
    sockbuf c = sb.accept ();

    // read data from c and put it in o
    char buf [1024];
    int  rdsz;

    while ((rdsz = c.sgetn (buf, 1024)) != EOF)
      o->write (buf, rdsz);
  } else if (i) {
    sockbuf c = sb.accept (); 

    // read data from i and send it to c
    char buf [1024];
    int  rdsz;
    streambuf* rb = i->rdbuf ();

    while ((rdsz = rb->sgetn (buf, 1024)) > 0) {
      int wrsz = c.sputn (buf, rdsz);
      if (rdsz != wrsz)
	cerr << "write error\n";
    }
  }

  // Note: sockbuf object c must have been destructed by the time you reach
  //       here.

  return get_response ();
} 

ftp::replycodea ftp::ftpbuf::get_response ()
     // get all the response that one can get and send all of them to o
{
  // if o is 0, then we trash data.
  int  firstline = 1;
  while (underflow () != EOF) {
    int n = in_avail ();
    if (n < 5)
      continue;

    // data is of this form: 221 repsonse <CRLF> or 221-response <CRLF>
    char* q = gptr ();
    char* p = q;

    // zap upto <CRLF>
    int i = 0;
    for (i = 2; i <= n; i++, p++)
      if (*p == '\r' && *(p+1) == '\n') {
	break;
      }
    if (o)
      o->write (q, i);
    gbump (i);

    if (firstline) {
      strncpy (replycode, q, 3);
      replycode [3] = ' ';
      if (q [3] == ' ')
	break;
      firstline = 0;
    } else if (strncmp (q, replycode, 4) == 0)
      break;
  }  

  return (replycodea) replycode [0];
}

ftp::replycodea ftp::ftpbuf::send_cmd (const char* cmd,
				       const char* arg)
{
  xsputn (cmd, ::strlen (cmd));
  if (arg) {
    xsputn (" ", 1);
    xsputn (arg, ::strlen (arg));
  }
  xsputn ("\r\n", 2);
  sync ();
  
  return get_response ();
}

ftp::ftp (ostream* out)
: ios (0)
{
  ios::init (new ftpbuf (out));
}

ftp::ftpbuf::ftpbuf (ostream* out)
: protocol::protocolbuf (protocol::tcp),
  o (out)
{
  replycode [4] = 0;
}

void ftp::ftpbuf::serve_clients (int portno)
// right now no server ftp class can be used as a server
{}

ftp::replycodea ftp::ftpbuf::cd (const char* dir)
{
  return send_cmd ("CWD", dir);
}

ftp::replycodea ftp::ftpbuf::useraddr (sockinetaddr sa)
{
  if (sa.sin_addr.s_addr == 0) {
    // local host
    char hostname [64];
    if (::gethostname (hostname, 63) == -1) throw sockerr (EADDRNOTAVAIL);
    hostent* hp = gethostbyname (hostname);
    if (hp == 0) throw sockerr (EADDRNOTAVAIL);
    memcpy (&sa.sin_addr, hp->h_addr, hp->h_length);
  }

  struct in_addr ina = sa.sin_addr;
  int    portno      = ntohs(sa.sin_port);
  char*  ina_p       = inet_ntoa (ina);
  char   addr [80];

  char* p = 0;
  strcpy (addr, ina_p);
  while ((p = strchr (addr, '.')))
    *p = ',';

  int hi_portno = portno >> 8;
  int lo_portno = portno & 0xff;

  sprintf (addr + strlen (addr), ",%d,%d", hi_portno, lo_portno);

  return send_cmd ("PORT", addr);
}

ftp::replycodea ftp::ftpbuf::useraddr (const char* hostname, int portno)
{
  return useraddr (sockinetaddr (hostname, portno));
}

ftp::replycodea ftp::ftpbuf::server_port (int portno)
{
  int hi_portno = portno >> 8;
  int lo_portno = portno & 0xff;
  char port [80];
  
  sprintf (port, "%d,%d", hi_portno, lo_portno);
  
  return send_cmd ("PASV", port);
}
  
ftp::replycodea ftp::ftpbuf::rep_type (ftp::reptype rt)
{
  return send_cmd ("TYPE", ::reptype [int(rt)]);
}

ftp::replycodea ftp::ftpbuf::file_stru (ftp::filestru fs)
{
  return send_cmd ("STRU", ::filestru [int(fs)]);
}

ftp::replycodea ftp::ftpbuf::trans_mode (ftp::transmode tm)
{
  return send_cmd ("STRU", ::transmode [int(tm)]);
}

ftp::replycodea ftp::ftpbuf::getfile (const char* rpath, const char* lpath)
{
  if (lpath == 0)
    lpath = rpath;

  if (rpath == 0)
    list ();

  ofstream f (lpath);
  return ftpdata (10000, 0, &f, "RETR", rpath);
}

ftp::replycodea ftp::ftpbuf::list (const char* rpath, int justnames)
{
  if (justnames)
    return ftpdata (10000, 0, o, "NLST", rpath);
  else
    return ftpdata (10000, 0, o, "LIST", rpath);
}

ftp::replycodea ftp::ftpbuf::putfile (const char* lpath, const char* rpath)
{
  if (rpath == 0)
    rpath = lpath;

  if (lpath == 0)
    return ftp::rca_error;

  ifstream f(lpath);
  return ftpdata (10000, &f, 0, "STOR", rpath);
}

ftp::replycodea ftp::ftpbuf::putfile (const char* lpath)
{
  if (lpath == 0)
    return ftp::rca_error;

  ifstream f(lpath);
  return ftpdata (10000, &f, 0, "STOU", lpath);
}
  
ftp::replycodea ftp::ftpbuf::append  (const char* lpath, const char* rpath)
{
  if (lpath == 0)
    return ftp::rca_error;

  if (rpath == 0)
    rpath = lpath;

  ifstream f(lpath);
  return ftpdata (10000, &f, 0, "APPE", 0);
}

ftp::replycodea ftp::ftpbuf::allocate (int numbytes)
{
  char b[32];
  sprintf (b, "%d", numbytes);
  return send_cmd ("ALLO", b);
}

ftp::replycodea ftp::ftpbuf::restart (int marker)
{
  char b[32];
  sprintf (b, "%d", marker);
  return send_cmd ("REST", b);
}

ftp::replycodea ftp::ftpbuf::rename (const char* rpath, const char* newrpath)
{
  if (rpath == 0 || newrpath == 0)
    return ftp::rca_error;

  if (send_cmd ("RNFR", rpath) >= ftp::rca_error)
    return rca_error;

  return send_cmd ("RNTO", newrpath);
}

ftp::replycodea ftp::ftpbuf::rmfile (const char* rpath)
{
  return send_cmd ("DELE", rpath);
}

ftp::replycodea ftp::ftpbuf::rmdir  (const char* rpath)
{
  return send_cmd ("RMD", rpath);
}

ftp::replycodea ftp::ftpbuf::mkdir  (const char* rpath)
{
  return send_cmd ("MKD", rpath);
}