summaryrefslogtreecommitdiff
path: root/apps/netutils/ftpc/ftpc_socket.c
blob: b2e76b44b65e95be1aac6eb63256c361ff5671bd (plain) (blame)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/****************************************************************************
 * apps/netutils/ftpc/ftpc_socket.c
 *
 *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include "ftpc_config.h"
#include <sys/socket.h>

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <debug.h>
#include <errno.h>

#include "ftpc_internal.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: ftpc_sockinit
 *
 * Description:
 *   Initialize a socket.  Create the socket and "wrap" it as C standard
 *   incoming and outgoing streams.
 *
 ****************************************************************************/

int ftpc_sockinit(FAR struct ftpc_socket_s *sock)
{
  /* Initialize the socket structure */

  memset(sock, 0, sizeof(struct ftpc_socket_s));

  /* Create a socket descriptor */

  sock->sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock->sd < 0)
    {
      ndbg("socket() failed: %d\n", errno);
      goto errout;
    }

  /* Call fdopen to "wrap" the socket descriptor as an input stream using C
   * buffered I/O.
   */

  sock->instream = fdopen(sock->sd, "r");
  if (!sock->instream)
    {
      ndbg("fdopen() failed: %d\n", errno);
      goto errout_with_sd;
    }

  /* Call fdopen to "wrap" the socket descriptor as an output stream using C
   * buffered I/O.
   */

  sock->outstream = fdopen(sock->sd, "w");
  if (!sock->outstream)
    {
      ndbg("fdopen() failed: %d\n", errno);
      goto errout_with_instream;
    }

  return OK;

/* Close the instream.  NOTE:  Since the underlying socket descriptor is
 * *not* dup'ed, the following close should fail harmlessly.
 */

errout_with_instream:
  fclose(sock->instream);
  sock->instream = NULL;
errout_with_sd:
  close(sock->sd);
  sock->sd = -1;
errout:
  return ERROR;
}

/****************************************************************************
 * Name: ftpc_sockclose
 *
 * Description:
 *   Close a socket
 *
 ****************************************************************************/

void ftpc_sockclose(struct ftpc_socket_s *sock)
{
  /* Note that the same underlying socket descriptor is used for both streams.
   * There should be harmless failures on the second fclose and the close.
   */

  fclose(sock->instream);
  fclose(sock->outstream);
  close(sock->sd);
  memset(sock, 0, sizeof(struct ftpc_socket_s));
  sock->sd = -1;
}

/****************************************************************************
 * Name: ftpc_sockconnect
 *
 * Description:
 *   Connect the socket to the host.  On a failure, the caller should call.
 *   ftpc_sockclose() to clean up.
 *
 ****************************************************************************/

int ftpc_sockconnect(struct ftpc_socket_s *sock, struct sockaddr_in *addr)
{
  int ret;

  /* Connect to the server */

  ret = connect(sock->sd, (struct sockaddr *)addr, sizeof(struct sockaddr));
  if (ret < 0)
    {
      ndbg("connect() failed: %d\n", errno);
      return ERROR;
    }

  /* Get the local address of the socket */

  ret = ftpc_sockgetsockname(sock, &sock->laddr);
  if (ret < 0)
    {
      ndbg("ftpc_sockgetsockname() failed: %d\n", errno);
      return ERROR;
    }

  sock->connected = true;
  return OK;
}

/****************************************************************************
 * Name: ftpc_sockcopy
 *
 * Description:
 *   Copy the socket state from one location to another.
 *
 ****************************************************************************/

void ftpc_sockcopy(FAR struct ftpc_socket_s *dest,
                   FAR const struct ftpc_socket_s *src)
{
  memcpy(&dest->laddr, &src->laddr, sizeof(struct sockaddr_in));
  dest->connected = ftpc_sockconnected(src);
}

/****************************************************************************
 * Name: ftpc_sockaccept
 *
 * Description:
 *   Accept a connection on the data socket.  This function is onlly used
 *   in active mode.
 *
 *   In active mode FTP the client connects from a random port (N>1023) to the
 *   FTP server's command port, port 21. Then, the client starts listening to
 *   port N+1 and sends the FTP command PORT N+1 to the FTP server. The server
 *   will then connect back to the client's specified data port from its local
 *   data port, which is port 20. In passive mode FTP the client initiates
 *   both connections to the server, solving the problem of firewalls filtering
 *   the incoming data port connection to the client from the server. When
 *   opening an FTP connection, the client opens two random ports locally
 *   (N>1023 and N+1). The first port contacts the server on port 21, but
 *   instead of then issuing a PORT command and allowing the server to connect
 *   back to its data port, the client will issue the PASV command. The result
 *   of this is that the server then opens a random unprivileged port (P >
 *   1023) and sends the PORT P command back to the client. The client then
 *   initiates the connection from port N+1 to port P on the server to transfer
 *   data.
 *
 ****************************************************************************/

int ftpc_sockaccept(FAR struct ftpc_socket_s *sock)
{
  struct sockaddr addr;
  socklen_t addrlen;

  /* Any previous socket should have been uninitialized (0) or explicitly
   * closed (-1).  But the path to this function may include a call to
   * ftpc_sockinit().  If so... close that socket and call accept to
   * get a new one.
   */

  if (sock->sd > 0)
    {
      ftpc_sockclose(sock);
    }

  addrlen  = sizeof(addr);
  sock->sd = accept(sock->sd, &addr, &addrlen);
  if (sock->sd == -1)
    {
      ndbg("accept() failed: %d\n", errno);
      return ERROR;
    }

  memcpy(&sock->laddr, &addr, sizeof(sock->laddr));

  /* Create in/out C buffer I/O streams on the data channel.  First,
   * create the incoming buffered stream.
   */

  sock->instream = fdopen(sock->sd, "r");
  if (!sock->instream)
    {
      ndbg("fdopen() failed: %d\n", errno);
      goto errout_with_sd;
    }

  /* Create the outgoing stream */

  sock->outstream = fdopen(sock->sd, "w");
  if (!sock->outstream)
    {
      ndbg("fdopen() failed: %d\n", errno);
      goto errout_with_instream;
    }

  return OK;

/* Close the instream.  NOTE:  Since the underlying socket descriptor is
 * *not* dup'ed, the following close should fail harmlessly.
 */

errout_with_instream:
  fclose(sock->instream);
  sock->instream = NULL;
errout_with_sd:
  close(sock->sd);
  sock->sd = -1;
  return ERROR;
}

/****************************************************************************
 * Name: ftpc_socklisten
 *
 * Description:
 *   Bind the socket to local address and wait for connection from the server.
 *
 ****************************************************************************/

int ftpc_socklisten(struct ftpc_socket_s *sock)
{
  unsigned int addrlen = sizeof(sock->laddr);
  int ret;

  /* Bind the local socket to the local address */

  sock->laddr.sin_port = 0;
  ret = bind(sock->sd, (struct sockaddr *)&sock->laddr, addrlen);
  if (ret < 0)
    {
      ndbg("bind() failed: %d\n", errno);
      return ERROR;
    }

  /* Wait for the connection to the server */

  if (listen(sock->sd, 1) == -1)
    {
      return ERROR;
    }

  /* Then get the local address selected by NuttX */

  ret = ftpc_sockgetsockname(sock, &sock->laddr);
  return ret;
}

/****************************************************************************
 * Name: ftpc_sockprintf
 *
 * Description:
 *   printf to a socket stream
 *
 ****************************************************************************/

int ftpc_sockprintf(struct ftpc_socket_s *sock, const char *fmt, ...)
{
  va_list ap;
  int r;

  va_start(ap, fmt);
  r = vfprintf(sock->outstream, fmt, ap);
  va_end(ap);
  return r;
}

/****************************************************************************
 * Name: ftpc_sockgetsockname
 *
 * Description:
 *   Get the address of the local socket
 *
 ****************************************************************************/

int ftpc_sockgetsockname(FAR struct ftpc_socket_s *sock,
                         FAR struct sockaddr_in *addr)
{
  socklen_t len = sizeof(struct sockaddr_in);
  int ret;

  ret = getsockname(sock->sd, (FAR struct sockaddr *)addr, &len);
  if (ret < 0)
    {
      ndbg("getsockname failed: %d\n", errno);
      return ERROR;
    }
  return OK;
}