From 960fec20f815d63341fb5308335719e288fa50c1 Mon Sep 17 00:00:00 2001 From: Godmar Back Date: Tue, 24 Apr 2018 20:07:50 -0400 Subject: [PATCH] added -e option to specify token expiration time bug fix to socket function --- src/globals.h | 1 + src/main.c | 14 ++++++++++++-- src/socket.c | 11 ++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/globals.h b/src/globals.h index 04a2d14..2051ca5 100644 --- a/src/globals.h +++ b/src/globals.h @@ -5,3 +5,4 @@ extern char *server_root; extern bool silent_mode; +extern int token_expiration_time; diff --git a/src/main.c b/src/main.c index 566fed9..35d4822 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ #include "globals.h" bool silent_mode = false; +int token_expiration_time = 24 * 60 * 60; // default token expiration time is 1 day /* * A non-concurrent, iterative server that serves one client at a time. @@ -41,7 +42,12 @@ server_loop(char *port_string) static void usage(char * av0) { - fprintf(stderr, "Usage: %s [-p port] [-R rootdir] [-h]\n", av0); + fprintf(stderr, "Usage: %s [-p port] [-R rootdir] [-h] [-e seconds]\n" + " -p port port number to bind to\n" + " -R rootdir root directory from which to serve files\n" + " -e seconds expiration time for tokens in seconds\n" + " -h display this help\n" + , av0); exit(EXIT_FAILURE); } @@ -50,12 +56,16 @@ main(int ac, char *av[]) { int opt; char *port_string = NULL; - while ((opt = getopt(ac, av, "hp:R:s")) != -1) { + while ((opt = getopt(ac, av, "hp:R:se:")) != -1) { switch (opt) { case 'p': port_string = optarg; break; + case 'e': + token_expiration_time = atoi(optarg); + break; + case 's': silent_mode = true; break; diff --git a/src/socket.c b/src/socket.c index 3fc02d2..99cc096 100644 --- a/src/socket.c +++ b/src/socket.c @@ -114,9 +114,14 @@ socket_open_bind_listen(char * port_number_string, int backlog) int socket_accept_client(int accepting_socket) { - struct sockaddr peer; + /* The address passed into accept must be large enough for either IPv4 & IPv6. + * Using a struct sockaddr is too small to hold a full IPv6 address and accept() + * would not return the full address. + */ + struct sockaddr_in6 peer; socklen_t peersize = sizeof(peer); - int client = accept(accepting_socket, &peer, &peersize); + + int client = accept(accepting_socket, (struct sockaddr *) &peer, &peersize); if (client == -1) { perror("accept"); return -1; @@ -127,7 +132,7 @@ socket_accept_client(int accepting_socket) */ if (!silent_mode) { char peer_addr[1024], peer_port[10]; - int rc = getnameinfo(&peer, peersize, + int rc = getnameinfo((struct sockaddr *) &peer, peersize, peer_addr, sizeof peer_addr, peer_port, sizeof peer_port, NI_NUMERICHOST | NI_NUMERICSERV); if (rc != 0) {