File: dosreduce.c

package info (click to toggle)
ns2 2.35%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,796 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (519 lines) | stat: -rw-r--r-- 11,775 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>

#define INFINITY -1;
#define INC 0

int debug=0;

typedef struct node {
  int c;   /* color  0:white 1:grey 2:black */
  int d;   /* Distance */
  int p;   /* parent */
  int id;   
} node;

typedef struct QueueItem{
  struct node *u;
  struct QueueItem *next;
} QueueItem;

typedef struct ATlinks{
  int n1;
  int n2;
  struct ATlinks *next;
} ATlinks;

typedef struct CTinfo{
  int src;          
  int dst; 
  int *path;    
  int flag;   /* whether it should be used or not */
} CTinfo;

QueueItem *head, *tail;
ATlinks *AThead, *ATtail;

void enqueue(struct node *vertex );
struct node* dequeue(void);
void cleanq(void);


int main(int argc, char **argv)
{
  int i,j,k, maxd,id1,id2,noattackers,wt;
  int node_count,edges,victim;
  FILE *fp;
  struct node **V, *u;
  struct ATlinks *newlink;
  struct ATlinks *p;
  struct CTinfo **CrossTraffic;
  int **matrix;
  char temp[30];
  int pn1,pn2,*attackers;
  int found,activeconn,pathlen;
  int tempi;
  int *FT,lc,nc;


  if (argc < 3 ){
    printf("Usage: %s inet_topology #attackers #activeconnections\n",argv[0]);
    exit(0);
  }

  noattackers = atoi(argv[2]);
  activeconn = atoi(argv[3]);
  
  /* Read the node_count and edges for topo file */ 
  if ( (fp = fopen(argv[1],"r")) == NULL ){
    printf(" Error openning the adjanceny file\n");
    exit(1);
  }

  fscanf(fp,"%d %d",&node_count,&edges);
  /* Ignore location infomation */
  for(i=0;i<=node_count;i++){
    fgets(temp,30,fp);
  }

  /* Randomly choose victim node */
  victim = (int)((float)node_count*rand()/(RAND_MAX+1.0));
  if(debug){
    printf("node_count= %d edges %d victim=%d\n",node_count,edges,victim);
  }

  /* Initialise all remaining vertices 
   */
  V = (struct node **)malloc(node_count*sizeof(node));
  for(i=0;i<node_count;i++){
    V[i] = (struct node *) malloc (sizeof(node));
    V[i]->id=i;
    V[i]->c=0;
    V[i]->d=INFINITY;
    V[i]->p=INFINITY;
  }
  
  /* Initialise queue */
  head = tail = NULL;
  
  /* Initialise the root of bfs tree 
   * rootid can be the id of any node. like node 0 or leaf */
  V[victim]->c = 1;
  V[victim]->d = 0;
  V[victim]->p = INFINITY;

  enqueue(V[victim]);
  
  /* open file with adjaceny information */
  matrix = (int**)malloc(node_count*sizeof(int*));
  for(i=0;i<node_count;i++){
    matrix[i] = (int*)malloc(node_count*sizeof(int));
    for(j=0;j<node_count;j++){
      matrix[i][j]=0;
    }
  }

  for(i=0;i<edges;i++){
    fscanf(fp,"%d %d %d",&id1,&id2,&wt);
    matrix[id1][id2]=1;
    matrix[id2][id1]=1;
  }

  close(fp);

  
  while(head){
    u = (struct node *) malloc (sizeof(node));
    if(u== NULL){
      printf("Error allocationg u\n");
      exit(1);
    }
    u=dequeue();
    id1= u->id;
    for(i=0;i<node_count;i++){
      /* refer to adj matrix */
      if(matrix[id1][i] == 1 ){
	if (V[i]->c == 0){
	  V[i]->c =1;
	  V[i]->d = u->d + 1;
	  V[i]->p = u->id;
	  enqueue(V[i]);
	} 
      }
    }

      V[id1]->c = 2;
      V[id1]->p = u->p;
      V[id1]->d = u->d;
  }

  maxd =0;
  /* print out all the information */
  for (i=0;i<node_count;i++){
    /* printf("id: %d c=%d d=%d p=%d\n",V[i]->id,V[i]->c,V[i]->d,V[i]->p);  */
     if(maxd < V[i]->d)
       maxd = V[i]->d;

  }    
  if (debug) printf(" Max depth %d\n",maxd);
  
  /* Choose the attackers randomly in the topology  */
  attackers = (int*)malloc(noattackers*sizeof(int));
  for(i=0;i<noattackers;i++){
    attackers[i] = victim;
    /* Ensure the victim and attackers are distinct */
    while(attackers[i] == victim){
      attackers[i]= (int)((float)node_count*rand()/(RAND_MAX+1.0));
    }
    if(debug)
      printf("attacker[%d] = %d\n",i,attackers[i]);
  }

  /* Record the attack tree links */
  AThead = ATtail = NULL;
  for(i=0;i<noattackers;i++){
    pn1 = V[attackers[i]]->id;
    pn2 = V[attackers[i]]->p;
    while(pn2 != victim) {

      if(AThead == NULL){
	if ( (newlink = (struct ATlinks *)malloc(sizeof(ATlinks))) == NULL){
	  printf("Error allocationg space for new ATlink\n");
	  exit(1);
	}
	newlink->n1=pn1;
	newlink->n2=pn2;
	AThead = newlink;
	ATtail = newlink;
	newlink->next = NULL;
      } else {
	/* insert only if not already present */
	ATlinks *p = AThead;
	while (p){
	  if( (p->n1 != pn1) || (p->n2 != pn2) )
	    p = p->next; 
	  else 
	    break;
	}
	/* link not found */ 
	if(p==NULL){
	  	if ( (newlink = (struct ATlinks *)malloc(sizeof(ATlinks))) == NULL){
		  printf("Error allocationg space for new ATlink\n");
		  exit(1);
		}
		newlink->n1=pn1;
		newlink->n2=pn2;
		newlink->next = NULL;
		ATtail->next = newlink;
		ATtail = newlink;
	} else {
	  /* reached common attack tree. Break from while */
	  break; 
	}

      }
      pn1 = pn2;
      pn2 = V[pn1]->p;
    }
    /* Insert the last link before victim */ 
    if(pn2 == victim){
       if(AThead == NULL){
	if ( (newlink = (struct ATlinks *)malloc(sizeof(ATlinks))) == NULL){
	  printf("Error allocationg space for new ATlink\n");
	  exit(1);
	}
	newlink->n1=pn1;
	newlink->n2=pn2;
	AThead = newlink;
	ATtail = newlink;
	newlink->next = NULL;
      } else {
      /* insert only if not already present */
	ATlinks *p = AThead;
	while (p){
	  if( (p->n1 != pn1) || (p->n2 != pn2) )
	    p = p->next;
	  else 
	    break;
	}
      	/* link not found */ 
	if(p==NULL){
	  	if((newlink = (struct ATlinks *)malloc(sizeof(ATlinks))) == NULL){
		  printf("Error allocationg space for new ATlink\n");
		  exit(1);
		}
		newlink->n1=pn1;
		newlink->n2=pn2;
		newlink->next = NULL;
		ATtail->next = newlink;
		ATtail = newlink;
	}
      } /* end else */
    }
    
  }
	
  
  /* print the attack tree  and encode into matrix */
  i=0;
  p = AThead;
  while(p){
     if(debug) printf("%d n1= %d n2= %d\n",i++,p->n1,p->n2);
      matrix[p->n1][p->n2] = 2;
      p = p->next;
  }

  /* Now randomly choose the cross traffic source and sinks */
  CrossTraffic = (struct CTinfo **)malloc(activeconn*sizeof(CTinfo));
  for(k=0;k<activeconn;k++){
    if ((CrossTraffic[k] = (struct CTinfo *)malloc(sizeof(CTinfo))) == NULL){
      printf("%d:Error allocation Crosstraffic structure\n",errno);
      exit(1);
    }
    CrossTraffic[k]->src =(int)((float)node_count*rand()/(RAND_MAX+ 1.0));
    CrossTraffic[k]->dst =(int)((float)node_count*rand()/(RAND_MAX+ 1.0));
    if(CrossTraffic[k]->src == CrossTraffic[k]->dst){
      while(CrossTraffic[k]->dst == CrossTraffic[k]->src)
	CrossTraffic[k]->dst =(int)((float)node_count*rand()/(RAND_MAX+ 1.0));
    }
    CrossTraffic[k]->flag = 0; /* Usable */
    /* printf("src %d dst %d\n",CrossTraffic[k]->src, CrossTraffic[k]->dst);
       fflush(NULL); */
    
    /* fill path after bfs */
    /* Initialise the V matrix */
    for(i=0;i<node_count;i++){
      V[i]->id=i;
      V[i]->c=0;
      V[i]->d=INFINITY;
      V[i]->p=INFINITY;
    }
  
    /* Initialise queue */
    head = tail = NULL;
    tempi = CrossTraffic[k]->src;

    /* Initialise the root of bfs tree 
     * rootid can be the id of any node. like node 0 or leaf */
    V[tempi]->c = 1;
    V[tempi]->d = 0;
    V[tempi]->p = INFINITY;

    enqueue(V[tempi]);
    found=0;
    while(head){
      if((u = (struct node *)malloc(sizeof(node))) == NULL){
	printf("out of memory for u %d\n",errno);
	exit(1);
      }
	
      u=dequeue();
      id1= u->id;
      for(i=0;i<node_count;i++){
	/* refer to adj matrix */
	if(matrix[id1][i] != 0 ){
	  if (V[i]->c == 0){
	    V[i]->c =1;
	    V[i]->d = u->d + 1;
	    V[i]->p = u->id;
	    if (V[i]->id == CrossTraffic[k]->dst){
	      found = 1;
	      pathlen = V[i]->d;
	      break; 
	    }
	    enqueue(V[i]);
	  } 
	}
      }
      if(found == 1){ 
	cleanq();
	break;
      }
      V[id1]->c = 2;
      V[id1]->p = u->p;
      V[id1]->d = u->d;
    }

    /* print the information */
    if(debug)
      printf("%d Src %d Dst %d Path ",k,CrossTraffic[k]->src,CrossTraffic[k]->dst);

    /* Find the path from src to dst */
    /* Stored dst, dst-1...src */ 

    CrossTraffic[k]->path = (int*)malloc(sizeof(pathlen));
    for(j=pathlen;j>=0;j--){
      if(j==pathlen)
	CrossTraffic[k]->path[j] = CrossTraffic[k]->dst;
      else {
	tempi = CrossTraffic[k]->path[j+1];
	CrossTraffic[k]->path[j] = V[tempi]->p;
      }
      if(debug)
	printf("%d ",CrossTraffic[k]->path[j]);

    }
  
    /* Matrix encoding 
     * 0 = no link 
     * 1 = link ( no traffic )
     * 2 = attack link only 
     * 3 = attack + cross traffic link 
     * 4 = cross traffic link only but path on AT  
     * 5 = only one flow of cross traffic 
     * 6 = more than one flow of cross traffic on link 
     *

     * Flag encoding 
     * 0 : discard 
     * 1 : shared attack tree 
     * 2 : shared cross traffic links 
     **** */ 
    /* Check is flag should change */ 
    for(j=0;j<pathlen;j++){
      if((matrix[CrossTraffic[k]->path[j]][CrossTraffic[k]->path[j+1]] == 2) \
      || (matrix[CrossTraffic[k]->path[j]][CrossTraffic[k]->path[j+1]] == 3)){
	CrossTraffic[k]->flag = 1;
      }
    }

    for(j=0;j<pathlen;j++){
      switch(matrix[CrossTraffic[k]->path[j]][CrossTraffic[k]->path[j+1]]){
      case 1:
	if(CrossTraffic[k]->flag == 1)
	  tempi=4;
	else 
	  tempi=5;
	break;
      case 2:
	if(CrossTraffic[k]->flag == 0){
	  printf("Flag has to be 1 or 2 for %d\n",k);
	  printf("info: link %d %d, matrix = %d\n",\
		 CrossTraffic[k]->path[j],CrossTraffic[k]->path[j+1], matrix[CrossTraffic[k]->path[j]][CrossTraffic[k]->path[j+1]]); 
	  exit(1);
	}
	tempi=3;
	break;
      case 3:
	if(CrossTraffic[k]->flag == 0){
	  printf("Flag has to be 1 or 2 for %d\n",k);
  	  printf("info: link %d %d, matrix = %d\n",\
		 CrossTraffic[k]->path[j],CrossTraffic[k]->path[j+1], matrix[CrossTraffic[k]->path[j]][CrossTraffic[k]->path[j+1]]); 

	  exit(1);
	}
	tempi=3;
	break;
	/*
       //case 4:
	//if(CrossTraffic[k]->flag == 1)
	 // tempi=4;
	//else {
	//  tempi=6;
	//  CrossTraffic[k]->flag = 2;
	//}
	//break;
      //case 5:
//	if (CrossTraffic[k]->flag == 0)
//	  tempi = 5;
//	else {
//	  CrossTraffic[k]->flag =2;
//	  tempi = 6;
//	}
//	break; 
	*/
	
}
      matrix[CrossTraffic[k]->path[j]][CrossTraffic[k]->path[j+1]] = tempi;
    } /* for the switch */ 

    if(debug) printf("Flag %d\n",CrossTraffic[k]->flag);
  } /* Close for number of active connections  */

  /* Get number of nodes and links in reducted topology */ 
  FT = (int *)malloc(node_count*sizeof(int));
  if ( FT == NULL){
    printf("out of memory\n");
    exit(0);
  }
  for(i=0;i<node_count;i++)
    FT[i] =0;
  lc=0;
  nc=0;

  for(i=0;i<node_count;i++){
    for(j=0;j<node_count;j++){
      // printf("[%d][%d]= %d\n",i,j,matrix[i][j]);
      if((matrix[i][j] == 2) || (matrix[i][j] == 3) || (matrix[i][j] == 4) || (matrix[i][j] == 6)){
	if(FT[i]==0){
	  FT[i] = 1;
	  printf("node %d\n",i);
	  nc++;
	}
	if(FT[j] == 0){
	  FT[j]=1;
  	  printf("node %d\n",j);
	  nc++;
	}
	printf("link %d %d\n",i,j);
	lc++;
      } 
    }
  }
  printf("\nSummary\n");
  printf("Total: Nodes %d Links %d\n",nc,lc);
  printf("Victim: %d\n",victim);
  printf("Attackers:");
  for(i=0;i<noattackers;i++) printf(" %d",attackers[i]);
  printf("\n");
  
  return 0; 
}
  
  

void cleanq(void)
{
  struct node *p;
  while(head)
    p = dequeue();
}

void enqueue(struct node *vertex)
{
  QueueItem *p = (struct QueueItem *) malloc (sizeof(QueueItem));
  if ( p == NULL){
    printf("Error here");
    exit(0);
  }
  p->u = vertex;
  p->next = NULL;

  if(head==NULL){
    head = p;
    tail = p;
  }
  else {
    tail->next= p;
    tail = p;
  }
}

struct node* dequeue(void)
{
  struct node *retvertex;
  QueueItem *p = (struct QueueItem *) malloc (sizeof(QueueItem));

  if (head == tail) {
    retvertex = head->u;
    head = tail = NULL;
  } else {
    p = head;
    retvertex=head->u;
    head = head->next;
    free(p);
  }
  return retvertex;
}