fork download
  1. #!/usr/bin/python
  2.  
  3. import socket
  4. import fcntl
  5. import struct
  6. import netifaces as ni
  7.  
  8. from mininet.topo import Topo
  9. from mininet.net import Mininet
  10. from mininet.node import Node
  11. from mininet.log import setLogLevel, info
  12. from mininet.cli import CLI
  13.  
  14. def ip_checksum(void* vdata,size_t length):
  15. char* data=(char*)vdata
  16. uint32_t acc=0xffff
  17. size_t i=0
  18. for i<length :
  19. uint16_t word
  20. memcpy(&word,data+i,2)
  21. acc+=ntohs(word)
  22. if acc>0xffff :
  23. acc-=0xffff
  24. i+=2
  25.  
  26.  
  27. if length&1 :
  28. uint16_t word=0
  29. memcpy(&word,data+length-1,1)
  30. acc+=ntohs(word)
  31. if acc>0xffff :
  32. acc-=0xffff
  33.  
  34.  
  35. return htons(~acc)
  36.  
  37.  
  38. class LinuxRouter( Node ):
  39. "A Node with IP forwarding enabled."
  40.  
  41. def config( self, **params ):
  42. super( LinuxRouter, self).config( **params )
  43. # Enable forwarding on the router
  44. self.cmd( 'sysctl net.ipv4.ip_forward=1' )
  45.  
  46. def terminate( self ):
  47. self.cmd( 'sysctl net.ipv4.ip_forward=0' )
  48. super( LinuxRouter, self ).terminate()
  49.  
  50. class NetworkTopo( Topo ):
  51. "A LinuxRouter connecting three IP subnets"
  52.  
  53. def build( self, **_opts ):
  54.  
  55. defaultIP = '192.168.1.1/24' # IP address for r0-eth1
  56. router = self.addNode( 'r0', cls=LinuxRouter, ip=defaultIP )
  57.  
  58. s1, s2, s3 = [ self.addSwitch( s ) for s in ( 's1', 's2', 's3' ) ]
  59.  
  60. self.addLink( s1, router, intfName2='r0-eth1',
  61. params2={ 'ip' : defaultIP } ) # for clarity
  62. self.addLink( s2, router, intfName2='r0-eth2',
  63. params2={ 'ip' : '172.16.0.1/12' } )
  64. self.addLink( s3, router, intfName2='r0-eth3',
  65. params2={ 'ip' : '10.0.0.1/8' } )
  66.  
  67. h1x1 = self.addHost( 'h1x1', ip='192.168.1.101/24', mac='00:00:00:00:00:11',
  68. defaultRoute='via 192.168.1.1' )
  69. h1x2 = self.addHost( 'h1x2', ip='192.168.1.102/24', mac='00:00:00:00:00:12',
  70. defaultRoute='via 192.168.1.1' )
  71. h2x1 = self.addHost( 'h2x1', ip='172.16.0.101/12', mac='00:00:00:00:00:21',
  72. defaultRoute='via 172.16.0.1' )
  73. h2x2 = self.addHost( 'h2x2', ip='172.16.0.102/12', mac='00:00:00:00:00:22',
  74. defaultRoute='via 172.16.0.1' )
  75. h3x1 = self.addHost( 'h3x1', ip='10.0.0.101/8', mac='00:00:00:00:00:31',
  76. defaultRoute='via 10.0.0.1' )
  77. h3x2 = self.addHost( 'h3x2', ip='10.0.0.102/8', mac='00:00:00:00:00:32',
  78. defaultRoute='via 10.0.0.1' )
  79.  
  80. for h, s in [ (h1x1, s1), (h1x2, s1), (h2x1, s2), (h2x2, s2), (h3x1, s3), (h3x2, s3) ]:
  81. self.addLink( h, s )
  82.  
  83. def run():
  84.  
  85. topo = NetworkTopo()
  86. net = Mininet( topo=topo ) # controller is used by s1-s3
  87. net.start()
  88. info( '*** Routing Table on Router:\n' )
  89. info( net[ 'r0' ].cmd( 'route' ) )
  90. mode=raw_input(‘send or recv’)
  91.  
  92. interfacename=rawinput('input interfacename')
  93. intfIP= ni.ifaddresses(interfacename)[ni.AF_INET][0]['addr']
  94.  
  95. if mode == 'send':
  96. destIP=rawinput('input destIP')
  97. routerIP=rawinput('input router')
  98. buf=rawinput('input message')
  99. message=ip_checksum(buf, 20)
  100.  
  101. s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  102. s.bind((intfIP, 7777))
  103. s.send(buf)
  104.  
  105. s.close
  106.  
  107. elif mode == 'recv':
  108. s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  109. s.bind((intfIP, 7777))
  110.  
  111. data = s.recv(2048)
  112. print('receive:')
  113. print data
  114.  
  115. s.close
  116.  
  117. else:
  118. print(''Error')
  119. exit()
  120.  
  121.  
  122. if __name__ == '__main__':
  123. setLogLevel( 'info' )
  124. run()
  125.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.7/py_compile.py", line 143, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./prog.py", line 14
    def ip_checksum(void* vdata,size_t length):
                        ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.7/py_compile.py", line 147, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 14
    def ip_checksum(void* vdata,size_t length):
                        ^
SyntaxError: invalid syntax

stdout
Standard output is empty