def initialize_vic(): # Read config file and get parameters. log.info ("Reading vic config from "+ DEFAULT_CONFIG_FILE) f = open(DEFAULT_CONFIG_FILE, "r") vic_config = yaml.load(f) f.close() # Configure each vic vic_id = 0 taps = {} for vic in vic_config: log.info ("Configuring vic id: "+ str(vic_id)) ip = vic['ip'] log.info("vic_ip: "+ ip) mac = vic['mac'] log.info("vic_mac: "+ mac) # Create virtual interface tap = TunTapDevice(name="vic-"+str(vic_id), flags=IFF_TAP) tap.hwaddr = '\x00\x11\x22\x33\x44\x55' tap.addr = ip tap.mtu = 1500 tap.up() log.info("Tap device {0} is up with mac: {1}".format("vic-"+str(vic_id), binascii.hexlify(tap.hwaddr))) taps[ip] = tap tap_list = [] for ip in taps.keys(): tap_list.append(taps[ip]) log.info("Waiting for packets on vics...") while True: handle_packets(tap_list)
def main(): global tap, my_mac, my_ip, server if len(sys.argv) != 3: print("Usage: %s url password" % sys.argv[0]) sys.exit(1) server = sys.argv[1] password = sys.argv[2] while server.endswith('/'): server = server[:-1] session = requests.Session() if os.path.exists("/tmp/tap0cache"): with open("/tmp/tap0cache", "rb") as f: data = f.read(10) my_mac = data[:6] my_ip = data[6:10] else: ans = session.post(server + '/connect', password) res = ans.content if ans.status_code != 200: raise ValueError("Failed to connect: " + str(res)) my_mac = res[:6] my_ip = res[6:10] with open("/tmp/tap0cache", "wb") as f: f.write(res) tap = TunTapDevice(flags=IFF_TAP) tap.addr = ".".join(map(str, my_ip)) print("My ip is:", tap.addr) tap.netmask = '255.255.0.0' tap.mtu = 1300 tap.hwaddr = my_mac tap.up() tap_reader = threading.Thread(target=read_data, daemon=True) tap_reader.start() sender = threading.Thread(target=send_data, daemon=True) sender.start() while True: ans = session.post(server + '/recv', my_mac) if ans.status_code == 204: continue if ans.status_code == 403: os.remove("/tmp/tap0cache") if ans.status_code != 200: print("recv: received status code " + str(ans.status_code) + ": " + ans.text) sys.exit(1) def process_packet(data): packet_mac = get_mac(data) if packet_mac == my_mac or packet_mac == BROADCAST: tap.write(data) parse_packets(BytesIO(ans.content), process_packet)
def run(self): """ Method called when starting the daemon. """ try: logger.info("TapDaemon has started") tap0 = TunTapDevice(name="tap0", flags=IFF_TAP) tap0.hwaddr = '\x00\x11\x22\x33\x44\x55' tap0.addr = '192.168.0.1' tap0.dstaddr = '192.168.0.2' tap0.netmask = '255.255.255.0' tap0.mtu = 1500 tap0.persist(True) tap0.up() logger.info("tap0 interface created") except: logger.exception("exception: ") try: tap1 = TunTapDevice(name="tap1", flags=IFF_TAP) tap1.hwaddr = '\x00\x11\x22\x33\x44\x66' tap1.addr = '192.168.1.1' tap1.dstaddr = '192.168.1.2' tap1.netmask = '255.255.255.0' tap1.mtu = 1500 tap1.persist(True) tap1.up() logger.info("tap1 interface created") except: logger.exception("exception: ") try: while True: time.sleep(2) frame = Ethernet(dst="\x01\x02\x03\x04\x05\x06", src="\x0A\x0B\x0C\x0D\x0E\x0F", type = 0x9100, data = "\x02\x55\x44\x00\x11\x00\x00\x00\x33\x22\x00\x00" + "\x61"*40) # "\x11\x00\x00\x00\x33\x22\x00\x00" #"\x00\x03\x08\x00" tap0.write("\x11\x22\x33\x44" + str(frame)) logger.info("Frame send to tap0") logger.info("Waiting for frame in tap1...") buf = tap1.read(tap1.mtu) logger.info("Received %s", hexlify(buf)) logger.info("\n\n ---------------------------------------------------") except: logger.exception("exception: ")
def main(): global tap, password if len(sys.argv) != 2: print("Usage: %s password" % sys.argv[0]) sys.exit(1) password = sys.argv[1] tap = TunTapDevice(flags=IFF_TAP) tap.addr = ".".join(map(str, IP_PREFIX + (0, 1))) tap.netmask = '255.255.0.0' tap.mtu = 1300 tap.hwaddr = MYMAC tap.up() tap_reader = threading.Thread(target=read_data, daemon=True) tap_reader.start() print('Serving on 8088...') WSGIServer(application, port=8088, numthreads=1000).start()
print('Create TAP device. Name = <' + DEVICE_NAME + '>') try: tap = TunTapDevice(name=DEVICE_NAME, flags=IFF_TAP | IFF_NO_PI) # tun = TunTapDevice(name=DEVICE_NAME) print('Success') except: print('TAP device create failure.') sys.exit(ERROR) print('Setting TAP device IP addr, Netmask, MTU, MAC') try: # ''' tap.addr = '10.20.1.1' tap.netmask = '255.255.255.0' tap.mtu = 1500 tap.hwaddr = b'\x00\x11\x22\x33\x44\x55' ''' tun.addr = '10.20.1.1' tun.netmask = '255.255.255.0' tun.mtu = 1500 #tun.dstaddr = '10.20.1.10' #tun.hwaddr = b'\x00\x11\x22\x33\x44\x55' ''' print('Success') except: print('Settings failure') sys.exit(ERROR) print('TAP device UP') try: tap.up()