\File{deal.c},{12:14},{Feb  6 1997}
\L{\LB{\K{\#include} \S{}\"squid.h\"\SE{}}}
\L{\LB{}}
\L{\LB{\C{}\/* global variable that points to the main host table *\/\CE{}}}
\L{\LB{\K{struct} hostinfo *tablePtr;}}
\L{\LB{}}
\L{\LB{\C{}\/* add a host to the host table, check for duplicates *\/\CE{}}}
\L{\LB{\K{int} addhost(\K{char} address[MAXADDR], time\_t ntime, \K{int} np)\{}}
\L{\LB{}}
\L{\LB{  \C{}\/* go through the table and check that the address is not already there *\/\CE{}}}
\L{\LB{  \K{struct} hostinfo *machinfo, *currPtr = tablePtr;}}
\L{\LB{  \K{while} (currPtr != NULL) \{}}
\L{\LB{    \K{if} ((strcmp(currPtr\-\>addr,address)) == 0)\{}}
\L{\LB{      debug(1,0,\S{}\"Duplicate address \%s.\!n\"\SE{},address);}}
\L{\LB{      \K{return}(1);}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \{}}
\L{\LB{      currPtr = currPtr\-\>nexthost;}}
\L{\LB{    \}}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* malloc space for a new entry in the table *\/\CE{}}}
\L{\LB{  machinfo = (\K{struct} hostinfo *) malloc(\K{sizeof}(\K{struct} hostinfo));}}
\L{\LB{  \K{if} (machinfo == NULL)\{}}
\L{\LB{    perror(\S{}\"malloc\"\SE{});}}
\L{\LB{    \K{return}(\-1);}}
\L{\LB{  \}}}
\L{\LB{  \K{else} \{}}
\L{\LB{    \C{}\/* put the new address and time in the malloc\'d space, and point the }}
\L{\LB{     * entry\'s nexthost pointer to the beginning of the old table *\/\CE{} }}
\L{\LB{    strcpy(machinfo\-\>addr,address);}}
\L{\LB{    machinfo\-\>time = ntime;}}
\L{\LB{    machinfo\-\>num\_points = np;}}
\L{\LB{    machinfo\-\>nexthost = tablePtr;}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* change the table to point to the new entry *\/\CE{}}}
\L{\LB{  tablePtr = machinfo;}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* delete a host from the host table if it is there *\/\CE{}}}
\L{\LB{\K{int} delhost(\K{char} address[MAXADDR]) \{}}
\L{\LB{  \K{int} found = 1;}}
\L{\LB{  \K{struct} hostinfo *freePtr, **lastPtr = \&tablePtr;}}
\L{\LB{}}
\L{\LB{  \C{}\/* go through the table, and if you find a match, point the previous entry\'s}}
\L{\LB{   * nexthost pointer to the following entry, then free the memory that }}
\L{\LB{   * contained the current entry *\/\CE{}}}
\L{\LB{  \K{while} (*lastPtr != NULL) \{}}
\L{\LB{    \K{if} ((strcmp((*lastPtr)\-\>addr,address)) == 0) \{}}
\L{\LB{      freePtr = *lastPtr;}}
\L{\LB{      *lastPtr = (*lastPtr)\-\>nexthost;}}
\L{\LB{      free(freePtr);}}
\L{\LB{}}
\L{\LB{      \C{}\/* set found to zero and stop looking through the table *\/\CE{}}}
\L{\LB{      found = 0;}}
\L{\LB{      \K{break};}}
\L{\LB{    \}}}
\L{\LB{    lastPtr = \&(*lastPtr)\-\>nexthost;}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* return zero if the host is found, and one if it is not *\/\CE{}}}
\L{\LB{  \K{return}(found);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* print a list that represents the current network state *\/\CE{}}}
\L{\LB{\K{int} list(\K{int} newsockfd) \{}}
\L{\LB{  \K{int} reply\_len;}}
\L{\LB{  \K{char} reply[MAXLINE] = \S{}\"\!0\"\SE{};}}
\L{\LB{  \K{struct} hostinfo *currPtr = tablePtr;}}
\L{\LB{}}
\L{\LB{  \C{}\/* print header line *\/\CE{}}}
\L{\LB{  sprintf(reply, \S{}\"200 Listing follows:\!n\"\SE{});}}
\L{\LB{  reply\_len = strlen(reply);}}
\L{\LB{  write(newsockfd,reply,reply\_len);}}
\L{\LB{}}
\L{\LB{  \C{}\/* go through the host table and print out a line for every entry *\/\CE{}}}
\L{\LB{  \K{while} (currPtr != NULL) \{}}
\L{\LB{    sprintf(reply, \S{}\"\%s \%d \%d\!n\"\SE{}, currPtr\-\>addr,(\K{int}) currPtr\-\>time, currPtr\-\>num\_points);}}
\L{\LB{    reply\_len = strlen(reply);}}
\L{\LB{    write(newsockfd,reply,reply\_len);}}
\L{\LB{    currPtr = currPtr\-\>nexthost;}}
\L{\LB{  \}}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* print out all the changes since time stime *\/\CE{}}}
\L{\LB{\K{int} since(\K{int} newsockfd, time\_t stime) \{}}
\L{\LB{  \K{int} reply\_len;}}
\L{\LB{  FILE *logPtr;}}
\L{\LB{  \K{char} reply[MAXLINE] = \S{}\"\!0\"\SE{};}}
\L{\LB{  \K{char} x[1], f[MAXADDR], s[MAXLINE];}}
\L{\LB{  \K{int} *ftimePtr = malloc(\K{sizeof}(\K{int}));}}
\L{\LB{}}
\L{\LB{  \C{}\/* print the header line *\/\CE{}}}
\L{\LB{  sprintf(reply,\S{}\"200 Changes since \%i:\!n\"\SE{},(\K{int}) stime);}}
\L{\LB{  reply\_len = strlen(reply);}}
\L{\LB{  write(newsockfd,reply,reply\_len);}}
\L{\LB{}}
\L{\LB{  \C{}\/* open the log file for reading *\/\CE{}}}
\L{\LB{  \K{if} ((logPtr = fopen(NLOG\_FILE,\S{}\"r\"\SE{})) == NULL) \{}}
\L{\LB{    perror(\S{}\"Error opening NLOG\_FILE\"\SE{});}}
\L{\LB{    exit(\-1);}}
\L{\LB{  \}}}
\L{\LB{ }}
\L{\LB{  \C{}\/* go through the log file and parse it *\/\CE{}}}
\L{\LB{  \K{while} (fgets(s,\K{sizeof}(s),logPtr) != NULL) \{}}
\L{\LB{    sscanf(s,\S{}\"\%s \%s \%d\"\SE{},x,f,ftimePtr);}}
\L{\LB{}}
\L{\LB{    \C{}\/* if the time the change was made is since stime, print out that line *\/\CE{}}}
\L{\LB{    \K{if} (stime \<= *ftimePtr) \{}}
\L{\LB{      sprintf(reply, \S{}\"\%s\"\SE{},s);}}
\L{\LB{      reply\_len = strlen(reply);}}
\L{\LB{      write(newsockfd,reply,reply\_len);}}
\L{\LB{    \}}}
\L{\LB{  \}}}
\L{\LB{  fclose(logPtr);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* dump the current network state to the dump file *\/\CE{}}}
\L{\LB{\K{int} dump() \{}}
\L{\LB{  FILE *dumpPtr;}}
\L{\LB{  \K{int} dumpfd;}}
\L{\LB{}}
\L{\LB{  \C{}\/* open the dump file to write *\/\CE{}}}
\L{\LB{  \K{if} ((dumpPtr = fopen(DUMP\_FILE,\S{}\"w\"\SE{})) == NULL) \{}}
\L{\LB{    perror(\S{}\"Couldn\'t open dump file\"\SE{});}}
\L{\LB{    \K{return} (1);}}
\L{\LB{  \}}}
\L{\LB{  dumpfd = fileno(dumpPtr);}}
\L{\LB{}}
\L{\LB{  debug(1,0,\S{}\"Dumping list.\!n\"\SE{});}}
\L{\LB{}}
\L{\LB{  \C{}\/* do a list out to the dump file *\/\CE{}}}
\L{\LB{  \K{if} ((list(dumpfd)) != 0) \{}}
\L{\LB{    debug(1,0,\S{}\"Error in dumping list.\!n\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* close the dump file *\/\CE{}}}
\L{\LB{  \K{if}((fclose(dumpPtr)) != 0) \{}}
\L{\LB{    perror(\S{}\"Couldn\'t close dump file\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* read a dump file and construct a host table from it *\/\CE{}}}
\L{\LB{\K{int} readdump() \{}}
\L{\LB{  FILE *dumpPtr;}}
\L{\LB{  \K{char} line[MAXLINE];}}
\L{\LB{  \K{char} addr[MAXADDR];}}
\L{\LB{  \K{int} *np=malloc(\K{sizeof}(\K{int})), *ptime=malloc(\K{sizeof}(\K{int}));}}
\L{\LB{  }}
\L{\LB{  \C{}\/* open the dump file to read *\/\CE{}}}
\L{\LB{  \K{if} ((dumpPtr = fopen(DUMP\_FILE,\S{}\"r\"\SE{})) == NULL) \{}}
\L{\LB{    perror(\S{}\"Couldn\'t open dump file, continuing without it.\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* get and throw away the header line *\/\CE{}}}
\L{\LB{  fgets(line,MAXLINE,dumpPtr);}}
\L{\LB{}}
\L{\LB{  \C{}\/* go through the rest of the file and add any hosts found *\/\CE{}}}
\L{\LB{  \K{while}((fgets(line,MAXLINE,dumpPtr)) != NULL) \{}}
\L{\LB{    sscanf(line,\S{}\"\%s \%d \%d\"\SE{},addr,ptime,np);}}
\L{\LB{    addhost(addr,(time\_t)*ptime,*np);}}
\L{\LB{  \}}}
\L{\LB{  fclose(dumpPtr);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
