1 /*
2 
3                           Firewall Builder
4 
5                  Copyright (C) 2000 NetCitadel, LLC
6 
7   Author:  Vadim Kurland     vadim@fwbuilder.org
8 
9   $Id$
10 
11 
12   This program is free software which we release under the GNU General Public
13   License. You may redistribute and/or modify this program under the terms
14   of that license as published by the Free Software Foundation; either
15   version 2 of the License, or (at your option) any later version.
16 
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21 
22   To get a copy of the GNU General Public License, write to the Free Software
23   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 
25 */
26 
27 #include <assert.h>
28 
29 #include "config.h"
30 #include "fwbuilder/libfwbuilder-config.h"
31 
32 
33 #include "fwbuilder/IPService.h"
34 #include "fwbuilder/XMLTools.h"
35 
36 #include <iostream>
37 
38 using namespace libfwbuilder;
39 using namespace std;
40 
41 const char *IPService::TYPENAME={"IPService"};
42 
43 std::map<int, std::string> IPService::named_protocols;
44 
45 
initNamedProtocols()46 void IPService::initNamedProtocols()
47 {
48     if (IPService::named_protocols.size() == 0)
49     {
50         IPService::named_protocols[0] = "ip";
51         IPService::named_protocols[1] = "icmp";
52         IPService::named_protocols[6] = "tcp";
53         IPService::named_protocols[17] = "udp";
54     }
55 }
56 
IPService()57 IPService::IPService()
58 {
59     setStr("protocol_num", "");
60     IPService::initNamedProtocols();
61 }
62 
~IPService()63 IPService::~IPService() {}
64 
addNamedProtocol(int proto_num,const std::string & proto_name)65 void IPService::addNamedProtocol(int proto_num, const std::string &proto_name)
66 {
67     // Call initialize function because we might be trying to register
68     // named protocol before any IPService objects have been created.
69     IPService::initNamedProtocols();
70     IPService::named_protocols[proto_num] = proto_name;
71 }
72 
setProtocolNumber(int n)73 void IPService::setProtocolNumber(int n)
74 {
75     setInt("protocol_num", n);
76 }
77 
getProtocolName() const78 string IPService::getProtocolName()  const
79 {
80     int proto_num = getInt("protocol_num");
81 
82     if (IPService::named_protocols.count(proto_num) > 0)
83         return IPService::named_protocols[proto_num];
84     else
85         return getStr("protocol_num");
86 }
87 
getProtocolNumber() const88 int IPService::getProtocolNumber() const
89 {
90     return getInt("protocol_num");
91 }
92 
93 
fromXML(xmlNodePtr root)94 void IPService::fromXML(xmlNodePtr root) throw(FWException)
95 {
96     FWObject::fromXML(root);
97 
98     const char *n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("protocol_num")));
99     assert(n!=NULL);
100     setStr("protocol_num", n);
101     FREEXMLBUFF(n);
102 
103     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("fragm")));
104     if(n!=NULL)
105     {
106         setStr("fragm", n);
107         FREEXMLBUFF(n);
108     }
109 
110     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("short_fragm")));
111     if(n!=NULL)
112     {
113         setStr("short_fragm", n);
114         FREEXMLBUFF(n);
115     }
116 
117     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("any_opt")));
118     if(n!=NULL)
119     {
120         setStr("any_opt", n);
121         FREEXMLBUFF(n);
122     }
123 
124     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("lsrr")));
125     if(n!=NULL)
126     {
127         setStr("lsrr", n);
128         FREEXMLBUFF(n);
129     }
130 
131     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("ssrr")));
132     if(n!=NULL)
133     {
134         setStr("ssrr", n);
135         FREEXMLBUFF(n);
136     }
137 
138     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("rr")));
139     if(n!=NULL)
140     {
141         setStr("rr", n);
142         FREEXMLBUFF(n);
143     }
144 
145     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("ts")));
146     if(n!=NULL)
147     {
148         setStr("ts", n);
149         FREEXMLBUFF(n);
150     }
151 
152     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("tos")));
153     if(n!=NULL)
154     {
155         setStr("tos", n);
156         FREEXMLBUFF(n);
157     }
158 
159     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("dscp")));
160     if(n!=NULL)
161     {
162         setStr("dscp", n);
163         FREEXMLBUFF(n);
164     }
165 
166     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("rtralt")));
167     if(n!=NULL)
168     {
169         setStr("rtralt", n);
170         FREEXMLBUFF(n);
171     }
172 
173     n=FROMXMLCAST(xmlGetProp(root,TOXMLCAST("rtralt_value")));
174     if(n!=NULL)
175     {
176         setStr("rtralt_value", n);
177         FREEXMLBUFF(n);
178     }
179 
180 }
181 
toXML(xmlNodePtr parent)182 xmlNodePtr IPService::toXML(xmlNodePtr parent) throw(FWException)
183 {
184     xmlNodePtr me = FWObject::toXML(parent, false);
185 
186     xmlNewProp(me, TOXMLCAST("name"), STRTOXMLCAST(getName()));
187     xmlNewProp(me, TOXMLCAST("comment"), STRTOXMLCAST(getComment()));
188     xmlNewProp(me, TOXMLCAST("ro"), TOXMLCAST(((getRO()) ? "True" : "False")));
189 
190     return me;
191 }
192 
hasIpOptions() const193 bool IPService::hasIpOptions() const
194 {
195     return (getBool("any_opt") ||
196             getBool("lsrr") ||
197             getBool("rr") ||
198             getBool("ssrr") ||
199             getBool("ts") ||
200             getBool("rtralt"));
201 }
202 
getTOSCode() const203 string IPService::getTOSCode() const
204 {
205     return getStr("tos");
206 }
207 
setTOSCode(const string & c)208 void IPService::setTOSCode(const string &c)
209 {
210     setStr("tos", c);
211 }
212 
getDSCPCode() const213 string IPService::getDSCPCode() const
214 {
215     return getStr("dscp");
216 }
217 
setDSCPCode(const string & c)218 void IPService::setDSCPCode(const string &c)
219 {
220     setStr("dscp", c);
221 }
222 
223 
224 
225 
226 
227 
228 
229 
230