added logging - fiche - A pastebin adjusted for gopher use
(HTM) git clone git://vernunftzentrum.de/fiche.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit d25e9baf02dc4a59d00e5878e2463f506e992413
(DIR) parent 716453901872eff2a0889d2a2eb73c0b16730968
(HTM) Author: solusipse <solus1ps3@gmail.com>
Date: Mon, 9 Sep 2013 21:02:33 +0200
added logging
Diffstat:
fiche.c | 48 ++++++++++++++++++++++++-------
fiche.h | 7 +++++--
2 files changed, 42 insertions(+), 13 deletions(-)
---
(DIR) diff --git a/fiche.c b/fiche.c
@@ -10,8 +10,9 @@ Live example: http://code.solusipse.net/
-------------------------------------------------------------------------------
usage: fiche [-bdpqs].
- [-d host_domain.com] [-p port] [-s slug_size]
- [-o output_directory] [-b buffer_size] [-q queue_size]
+ [-d domain] [-p port] [-s slug_size]
+ [-o output directory] [-b buffer_size]
+ [-l log file] [-q queue_size]
Compile with Makefile or manually with -O2 and -pthread flags.
To install use `make install` command.
@@ -59,10 +60,11 @@ void *thread_connection(void *args)
if (status != -1)
{
- get_client_address(client_address);
char slug[SLUG_SIZE];
generate_url(buffer, slug);
+ get_client_address(client_address, slug);
+
char response[strlen(slug) + strlen(DOMAIN) + 2];
strcpy(response, DOMAIN);
strcat(response, slug);
@@ -71,7 +73,7 @@ void *thread_connection(void *args)
}
else
{
- get_client_address(client_address);
+ get_client_address(client_address, NULL);
printf("Invalid connection.\n");
write(connection_socket, "Use netcat.\n", 13);
}
@@ -119,10 +121,8 @@ void display_date()
printf("%s", asctime(timeinfo));
}
-void get_client_address(struct sockaddr_in client_address)
+void get_client_address(struct sockaddr_in client_address, char *slug)
{
- display_line();
-
struct hostent *hostp;
char *hostaddrp;
@@ -134,6 +134,25 @@ void get_client_address(struct sockaddr_in client_address)
display_date();
printf("Client: %s (%s)\n", hostaddrp, hostp->h_name);
+
+ if (LOG != NULL)
+ save_log(slug, hostaddrp, hostp->h_name);
+}
+
+void save_log(char *slug, char *hostaddrp, char *h_name)
+{
+ char contents[256];
+ snprintf(contents, sizeof contents, "%s:%s:%s\n", slug, hostaddrp, h_name);
+
+ if (slug != NULL)
+ snprintf(contents, sizeof contents, "%s:%s:%s\n", slug, hostaddrp, h_name);
+ else
+ snprintf(contents, sizeof contents, "%s:%s:%s\n", "error", hostaddrp, h_name);
+
+ FILE *fp;
+ fp = fopen(LOG, "a");
+ fprintf(fp, "%s", contents);
+ fclose(fp);
}
int create_socket()
@@ -208,6 +227,8 @@ void save_to_file(char *slug, char *buffer)
fprintf(fp, "%s", buffer);
fclose(fp);
+ display_line();
+
printf("Saved to: %s\n", directory);
free(directory);
}
@@ -220,16 +241,16 @@ void set_basedir()
void startup_message()
{
- printf("Fiche started listening on port %d.\n", PORT);
printf("Domain name: %s\n", DOMAIN);
printf("Saving files to: %s\n", BASEDIR);
+ printf("Fiche started listening on port %d.\n", PORT);
}
void parse_parameters(int argc, char **argv)
{
int c;
- while ((c = getopt (argc, argv, "p:b:q:s:d:o:")) != -1)
+ while ((c = getopt (argc, argv, "p:b:q:s:d:o:l:")) != -1)
switch (c)
{
case 'd':
@@ -255,10 +276,15 @@ void parse_parameters(int argc, char **argv)
if((BASEDIR[strlen(BASEDIR) - 1]) != '/')
strcat(BASEDIR, "/");
break;
+ case 'l':
+ LOG = optarg;
+ printf("Log file: %s\n", LOG);
+ break;
default:
printf("usage: fiche [-bdpqs].\n");
- printf(" [-d host_domain.com] [-p port] [-s slug_size]\n");
- printf(" [-o output_directory] [-b buffer_size] [-q queue_size]\n");
+ printf(" [-d domain] [-p port] [-s slug_size]\n");
+ printf(" [-o output directory] [-b buffer_size]\n");
+ printf(" [-l log file] [-q queue_size]\n");
exit(1);
}
}
\ No newline at end of file
(DIR) diff --git a/fiche.h b/fiche.h
@@ -39,13 +39,15 @@ $ cat fiche.c | nc localhost 9999
#include <sys/socket.h>
#include <netinet/in.h>
-int time_seed;
+char *LOG;
char *BASEDIR;
int PORT = 9999;
int SLUG_SIZE = 4;
int BUFSIZE = 8192;
int QUEUE_SIZE = 100;
char DOMAIN[128] = "http://localhost/";
+
+int time_seed;
const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
int create_socket();
@@ -55,13 +57,14 @@ void bind_to_port(int listen_socket, struct sockaddr_in serveraddr);
void display_line(){printf("====================================\n");}
void error(){perror("ERROR"); exit(1);}
void display_date();
-void get_client_address(struct sockaddr_in client_address);
+void get_client_address(struct sockaddr_in client_address, char *slug);
void perform_connection(int listen_socket);
void generate_url(char *buffer, char *slug);
void save_to_file(char *buffer, char *slug);
void startup_message();
void set_basedir();
void parse_parameters(int argc, char **argv);
+void save_log(char *slug, char *hostaddrp, char *h_name);
struct sockaddr_in set_address(struct sockaddr_in serveraddr);