/**************************************************************************** * lsrv_funcs2.c * functions for the license server - version 2 * includes ticket_reclaim(), do_validate(), and expanded handle_request */ #include #include #include #include #include #include #include #define SERVER_PORTNUM 2020 /* Our server's port number */ #define MSGLEN 128 /* Size of our datagrams */ #define TICKET_AVAIL 0 /* Slot is available for use */ #define MAXUSERS 3 /* Only 3 users for us */ #define oops(x) { perror(x); exit(-1); } #define RECLAIM_INTERVAL 5 /* Expire every 60 seconds */ /**************************************************************************** * Important variables */ int ticket_array[MAXUSERS]; /* Our ticket array */ int sd = -1; /* Our socket */ int num_tickets_out = 0; /* Number of tickets outstanding */ static char *do_hello(); static char *do_goodbye(); static char *do_validate(); /**************************************************************************** * setup() - initialize license server */ setup() { sd = make_dgram_server_socket(SERVER_PORTNUM); if ( sd == -1 ) oops("make socket"); free_all_tickets(); return sd; } free_all_tickets() { int i; for(i=0; i= MAXUSERS) return("FAIL no tickets available"); /* else find a free ticket and give it to client */ for(x = 0; xsin_addr), ntohs(clientp->sin_port) ); putc('\n', stderr); } /* ------------------------ VERSION 2 ADDITIONS HERE --------------------- */ /**************************************************************************** * do_validate * Validate client's ticket * IN msg_p message received from client * Results: ptr to response */ static char *do_validate(char *msg) { int pid, slot; /* components of ticket */ /* msg looks like VAD pid.slot - parse it and validate */ if (sscanf(msg+5,"%d.%d",&pid,&slot)==2 && ticket_array[slot] == pid) return("GOOD Valid ticket"); /* bad ticket */ narrate("Bogus ticket", msg+5, NULL); return("FAIL invalid ticket"); } /**************************************************************************** * ticket_reclaim * go through all tickets and reclaim ones belonging to dead processes * Results: none */ void ticket_reclaim() { int i; char tick[BUFSIZ]; for(i = 0; i < MAXUSERS; i++) { if((ticket_array[i] != TICKET_AVAIL) && (kill(ticket_array[i], 0) == -1) && (errno == ESRCH)) { /* Process is gone - free up slot */ sprintf(tick, "%d.%d", ticket_array[i],i); narrate("freeing", tick, NULL); ticket_array[i] = TICKET_AVAIL; num_tickets_out--; } } alarm(RECLAIM_INTERVAL); /* reset alarm clock */ }