From 0a03a183057549b52d95f26cbfd5f185326a693f Mon Sep 17 00:00:00 2001 From: etanol Date: Mon, 18 Aug 2008 20:33:27 +0200 Subject: [PATCH] Normalized coding style. Also reduced the passive reply buffer size. --- client_port.c | 26 ++++++++++++++++---------- command_loop.c | 11 ++++++----- path.c | 1 + send_file.c | 9 ++++++--- session.c | 23 ++++++++++++++--------- uftps.c | 19 +++++++++++++------ uftps.h | 3 +-- 7 files changed, 57 insertions(+), 35 deletions(-) diff --git a/client_port.c b/client_port.c index b7784c1..1b3a73e 100644 --- a/client_port.c +++ b/client_port.c @@ -58,24 +58,28 @@ void client_port (void) str = SS.arg; commas = 0; - while (commas < 4) { - switch (*str) { - case '\0': + while (commas < 4) + { + if (*str == '\0') + { reply_c(ERR_BAD_PARAMETER); return; - case ',': + } + + if (*str == ',') + { commas++; - *str = '.'; /* Fall through */ - default: - str++; + *str = '.'; } + str++; } str[-1] = '\0'; /* *--str++ = '\0'; */ /* "h1.h2.h3.h4" ==> struct in_addr */ err = !inet_aton(SS.arg, &(saddr.sin_addr)); - if (err) { + if (err) + { reply_c(ERR_BAD_PARAMETER); return; } @@ -94,13 +98,15 @@ void client_port (void) saddr.sin_port = htons(port); SS.data_sk = socket(PF_INET, SOCK_STREAM, 0); - if (SS.data_sk == -1) { + if (SS.data_sk == -1) + { reply_c(ERR_BAD_CONNECTION); return; } err = connect(SS.data_sk, (struct sockaddr *) &saddr, sizeof(saddr)); - if (err == -1) { + if (err == -1) + { reply_c(ERR_BAD_CONNECTION); close(SS.data_sk); SS.data_sk = -1; diff --git a/command_loop.c b/command_loop.c index 5517451..455c4d6 100644 --- a/command_loop.c +++ b/command_loop.c @@ -39,9 +39,9 @@ void command_loop (void) { int l; - while (1) { - switch (next_command()) { - + do { + switch (next_command()) + { /* * Straightforward implementations. */ @@ -98,7 +98,8 @@ void command_loop (void) break; case FTP_TYPE: - switch (toupper(SS.arg[0])) { + switch (toupper(SS.arg[0])) + { case 'I': case 'A': case 'L': @@ -172,6 +173,6 @@ void command_loop (void) default: reply_c("500 Command not implemented.\r\n"); } - } + } while (1); } diff --git a/path.c b/path.c index af0b63b..60c8648 100644 --- a/path.c +++ b/path.c @@ -28,6 +28,7 @@ #include + /* * Apply new path components to an existing working directory. The working * directory is contained (full path) in wd, which is a buffer with a capacity diff --git a/send_file.c b/send_file.c index 4f38a9d..a97d636 100644 --- a/send_file.c +++ b/send_file.c @@ -60,13 +60,15 @@ void send_file (void) expand_arg(); fd = open(SS.arg, O_RDONLY, 0); - if (fd == -1) { + if (fd == -1) + { reply_c("550 Could not open file.\r\n"); goto finish; } err = fstat(fd, &st); - if (err == -1 || !S_ISREG(st.st_mode)) { + if (err == -1 || !S_ISREG(st.st_mode)) + { reply_c("550 Could not stat file.\r\n"); goto finish; } @@ -78,7 +80,8 @@ void send_file (void) if (SS.file_offset > 0) lseek(fd, SS.file_offset, SEEK_SET); - while (SS.file_offset < st.st_size) { + while (SS.file_offset < st.st_size) + { debug("Offset step: %lld", SS.file_offset); err = sendfile(SS.data_sk, fd, &SS.file_offset, INT_MAX); diff --git a/session.c b/session.c index 7bda606..68e0441 100644 --- a/session.c +++ b/session.c @@ -72,7 +72,8 @@ void init_session (int cmd_sk) * we won't know the real IP until we are connected to someone */ saddr_len = (socklen_t) sizeof(saddr); err = getsockname(cmd_sk, (struct sockaddr *) &saddr, &saddr_len); - if (err == -1) { + if (err == -1) + { reply_c(ERR_FINISH_MSG); fatal("trying to get my own address"); } @@ -83,7 +84,8 @@ void init_session (int cmd_sk) * and only one of these sockets is created per client. Thus, the * response to PASV is easier (and, maybe, quicker) */ sk = socket(PF_INET, SOCK_STREAM, 0); - if (sk == -1) { + if (sk == -1) + { reply_c(ERR_FINISH_MSG); fatal("Could not create data socket"); } @@ -93,12 +95,14 @@ void init_session (int cmd_sk) err = -1; setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i)); srand((unsigned int) times (NULL)); - for (i = 9; i > 0 && err == -1; i--) { + for (i = 9; i > 0 && err == -1; i--) + { port = get_random_port(); saddr.sin_port = htons(port); err = bind(sk, (struct sockaddr *) &saddr, saddr_len); } - if (i <= 0) { + if (i <= 0) + { reply_c(ERR_FINISH_MSG); fatal("Could not find any bindable port"); } @@ -106,7 +110,8 @@ void init_session (int cmd_sk) debug("Number of bind() tries for PASV port: %d\n", 9 - i); err = listen(sk, 1); - if (err == -1) { + if (err == -1) + { reply_c(ERR_FINISH_MSG); fatal("cannot listen on port"); } @@ -114,13 +119,13 @@ void init_session (int cmd_sk) /* As de passive socket is cached, we can do the same for the PASV * response string */ strncpy(address, inet_ntoa(saddr.sin_addr), 16); - for (i = 0; i < 16; i++) { + for (i = 0; i < 16; i++) + { if (address[i] == '.') address[i] = ','; } - SS.passive_len = snprintf(SS.passive_str, 64, - "227 =%s,%d,%d\r\n", address, port >> 8, - port & 0x00FF); + SS.passive_len = snprintf(SS.passive_str, 32, "227 =%s,%d,%d\r\n", + address, port >> 8, port & 0x00FF); debug("Passive data port: %d\n", port); debug("Passive string reply: %s", SS.passive_str); diff --git a/uftps.c b/uftps.c index 162ac76..9b85efe 100644 --- a/uftps.c +++ b/uftps.c @@ -116,6 +116,7 @@ int main (int argc, char **argv) "\t%s \n\n" "Where port must be between 1025 and 65535.\n", argv[0]); +#if 0 /* It's nearly impossible, to my knowledge, debug a program that forks. * First try to find the error using the debug() facility. Then, if you * really need to use a debugger, then replace the 0 with a 1 to compile @@ -130,27 +131,33 @@ int main (int argc, char **argv) init_session(cmd_sk); command_loop(); return EXIT_SUCCESS; +#endif /* Main loop (accepting connections) */ - while (1) { + do { cmd_sk = accept(bind_sk, (struct sockaddr *) &saddr, - (socklen_t *) &yes); - if (cmd_sk == -1) { + (socklen_t *) &yes); + if (cmd_sk == -1) + { if (errno == EINTR) continue; else fatal("Could not open command connection"); } - if (fork() == 0) { + + if (fork() == 0) + { /* Child */ close(bind_sk); init_session(cmd_sk); command_loop(); - } else { + } + else + { /* Parent */ close(cmd_sk); } - } + } while (1); return EXIT_SUCCESS; } diff --git a/uftps.h b/uftps.h index 017cf85..13e977e 100644 --- a/uftps.h +++ b/uftps.h @@ -69,11 +69,10 @@ struct _SessionScope char *arg; /* Pointer to comand line argument */ /* Buffers */ + char passive_str[32]; /* Cached reply for PASV */ char input[LINE_SIZE]; /* Incoming command buffer */ char aux[LINE_SIZE]; /* Auxiliary buffer */ char cwd[LINE_SIZE]; /* Current Working Directory */ - char passive_str[64]; /* Cached reply for PASV */ - }; extern struct _SessionScope SS; /* SS --> Session State */ -- 2.11.4.GIT