1*9c5db199SXin Li# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 2*9c5db199SXin Li# Use of this source code is governed by a BSD-style license that can be 3*9c5db199SXin Li# found in the LICENSE file. 4*9c5db199SXin Li 5*9c5db199SXin LiDOC = """\ 6*9c5db199SXin Lilansim is a LAN simulator that runs over a TAP network interface and 7*9c5db199SXin Liallows to simulate network traffic on that interface from Python code. 8*9c5db199SXin Li 9*9c5db199SXin LiA TAP interface is a virtual network kernel device that acts as any 10*9c5db199SXin Liother network interface, except that instead of sending and receiving 11*9c5db199SXin Lithe traffic through a hardware interface it allows a given program to 12*9c5db199SXin Lihandle that traffic through a socket. It is essentially a 13*9c5db199SXin Libi-directional pipe where one side is a network interface and the 14*9c5db199SXin Liother side is a socket on a program. 15*9c5db199SXin Li 16*9c5db199SXin LiThe lansim Simulator class allows a Python function to attend all the 17*9c5db199SXin Lioutbound traffic matching certain rules and take an action over that 18*9c5db199SXin Lilike send back a packet to this interface. The kernel network stack 19*9c5db199SXin Liwill see this packet as an inbound packet from the fake interface. 20*9c5db199SXin LiThese actions can also be time driven, like a simulation timeout or 21*9c5db199SXin Lisend a ping packet every second. 22*9c5db199SXin Li 23*9c5db199SXin LiThis simulator is useful on situations where you can't fake a network 24*9c5db199SXin Liservice using the normal kernel network stack. For example, if you 25*9c5db199SXin Lineed to fake a network of several hosts publishing services via mDNS 26*9c5db199SXin Liwith multicast you can write those services using this simulator but 27*9c5db199SXin Liit's more complicated to do the same using the system's network stack 28*9c5db199SXin Lisince an outbound multicast packet will be sent out on the real 29*9c5db199SXin Liinterface. 30*9c5db199SXin Li 31*9c5db199SXin LiThe Simulator class requires a TAP network interface and allows other 32*9c5db199SXin LiPython classes to subscribe to its traffic. The TAP interface needs to 33*9c5db199SXin Libe up when the Simulator runs. 34*9c5db199SXin Li 35*9c5db199SXin LiFor example, to create a tap interface you can do the following: 36*9c5db199SXin Li 37*9c5db199SXin Li tap = tuntap.TunTap(tuntap.IFF_TAP, name="faketap") 38*9c5db199SXin Li print "Interface running on", tap.name 39*9c5db199SXin Li 40*9c5db199SXin LiThis will create a TAP interface with a name like "faketap0". You can 41*9c5db199SXin Lithen configure it manually, or just bring it up without an IP address 42*9c5db199SXin Liif you have a Python class implementing a fake DHCP server to 43*9c5db199SXin Liconfigure it later. To set the IP address manually, you can use any 44*9c5db199SXin Lisystem command like "ip" or "ifconfig", or from Python in this way: 45*9c5db199SXin Li 46*9c5db199SXin Li tap.set_addr("192.168.0.123") 47*9c5db199SXin Li tap.up() 48*9c5db199SXin Li 49*9c5db199SXin LiA very simple example of the usage of this simulator is the 50*9c5db199SXin Lifollowing, where three hosts are created on the faketap0 interface 51*9c5db199SXin Liand will respond to ARP requests. Running the command 52*9c5db199SXin Li"arping -I faketap0 192.168.0.4" will show two responses for that 53*9c5db199SXin Ligiven IP address: 54*9c5db199SXin Li 55*9c5db199SXin Li simu = simulator.SimulatorThread(tap) 56*9c5db199SXin Li 57*9c5db199SXin Li host_a = host.SimpleHost(simu, '12:34:56:78:90:AB', '192.168.0.3') 58*9c5db199SXin Li host_b = host.SimpleHost(simu, '12:34:56:78:90:CD', '192.168.0.4') 59*9c5db199SXin Li host_c = host.SimpleHost(simu, '12:34:56:78:90:EF', '192.168.0.4') 60*9c5db199SXin Li 61*9c5db199SXin Li simu.start() # Run the thread. 62*9c5db199SXin Li print "Press enter to stop the simulation." 63*9c5db199SXin Li raw_input() 64*9c5db199SXin Li simu.stop() 65*9c5db199SXin Li simu.join() 66*9c5db199SXin Li""" 67*9c5db199SXin Li 68*9c5db199SXin Lijob.setup_dep(['lansim']) 69