Projects/1. Router/router.c
int read_rtable(char *filename, struct route_table_entry *r_table) {
	FILE *fp;
	char *line;
	char *addr_byte_char;
	char buffer [100];
	uint32_t addr_byte, entries_no;
	uint32_t curr_char;
	uint8_t byte_offset[4] = {0, 8, 16, 24};
  
	fp = fopen(filename,"r");
	if(fp == NULL) {
		perror("Cannot open Routing table file");
		exit(-1);
	}
	entries_no = 0;
	/* Read file line by line */
	while ((line = fgets(buffer, 64, fp))) {
		/* Separate line by dots and spaces */
		addr_byte_char = strtok(buffer, " .");
		curr_char = 0;
		while (addr_byte_char != NULL) {
			addr_byte = atoi(addr_byte_char);
			/* Prefix */
			if (curr_char < 4)
				// << 24 if curr_char % 4 == 0, << 16 if curr_char % 4 == 1 .. 
				r_table[entries_no].prefix += addr_byte << 
												byte_offset[curr_char % 4];
	  		/* Next hop */
	 		else if (curr_char < 8)
				r_table[entries_no].next_hop += addr_byte << 
												byte_offset[curr_char % 4];
			/* Mask */
	  		else if (curr_char < 12)
				r_table[entries_no].mask += addr_byte << 
												byte_offset[curr_char % 4];
			/* Interface */
			else if (curr_char == 12)
				r_table[entries_no].interface = addr_byte;
			curr_char = curr_char + 1;
			addr_byte_char = strtok(NULL, " .");
		}
		/* Line is finished processing */
		entries_no = entries_no + 1;
	}
	fclose(fp);
	return entries_no;
}
/* Reads routing table from given file into the r_table parameter
 * Returns the routing table size
 */
Reads routing table from given file into the r_table parameter
Returns the routing table size
Reads routing table from given file into the r_table parameter Returns the routing table size
int read_rtable(char *filename, struct route_table_entry *r_table) {
	FILE *fp;
	char *line;
	char *addr_byte_char;
	char buffer [100];
	uint32_t addr_byte, entries_no;
	uint32_t curr_char;
	uint8_t byte_offset[4] = {0, 8, 16, 24};
	fp = fopen(filename,"r");
	if(fp == NULL) {
		perror("Cannot open Routing table file");
		exit(-1);
	}
	entries_no = 0;
	while ((line = fgets(buffer, 64, fp))) {
		addr_byte_char = strtok(buffer, " .");
		curr_char = 0;
		while (addr_byte_char != NULL) {
			addr_byte = atoi(addr_byte_char);
			if (curr_char < 4)
				r_table[entries_no].prefix += addr_byte << 
												byte_offset[curr_char % 4];
	 		else if (curr_char < 8)
				r_table[entries_no].next_hop += addr_byte << 
												byte_offset[curr_char % 4];
	  		else if (curr_char < 12)
				r_table[entries_no].mask += addr_byte << 
												byte_offset[curr_char % 4];
			else if (curr_char == 12)
				r_table[entries_no].interface = addr_byte;
			curr_char = curr_char + 1;
			addr_byte_char = strtok(NULL, " .");
		}
		entries_no = entries_no + 1;
	}
	fclose(fp);
	return entries_no;
}
int read_rtable ( char * filename , struct route_table_entry * r_table ) { FILE * fp ; char * line ; char * addr_byte_char ; char buffer [ 100 ] ; uint32_t addr_byte , entries_no ; uint32_t curr_char ; uint8_t byte_offset [ 4 ] = { 0 , 8 , 16 , 24 } ; fp = fopen ( filename , " " ) ; if ( fp == NULL ) { perror ( " " ) ; exit ( -1 ) ; } entries_no = 0 ; while ( ( line = fgets ( buffer , 64 , fp ) ) ) { addr_byte_char = strtok ( buffer , " " ) ; curr_char = 0 ; while ( addr_byte_char != NULL ) { addr_byte = atoi ( addr_byte_char ) ; if ( curr_char < 4 ) r_table [ entries_no ] . prefix += addr_byte << byte_offset [ curr_char % 4 ] ; else if ( curr_char < 8 ) r_table [ entries_no ] . next_hop += addr_byte << byte_offset [ curr_char % 4 ] ; else if ( curr_char < 12 ) r_table [ entries_no ] . mask += addr_byte << byte_offset [ curr_char % 4 ] ; else if ( curr_char == 12 ) r_table [ entries_no ] . interface = addr_byte ; curr_char = curr_char + 1 ; addr_byte_char = strtok ( NULL , " " ) ; } entries_no = entries_no + 1 ; } fclose ( fp ) ; return entries_no ; }
Reads routing table from given file into the r_table parameter
 Returns the routing table size
Reads routing table from given file into the r_table parameter Returns the routing table size
/* Read file line by line */ /* Separate line by dots and spaces */ /* Prefix */ // << 24 if curr_char % 4 == 0, << 16 if curr_char % 4 == 1 ..  /* Next hop */ /* Mask */ /* Interface */ /* Line is finished processing */