简体   繁体   English

linux loopback接口到python中的特定接口

[英]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 . 我正在尝试将一些数据发送到本地地址,然后使用特定的接口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 在Python方面,我绑定了套接字

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. 如果我尝试简单地ping外部地址,则效果很好。 Now I send my data to a local point using netcat 现在我使用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 ) 然后,我在该本地端口上进行侦听并将其转发(有充分的理由说明为什么我不只是使用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. 该数据具有经由去外部地址ppp0 ,并使用“进程netcat到本地到外部的”设置(如上所述)是优选的。 The s_vid socket does not need to listen for any external traffic, it is simply sending external and listening local. s_vid套接字不需要侦听任何外部流量,它只是发送外部数据并侦听本地数据。

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. 我猜我的套接字绑定到ppp0 ,因此无法读取回送数据,但是我不知道如何解决它。 Either a solution on the Python end or on the ip routing end will work, but I'm new to routing and interfaces. 在Python端或ip路由端上的解决方案都可以使用,但是我对路由和接口还是陌生的。

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 ). 最终只需在我的代码中添加一个新套接字即可,该套接字绑定到lo接口(类似于另一个套接字绑定到ppp0 )。 The lo socket reads the local socket ( s_lo.recvfrom() in the loop), while we keep s_vid.sendto() . lo套接字读取本地套接字(循环中的s_lo.recvfrom() ),而我们保留s_vid.sendto()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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