#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 15
#define N 150
struct
node {
char
* data;
struct
node* next;
} * head[15];
void
storeData(
FILE
* fp,
char
buf[M][N],
char
net[M][N],
char
mask[M][N],
char
gateway[M][N],
char
port[M][N])
{
char
line[200];
int
c, i = 0, j, k = 0, m = 0;
while
(
fgets
(line,
sizeof
(line), fp)) {
j = 0;
for
(
int
l = 0; l <
strlen
(line); l++) {
buf[i][j] = line[l];
j++;
}
i++;
}
for
(i = 0; i < 15; i++) {
k = 0;
for
(j = 0; buf[i][j] !=
','
; j++) {
net[i][k] = buf[i][j];
k++;
}
m = j + 2;
k = 0;
for
(j = m; buf[i][j] !=
','
; j++) {
mask[i][k] = buf[i][j];
k++;
}
m = j + 2;
k = 0;
for
(j = m; buf[i][j] !=
','
; j++) {
gateway[i][k] = buf[i][j];
k++;
}
m = j + 2;
k = 0;
for
(j = m; buf[i][j] !=
'\0'
; j++) {
port[i][k] = buf[i][j];
k++;
}
}
}
void
insert(
char
net[M][N],
char
mask[M][N],
char
gateway[M][N],
char
port[M][N],
char
buf[M][N])
{
char
*temp1, *temp2, *temp3, *temp4;
struct
node*
new
;
for
(
int
i = 0; i < M; i++) {
head[i] = NULL;
}
for
(
int
i = 0; i < M; i++) {
for
(
int
j = 0; j < 4; j++) {
if
(head[i] == NULL) {
new
= (
struct
node*)
malloc
(
sizeof
(
struct
node));
new
->data = net[i];
new
->next = NULL;
head[i] =
new
;
}
else
if
(j == 1) {
new
->next = (
struct
node*)
malloc
(
sizeof
(
struct
node));
new
=
new
->next;
new
->data = mask[i];
new
->next = NULL;
}
else
if
(j == 2) {
new
->next = (
struct
node*)
malloc
(
sizeof
(
struct
node));
new
=
new
->next;
new
->data = gateway[i];
}
else
if
(j == 3) {
new
->next = (
struct
node*)
malloc
(
sizeof
(
struct
node));
new
=
new
->next;
new
->data = port[i];
}
}
}
for
(
int
i = 0; i < M; i++) {
for
(
int
j = i; j < M; j++) {
if
(inet_addr(head[i]->next->data)
< inet_addr(head[j]->next->data)) {
struct
node* temp = head[i];
head[i] = head[j];
head[j] = temp;
}
}
}
}
void
search(
FILE
* fp1,
FILE
* fp2)
{
char
str[100];
struct
in_addr addr;
unsigned
int
val;
fprintf
(fp2,
"%c"
,
' '
);
while
(
fgets
(str,
sizeof
(str), fp1)) {
for
(
int
i = 0; i < M; i++) {
val = inet_addr(str) & inet_addr(head[i]->next->data);
addr.s_addr = val;
char
* str1 = inet_ntoa(addr);
char
* str2 = head[i]->data;
int
count = 0;
for
(
int
i = 0; str1[i] !=
'\0'
; i++) {
if
(str1[i] == str2[i]) {
count++;
}
}
if
(count ==
strlen
(str1)) {
struct
node* ptr = head[i]->next;
struct
node* temp = ptr->next;
while
(temp != NULL) {
fprintf
(fp2,
"%s "
, temp->data);
temp = temp->next;
}
break
;
}
}
}
}
int
main(
int
argc,
char
* argv[])
{
FILE
*fin, *fout, *fp;
char
buf[M][N] = { { 0 } };
char
net[M][N] = { { 0 } };
char
mask[M][N] = { { 0 } };
char
gateway[M][N] = { { 0 } };
char
port[M][N] = { { 0 } };
if
(argc < 3) {
fprintf
(stderr,
"File name:%s\n"
, argv[0]);
return
1;
}
else
{
fin =
fopen
(argv[1],
"r"
);
fout =
fopen
(argv[2],
"w"
);
fp =
fopen
(argv[3],
"r"
);
}
if
(fp == NULL || fin == NULL || fout == NULL) {
printf
(
"Error"
);
return
0;
}
storeData(fp, buf, net, mask, gateway, port);
insert(net, mask, gateway, port, buf);
search(fin, fout);
printf
(
"Forwarding table has been implemented successfully"
);
printf
(
"See the output in %s file\n"
, argv[2]);
fclose
(fin);
fclose
(fp);
fclose
(fout);
return
0;
}