0

I have a program which is a udp socket client. I can determine an IP adress to which this program should send. Unfortunately, I configured the computer as Access Point and don't know the address of the UDP server on the other site (I mean I know, but the Access Point doesn't). What, I had in mind is to catch the message and send it to all possible IPs in the range of the network interface (wlan0). For this I wrote a small python proxy, which also shall illustrate the problem. It seems like the server on the other side even gets a message. However there seems no communication back.

Is there a way to catch and forward udp socket communication between client and server programs? Maybe an already existing program/tool for this? I really have not much administration experience.

# Make this Python 2.7 script compatible to Python 3 standard
from __future__ import print_function
# For remote control
import socket
# For sensor readout
import logging
import threading
# For system specific functions
import sys
import os
import time
import datetime
import fcntl
import struct

# Create a sensor log with date and time
layout = '%(asctime)s - %(levelname)s - %(message)s'

# find suitable place for the log file
logging_dir   = "/var/log/"
logging_file  = "ardu_proxy.log"
if not os.path.isdir(logging_dir):
  logging_dir = "/"
# configure logging
logging.basicConfig(filename=logging_dir+logging_file, level=logging.INFO, format=layout)

# Socket for WiFi data transport
udp_port = 14550
udp_server  = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_server.bind(('0.0.0.0', udp_port))

def get_if_address(ifname):
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  return socket.inet_ntoa(fcntl.ioctl(
    s.fileno(),
    0x8915,  # SIOCGIFADDR
    struct.pack('256s', ifname[:15])
  )[20:24])

def get_ip4_adr(adr):
  array = adr.split('.')
  str = ""
  for i in xrange(0, 3):
    str += array[i] + "."
  return str

def echo_thr():                                                         # trnm_thr() sends commands to Arduino
  udp_msg    = None
  if_name = "wlan0"

  # get local interface address
  apif_wlan0 = get_if_address(if_name)
  apif_wlan0 = get_ip4_adr(apif_wlan0)

  while True:
    # receive a message running on local host
    udp_msg, udp_client = udp_server.recvfrom(512)                    # Wait for UDP packet from ground station
    logging.debug(udp_msg)
    print(udp_msg)

    # forward the msg and broadcast it in the complete network :D
    for ofs in xrange(1, 254):
      adr = apif_wlan0 + str(ofs)
      print(adr, ": ", udp_msg)
      udp_server.sendto(udp_msg, (adr, udp_port) )

echo_thr()
dgrat
  • 143
  • 3
  • 8

1 Answers1

1

Maybe you can have a look at socat. Example:

 socat -u tcp-listen:50505,reuseaddr - | P | socat -u - tcp-listen:60606,reuseaddr

where P is your programm taking the input on port 50505 and forwarding the output to port 60606.

The example is borrowed from Redirect stdin and stdout to ports

Piccolo
  • 111
  • 3