Python script

The tun-ping-responder.py file is a standalone Python script which contains all the code you need to use tun on Windows.

  File Modified

PNG File image2013-3-5 1:5:7.png

Mar 05, 2013 by Thomas Watteyne

File tun-ping-responder.py

Mar 05, 2013 by Thomas Watteyne

Overview

The file should be self-explanatory. Here are some keys and gotchas for understanding it:

  • it creates two threads:
    • the ReadThread listens for IPv4 and IPv6 packets sent by the kernel; if these are echo requests, it formats an echo reply and sends that back.
    • the WriteThread periodically sends IPv4 and IPv6 echo requests, and prints when it receives the corresponding replies.
  • The tun interface is a Windows file which is accessed through the ReadFile() and WriteFile() functions. It does so in OVERLAPPED mode, so that a call to ReadFile() does not block the call of WriteFile().
  • It contains the code to parse and create IPv6/IPv4 echo request/reply which you might enjoy looking at.

Setup

  • Install Python 2.7.
  • Install pywin32, which allows the (platform-independent) Python to access the Windows API.
  • Follow the earlier steps of this tutorial. If you chose different addresses for your interfaces, you'll have to edit the script to match those.

Running the Script

Start the script. You will see that the script (in fact the WriteThread thread) sends period echo requests.

componentId = {A9A413D7-4D1C-47BA-A3A9-92F091828881}
tuntap      = 208
Press enter to stop...
Transmitting IPv6 echo request
Received IPv6 echo reply
Transmitting IPv4 echo request
Received IPv4 echo reply
Transmitting IPv6 echo request
Received IPv6 echo reply
Transmitting IPv4 echo request
Received IPv4 echo reply
Transmitting IPv6 echo request
Received IPv6 echo reply

Start Wireshark on your tun interface to see the packets:

Open a command prompt and ping any IP address in the 10.2.0.xx and bbbb:/64 address ranges. The script will answer those.

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.


C:\Users\Thomas>ping 10.2.0.55


Pinging 10.2.0.55 with 32 bytes of data:
Reply from 10.2.0.55: bytes=32 time=1ms TTL=128
Reply from 10.2.0.55: bytes=32 time=1ms TTL=128
Reply from 10.2.0.55: bytes=32 time=1ms TTL=128
Reply from 10.2.0.55: bytes=32 time=1ms TTL=128


Ping statistics for 10.2.0.55:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 1ms, Maximum = 1ms, Average = 1ms


C:\Users\Thomas>ping bbbb::21


Pinging bbbb::21 from bbbb::1 with 32 bytes of data:
Reply from bbbb::21: time=1ms
Reply from bbbb::21: time=1ms
Reply from bbbb::21: time=1ms
Reply from bbbb::21: time=1ms


Ping statistics for bbbb::21:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 1ms, Maximum = 1ms, Average = 1ms