From ffeb16eb66fdd5d0e8b5e48bc0b979ae0472b707 Mon Sep 17 00:00:00 2001
From: Brian O'Connor <bocon13@gmail.com>
Date: Thu, 3 Oct 2013 16:29:05 -0700
Subject: [PATCH] fixing --nat option in mn

---
 bin/mn             |  6 ++++--
 examples/natnet.py |  4 ++--
 mininet/net.py     | 14 +++++++++-----
 mininet/node.py    |  9 +--------
 4 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/bin/mn b/bin/mn
index 0424d355..474a3c20 100755
--- a/bin/mn
+++ b/bin/mn
@@ -226,8 +226,6 @@ class MininetRunner( object ):
         start = time.time()
 
         topo = buildTopo( TOPOS, self.options.topo )
-        if self.options.nat:
-          topo.addNAT()
         switch = customConstructor( SWITCHES, self.options.switch )
         host = customConstructor( HOSTS, self.options.host )
         controller = customConstructor( CONTROLLERS, self.options.controller )
@@ -255,6 +253,10 @@ class MininetRunner( object ):
                   autoStaticArp=arp, autoPinCpus=pin,
                   listenPort=listenPort )
 
+        if self.options.nat:
+            nat = mn.addNAT()
+            mn.configHosts()
+
         if self.options.pre:
             CLI( mn, script=self.options.pre )
 
diff --git a/examples/natnet.py b/examples/natnet.py
index 718ca42f..cb0c44c6 100755
--- a/examples/natnet.py
+++ b/examples/natnet.py
@@ -56,7 +56,7 @@ def __init__(self, n=2, h=1, **opts):
                                 defaultRoute='via %s' % localIP)
             self.addLink(host, switch)
 
-def perfTest():
+def run():
     "Create network and run simple performance test"
     topo = InternetTopo()
     net = Mininet(topo=topo)
@@ -66,4 +66,4 @@ def perfTest():
 
 if __name__ == '__main__':
     setLogLevel('info')
-    perfTest()
\ No newline at end of file
+    run()
\ No newline at end of file
diff --git a/mininet/net.py b/mininet/net.py
index f2f49b30..1912fba6 100755
--- a/mininet/net.py
+++ b/mininet/net.py
@@ -227,14 +227,18 @@ def addController( self, name='c0', controller=None, **params ):
             self.nameToNode[ name ] = controller_new
         return controller_new
 
-    # TODO: incomplete
-    def addNAT( self, name='nat0', connect=True, **params ):
-        nat = self.addHost( name, cls=NAT, **params )
+    def addNAT( self, name='nat0', connect=True, inNamespace=False, **params ):
+        nat = self.addHost( name, cls=NAT, inNamespace=inNamespace, 
+                            subnet=self.ipBase, **params )
         # find first switch and create link
-        print "******* &&&&&& net/addNAT"
         if connect:
-            #connect the nat to the first switch
+            # connect the nat to the first switch
             self.addLink( nat, self.switches[ 0 ] )
+            # set the default route on hosts
+            natIP = nat.params[ 'ip' ].split('/')[ 0 ]
+            for host in self.hosts:
+                if host.inNamespace:
+                    host.setDefaultRoute( 'via %s' % natIP )
         return nat
 
     # BL: We now have four ways to look up nodes
diff --git a/mininet/node.py b/mininet/node.py
index d6e8f875..edc6de81 100644
--- a/mininet/node.py
+++ b/mininet/node.py
@@ -1253,7 +1253,7 @@ def __init__( self, name, inetIntf='eth0', subnet='10.0/8', localIntf=None, **pa
         inetIntf: interface for internet access
         subnet: Mininet subnet (default 10.0/8)="""
         self.inetIntf = inetIntf
-        self.subnet = subnet #TODO: get subnet from Mininet directly
+        self.subnet = subnet
         self.localIntf = localIntf
 
     def config( self, **params ):
@@ -1263,13 +1263,6 @@ def config( self, **params ):
         if not self.localIntf:
             self.localIntf =  self.defaultIntf()
 
-        #-------------------------
-        print "inetIntf:", self.inetIntf
-        print "subnet:", self.subnet
-        # Identify the interface connecting to the mininet network
-        print "LocalIntf:", self.localIntf
-        #-------------------------
-
         self.cmd( 'sysctl net.ipv4.ip_forward=0' )
 
         # Flush any currently active rules
-- 
GitLab