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. t = group_head->next ;
  51. if(t->next==NULL)
  52. { t->next=node;
  53. return;}
  54. while(t->next!=NULL && t->next->IP < node->IP)
  55. t=t->next;
  56. node->next=t->next;
  57. t->next=node;
  58. return;
  59. }
  60. Prefix *build_list_no_order(Prefix *head,Prefix *node){
  61.  
  62. if (head == NULL)
  63. return node ;
  64. if (node == NULL)
  65. return head ;
  66. Prefix *cur ;
  67. cur = head ;
  68. while ( cur->next != NULL)
  69. cur = cur->next ;
  70. cur->next = node ;
  71. return head ;
  72.  
  73. }
  74.  
  75. Prefix *build_routing_table(){
  76. int a[5],i ;
  77. int ip = 0 ;
  78. Prefix *head ;
  79. head = NULL ;
  80. FILE *ofp ;
  81. ofp = fopen("routing_table","r") ;
  82. while( fscanf(ofp,"%d.%d.%d.%d/%d",&a[0],&a[1],&a[2],&a[3],&a[4]) != EOF){
  83. Prefix *node = (Prefix*)malloc(sizeof(Prefix)) ;
  84. for ( i =0 ; i < 4 ; i++)
  85. { ip = ip + a[i] ;
  86. if ( i != 3 )
  87. ip = ip<<8 ; }
  88. node->IP = ip ;
  89. ip = 0 ;
  90. node->len=a[4];
  91. head = build_list_no_order(head,node); }
  92. return head ;
  93.  
  94. }
  95.  
  96. void segment(int d,Prefix *rout ,Prefix group[]){
  97. int index=0,i;
  98.  
  99. for(i=31 ; i>31-d ; i--){
  100. if(rout->len<d)
  101. index=cal(d)-1;
  102. if( ( rout->IP & 1<<i ) && ( i!=32-d))
  103. {index++;
  104. index=index<<1;}
  105. if( (rout->IP & 1<<i) && i==32-d)
  106. index++;
  107. if( !(rout->IP & 1<<i) && i!=32-d)
  108. index=index<<1;
  109. }
  110. insert_prefix(&group[index],rout,index);
  111.  
  112. }
  113. int main ( int argc , char *argv[]){
  114. Prefix *head ;
  115. Prefix *routing_head;
  116. Prefix *trace_head;
  117. Prefix *temp_rout ;
  118. int d = 2 ; //need use argc finally
  119. int group_num;
  120. group_num = cal(d);
  121. Prefix group[group_num] ;
  122. trace_head = build_trace();
  123. routing_head = build_routing_table();
  124.  
  125. while(routing_head != NULL ){
  126. segment(d,routing_head,group);
  127. routing_head = routing_head->next;
  128. }
  129.  
  130. return 0 ;
  131. }
  132.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘insert_prefix’:
prog.c:34:7: warning: unused variable ‘i’ [-Wunused-variable]
   int i = 0;
       ^
prog.c: In function ‘build_list_no_order’:
prog.c:64:4: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
    while ( cur->next != NULL)
    ^~~~~
prog.c:66:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘while’
     cur->next = node ;
     ^~~
prog.c: In function ‘main’:
prog.c:118:16: warning: implicit declaration of function ‘build_trace’ [-Wimplicit-function-declaration]
   trace_head = build_trace();
                ^~~~~~~~~~~
prog.c:118:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   trace_head = build_trace();
              ^
prog.c:113:11: warning: unused variable ‘temp_rout’ [-Wunused-variable]
   Prefix *temp_rout ;
           ^~~~~~~~~
prog.c:112:11: warning: variable ‘trace_head’ set but not used [-Wunused-but-set-variable]
   Prefix *trace_head;
           ^~~~~~~~~~
prog.c:110:11: warning: unused variable ‘head’ [-Wunused-variable]
   Prefix *head ;
           ^~~~
prog.c: In function ‘insert_prefix’:
prog.c:50:10: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   while(t->next!=NULL && t->next->IP < node->IP)
         ~^~~~~~
prog.c: In function ‘segment’:
prog.c:50:10: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   while(t->next!=NULL && t->next->IP < node->IP)
         ~^~~~~~
prog.c:35:11: note: ‘t’ was declared here
   Prefix *t;
           ^
/home/Dp5Ur3/ccXMxJGT.o: In function `main':
prog.c:(.text.startup+0x9): undefined reference to `build_trace'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty