From 7f66d324b3637d3cb6f18315b9b504c266a7f404 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Wed, 23 Dec 2020 17:45:54 +0800 Subject: [PATCH] allow arbitrary subnets for the ip tunnel --- meshtastic/__main__.py | 5 ++++- meshtastic/portnums_pb2.py | 10 +++++----- meshtastic/tunnel.py | 16 +++++++++------- proto | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index df4c8e6..22f2932 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -277,7 +277,7 @@ def onConnected(interface): if args.tunnel: closeNow = False # Even if others said we could close, stay open if the user asked for a tunnel - tunnel.Tunnel(interface) + tunnel.Tunnel(interface, subnet=args.tunnel_net) except Exception as ex: print(ex) @@ -402,6 +402,9 @@ def main(): parser.add_argument('--tunnel', action='store_true', help="Create a TUN tunnel device for forwarding IP packets over the mesh") + parser.add_argument( + "--subnet", dest='tunnel_net', help="Read from a GPIO mask", default=None) + parser.set_defaults(router=None) parser.add_argument('--version', action='version', version=f"{pkg_resources.require('meshtastic')[0].version}") diff --git a/meshtastic/portnums_pb2.py b/meshtastic/portnums_pb2.py index 6b0f2eb..389ab87 100644 --- a/meshtastic/portnums_pb2.py +++ b/meshtastic/portnums_pb2.py @@ -20,7 +20,7 @@ DESCRIPTOR = _descriptor.FileDescriptor( syntax='proto3', serialized_options=b'\n\023com.geeksville.meshB\010PortnumsH\003', create_key=_descriptor._internal_create_key, - serialized_pb=b'\n\x0eportnums.proto*\xa2\x01\n\x07PortNum\x12\x0f\n\x0bUNKNOWN_APP\x10\x00\x12\x14\n\x10TEXT_MESSAGE_APP\x10\x01\x12\x17\n\x13REMOTE_HARDWARE_APP\x10\x02\x12\x10\n\x0cPOSITION_APP\x10\x03\x12\x10\n\x0cNODEINFO_APP\x10\x04\x12\r\n\tREPLY_APP\x10 \x12\x10\n\x0bPRIVATE_APP\x10\x80\x02\x12\x12\n\rIP_TUNNEL_APP\x10\x80\x08\x42!\n\x13\x63om.geeksville.meshB\x08PortnumsH\x03\x62\x06proto3' + serialized_pb=b'\n\x0eportnums.proto*\xa1\x01\n\x07PortNum\x12\x0f\n\x0bUNKNOWN_APP\x10\x00\x12\x14\n\x10TEXT_MESSAGE_APP\x10\x01\x12\x17\n\x13REMOTE_HARDWARE_APP\x10\x02\x12\x10\n\x0cPOSITION_APP\x10\x03\x12\x10\n\x0cNODEINFO_APP\x10\x04\x12\r\n\tREPLY_APP\x10 \x12\x11\n\rIP_TUNNEL_APP\x10!\x12\x10\n\x0bPRIVATE_APP\x10\x80\x02\x42!\n\x13\x63om.geeksville.meshB\x08PortnumsH\x03\x62\x06proto3' ) _PORTNUM = _descriptor.EnumDescriptor( @@ -61,12 +61,12 @@ _PORTNUM = _descriptor.EnumDescriptor( type=None, create_key=_descriptor._internal_create_key), _descriptor.EnumValueDescriptor( - name='PRIVATE_APP', index=6, number=256, + name='IP_TUNNEL_APP', index=6, number=33, serialized_options=None, type=None, create_key=_descriptor._internal_create_key), _descriptor.EnumValueDescriptor( - name='IP_TUNNEL_APP', index=7, number=1024, + name='PRIVATE_APP', index=7, number=256, serialized_options=None, type=None, create_key=_descriptor._internal_create_key), @@ -74,7 +74,7 @@ _PORTNUM = _descriptor.EnumDescriptor( containing_type=None, serialized_options=None, serialized_start=19, - serialized_end=181, + serialized_end=180, ) _sym_db.RegisterEnumDescriptor(_PORTNUM) @@ -85,8 +85,8 @@ REMOTE_HARDWARE_APP = 2 POSITION_APP = 3 NODEINFO_APP = 4 REPLY_APP = 32 +IP_TUNNEL_APP = 33 PRIVATE_APP = 256 -IP_TUNNEL_APP = 1024 DESCRIPTOR.enum_types_by_name['PortNum'] = _PORTNUM diff --git a/meshtastic/tunnel.py b/meshtastic/tunnel.py index 9e86763..89b4f65 100644 --- a/meshtastic/tunnel.py +++ b/meshtastic/tunnel.py @@ -7,7 +7,6 @@ # sudo bin/run.sh --port /dev/ttyUSB0 --tunnel --debug # FIXME: set MTU correctly -# allow setting arbitary IP addresses from . import portnums_pb2 from pubsub import pub @@ -52,19 +51,22 @@ def onTunnelReceive(packet, interface): FIXME figure out how to do closures with methods in python""" tunnelInstance.onReceive(packet) - -subnetPrefix = "10.115" - class Tunnel: """A TUN based IP tunnel over meshtastic""" - def __init__(self, iface): + def __init__(self, iface, subnet=None, netmask="255.255.0.0"): """ Constructor iface is the already open MeshInterface instance + subnet is used to construct our network number (normally 10.115.x.x) """ + + if subnet is None: + subnet = "10.115" + self.iface = iface + self.subnetPrefix = subnet global tunnelInstance tunnelInstance = self @@ -83,7 +85,7 @@ class Tunnel: self.tun = TapDevice(name="mesh", mtu=200) # tun.create() self.tun.up() - self.tun.ifconfig(address=myAddr,netmask="255.255.0.0") + self.tun.ifconfig(address=myAddr,netmask=netmask) logging.debug(f"starting TUN reader, our IP address is {myAddr}") self._rxThread = threading.Thread(target=self.__tunReader, args=(), daemon=True) self._rxThread.start() @@ -150,7 +152,7 @@ class Tunnel: return None def _nodeNumToIp(self, nodeNum): - return f"{subnetPrefix}.{(nodeNum >> 8) & 0xff}.{nodeNum & 0xff}" + return f"{self.subnetPrefix}.{(nodeNum >> 8) & 0xff}.{nodeNum & 0xff}" def sendPacket(self, destAddr, p): """Forward the provided IP packet into the mesh""" diff --git a/proto b/proto index 3c06a59..3473446 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 3c06a5962b35f3959f17b9120802bf286dba01c7 +Subproject commit 3473446418a4e83ff0ff4c7588101c2e1b29e604