added logging
This commit is contained in:
48
fiche.c
48
fiche.c
@@ -10,8 +10,9 @@ Live example: http://code.solusipse.net/
|
|||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
usage: fiche [-bdpqs].
|
usage: fiche [-bdpqs].
|
||||||
[-d host_domain.com] [-p port] [-s slug_size]
|
[-d domain] [-p port] [-s slug_size]
|
||||||
[-o output_directory] [-b buffer_size] [-q queue_size]
|
[-o output directory] [-b buffer_size]
|
||||||
|
[-l log file] [-q queue_size]
|
||||||
|
|
||||||
Compile with Makefile or manually with -O2 and -pthread flags.
|
Compile with Makefile or manually with -O2 and -pthread flags.
|
||||||
To install use `make install` command.
|
To install use `make install` command.
|
||||||
@@ -59,10 +60,11 @@ void *thread_connection(void *args)
|
|||||||
|
|
||||||
if (status != -1)
|
if (status != -1)
|
||||||
{
|
{
|
||||||
get_client_address(client_address);
|
|
||||||
char slug[SLUG_SIZE];
|
char slug[SLUG_SIZE];
|
||||||
generate_url(buffer, slug);
|
generate_url(buffer, slug);
|
||||||
|
|
||||||
|
get_client_address(client_address, slug);
|
||||||
|
|
||||||
char response[strlen(slug) + strlen(DOMAIN) + 2];
|
char response[strlen(slug) + strlen(DOMAIN) + 2];
|
||||||
strcpy(response, DOMAIN);
|
strcpy(response, DOMAIN);
|
||||||
strcat(response, slug);
|
strcat(response, slug);
|
||||||
@@ -71,7 +73,7 @@ void *thread_connection(void *args)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
get_client_address(client_address);
|
get_client_address(client_address, NULL);
|
||||||
printf("Invalid connection.\n");
|
printf("Invalid connection.\n");
|
||||||
write(connection_socket, "Use netcat.\n", 13);
|
write(connection_socket, "Use netcat.\n", 13);
|
||||||
}
|
}
|
||||||
@@ -119,10 +121,8 @@ void display_date()
|
|||||||
printf("%s", asctime(timeinfo));
|
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;
|
struct hostent *hostp;
|
||||||
char *hostaddrp;
|
char *hostaddrp;
|
||||||
|
|
||||||
@@ -134,6 +134,25 @@ void get_client_address(struct sockaddr_in client_address)
|
|||||||
|
|
||||||
display_date();
|
display_date();
|
||||||
printf("Client: %s (%s)\n", hostaddrp, hostp->h_name);
|
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()
|
int create_socket()
|
||||||
@@ -208,6 +227,8 @@ void save_to_file(char *slug, char *buffer)
|
|||||||
fprintf(fp, "%s", buffer);
|
fprintf(fp, "%s", buffer);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
display_line();
|
||||||
|
|
||||||
printf("Saved to: %s\n", directory);
|
printf("Saved to: %s\n", directory);
|
||||||
free(directory);
|
free(directory);
|
||||||
}
|
}
|
||||||
@@ -220,16 +241,16 @@ void set_basedir()
|
|||||||
|
|
||||||
void startup_message()
|
void startup_message()
|
||||||
{
|
{
|
||||||
printf("Fiche started listening on port %d.\n", PORT);
|
|
||||||
printf("Domain name: %s\n", DOMAIN);
|
printf("Domain name: %s\n", DOMAIN);
|
||||||
printf("Saving files to: %s\n", BASEDIR);
|
printf("Saving files to: %s\n", BASEDIR);
|
||||||
|
printf("Fiche started listening on port %d.\n", PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_parameters(int argc, char **argv)
|
void parse_parameters(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c;
|
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)
|
switch (c)
|
||||||
{
|
{
|
||||||
case 'd':
|
case 'd':
|
||||||
@@ -255,10 +276,15 @@ void parse_parameters(int argc, char **argv)
|
|||||||
if((BASEDIR[strlen(BASEDIR) - 1]) != '/')
|
if((BASEDIR[strlen(BASEDIR) - 1]) != '/')
|
||||||
strcat(BASEDIR, "/");
|
strcat(BASEDIR, "/");
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
LOG = optarg;
|
||||||
|
printf("Log file: %s\n", LOG);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
printf("usage: fiche [-bdpqs].\n");
|
printf("usage: fiche [-bdpqs].\n");
|
||||||
printf(" [-d host_domain.com] [-p port] [-s slug_size]\n");
|
printf(" [-d domain] [-p port] [-s slug_size]\n");
|
||||||
printf(" [-o output_directory] [-b buffer_size] [-q queue_size]\n");
|
printf(" [-o output directory] [-b buffer_size]\n");
|
||||||
|
printf(" [-l log file] [-q queue_size]\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
7
fiche.h
7
fiche.h
@@ -39,13 +39,15 @@ $ cat fiche.c | nc localhost 9999
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
int time_seed;
|
char *LOG;
|
||||||
char *BASEDIR;
|
char *BASEDIR;
|
||||||
int PORT = 9999;
|
int PORT = 9999;
|
||||||
int SLUG_SIZE = 4;
|
int SLUG_SIZE = 4;
|
||||||
int BUFSIZE = 8192;
|
int BUFSIZE = 8192;
|
||||||
int QUEUE_SIZE = 100;
|
int QUEUE_SIZE = 100;
|
||||||
char DOMAIN[128] = "http://localhost/";
|
char DOMAIN[128] = "http://localhost/";
|
||||||
|
|
||||||
|
int time_seed;
|
||||||
const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
|
const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
|
||||||
int create_socket();
|
int create_socket();
|
||||||
@@ -55,13 +57,14 @@ void bind_to_port(int listen_socket, struct sockaddr_in serveraddr);
|
|||||||
void display_line(){printf("====================================\n");}
|
void display_line(){printf("====================================\n");}
|
||||||
void error(){perror("ERROR"); exit(1);}
|
void error(){perror("ERROR"); exit(1);}
|
||||||
void display_date();
|
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 perform_connection(int listen_socket);
|
||||||
void generate_url(char *buffer, char *slug);
|
void generate_url(char *buffer, char *slug);
|
||||||
void save_to_file(char *buffer, char *slug);
|
void save_to_file(char *buffer, char *slug);
|
||||||
void startup_message();
|
void startup_message();
|
||||||
void set_basedir();
|
void set_basedir();
|
||||||
void parse_parameters(int argc, char **argv);
|
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);
|
struct sockaddr_in set_address(struct sockaddr_in serveraddr);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user