简体   繁体   中英

linux loopback interface to specific interface in python

I'm trying to send some data to a local address and then 'forward' it to an external address using a specific interface ppp0 . I think my issue is related to packet routing/socket binding issues but I'm too much of a novice to make sense of it.

My network setup:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
0.0.0.0         192.168.1.1     0.0.0.0         UG    303    0        0 wlan0
10.64.64.64     0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
169.254.0.0     0.0.0.0         255.255.0.0     U     304    0        0 wwan0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0

with the loopback:

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

On the Python side, I bind the socket

HOST = ''
PORT_vid = 0

try:
    s_vid = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s_vid.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s_vid.setsockopt(socket.SOL_SOCKET, 25, "ppp0")#this works to bind it to ppp0
    print("Socket created.")
except socket.error, msg:
    print("Failed. Error: " + str(msg))
    sys.exit()

try:
    s_vid.bind((HOST,PORT_vid))
    print("Socket binding complete.")
except socket.error, msg:
    print("Bind failed. Error: " + str(msg))
    s_vid.close()
    sys.exit()

Which works fine if I try to simply ping an external address. Now I send my data to a local point using netcat

#p_nc is taking some data from a local process and sending it to the local point
p_nc = subprocess.Popen(["nc", "-u", "127.0.0.1", str(s_vid.getsockname()[1])],stdin=another_process.stdout)

And then I listen on that local port and forward it (there are good reasons why I don't just send it off with netcat )

while 1:
    data, addr = s_vid.recvfrom(1024)
    print('local reception')

    #do stuff here with 'data' - never reached
    s_vid.sendto(data_amended,(external_ip,external_port))

The data has to go to the external address via ppp0 , and using the "process to netcat to local to external" setup (as above) would be preferred. The s_vid socket does not need to listen for any external traffic, it is simply sending external and listening local.

I'm guessing my socket is bound to ppp0 and so can't read the loopback data, but I don't know how to fix it. Either a solution on the Python end or on the ip routing end will work, but I'm new to routing and interfaces.

Ended up simply adding a new socket to my code, which is bound to the lo interface (similar to how the other is bound to ppp0 ). The lo socket reads the local socket ( s_lo.recvfrom() in the loop), while we keep s_vid.sendto() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM