File: cidr_net.c

package info (click to toggle)
aggregate 1.0.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 304 kB
  • ctags: 117
  • sloc: ansic: 1,036; sh: 335; perl: 132; makefile: 83
file content (349 lines) | stat: -rw-r--r-- 9,692 bytes parent folder | download
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
/**********************************************************************
 * cidr_bet.c                                                March 2001
 * Horms                                             horms@verge.net.au
 *
 * aggregate
 * CIDR network aggregation and filtering
 * Copyright (C) 1999-2002  Horms
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 *
 **********************************************************************/


#include "cidr_net.h"
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

/*Lookup table for 2^index */
unsigned int cidr_pow_2_lookup[IP_BITS];


/**********************************************************************
 * cidr_net_create
 * Initialise a cidr_net and seed values
 * pre: a: base address of network
 *      p: prefix of network
 * post: network is initialise and values are seeded
 * return: network
 *         NULL on error
 **********************************************************************/

cidr_net_t *cidr_net_create(ip_address_t a, prefix_t p){
  cidr_net_t *n;

  extern int errno;

  if((n=(cidr_net_t*)malloc(sizeof(cidr_net_t)))==NULL){
    AGGREGATE_DEBUG_ERRNO("cidr_net_create: malloc", errno);
    return(NULL);
  }

  if(cidr_net_assign(n, a, p)==NULL){
    AGGREGATE_DEBUG("cidr_net_create: cidr_net_assign");
    free(n);
    return(NULL);
  }

  return(n);
}


/**********************************************************************
 * cidr_net_assign
 * Assign values to a cidr_net structure
 * pre: n: pointer to cidr_net structure
 *      a: base address of network
 *      p: prefix of network
 * post: n is initialised
 *       n is destroyed on error
 * return: n
 *         NULL on error
 **********************************************************************/

cidr_net_t *cidr_net_assign(cidr_net_t *n, ip_address_t a, prefix_t p){
  extern ip_address_t cidr_pow_2_lookup[];

  if(n==NULL){ return(NULL); }
  if( (a>>(IP_BITS-p)<<(IP_BITS-p)) != a ){
    AGGREGATE_LOG(
      LOG_INFO,
      "invalid prefix (%d) for network (%s)",
      p,
      ip_address_decimal_to_dotted_quad(a)
    );
    AGGREGATE_LOG(
      LOG_DEBUG,
      "cidr_net_assign: invalid prefix (%d) for network (%s)",
      p,
      ip_address_decimal_to_dotted_quad(a)
    );
    return(NULL);
  }
  n->address=a;
  n->prefix=p;
  n->top_address=a-1+cidr_pow_2_lookup[IP_BITS-p];

  return(n);
}


/**********************************************************************
 * cidr_net_destroy
 * Destory a cidr_net
 * pre: n: pointer to cidr_net to destroy
 * post: n is destroyed
 * return: none
 **********************************************************************/

void cidr_net_destroy(cidr_net_t *n){
  if(n==NULL){ return; }
  free(n);

  return;
}


/**********************************************************************
 * cidr_net_to_a
 * Print to stdout the network in address/prefix notation, where
 * address is in dotted quad notation
 * pre: n: network to display
 *      minimim_prefix: Don't display networks with a prefix less than
 *                      this value. Valid values 0-32. 
 *                      -1 for no mimimium prefix.
 *      maximim_prefix: Don't display networks with a prefix greater than
 *                      this value. Valid values 0-32. 
 *                      -1 for no maximium prefix.
 *      flag: as per cidr_net.h
 * post: network is displayed to rendered to ASCII
 * return: String containing network
 *         NULL on error
 * Note: Result is rendered into a static buffer
 **********************************************************************/

static char __cidr_net_str[CIDR_NET_STR_LEN];

char *cidr_net_to_a(
  cidr_net_t *n, 
  prefix_t minimum_prefix,
  prefix_t maximum_prefix,
  cidr_flag_t flag
){
  int status;

  if(n==NULL){
    return(NULL);
  }

  if( 
    (maximum_prefix!=-1 && n->prefix > maximum_prefix) ||
    (minimum_prefix!=-1 && n->prefix < minimum_prefix)
  ){
    *__cidr_net_str='\0';
    return(__cidr_net_str);
  }
  if(flag&CIDR_ADDRESS_RANGE){
    if((status=snprintf(
      __cidr_net_str,
      CIDR_NET_STR_LEN,
      "%s - ", 
      ip_address_decimal_to_dotted_quad(n->address)
    ))<0){
      return(NULL);
    }
    if((status=snprintf(
      __cidr_net_str+status,
      CIDR_NET_STR_LEN-status,
      "%s",  
      ip_address_decimal_to_dotted_quad(n->top_address)
    ))<0){
      return(NULL);
    }
  }
  if(flag&CIDR_NET_NETMASK){
    if((status=snprintf(
      __cidr_net_str,
      CIDR_NET_STR_LEN,
      "%s/", 
      ip_address_decimal_to_dotted_quad(n->address)
    ))<0){
      return(NULL);
    }
    if((status=snprintf(
      __cidr_net_str+status,
      CIDR_NET_STR_LEN-status,
      "%s", 
      ip_address_decimal_to_dotted_quad(cidr_netmask(n->prefix))
    ))<0){
      return(NULL);
    }
  }
  if(flag&CIDR_NET_PREFIX){
    if((status=snprintf(
      __cidr_net_str,
      CIDR_NET_STR_LEN,
      "%s/%s", 
      ip_address_decimal_to_dotted_quad(n->address), 
      prefix_decimal_to_a(n->prefix)
    ))<0){
      return(NULL);
    }
  }

  return(__cidr_net_str);
}


/**********************************************************************
 * cidr_net_distance
 * Find the distance between two networks base address
 * pre: a, b: networks to find the distance between
 * return: absolute diffrence of the base address of a and b
 **********************************************************************/

ip_address_t cidr_net_distance(cidr_net_t *a, cidr_net_t *b){
  if(a==NULL || b==NULL) return(~0);
  return(a->address>b->address)?a->address-b->address:b->address-a->address;
}


/**********************************************************************
 * cidr_net_cmp
 * Compare two cidr networks 
 * pre: a, b: networks to compare
 * return: 1 if a and b are the same
 *         0 if and are be are not the same
 *         -1 on error
 **********************************************************************/

int cidr_net_cmp(cidr_net_t *a, cidr_net_t *b){
  if(a==NULL || b==NULL) return(-1);
  return((a->address==b->address)&&(a->prefix==b->prefix))?1:0;
}


/**********************************************************************
 * cidr_generate_pow_2_lookup
 * Generate an array with IP_BITS elements where element n = 2^n
 * pre: none
 * post: @bit_loookup is seeded (or seedy as the case may be)
 **********************************************************************/

void cidr_generate_pow_2_lookup(void){
  int i;
  unsigned int current_seed;

  extern unsigned int cidr_pow_2_lookup[];

  for(i=0,current_seed=1;i<IP_BITS;i++,current_seed=current_seed<<1){
    cidr_pow_2_lookup[i]=current_seed;
  }
}


/**********************************************************************
 * cidr_log_2
 * Arghh this is really nasty, I am sure there is a better way to do 
 * this.
 * Get log base 2 of a IP_BITS bit integer, rounded down to the nearest 
 * integer.
 * Works on IP_BITSbit numbers.
 * pre: address: address to find log base 2 of
 * post: integer log base 2
 **********************************************************************/

int cidr_log_2 (ip_address_t address) {
  int i;
  int j;

  for(i=IP_BITS-1,j=1<<(IP_BITS-1);i>0;i--,j>>=1){
    if(address & j){
      goto leave;
    }
  }
 
  leave:
  return(i);
}


/**********************************************************************
 * cidr_netmask
 * Calculate the netmask given a prefix
 * pre: prefix: prefix to find the netmask off
 * return: netmask in decimal
 **********************************************************************/

ip_address_t cidr_netmask(prefix_t prefix){
  extern unsigned int cidr_pow_2_lookup[];

  if(prefix==0) return (ip_address_t)0;
  return( ~(cidr_pow_2_lookup[IP_BITS-prefix]-1) );
}
  

/**********************************************************************
 * cidr_prefix
 * Arghh this is really nasty, I am sure there is a better way to do 
 * this.
 * Get the network prefix (IP_BITS-number of trailing zeros)
 * Works on IP_BITSbit numbers
 * pre: address: the address to find the prefix of
 * post: number of trailing zeros.
 **********************************************************************/

prefix_t cidr_prefix (ip_address_t address){
  int i;
  int j;

  for(i=IP_BITS,j=1;i>0;i--,j<<=1){
    if(address & j){
      goto leave;
    }
  }
 
  leave:
  return(i);
}


/**********************************************************************
 * cidr_classful_prefix
 * Given an ip address find the classful network prefix length
 * pre: address: the address to find the prefix of
 * post: The number of trailing zeros, rounded to the nearest 
 *       classful prefix length. That is one of 0, 8, 16, 24 or 32.
 **********************************************************************/

prefix_t cidr_classful_prefix(ip_address_t address){
  if(address==0){
    return((prefix_t)0);
  }
  if(!(address&0xffffff)){
    return((prefix_t)8);
  }
  else if(!(address&0xffff)){
    return((prefix_t)16);
  }
  else if(!(address&0xff)){
    return((prefix_t)24);
  }
  
  return((prefix_t)32);
}