fork(1) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct prefix {
  4. unsigned int IP ;
  5. unsigned char len ;
  6. struct prefix *next ;
  7. };
  8. typedef struct prefix Prefix ;
  9. int cal(int d){
  10. int i , sum ;
  11. sum = 2 ;
  12. for( i = 0 ; i < d-1 ; i++)
  13. sum = sum * 2 ;
  14. return sum+1 ; }
  15.  
  16. Prefix *insert_a_node(Prefix *head,Prefix *node ){
  17. Prefix *cur ;
  18. cur = head ;
  19. if(head == NULL)
  20. return node ;
  21. if( node == NULL )
  22. return head ;
  23. if( node->IP < head->IP)
  24. { node->next = head ;
  25. return node ; }
  26. while( cur->next != NULL && cur->next->IP <= node->IP)
  27. cur = cur->next ;
  28. node->next = cur->next ;
  29. cur->next = node ;
  30. return head ;
  31. }
  32.  
  33. void insert_prefix(Prefix *group_head,Prefix *node,int index){
  34. int i = 0;
  35. Prefix *t;
  36.  
  37. if(node == NULL) return;
  38. if(group_head->next == NULL)
  39. {
  40. group_head->next=node;
  41. t = group_head;
  42. return ;
  43. }
  44. if(node->IP <= group_head->next->IP)
  45. {
  46. node->next = group_head->next ;
  47. group_head->next=node;
  48. return ;
  49. }
  50. while(t->next!=NULL && t->next->IP < node->IP)
  51. t=t->next;
  52. node->next=t->next;
  53. t->next=node;
  54. return;
  55. }
  56. Prefix *build_list_no_order(Prefix *head,Prefix *node){
  57.  
  58. if (head == NULL)
  59. return node ;
  60. if (node == NULL)
  61. return head ;
  62. Prefix *cur ;
  63. cur = head ;
  64. while ( cur->next != NULL)
  65. cur = cur->next ;
  66. cur->next = node ;
  67. return head ;
  68.  
  69. }
  70.  
  71. Prefix *build_routing_table(){
  72. int a[5],i ;
  73. int ip = 0 ;
  74. Prefix *head ;
  75. head = NULL ;
  76. FILE *ofp ;
  77. ofp = fopen("routing_table","r") ;
  78. while( fscanf(ofp,"%d.%d.%d.%d/%d",&a[0],&a[1],&a[2],&a[3],&a[4]) != EOF){
  79. Prefix *node = (Prefix*)malloc(sizeof(Prefix)) ;
  80. for ( i =0 ; i < 4 ; i++)
  81. { ip = ip + a[i] ;
  82. if ( i != 3 )
  83. ip = ip<<8 ; }
  84. node->IP = ip ;
  85. ip = 0 ;
  86. node->len=a[4];
  87. head = build_list_no_order(head,node); }
  88. return head ;
  89.  
  90. }
  91.  
  92. void segment(int d,Prefix *rout ,Prefix group[]){
  93. int index=0,i;
  94.  
  95. for(i=31 ; i>31-d ; i--){
  96. if(rout->len<d)
  97. index=cal(d)-1;
  98. if( ( rout->IP & 1<<i ) && ( i!=32-d))
  99. {index++;
  100. index=index<<1;}
  101. if( (rout->IP & 1<<i) && i==32-d)
  102. index++;
  103. if( !(rout->IP & 1<<i) && i!=32-d)
  104. index=index<<1;
  105. }
  106. insert_prefix(&group[index],rout,index);
  107.  
  108. }
  109. int main ( int argc , char *argv[]){
  110. Prefix *head ;
  111. Prefix *routing_head;
  112. Prefix *trace_head;
  113. Prefix *temp_rout ;
  114. int d = 2 ; //need use argc finally
  115. int group_num;
  116. group_num = cal(d);
  117. Prefix group[group_num] ;
  118. trace_head = build_trace();
  119. routing_head = build_routing_table();
  120.  
  121. while(routing_head != NULL ){
  122. segment(d,routing_head,group);
  123. routing_head = routing_head->next;
  124. }
  125.  
  126. return 0 ;
  127. }
  128.  
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
Standard output is empty