: nply/fiche2

commit 249413374d23479c3d485a565736639878c127da
parent 33f00487d0baba30eac8db396af42919e2715601
Author: solusipse <solus1ps3@gmail.com>
Date: Thu, 26 Sep 2013 13:06:52 +0200

changed printing policy
2 files changed, 36 insertions(+), 41 deletions(-)
M
fiche.c
|
48
+++++++++++++++++++++---------------------------
M
fiche.h
|
29
+++++++++++++++--------------
diff --git a/fiche.c b/fiche.c
@@ -63,8 +63,7 @@ void *thread_connection(void *args)
     if (WHITELIST != NULL)
         if (check_whitelist(data.ip_address) == NULL)
         {
-            printf("Rejected connection from unknown user.\n");
-            display_line();
+            display_info(data, NULL, "Rejected connection from unknown user.");
             save_log(NULL, data.ip_address, data.hostname);
             write(connection_socket, "You are not whitelisted!\n", 26);
             close(connection_socket);

@@ -74,8 +73,7 @@ void *thread_connection(void *args)
     if (BANLIST != NULL)
         if (check_banlist(data.ip_address) != NULL)
         {
-            printf("Rejected connection from banned user.\n");
-            display_line();
+            display_info(data, NULL, "Rejected connection from banned user.");
             save_log(NULL, data.ip_address, data.hostname);
             write(connection_socket, "You are banned!\n", 17);
             close(connection_socket);

@@ -83,19 +81,12 @@ void *thread_connection(void *args)
         }
 
     if (check_protocol(buffer) == 1)
-    {
-        printf("Rejected due to wrong protocol.\n");
-        display_line();
-        save_log(NULL, data.ip_address, data.hostname);
-        write(connection_socket, "Use netcat!", 11);
-        close(connection_socket);
-        pthread_exit(NULL);
-    }
+        status = -1;
 
     if (status != -1)
     {
         char slug[SLUG_SIZE+8];
-        generate_url(buffer, slug, SLUG_SIZE+8);
+        generate_url(buffer, slug, SLUG_SIZE+8, data);
         save_log(slug, data.ip_address, data.hostname);
         char response[strlen(slug) + strlen(DOMAIN) + 2];
         snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug);

@@ -103,10 +94,9 @@ void *thread_connection(void *args)
     }
     else
     {
-        printf("Invalid connection.\n");
-        display_line();
+        display_info(data, NULL, "Invalid connection.");
         save_log(NULL, data.ip_address, data.hostname);
-        write(connection_socket, "Use netcat.\n", 13);
+        write(connection_socket, "Use netcat.\n", 12);
     }
 
     close(connection_socket);

@@ -181,11 +171,6 @@ struct client_data get_client_address(struct sockaddr_in client_address)
     else
         data.ip_address = hostaddrp;
 
-    display_date();
-    printf("Client: %s (%s)\n", data.ip_address, data.hostname);
-
-    
-
     return data;
 }
 

@@ -207,6 +192,16 @@ void save_log(char *slug, char *hostaddrp, char *h_name)
     }
 }
 
+void display_info(struct client_data data, char *slug, char *message)
+{
+    if (slug == NULL)
+        printf("%s\n", message);
+    else printf("Saved to: %s\n", slug);
+    display_date();
+    printf("Client: %s (%s)\n", data.ip_address, data.hostname);
+    display_line();
+}
+
 char *check_banlist(char *ip_address)
 {
     load_list(BANFILE, 0);

@@ -265,7 +260,7 @@ void bind_to_port(int listen_socket, struct sockaddr_in server_address)
         error("ERROR while starting listening");
 }
 
-void generate_url(char *buffer, char *slug, size_t slug_length)
+void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data)
 {
     int i;
     memset(slug, '\0', slug_length);

@@ -282,7 +277,7 @@ void generate_url(char *buffer, char *slug, size_t slug_length)
         slug[strlen(slug)] = symbols[symbol_id];
     }
 
-    save_to_file(slug, buffer);
+    save_to_file(slug, buffer, data);
 }
 
 int create_directory(char *slug)

@@ -302,7 +297,7 @@ int create_directory(char *slug)
     return result;
 }
 
-void save_to_file(char *slug, char *buffer)
+void save_to_file(char *slug, char *buffer, struct client_data data)
 {
     char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + 1);
     strcpy(directory, BASEDIR);

@@ -315,9 +310,8 @@ void save_to_file(char *slug, char *buffer)
     fclose(fp);
 
     change_owner(directory);
+    display_info(data, directory, "");
 
-    printf("Saved to: %s\n", directory);
-    display_line();
     free(directory);
 }
 

@@ -339,7 +333,7 @@ void set_uid_gid(char *username)
 
 int check_protocol(char *buffer)
 {
-    if (strlen(buffer) < 1)
+    if (strlen(buffer) < 3)
         return 1;
     if ((strncmp(buffer, "GET", 3) == 0)||(strncmp(buffer, "POST", 4) == 0))
         if (strstr(buffer, "HTTP/1."))
diff --git a/fiche.h b/fiche.h
@@ -58,6 +58,18 @@ char DOMAIN[128] = "http://localhost/";
 int time_seed;
 const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
 
+struct thread_arguments
+{
+	int connection_socket;
+	struct sockaddr_in client_address;
+};
+
+struct client_data
+{
+	char *ip_address;
+	char *hostname;
+};
+
 int create_socket();
 int create_directory(char *slug);
 int check_protocol(char *buffer);

@@ -67,8 +79,9 @@ void display_line(){printf("====================================\n");}
 void error(char *error_code){perror(error_code); exit(1);}
 void display_date();
 void perform_connection(int listen_socket);
-void generate_url(char *buffer, char *slug, size_t slug_length);
-void save_to_file(char *buffer, char *slug);
+void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data);
+void save_to_file(char *buffer, char *slug, struct client_data data);
+void display_info(struct client_data data, char *slug, char *message);
 void startup_message();
 void set_basedir();
 void load_list(char *file_path, int type);

@@ -85,16 +98,4 @@ char *get_date();
 struct sockaddr_in set_address(struct sockaddr_in serveraddr);
 struct client_data get_client_address(struct sockaddr_in client_address);
 
-struct thread_arguments
-{
-	int connection_socket;
-	struct sockaddr_in client_address;
-};
-
-struct client_data
-{
-	char *ip_address;
-	char *hostname;
-};
-
 #endif