1 //  $Id$
2 // Copyright (c) 2001,2002                        RIPE NCC
3 //
4 // All Rights Reserved
5 //
6 // Permission to use, copy, modify, and distribute this software and its
7 // documentation for any purpose and without fee is hereby granted,
8 // provided that the above copyright notice appear in all copies and that
9 // both that copyright notice and this permission notice appear in
10 // supporting documentation, and that the name of the author not be
11 // used in advertising or publicity pertaining to distribution of the
12 // software without specific, written prior permission.
13 //
14 // THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 // ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
16 // AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
17 // DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
18 // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 //
21 //
22 //  Copyright (c) 1994 by the University of Southern California
23 //  All rights reserved.
24 //
25 //    Permission is hereby granted, free of charge, to any person obtaining a copy
26 //    of this software and associated documentation files (the "Software"), to deal
27 //    in the Software without restriction, including without limitation the rights
28 //    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 //    copies of the Software, and to permit persons to whom the Software is
30 //    furnished to do so, subject to the following conditions:
31 //
32 //    The above copyright notice and this permission notice shall be included in
33 //    all copies or substantial portions of the Software.
34 //
35 //    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 //    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 //    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 //    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 //    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 //    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 //    THE SOFTWARE.
42 //
43 //  Questions concerning this software should be directed to
44 //  irrtoolset@cs.usc.edu.
45 //
46 //  Author(s): Cengiz Alaettinoglu <cengiz@ISI.EDU>
47 
48 #ifndef NE_H
49 #define NE_H
50 
51 #include "config.h"
52 #include <iostream>
53 extern "C" {
54 #include <sys/types.h>
55 }
56 #include "NT.hh"
57 #include "dataset/NormalTermPtr.DLList.h"
58 #include "irrutil/debug.hh"
59 #include "rpsl/rpsl_filter.hh"
60 
61 const unsigned int EXPAND_AS_MACROS         = 0x000001;
62 const unsigned int EXPAND_COMMUNITIES       = 0x000002;
63 const unsigned int EXPAND_AS                = 0x000004;
64 const unsigned int EXPAND_NOTHING           = 0;
65 const unsigned int EXPAND_ALL               = (~0);
66 
67 
68 #define NOT_ANY 0
69 #define ANY     1
70 #define NEITHER 2
71 
72 class NormalExpression {
73 public:
NormalExpression()74    NormalExpression() : terms() {
75       singleton_flag = -1;
76    }
77    NormalExpression(NormalExpression& a);
~NormalExpression()78    ~NormalExpression() {
79       terms.clear();
80    }
81 
82    friend std::ostream& operator<<(std::ostream& stream, NormalExpression& ne);
83 
84    void do_and(NormalExpression &other);
85    void do_or(NormalExpression &other);
86    void restrict(FilterAFI *af);
87    void do_not();
88 
is_universal()89    int is_universal() {
90       return length() == 1 && terms(terms.first())->is_universal();
91    }
isEmpty()92    int isEmpty () {
93       return (length() == 0);
94    }
is_any()95    int is_any() {
96       if (isEmpty())
97 	 return NOT_ANY;
98       if (is_universal())
99 	 return ANY;
100       return NEITHER;
101    }
102 
become_universal()103    void become_universal () {
104       NormalTerm* nt = new NormalTerm;
105       nt->make_universal();
106       terms.clear();
107       terms.append(nt);
108 
109       singleton_flag = -1;
110    }
111 
become_empty()112    void become_empty () {
113       terms.clear();
114       singleton_flag = -1;
115    }
116 
makeMoreSpecific(int code,int n,int m)117    void makeMoreSpecific(int code, int n, int m) {
118       NormalTerm *nt = first();
119       if (nt) {
120 	 nt->prfx_set.makeMoreSpecific(code, n, m);
121 	 if (nt->isEmpty())
122 	    become_empty();
123       }
124    }
125 
operator +=(NormalTerm * nt)126    void operator +=(NormalTerm* nt) {
127       terms.append(nt);
128    }
129 
130    CLASS_DEBUG_MEMORY_HH(NormalExpression);
131 
first()132    NormalTerm *first () {
133       i = terms.first();
134       return i ? terms(i) : (NormalTerm *)0;
135    }
136 
next()137    NormalTerm *next () {
138       terms.next(i);
139       return i ? terms(i) : (NormalTerm *)0;
140    }
141 
length()142    int length () {
143       return terms.length();
144    }
145 
146    static NormalExpression *evaluate(const FilterRPAttribute *ptree,
147 				     ASt peerAS,
148 				     unsigned int expand = EXPAND_ALL);
149    static NormalExpression *evaluate(const Filter *ptree,
150 				     ASt peerAS,
151 				     unsigned int expand = EXPAND_ALL);
152 
153 private:
154    // Normal Expression is the union of Normal Terms in the following list
155    NormalTermPtrDLList terms;
156 
157    Pix i;
158    void reduce();
159 
160 public:
161    int singleton_flag;          // If the NE contains one NT
162                                 // and that one NT contains only one filter
163                                 // which is not universal,
164                                 // then this flag contains that filter's index
165                                 // the value of this field is -1 otherwise
166                                 // the value of the flag is set by
167                                 // Node::Evalutate and do_and, do_or, do_not
168 				// used to speed up NE manipulation
169 };
170 
171 #endif   // NE_H
172