router.c
// Send an init request and wait for the init response
Send an init request and wait for the init response
Send an init request and wait for the init response
void initialize(int fd, struct sockaddr_in network, int id){
  struct pkt_INIT_REQUEST request;
  struct pkt_INIT_RESPONSE response;
  request.router_id = htonl(id);
  socklen_t addr_len=sizeof(struct sockaddr_in);
  int len;
  sendto(fd,(char*)&request,sizeof(request),0,(struct sockaddr*)&network,addr_len);
  len = recvfrom(fd, (char*)&response, sizeof(response),0, (struct sockaddr*)&network,&addr_len);
  ntoh_pkt_INIT_RESPONSE(&response);
  InitRoutingTbl(&response, id);
  num_neighbors =  response.no_nbr;
  int i;
  time_t current_time = time(0);
  for (i=0; i < num_neighbors; i++) {
    neighbor_list[i].neighbor = response.nbrcost[i].nbr;
    neighbor_list[i].cost =	response.nbrcost[i].cost;
    neighbor_list[i].last_update = current_time;
  }
}
void initialize ( int fd , struct sockaddr_in network , int id ) { struct pkt_INIT_REQUEST request ; struct pkt_INIT_RESPONSE response ; request . router_id = htonl ( id ) ; socklen_t addr_len = sizeof ( struct sockaddr_in ) ; int len ; sendto ( fd , ( char * ) & request , sizeof ( request ) , 0 , ( struct sockaddr * ) & network , addr_len ) ; len = recvfrom ( fd , ( char * ) & response , sizeof ( response ) , 0 , ( struct sockaddr * ) & network , & addr_len ) ; ntoh_pkt_INIT_RESPONSE ( & response ) ; InitRoutingTbl ( & response , id ) ; num_neighbors = response . no_nbr ; int i ; time_t current_time = time ( 0 ) ; for ( i = 0 ; i < num_neighbors ; i ++ ) { neighbor_list [ i ] . neighbor = response . nbrcost [ i ] . nbr ; neighbor_list [ i ] . cost = response . nbrcost [ i ] . cost ; neighbor_list [ i ] . last_update = current_time ; } }
Send an init request and wait for the init response
Send an init request and wait for the init response