2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 # include <sys/socket.h>
32 #include "command_parser.h"
36 * Read data from the control channel until a complete request (delimited by
39 * Control channel processing is implemented in a pipelined fashion. Trailing
40 * bytes belonging to the following request are left in the buffer; to be read
41 * in the next call to this function.
43 static void read_request (void)
49 /* Shift trailing data from previous call */
50 if (SS
.input_offset
> 0)
51 memmove(SS
.input
, SS
.input
+ SS
.input_offset
, l
);
54 while (i
< l
&& SS
.input
[i
] != '\n')
56 if (SS
.input
[i
] == '\n')
58 if (i
> 0 && SS
.input
[i
- 1] == '\r')
64 /* Buffer data exhausted, get more from the network */
65 b
= recv(SS
.control_sk
, SS
.input
+ l
, LINE_SIZE
- l
, 0);
69 fatal("Control channel input");
70 else if (l
== LINE_SIZE
)
73 fatal("Input buffer overflow");
77 notice("Peer closed control connection");
78 closesocket(SS
.control_sk
);
86 /* Mark residual (trailing) bytes for the next call */
87 SS
.input
[i
- 1] = '\0';
91 SS
.input_offset
= (l
- i
> 0 ? i
: 0);
93 debug("Request : %s", SS
.input
);
98 * Parse the current request from the control channel and return the
99 * corresponding command number. The command argument (SS.arg) is filled
102 enum command
next_command (void)
104 const struct Cmd
*cmd
;
110 while (SS
.input
[i
] != ' ' && SS
.input
[i
] != '\0')
112 SS
.input
[i
] = toupper(SS
.input
[i
] & 0x07F);
116 SS
.arg
= (SS
.input
[i
] == ' ' ? &SS
.input
[i
+ 1] : NULL
);
119 cmd
= parse_command(SS
.input
, i
);