\File{getnet.c},{12:18},{Feb  6 1997}
\L{\LB{\K{\#include} \S{}\"squid.h\"\SE{}}}
\L{\LB{}}
\L{\LB{\C{}\/* default number of points to assign a host *\/\CE{}}}
\L{\LB{\K{int} opt\_points = 1;}}
\L{\LB{}}
\L{\LB{\C{}\/* set the default servername from the .h file *\/\CE{}}}
\L{\LB{\K{char} opt\_server[MAXADDR] = SERVER;}}
\L{\LB{}}
\L{\LB{\C{}\/* get the initial network *\/\CE{}}}
\L{\LB{\K{int} getnet() \{}}
\L{\LB{  \K{int} n, sockfd;}}
\L{\LB{  \K{char} buf[MAXLINE];}}
\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{  \C{}\/* open a socket to the server *\/\CE{}}}
\L{\LB{  \K{if} ((sockfd = sopen()) == 0) \{}}
\L{\LB{    debug(1,0,\S{}\"Couldn\'t open socket to server.\!n\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  debug(1,0,\S{}\"Getting initial listing.\!n\"\SE{});}}
\L{\LB{}}
\L{\LB{  \C{}\/* send the request *\/\CE{}}}
\L{\LB{  write(sockfd,\S{}\"list\!n\"\SE{},5);}}
\L{\LB{}}
\L{\LB{  \C{}\/* read the response and write it out the dump file *\/\CE{}}}
\L{\LB{  n = MAXLINE;}}
\L{\LB{  \K{while}(n != 0) \{}}
\L{\LB{    n = read(sockfd,buf,MAXLINE);}}
\L{\LB{    n = write(dumpfd,buf,n);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* read the dump file that we have just written *\/\CE{}}}
\L{\LB{  readdump();}}
\L{\LB{  close(sockfd);}}
\L{\LB{  fclose(dumpPtr);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* get the changes since ltime *\/\CE{}}}
\L{\LB{\K{int} getupdate(time\_t ltime) \{}}
\L{\LB{  \K{int} sockfd, logfd, n;}}
\L{\LB{  FILE *logPtr;}}
\L{\LB{  \K{char} buf[MAXLINE];}}
\L{\LB{}}
\L{\LB{  \C{}\/* open the log file for writing *\/\CE{}}}
\L{\LB{  \K{if} ((logPtr = fopen(GLOG\_FILE,\S{}\"w\"\SE{})) == NULL) \{}}
\L{\LB{    perror(\S{}\"Can\'t open GLOG\_FILE\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{  logfd = fileno(logPtr);}}
\L{\LB{}}
\L{\LB{  \C{}\/* open a socket to the server *\/\CE{}}}
\L{\LB{  \K{if} ((sockfd = sopen()) == 0) \{}}
\L{\LB{    debug(1,0,\S{}\"Couldn\'t open socket to server.\!n\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{  debug(1,0,\S{}\"Getting update.\!n\"\SE{});}}
\L{\LB{}}
\L{\LB{  \C{}\/* send the request *\/\CE{}}}
\L{\LB{  sprintf(buf,\S{}\"since \%d\!n\"\SE{},(\K{int}) ltime);}}
\L{\LB{  write(sockfd,buf,MAXLINE);}}
\L{\LB{}}
\L{\LB{  \C{}\/* read the response and write it out the log file *\/\CE{}}}
\L{\LB{  n = MAXLINE;}}
\L{\LB{  \K{while}(n != 0) \{}}
\L{\LB{    n = read(sockfd,buf,MAXLINE);}}
\L{\LB{    n = write(logfd,buf,n);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* close the log file *\/\CE{}}}
\L{\LB{  fclose(logPtr);}}
\L{\LB{}}
\L{\LB{  \C{}\/* read the log file that we have just written *\/\CE{}}}
\L{\LB{  readupdate();}}
\L{\LB{  close(sockfd);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* parse the log file and do the changes indicated *\/\CE{}}}
\L{\LB{\K{int} readupdate() \{}}
\L{\LB{  \K{int} changed = 1;}}
\L{\LB{  \K{int} *ptime = malloc(\K{sizeof}(\K{int})), *np = malloc(\K{sizeof}(\K{int}));}}
\L{\LB{  \K{char} line[MAXLINE], x[1], addr[MAXADDR];}}
\L{\LB{  FILE *logPtr;}}
\L{\LB{}}
\L{\LB{  \C{}\/* open the log file for reading *\/\CE{}}}
\L{\LB{  \K{if} ((logPtr = fopen(GLOG\_FILE,\S{}\"r\"\SE{})) == NULL) \{}}
\L{\LB{    perror(\S{}\"Can\'t open GLOG\_FILE\"\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,logPtr);}}
\L{\LB{}}
\L{\LB{  \C{}\/* go through the rest of the log file and parse it *\/\CE{}}}
\L{\LB{  \K{while}((fgets(line,MAXLINE,logPtr)) != NULL) \{}}
\L{\LB{    sscanf(line,\S{}\"\%s \%s \%d \%d\"\SE{},x,addr,ptime,np);}}
\L{\LB{}}
\L{\LB{    \C{}\/* if there were any changes, set this variable *\/\CE{}}}
\L{\LB{    changed = 0;}}
\L{\LB{}}
\L{\LB{    \C{}\/* if the line begins with a plus sign, add the host *\/\CE{}}}
\L{\LB{    \K{if} ((strcmp(x,\S{}\"+\"\SE{})) == 0) \{}}
\L{\LB{      addhost(addr,(time\_t)*ptime,*np);}}
\L{\LB{      debug(1,0,\S{}\"Adding \%s \%d \%d.\!n\"\SE{},addr,*ptime, *np);}}
\L{\LB{    \}}}
\L{\LB{}}
\L{\LB{    \C{}\/* if the line begins with a minus sign, delete the host *\/\CE{}}}
\L{\LB{    \K{else} \K{if} ((strcmp(x,\S{}\"\-\"\SE{})) == 0) \{}}
\L{\LB{      delhost(addr);}}
\L{\LB{      debug(1,0,\S{}\"Deleting \%s.\!n\"\SE{},addr);}}
\L{\LB{    \}}}
\L{\LB{}}
\L{\LB{    \C{}\/* otherwise, we have a badly formatted line *\/\CE{}}}
\L{\LB{    \K{else} \{}}
\L{\LB{      debug(1,0,\S{}\"Error in parsing results from since.\!n\"\SE{});}}
\L{\LB{    \}}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* if anything changed, do a dump *\/\CE{}}}
\L{\LB{  \K{if} (changed == 0) \{}}
\L{\LB{    debug(1,0,\S{}\"Changes made, doing a dump.\!n\"\SE{});}}
\L{\LB{    dump();}}
\L{\LB{  \}}}
\L{\LB{  }}
\L{\LB{  \C{}\/* close the log file *\/\CE{}}}
\L{\LB{  fclose(logPtr);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* add this host to the network *\/\CE{}}}
\L{\LB{\K{int} addself() \{}}
\L{\LB{  \K{int} sockfd, message\_len;}}
\L{\LB{  \K{char} message[MAXLINE], buf[MAXLINE];}}
\L{\LB{  }}
\L{\LB{  \C{}\/* open a socket to the server *\/\CE{}}}
\L{\LB{  \K{if} ((sockfd = sopen()) == 0) \{}}
\L{\LB{    debug(1,0,\S{}\"Couldn\'t open socket to server.\!n\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{  debug(1,0,\S{}\"Joining network.\!n\"\SE{});}}
\L{\LB{}}
\L{\LB{  \C{}\/* message is join and the number of points to grab *\/\CE{}}}
\L{\LB{  sprintf(message,\S{}\"join \%d\!n\"\SE{},opt\_points);}}
\L{\LB{  message\_len = strlen(message);}}
\L{\LB{}}
\L{\LB{  \C{}\/* send the request *\/\CE{}}}
\L{\LB{  write(sockfd,message,message\_len);}}
\L{\LB{}}
\L{\LB{  \C{}\/* read the response *\/\CE{}}}
\L{\LB{  read(sockfd,buf,MAXLINE);}}
\L{\LB{  debug(1,0,\S{}\"\%s\"\SE{},buf);}}
\L{\LB{  }}
\L{\LB{  close(sockfd);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* delete this host from the network *\/\CE{}}}
\L{\LB{\K{int} delself() \{}}
\L{\LB{  \K{int} sockfd;}}
\L{\LB{  \K{char} buf[MAXLINE];}}
\L{\LB{}}
\L{\LB{  \C{}\/* open a socket to the server *\/\CE{}}}
\L{\LB{  \K{if} ((sockfd = sopen()) == 0) \{}}
\L{\LB{    debug(1,0,\S{}\"Couldn\'t open socket to server.\!n\"\SE{});}}
\L{\LB{    \K{return}(1);}}
\L{\LB{  \}}}
\L{\LB{  debug(1,0,\S{}\"Leaving network.\!n\"\SE{});}}
\L{\LB{}}
\L{\LB{  \C{}\/* send the request *\/\CE{}}}
\L{\LB{  write(sockfd,\S{}\"dele\!n\"\SE{},5);}}
\L{\LB{    }}
\L{\LB{  \C{}\/* read the response *\/\CE{}}}
\L{\LB{  read(sockfd,buf,MAXLINE);}}
\L{\LB{  debug(1,0,\S{}\"\%s\"\SE{},buf);}}
\L{\LB{  close(sockfd);}}
\L{\LB{  \K{return}(0);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* open a socket to the server *\/\CE{}}}
\L{\LB{\K{int} sopen() \{}}
\L{\LB{  \K{int} sockfd;}}
\L{\LB{  \K{struct} sockaddr\_in serv\_addr;}}
\L{\LB{  \K{struct} hostent *hostPtr;}}
\L{\LB{  \K{int} one = 1;}}
\L{\LB{}}
\L{\LB{  \C{}\/* translate the server\'s name to an IP address *\/\CE{}}}
\L{\LB{  \K{if} ((hostPtr = gethostbyname(opt\_server)) == NULL) \{}}
\L{\LB{    perror(\S{}\"gethostbyname\"\SE{});}}
\L{\LB{    \K{return}(0);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* open the socket *\/\CE{}}}
\L{\LB{  \K{if} ((sockfd = socket(AF\_INET,SOCK\_STREAM,0)) \< 0) \{}}
\L{\LB{    perror(\S{}\"socket\"\SE{});}}
\L{\LB{    \K{return}(0);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \C{}\/* clear out and set the struct for the server address *\/\CE{}  }}
\L{\LB{  memset(\&serv\_addr, 0, \K{sizeof}(serv\_addr));}}
\L{\LB{  serv\_addr.sin\_family = AF\_INET;}}
\L{\LB{  memcpy(\&serv\_addr.sin\_addr, hostPtr\-\>h\_addr,\K{sizeof}(\K{struct} in\_addr));}}
\L{\LB{  serv\_addr.sin\_port = htons(SERV\_PORT);}}
\L{\LB{}}
\L{\LB{  \C{}\/* allow the socket to be reused immediately *\/\CE{}}}
\L{\LB{  setsockopt(sockfd, SOL\_SOCKET, SO\_REUSEADDR, (\K{char} *)\&one, \K{sizeof}(one));}}
\L{\LB{}}
\L{\LB{  \C{}\/* connect to the server *\/\CE{}}}
\L{\LB{  \K{if} (connect(sockfd,(\K{struct} sockaddr *)\&serv\_addr,\K{sizeof}(serv\_addr)) \< 0) \{}}
\L{\LB{    perror(\S{}\"connect\"\SE{});}}
\L{\LB{    \K{return}(0);}}
\L{\LB{  \}}}
\L{\LB{  \K{return}(sockfd);}}
\L{\LB{\}}}
