root/trunk/src/prefix_mgmt.c

Revision 154, 3.6 KB (checked in by andreu, 12 years ago)

Typo correction and copyright update

  • Property svn:eol-style set to native
Line 
1/*
2 * File: prefix_mgmt.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005-2011 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include "prefix_mgmt.h"
27
28/*
29 * prefCmp()
30 */
31int
32prefCmp(const void *elem1, const void *elem2){
33  struct PrefixV4 *el1 = (struct PrefixV4 *) elem1;
34  struct PrefixV4 *el2 = (struct PrefixV4 *) elem2;
35  return (
36          el1->beginning - el2->beginning
37          );
38}
39
40
41/*
42 * prefGlobalCmp()
43 */
44int
45prefGlobalCmp(const void *elem1, const void *elem2){
46  struct PrefixV4 *el1 = (struct PrefixV4 *) elem1;
47  struct PrefixV4 *el2 = (struct PrefixV4 *) elem2;
48  if ( el1->beginning >= el2->beginning && el1->beginning <= el2->end ) {
49    return 0;
50  } else if (el1->beginning < el2->beginning) {
51    return -1;
52  } else if (el1->beginning > el2->end) {
53    return 1;
54  } else {
55    syslog(LOG_ERR,"You have won !!! This case doesn't exist !!!!");
56    exit(1);
57    return 0;
58  }
59}
60
61/*
62 * prefV6Cmp()
63 */
64int
65prefV6Cmp(const void *elem1, const void *elem2){
66  struct PrefixV6 *el1 = (struct PrefixV6 *) elem1;
67  struct PrefixV6 *el2 = (struct PrefixV6 *) elem2;
68  int i;
69  int moreIsNecessary = 1;
70
71  for (i=0;i<4;i++) {
72    switch(i){
73    case 0:
74      if ( el2->mask <= 32 ) {
75        if ( el2->start[i] == el1->start[i] ) {
76          return 0;
77        }else{
78          if (el2->end[i] < el1->start[i]) {
79            return 1;
80          }else{
81            return 1;
82          }
83        }
84      } else { /* el2 > /32 */
85        if ( el2->start[i] != el1->start[i] ) {
86          if (el2->end[i] < el1->start[i]) {
87            return 1;
88          }else{
89            return 1;
90          }
91        } /* here we don't return, we'll continue in case */
92      }
93      break;
94    case 1:
95      if ( el2->mask <= 64 ) {
96        if ( el2->start[i] == el1->start[i] ) {
97          return 0;
98        }else{
99          if (el2->end[i] < el1->start[i]) {
100            return 1;
101          }else{
102            return 1;
103          }
104        }
105      } else { /* el2 > /64 */
106        if ( el2->start[i] != el1->start[i] ) {
107          if (el2->end[i] < el1->start[i]) {
108            return 1;
109          }else{
110            return 1;
111          }
112        } /* here we don't return, we'll continue in case */
113      }
114      break;
115    case 2:
116      if ( el2->mask <= 96 ) {
117        if ( el2->start[i] == el1->start[i] ) {
118          return 0;
119        }else{
120          if (el2->end[i] < el1->start[i]) {
121            return 1;
122          }else{
123            return 1;
124          }
125        }
126      } else { /* el2 > /96 */
127        if ( el2->start[i] != el1->start[i] ) {
128          if (el2->end[i] < el1->start[i]) {
129            return 1;
130          }else{
131            return 1;
132          }
133        } /* here we don't return, we'll continue in case */
134      }
135      break;
136    case 3:
137      if ( el2->mask <= 128 ) {
138        if ( el2->start[i] == el1->start[i] ) {
139          return 0;
140        }else{
141          if (el2->end[i] < el1->start[i]) {
142            return 1;
143          }else{
144            return 1;
145          }
146        }
147      } else { /* el2 > /128 */
148        if ( el2->start[i] != el1->start[i] ) {
149          if (el2->end[i] < el1->start[i]) {
150            return 1;
151          }else{
152            return 1;
153          }
154        } /* here we don't return, we'll continue in case */
155      }
156      break;
157    default:
158      syslog(LOG_ERR,"You have won !!! This IPv6 case doesn't exist !!!!");
159      exit(1);
160      return 0;
161      break;
162    }
163  }
164}
Note: See TracBrowser for help on using the browser.