From 318987c10021078baad8aa2cf4463bf8ee1cf74e Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 10:33:39 +0200 Subject: [PATCH 1/8] Correct a typo: lenght -> length --- fiche.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fiche.c b/fiche.c index dfaca6e..6c10db2 100644 --- a/fiche.c +++ b/fiche.c @@ -107,8 +107,8 @@ void perform_connection(int listen_socket) pthread_t thread_id; struct sockaddr_in client_address; - int address_lenght = sizeof(client_address); - int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_lenght); + int address_length = sizeof(client_address); + int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_length); struct timeval timeout; timeout.tv_sec = 5; From 49ea4c7ccd736ed5cc42b7cb0075a5feebf2ca25 Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 11:51:25 +0200 Subject: [PATCH 2/8] Use MSG_DONTWAIT to use non blocking operation. This solves issue #14 --- fiche.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fiche.c b/fiche.c index 6c10db2..7267e8a 100644 --- a/fiche.c +++ b/fiche.c @@ -57,7 +57,7 @@ void *thread_connection(void *args) char buffer[BUFSIZE]; bzero(buffer, BUFSIZE); - int status = recv(connection_socket, buffer, BUFSIZE, MSG_WAITALL); + int status = recv(connection_socket, buffer, BUFSIZE, MSG_DONTWAIT); if (WHITELIST != NULL) if (check_whitelist(data.ip_address) == NULL) From d771d7bdfd3953a18e40734cd83a207b133a18c5 Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 14:36:36 +0200 Subject: [PATCH 3/8] Decrease the likeliness of name collisions --- fiche.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fiche.h b/fiche.h index cc8c554..2189374 100644 --- a/fiche.h +++ b/fiche.h @@ -50,13 +50,13 @@ char *BANFILE; char *WHITEFILE; char *WHITELIST; int PORT = 9999; -int SLUG_SIZE = 4; +int SLUG_SIZE = 8; int BUFSIZE = 32768; int QUEUE_SIZE = 500; char DOMAIN[128] = "http://localhost/"; unsigned int time_seed; -const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789"; +const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ"; struct thread_arguments { From 1bc2132e1d13cebf8b2dba84fcfebd34c0da94cf Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 14:48:21 +0200 Subject: [PATCH 4/8] Do not append a / at the end of the output path Solves issue #13 --- fiche.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fiche.c b/fiche.c index 7267e8a..8a1a370 100644 --- a/fiche.c +++ b/fiche.c @@ -286,9 +286,10 @@ void generate_url(char *buffer, char *slug, size_t slug_length, struct client_da int create_directory(char *slug) { - char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 1); + char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 2); strcpy(directory, BASEDIR); + strcat(directory, "/"); strcat(directory, slug); mkdir(BASEDIR, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP); @@ -303,8 +304,9 @@ int create_directory(char *slug) void save_to_file(char *slug, char *buffer, struct client_data data) { - char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + 1); + char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + 2); strcpy(directory, BASEDIR); + strcat(directory, "/"); strcat(directory, slug); strcat(directory, "/index.txt"); @@ -348,7 +350,7 @@ int check_protocol(char *buffer) void set_basedir() { BASEDIR = getenv("HOME"); - strcat(BASEDIR, "/code/"); + strcat(BASEDIR, "/code"); } void startup_message() @@ -387,8 +389,6 @@ void parse_parameters(int argc, char **argv) break; case 'o': BASEDIR = optarg; - if((BASEDIR[strlen(BASEDIR) - 1]) != '/') - strcat(BASEDIR, "/"); break; case 'l': LOG = optarg; From caa6f5ed6a35512f820d46026036fa80734be6b3 Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 14:59:08 +0200 Subject: [PATCH 5/8] Former patch had a small bug, this has been corrected --- fiche.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fiche.c b/fiche.c index 8a1a370..eaa09b5 100644 --- a/fiche.c +++ b/fiche.c @@ -286,7 +286,7 @@ void generate_url(char *buffer, char *slug, size_t slug_length, struct client_da int create_directory(char *slug) { - char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 2); + char *directory = malloc(strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1); strcpy(directory, BASEDIR); strcat(directory, "/"); @@ -304,7 +304,7 @@ int create_directory(char *slug) void save_to_file(char *slug, char *buffer, struct client_data data) { - char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + 2); + char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + sizeof(char) + 1 ); strcpy(directory, BASEDIR); strcat(directory, "/"); strcat(directory, slug); From 8ccfcc8c06dd2ab6bd7dd44e4673bc8e041ac7a7 Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 15:55:09 +0200 Subject: [PATCH 6/8] Avoid using strcpy and strcat That will also remove the warnings given by picky compilers --- fiche.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/fiche.c b/fiche.c index eaa09b5..60c7fde 100644 --- a/fiche.c +++ b/fiche.c @@ -288,9 +288,7 @@ int create_directory(char *slug) { char *directory = malloc(strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1); - strcpy(directory, BASEDIR); - strcat(directory, "/"); - strcat(directory, slug); + snprintf(directory, strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1, "%s%s%s", BASEDIR, "/", slug); mkdir(BASEDIR, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP); int result = mkdir(directory, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP); @@ -304,11 +302,9 @@ int create_directory(char *slug) void save_to_file(char *slug, char *buffer, struct client_data data) { - char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + sizeof(char) + 1 ); - strcpy(directory, BASEDIR); - strcat(directory, "/"); - strcat(directory, slug); - strcat(directory, "/index.txt"); + char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1 ); + + snprintf(directory, strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1, "%s%s%s%s", BASEDIR , "/", slug, "/index.txt"); FILE *fp; fp = fopen(directory, "w"); @@ -350,7 +346,7 @@ int check_protocol(char *buffer) void set_basedir() { BASEDIR = getenv("HOME"); - strcat(BASEDIR, "/code"); + strncat(BASEDIR, "/code", 5 * sizeof(char)); } void startup_message() From 73bc067641372d4c0001601f6fef9bfa5b9db7ff Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Thu, 27 Aug 2015 16:37:13 +0200 Subject: [PATCH 7/8] Use arc4random on BSD systems to avoid complaints from the compiler --- fiche.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fiche.c b/fiche.c index 60c7fde..b9e5b32 100644 --- a/fiche.c +++ b/fiche.c @@ -24,6 +24,7 @@ $ cat fiche.c | nc localhost 9999 ------------------------------------------------------------------------------- */ +#include #include "fiche.h" int main(int argc, char **argv) @@ -271,13 +272,21 @@ void generate_url(char *buffer, char *slug, size_t slug_length, struct client_da for (i = 0; i <= SLUG_SIZE - 1; i++) { +#if defined(BSD) + int symbol_id = arc4random() % strlen(symbols); +#else int symbol_id = rand_r(&time_seed) % strlen(symbols); +#endif slug[i] = symbols[symbol_id]; } while (create_directory(slug) == -1) { +#if defined(BSD) + int symbol_id = arc4random() % strlen(symbols); +#else int symbol_id = rand_r(&time_seed) % strlen(symbols); +#endif slug[strlen(slug)] = symbols[symbol_id]; } From 047ecf672c3c9809ff885b2e1491c1459c3ba3fb Mon Sep 17 00:00:00 2001 From: Renaud Allard Date: Sat, 29 Aug 2015 11:34:32 +0200 Subject: [PATCH 8/8] Add -e option to use the extended charset I committed before. "reduced" charset makes it easier to remember the URL by default --- fiche.c | 11 ++++++++--- fiche.h | 10 ++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/fiche.c b/fiche.c index b9e5b32..89141b4 100644 --- a/fiche.c +++ b/fiche.c @@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/ ------------------------------------------------------------------------------- -usage: fiche [-pbsdolBuw]. - [-d domain] [-p port] [-s slug size] +usage: fiche [-epbsdolBuw]. + [-e] [-d domain] [-p port] [-s slug size] [-o output directory] [-B buffer size] [-u user name] [-l log file] [-b banlist] [-w whitelist] +-e option is for using an extended character set for the URL + Compile with Makefile or manually with -O2 and -pthread flags. To install use `make install` command. @@ -371,9 +373,12 @@ void parse_parameters(int argc, char **argv) { int c; - while ((c = getopt (argc, argv, "p:b:s:d:o:l:B:u:w:")) != -1) + while ((c = getopt (argc, argv, "ep:b:s:d:o:l:B:u:w:")) != -1) switch (c) { + case 'e': + snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + break; case 'd': snprintf(DOMAIN, sizeof DOMAIN, "%s%s%s", "http://", optarg, "/"); break; diff --git a/fiche.h b/fiche.h index 2189374..9aca10b 100644 --- a/fiche.h +++ b/fiche.h @@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/ ------------------------------------------------------------------------------- -usage: fiche [-pbsdolBuw]. - [-d domain] [-p port] [-s slug size] +usage: fiche [-epbsdolBuw]. + [-e] [-d domain] [-p port] [-s slug size] [-o output directory] [-B buffer size] [-u user name] [-l log file] [-b banlist] [-w whitelist] +-e option is for using an extended character set for the URL + Compile with Makefile or manually with -O2 and -pthread flags. To install use `make install` command. @@ -50,13 +52,13 @@ char *BANFILE; char *WHITEFILE; char *WHITELIST; int PORT = 9999; -int SLUG_SIZE = 8; +int SLUG_SIZE = 4; int BUFSIZE = 32768; int QUEUE_SIZE = 500; char DOMAIN[128] = "http://localhost/"; +char symbols[67] = "abcdefghijklmnopqrstuvwxyz0123456789"; unsigned int time_seed; -const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ"; struct thread_arguments {