11#!/usr/bin/python3
22
3+ import os
34import sys
45import argparse
6+
7+ sys .path .append (os .path .join (os .path .dirname (__file__ ), '../../src' ))
58from config import *
69
710
811def confgen (subnets , hosts , fault ):
9- network = Network ()
12+ config = Config ()
1013
1114 ## firewall rules
1215 fw_rules = """
@@ -38,7 +41,7 @@ def confgen(subnets, hosts, fault):
3841 internet_node .add_static_route (Route ('10.0.0.0/8' , '8.0.0.2' ))
3942 internet_node .add_static_route (Route ('11.0.0.0/8' , '8.0.0.2' ))
4043 internet_node .add_static_route (Route ('12.0.0.0/8' , '8.0.0.2' ))
41- network .add_node (internet_node )
44+ config .add_node (internet_node )
4245 fw = Middlebox ('fw' , 'netns' , 'netfilter' )
4346 fw .add_interface (Interface ('eth0' , '8.0.0.2/24' ))
4447 fw .add_interface (Interface ('eth1' , '9.0.0.1/24' ))
@@ -51,7 +54,7 @@ def confgen(subnets, hosts, fault):
5154 fw .add_config ('rules' , fw_bad_rules )
5255 else :
5356 fw .add_config ('rules' , fw_rules )
54- network .add_node (fw )
57+ config .add_node (fw )
5558 gw = Node ('gw' )
5659 gw .add_interface (Interface ('eth0' , '9.0.0.2/24' ))
5760 for subnet in range (subnets ): # add "public"-connecting interfaces
@@ -66,18 +69,18 @@ def confgen(subnets, hosts, fault):
6669 '10.%d.0.1/16' % subnet )
6770 gw .add_interface (intf )
6871 gw .add_static_route (Route ('0.0.0.0/0' , '9.0.0.1' ))
69- network .add_node (gw )
70- network .add_link (Link ('internet' , 'eth0' , 'fw' , 'eth0' ))
71- network .add_link (Link ('fw' , 'eth1' , 'gw' , 'eth0' ))
72+ config .add_node (gw )
73+ config .add_link (Link ('internet' , 'eth0' , 'fw' , 'eth0' ))
74+ config .add_link (Link ('fw' , 'eth1' , 'gw' , 'eth0' ))
7275
7376 ## add nodes and links in the public subnets
7477 for subnet in range (subnets ):
7578 sw = Node ('public%d-sw' % subnet )
7679 sw .add_interface (Interface ('eth0' ))
7780 for i in range (1 , hosts + 1 ):
7881 sw .add_interface (Interface ('eth%d' % i ))
79- network .add_node (sw )
80- network .add_link (Link (sw .name , 'eth0' , 'gw' , 'eth%d' % (subnet + 1 )))
82+ config .add_node (sw )
83+ config .add_link (Link (sw .name , 'eth0' , 'gw' , 'eth%d' % (subnet + 1 )))
8184 for i in range (1 , hosts + 1 ):
8285 host = Node ('public%d-host%d' % (subnet , i - 1 ))
8386 second_last = ((i + 1 ) // 256 ) % 256
@@ -86,17 +89,17 @@ def confgen(subnets, hosts, fault):
8689 Interface ('eth0' ,
8790 '12.%d.%d.%d/16' % (subnet , second_last , last )))
8891 host .add_static_route (Route ('0.0.0.0/0' , '12.%d.0.1' % subnet ))
89- network .add_node (host )
90- network .add_link (Link (host .name , 'eth0' , sw .name , 'eth%d' % i ))
92+ config .add_node (host )
93+ config .add_link (Link (host .name , 'eth0' , sw .name , 'eth%d' % i ))
9194
9295 ## add nodes and links in the private subnets
9396 for subnet in range (subnets ):
9497 sw = Node ('private%d-sw' % subnet )
9598 sw .add_interface (Interface ('eth0' ))
9699 for i in range (1 , hosts + 1 ):
97100 sw .add_interface (Interface ('eth%d' % i ))
98- network .add_node (sw )
99- network .add_link (
101+ config .add_node (sw )
102+ config .add_link (
100103 Link (sw .name , 'eth0' , 'gw' , 'eth%d' % (subnet + 1 + subnets )))
101104 for i in range (1 , hosts + 1 ):
102105 host = Node ('private%d-host%d' % (subnet , i - 1 ))
@@ -106,17 +109,17 @@ def confgen(subnets, hosts, fault):
106109 Interface ('eth0' ,
107110 '11.%d.%d.%d/16' % (subnet , second_last , last )))
108111 host .add_static_route (Route ('0.0.0.0/0' , '11.%d.0.1' % subnet ))
109- network .add_node (host )
110- network .add_link (Link (host .name , 'eth0' , sw .name , 'eth%d' % i ))
112+ config .add_node (host )
113+ config .add_link (Link (host .name , 'eth0' , sw .name , 'eth%d' % i ))
111114
112115 ## add nodes and links in the quarantined subnets
113116 for subnet in range (subnets ):
114117 sw = Node ('quarantined%d-sw' % subnet )
115118 sw .add_interface (Interface ('eth0' ))
116119 for i in range (1 , hosts + 1 ):
117120 sw .add_interface (Interface ('eth%d' % i ))
118- network .add_node (sw )
119- network .add_link (
121+ config .add_node (sw )
122+ config .add_link (
120123 Link (sw .name , 'eth0' , 'gw' , 'eth%d' % (subnet + 1 + subnets * 2 )))
121124 for i in range (1 , hosts + 1 ):
122125 host = Node ('quarantined%d-host%d' % (subnet , i - 1 ))
@@ -126,13 +129,12 @@ def confgen(subnets, hosts, fault):
126129 Interface ('eth0' ,
127130 '10.%d.%d.%d/16' % (subnet , second_last , last )))
128131 host .add_static_route (Route ('0.0.0.0/0' , '10.%d.0.1' % subnet ))
129- network .add_node (host )
130- network .add_link (Link (host .name , 'eth0' , sw .name , 'eth%d' % i ))
132+ config .add_node (host )
133+ config .add_link (Link (host .name , 'eth0' , sw .name , 'eth%d' % i ))
131134
132135 ## add policies
133- policies = Policies ()
134136 # public subnets can initiate connections to the outside world
135- policies .add_policy (
137+ config .add_policy (
136138 ReachabilityPolicy (target_node = 'internet' ,
137139 reachable = True ,
138140 protocol = 'tcp' ,
@@ -141,7 +143,7 @@ def confgen(subnets, hosts, fault):
141143 dst_port = [80 ],
142144 owned_dst_only = True ))
143145 # public subnets can accept connections from the outside world
144- policies .add_policy (
146+ config .add_policy (
145147 ReachabilityPolicy (target_node = '(public.*-host.*)|gw' ,
146148 reachable = True ,
147149 protocol = 'tcp' ,
@@ -151,7 +153,7 @@ def confgen(subnets, hosts, fault):
151153 owned_dst_only = True ))
152154 # private subnets can initiate connections to the outside world and replies
153155 # from the outside world can reach the private subnets
154- policies .add_policy (
156+ config .add_policy (
155157 ReplyReachabilityPolicy (target_node = 'internet' ,
156158 reachable = True ,
157159 protocol = 'tcp' ,
@@ -160,7 +162,7 @@ def confgen(subnets, hosts, fault):
160162 dst_port = [80 ],
161163 owned_dst_only = True ))
162164 # private subnets can't accept connections from the outside world
163- policies .add_policy (
165+ config .add_policy (
164166 ReachabilityPolicy (target_node = '(private.*-host.*)|gw' ,
165167 reachable = False ,
166168 protocol = 'tcp' ,
@@ -169,7 +171,7 @@ def confgen(subnets, hosts, fault):
169171 dst_port = [80 ],
170172 owned_dst_only = True ))
171173 # quarantined subnets can't initiate connections to the outside world
172- policies .add_policy (
174+ config .add_policy (
173175 ReachabilityPolicy (target_node = 'internet' ,
174176 reachable = False ,
175177 protocol = 'tcp' ,
@@ -178,7 +180,7 @@ def confgen(subnets, hosts, fault):
178180 dst_port = [80 ],
179181 owned_dst_only = True ))
180182 # quarantined subnets can't accept connections from the outside world
181- policies .add_policy (
183+ config .add_policy (
182184 ReachabilityPolicy (target_node = '(quarantined.*-host.*)|gw' ,
183185 reachable = False ,
184186 protocol = 'tcp' ,
@@ -188,7 +190,7 @@ def confgen(subnets, hosts, fault):
188190 owned_dst_only = True ))
189191
190192 ## output as TOML
191- output_toml (network , None , policies )
193+ config . output_toml ()
192194
193195
194196def main ():
0 commit comments