Reticulum/RNS/Interfaces/UDPInterface.py

86 lines
2.7 KiB
Python
Raw Normal View History

2020-04-22 12:07:13 +02:00
from .Interface import Interface
import socketserver
2016-06-03 19:02:02 +02:00
import threading
import netifaces
2016-06-03 19:02:02 +02:00
import socket
import time
2016-06-03 19:02:02 +02:00
import sys
import RNS
2016-06-03 19:02:02 +02:00
2020-08-13 12:37:54 +02:00
class UDPInterface(Interface):
2016-06-03 19:02:02 +02:00
@staticmethod
def get_address_for_if(name):
return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr']
def get_broadcast_for_if(name):
return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast']
def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None):
2021-09-24 20:10:04 +02:00
self.rxb = 0
self.txb = 0
2016-06-03 19:02:02 +02:00
self.IN = True
self.OUT = False
2018-04-05 19:12:21 +02:00
self.name = name
2021-09-24 20:10:04 +02:00
self.online = False
2018-04-05 19:12:21 +02:00
if device != None:
if bindip == None:
bindip = UDPInterface.get_broadcast_for_if(device)
if forwardip == None:
forwardip = UDPInterface.get_broadcast_for_if(device)
2016-06-03 19:02:02 +02:00
if (bindip != None and bindport != None):
self.receives = True
self.bind_ip = bindip
self.bind_port = bindport
2020-03-01 16:56:49 +01:00
def handlerFactory(callback):
def createHandler(*args, **keys):
2020-08-13 12:37:54 +02:00
return UDPInterfaceHandler(callback, *args, **keys)
2020-03-01 16:56:49 +01:00
return createHandler
2016-06-03 19:02:02 +02:00
self.owner = owner
address = (self.bind_ip, self.bind_port)
2020-04-22 12:07:13 +02:00
self.server = socketserver.UDPServer(address, handlerFactory(self.processIncoming))
2016-06-03 19:02:02 +02:00
thread = threading.Thread(target=self.server.serve_forever)
thread.setDaemon(True)
thread.start()
2021-09-24 20:10:04 +02:00
self.online = True
2016-06-03 19:02:02 +02:00
if (forwardip != None and forwardport != None):
self.forwards = True
self.forward_ip = forwardip
self.forward_port = forwardport
def processIncoming(self, data):
2021-09-24 20:10:04 +02:00
self.rxb += len(data)
self.owner.inbound(data, self)
2016-06-03 19:02:02 +02:00
def processOutgoing(self,data):
try:
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
udp_socket.sendto(data, (self.forward_ip, self.forward_port))
2021-09-24 20:10:04 +02:00
self.txb += len(data)
except Exception as e:
RNS.log("Could not transmit on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
2016-06-03 19:02:02 +02:00
def __str__(self):
2020-08-13 12:37:54 +02:00
return "UDPInterface["+self.name+"/"+self.bind_ip+":"+str(self.bind_port)+"]"
2016-06-03 19:02:02 +02:00
2020-08-13 12:37:54 +02:00
class UDPInterfaceHandler(socketserver.BaseRequestHandler):
2020-03-01 16:56:49 +01:00
def __init__(self, callback, *args, **keys):
self.callback = callback
2020-04-22 12:07:13 +02:00
socketserver.BaseRequestHandler.__init__(self, *args, **keys)
2016-06-03 19:02:02 +02:00
def handle(self):
2020-03-01 16:56:49 +01:00
data = self.request[0]
self.callback(data)