#include<stdio.h>
#include<stdlib.h>
struct prefix {
unsigned int IP ;
unsigned char len ;
struct prefix *next ;
};
typedef struct prefix Prefix ;
int cal(int d){
int i , sum ;
sum = 2 ;
for( i = 0 ; i < d-1 ; i++)
sum = sum * 2 ;
return sum+1 ; }
Prefix *insert_a_node(Prefix *head,Prefix *node ){
Prefix *cur ;
cur = head ;
if(head == NULL)
return node ;
if( node == NULL )
return head ;
if( node->IP < head->IP)
{ node->next = head ;
return node ; }
while( cur->next != NULL && cur->next->IP <= node->IP)
cur = cur->next ;
node->next = cur->next ;
cur->next = node ;
return head ;
}
void insert_prefix(Prefix *group_head,Prefix *node,int index){
int i = 0;
Prefix *t;
if(node == NULL) return;
if(group_head->next == NULL)
{
group_head->next=node;
t = group_head;
return ;
}
if(node->IP <= group_head->next->IP)
{
node->next = group_head->next ;
group_head->next=node;
return ;
}
while(t->next!=NULL && t->next->IP < node->IP)
t=t->next;
node->next=t->next;
t->next=node;
return;
}
Prefix *build_list_no_order(Prefix *head,Prefix *node){
if (head == NULL)
return node ;
if (node == NULL)
return head ;
Prefix *cur ;
cur = head ;
while ( cur->next != NULL)
cur = cur->next ;
cur->next = node ;
return head ;
}
Prefix *build_routing_table(){
int a[5],i ;
int ip = 0 ;
Prefix *head ;
head = NULL ;
FILE *ofp ;
ofp
= fopen("routing_table","r") ; while( fscanf(ofp
,"%d.%d.%d.%d/%d",&a
[0],&a
[1],&a
[2],&a
[3],&a
[4]) != EOF
){ Prefix
*node
= (Prefix
*)malloc(sizeof(Prefix
)) ; for ( i =0 ; i < 4 ; i++)
{ ip = ip + a[i] ;
if ( i != 3 )
ip = ip<<8 ; }
node->IP = ip ;
ip = 0 ;
node->len=a[4];
head = build_list_no_order(head,node); }
return head ;
}
void segment(int d,Prefix *rout ,Prefix group[]){
int index=0,i;
for(i=31 ; i>31-d ; i--){
if(rout->len<d)
index=cal(d)-1;
if( ( rout->IP & 1<<i ) && ( i!=32-d))
{index++;
index=index<<1;}
if( (rout->IP & 1<<i) && i==32-d)
index++;
if( !(rout->IP & 1<<i) && i!=32-d)
index=index<<1;
}
insert_prefix(&group[index],rout,index);
}
int main ( int argc , char *argv[]){
Prefix *head ;
Prefix *routing_head;
Prefix *trace_head;
Prefix *temp_rout ;
int d = 2 ; //need use argc finally
int group_num;
group_num = cal(d);
Prefix group[group_num] ;
trace_head = build_trace();
routing_head = build_routing_table();
while(routing_head != NULL ){
segment(d,routing_head,group);
routing_head = routing_head->next;
}
return 0 ;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CnN0cnVjdCBwcmVmaXggewogICB1bnNpZ25lZCBpbnQgSVAgOwogICB1bnNpZ25lZCBjaGFyIGxlbiA7CiAgc3RydWN0IHByZWZpeCAqbmV4dCA7CiB9Owp0eXBlZGVmIHN0cnVjdCBwcmVmaXggUHJlZml4IDsKaW50IGNhbChpbnQgZCl7CiAgIGludCBpICwgc3VtIDsKICAgc3VtID0gMiA7CiAgIGZvciggaSA9IDAgOyBpIDwgZC0xIDsgaSsrKQogICAgIHN1bSA9IHN1bSAqIDIgOwogICAgICByZXR1cm4gc3VtKzEgOyB9CgpQcmVmaXggKmluc2VydF9hX25vZGUoUHJlZml4ICpoZWFkLFByZWZpeCAqbm9kZSApewogICAgUHJlZml4ICpjdXIgOwogICAgICBjdXIgPSBoZWFkIDsKICAgIGlmKGhlYWQgPT0gTlVMTCkKICAgICAgICByZXR1cm4gbm9kZSA7CiAgICBpZiggbm9kZSA9PSBOVUxMICkKICAgICAgIHJldHVybiBoZWFkIDsKICAgIGlmKCBub2RlLT5JUCA8IGhlYWQtPklQKQogICAgICB7IG5vZGUtPm5leHQgPSBoZWFkIDsKICAgICAgICAgIHJldHVybiBub2RlIDsgIH0KICAgIHdoaWxlKCBjdXItPm5leHQgIT0gTlVMTCAmJiBjdXItPm5leHQtPklQIDw9IG5vZGUtPklQKQogICAgICAgIGN1ciA9IGN1ci0+bmV4dCA7CiAgICAgICBub2RlLT5uZXh0ID0gY3VyLT5uZXh0IDsKICAgICAgIGN1ci0+bmV4dCA9IG5vZGUgOwogICAgICByZXR1cm4gaGVhZCA7CiAgICAgICAgICAgICAgICAgICAgICAgfQoKdm9pZCBpbnNlcnRfcHJlZml4KFByZWZpeCAqZ3JvdXBfaGVhZCxQcmVmaXggKm5vZGUsaW50IGluZGV4KXsKICBpbnQgaSA9IDA7CiAgUHJlZml4ICp0OwoKICBpZihub2RlID09IE5VTEwpIHJldHVybjsKICBpZihncm91cF9oZWFkLT5uZXh0ID09IE5VTEwpCiAgICAgICAgewogICAgICAgICBncm91cF9oZWFkLT5uZXh0PW5vZGU7CiAgICAgICAgIHQgPSBncm91cF9oZWFkOwogICAgICAgICByZXR1cm4gOwogICAgICAgIH0KICBpZihub2RlLT5JUCA8PSBncm91cF9oZWFkLT5uZXh0LT5JUCkKICAgICAgICB7CiAgICAgICAgbm9kZS0+bmV4dCA9IGdyb3VwX2hlYWQtPm5leHQgOwogICAgICAgIGdyb3VwX2hlYWQtPm5leHQ9bm9kZTsKICAgICAgICByZXR1cm4gOwogICAgICAgIH0KICB3aGlsZSh0LT5uZXh0IT1OVUxMICYmIHQtPm5leHQtPklQIDwgbm9kZS0+SVApCiAgICAgICAgdD10LT5uZXh0OwogICAgbm9kZS0+bmV4dD10LT5uZXh0OwogICAgdC0+bmV4dD1ub2RlOwogICAgcmV0dXJuOwp9ClByZWZpeCAqYnVpbGRfbGlzdF9ub19vcmRlcihQcmVmaXggKmhlYWQsUHJlZml4ICpub2RlKXsKCiAgICAgaWYgKGhlYWQgPT0gTlVMTCkKICAgICAgICAgcmV0dXJuIG5vZGUgOwogICAgIGlmIChub2RlID09IE5VTEwpCiAgICAgICAgcmV0dXJuIGhlYWQgOwogICBQcmVmaXggKmN1ciA7CiAgICAgY3VyID0gaGVhZCA7CiAgIHdoaWxlICggY3VyLT5uZXh0ICE9IE5VTEwpCiAgICBjdXIgPSBjdXItPm5leHQgOwogICAgY3VyLT5uZXh0ID0gbm9kZSA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