added details to error messages
This commit is contained in:
20
fiche.c
20
fiche.c
@@ -117,16 +117,16 @@ void perform_connection(int listen_socket)
|
|||||||
timeout.tv_usec = 0;
|
timeout.tv_usec = 0;
|
||||||
|
|
||||||
if (setsockopt (connection_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
|
if (setsockopt (connection_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
|
||||||
error();
|
error("ERROR while setting setsockopt timeout");
|
||||||
if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
|
if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
|
||||||
error();
|
error("ERROR while setting setsockopt timeout");
|
||||||
|
|
||||||
struct thread_arguments arguments;
|
struct thread_arguments arguments;
|
||||||
arguments.connection_socket = connection_socket;
|
arguments.connection_socket = connection_socket;
|
||||||
arguments.client_address = client_address;
|
arguments.client_address = client_address;
|
||||||
|
|
||||||
if (pthread_create(&thread_id, NULL, &thread_connection, &arguments) != 0)
|
if (pthread_create(&thread_id, NULL, &thread_connection, &arguments) != 0)
|
||||||
error();
|
error("ERROR on thread creation");
|
||||||
else
|
else
|
||||||
pthread_detach(thread_id);
|
pthread_detach(thread_id);
|
||||||
}
|
}
|
||||||
@@ -154,10 +154,12 @@ struct client_data get_client_address(struct sockaddr_in client_address)
|
|||||||
char *hostaddrp;
|
char *hostaddrp;
|
||||||
|
|
||||||
hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET);
|
hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET);
|
||||||
if (hostp == NULL) error();
|
if (hostp == NULL)
|
||||||
|
nerror("ERROR: Couldn't obtain client's address");
|
||||||
|
|
||||||
hostaddrp = inet_ntoa(client_address.sin_addr);
|
hostaddrp = inet_ntoa(client_address.sin_addr);
|
||||||
if (hostaddrp == NULL) error();
|
if (hostaddrp == NULL)
|
||||||
|
nerror("ERROR: Couldn't obtain client's address");
|
||||||
|
|
||||||
display_date();
|
display_date();
|
||||||
printf("Client: %s (%s)\n", hostaddrp, hostp->h_name);
|
printf("Client: %s (%s)\n", hostaddrp, hostp->h_name);
|
||||||
@@ -223,7 +225,7 @@ int create_socket()
|
|||||||
{
|
{
|
||||||
int lsocket = socket(AF_INET, SOCK_STREAM, 0);
|
int lsocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (lsocket < 0)
|
if (lsocket < 0)
|
||||||
error();
|
error("ERROR: Couldn't open socket");
|
||||||
else return lsocket;
|
else return lsocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,9 +241,9 @@ struct sockaddr_in set_address(struct sockaddr_in server_address)
|
|||||||
void bind_to_port(int listen_socket, struct sockaddr_in server_address)
|
void bind_to_port(int listen_socket, struct sockaddr_in server_address)
|
||||||
{
|
{
|
||||||
if (bind(listen_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
|
if (bind(listen_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
|
||||||
error();
|
error("ERROR while binding to port");
|
||||||
if (listen(listen_socket, QUEUE_SIZE) < 0)
|
if (listen(listen_socket, QUEUE_SIZE) < 0)
|
||||||
error();
|
error("ERROR while starting listening");
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_url(char *buffer, char *slug)
|
void generate_url(char *buffer, char *slug)
|
||||||
@@ -310,7 +312,7 @@ void set_uid_gid(char *username)
|
|||||||
{
|
{
|
||||||
struct passwd *userdata = getpwnam(username);
|
struct passwd *userdata = getpwnam(username);
|
||||||
if (userdata == NULL)
|
if (userdata == NULL)
|
||||||
error();
|
error("Provided user doesn't exist");
|
||||||
|
|
||||||
UID = userdata->pw_uid;
|
UID = userdata->pw_uid;
|
||||||
GID = userdata->pw_gid;
|
GID = userdata->pw_gid;
|
||||||
|
|||||||
3
fiche.h
3
fiche.h
@@ -63,7 +63,8 @@ int create_directory(char *slug);
|
|||||||
|
|
||||||
void bind_to_port(int listen_socket, struct sockaddr_in serveraddr);
|
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(char *error_code){perror(error_code); exit(1);}
|
||||||
|
void nerror(char *error_code){perror(error_code);}
|
||||||
void display_date();
|
void display_date();
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user