added -e option to specify token expiration time

bug fix to socket function
This commit is contained in:
Godmar Back 2018-04-24 20:07:50 -04:00
parent 94c94ca6de
commit 960fec20f8
3 changed files with 21 additions and 5 deletions

View File

@ -5,3 +5,4 @@
extern char *server_root;
extern bool silent_mode;
extern int token_expiration_time;

View File

@ -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;

View File

@ -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) {