2022-04-01 15:18:18 +00:00
|
|
|
# MIT License
|
|
|
|
#
|
|
|
|
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
2020-04-22 10:07:13 +00:00
|
|
|
from .Interface import Interface
|
|
|
|
import socketserver
|
2016-06-03 17:02:02 +00:00
|
|
|
import threading
|
|
|
|
import socket
|
2018-04-25 20:08:17 +00:00
|
|
|
import time
|
2016-06-03 17:02:02 +00:00
|
|
|
import sys
|
2018-04-04 12:14:22 +00:00
|
|
|
import RNS
|
2016-06-03 17:02:02 +00:00
|
|
|
|
2021-08-19 17:56:35 +00:00
|
|
|
|
2020-08-13 10:37:54 +00:00
|
|
|
class UDPInterface(Interface):
|
2022-04-18 16:09:31 +00:00
|
|
|
BITRATE_GUESS = 10*1000*1000
|
2024-11-20 19:20:40 +00:00
|
|
|
DEFAULT_IFAC_SIZE = 16
|
2016-06-03 17:02:02 +00:00
|
|
|
|
2021-08-19 17:56:35 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_address_for_if(name):
|
2023-05-04 21:19:43 +00:00
|
|
|
import RNS.vendor.ifaddr.niwrapper as netinfo
|
|
|
|
ifaddr = netinfo.ifaddresses(name)
|
|
|
|
return ifaddr[netinfo.AF_INET][0]["addr"]
|
2021-08-19 17:56:35 +00:00
|
|
|
|
2021-12-01 10:39:40 +00:00
|
|
|
@staticmethod
|
2021-08-19 17:56:35 +00:00
|
|
|
def get_broadcast_for_if(name):
|
2023-05-04 21:19:43 +00:00
|
|
|
import RNS.vendor.ifaddr.niwrapper as netinfo
|
|
|
|
ifaddr = netinfo.ifaddresses(name)
|
|
|
|
return ifaddr[netinfo.AF_INET][0]["broadcast"]
|
2021-08-19 17:56:35 +00:00
|
|
|
|
2024-11-20 19:20:40 +00:00
|
|
|
def __init__(self, owner, configuration):
|
2023-09-30 17:11:10 +00:00
|
|
|
super().__init__()
|
2022-05-29 13:43:50 +00:00
|
|
|
|
2024-11-21 13:41:22 +00:00
|
|
|
c = Interface.get_config_obj(configuration)
|
2024-11-20 19:20:40 +00:00
|
|
|
name = c["name"]
|
|
|
|
device = c["device"] if "device" in c else None
|
|
|
|
port = int(c["port"]) if "port" in c else None
|
|
|
|
bindip = c["listen_ip"] if "listen_ip" in c else None
|
|
|
|
bindport = int(c["listen_port"]) if "listen_port" in c else None
|
|
|
|
forwardip = c["forward_ip"] if "forward_ip" in c else None
|
|
|
|
forwardport = int(c["forward_port"]) if "forward_port" in c else None
|
|
|
|
|
|
|
|
if port != None:
|
|
|
|
if bindport == None:
|
|
|
|
bindport = port
|
|
|
|
if forwardport == None:
|
|
|
|
forwardport = port
|
|
|
|
|
2022-05-29 13:43:50 +00:00
|
|
|
self.HW_MTU = 1064
|
|
|
|
|
2016-06-03 17:02:02 +00:00
|
|
|
self.IN = True
|
|
|
|
self.OUT = False
|
2018-04-05 17:12:21 +00:00
|
|
|
self.name = name
|
2021-09-24 18:10:04 +00:00
|
|
|
self.online = False
|
2022-04-18 16:09:31 +00:00
|
|
|
self.bitrate = UDPInterface.BITRATE_GUESS
|
2018-04-05 17:12:21 +00:00
|
|
|
|
2021-08-19 17:56:35 +00: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 17:02:02 +00:00
|
|
|
if (bindip != None and bindport != None):
|
|
|
|
self.receives = True
|
|
|
|
self.bind_ip = bindip
|
|
|
|
self.bind_port = bindport
|
|
|
|
|
2020-03-01 15:56:49 +00:00
|
|
|
def handlerFactory(callback):
|
|
|
|
def createHandler(*args, **keys):
|
2020-08-13 10:37:54 +00:00
|
|
|
return UDPInterfaceHandler(callback, *args, **keys)
|
2020-03-01 15:56:49 +00:00
|
|
|
return createHandler
|
|
|
|
|
2016-06-03 17:02:02 +00:00
|
|
|
self.owner = owner
|
|
|
|
address = (self.bind_ip, self.bind_port)
|
2021-12-08 19:45:41 +00:00
|
|
|
socketserver.UDPServer.address_family = socket.AF_INET
|
2020-04-22 10:07:13 +00:00
|
|
|
self.server = socketserver.UDPServer(address, handlerFactory(self.processIncoming))
|
2016-06-03 17:02:02 +00:00
|
|
|
|
|
|
|
thread = threading.Thread(target=self.server.serve_forever)
|
2022-09-30 17:02:25 +00:00
|
|
|
thread.daemon = True
|
2016-06-03 17:02:02 +00:00
|
|
|
thread.start()
|
|
|
|
|
2021-09-24 18:10:04 +00:00
|
|
|
self.online = True
|
|
|
|
|
2016-06-03 17:02:02 +00: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 18:10:04 +00:00
|
|
|
self.rxb += len(data)
|
2018-04-04 12:14:22 +00:00
|
|
|
self.owner.inbound(data, self)
|
2016-06-03 17:02:02 +00:00
|
|
|
|
2024-11-22 13:12:55 +00:00
|
|
|
def process_outgoing(self,data):
|
2021-09-23 14:07:57 +00:00
|
|
|
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 18:10:04 +00:00
|
|
|
self.txb += len(data)
|
|
|
|
|
2021-09-23 14:07:57 +00:00
|
|
|
except Exception as e:
|
|
|
|
RNS.log("Could not transmit on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
2016-06-03 17:02:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-08-13 10:37:54 +00:00
|
|
|
return "UDPInterface["+self.name+"/"+self.bind_ip+":"+str(self.bind_port)+"]"
|
2016-06-03 17:02:02 +00:00
|
|
|
|
2020-08-13 10:37:54 +00:00
|
|
|
class UDPInterfaceHandler(socketserver.BaseRequestHandler):
|
2020-03-01 15:56:49 +00:00
|
|
|
def __init__(self, callback, *args, **keys):
|
|
|
|
self.callback = callback
|
2020-04-22 10:07:13 +00:00
|
|
|
socketserver.BaseRequestHandler.__init__(self, *args, **keys)
|
2016-06-03 17:02:02 +00:00
|
|
|
|
|
|
|
def handle(self):
|
2020-03-01 15:56:49 +00:00
|
|
|
data = self.request[0]
|
|
|
|
self.callback(data)
|