简体   繁体   中英

Python UDP Sockets with Multiple Interfaces

I'm Writing a script in python2.7 on a windows XP machine. The machine is connected to multiple networks using different network cards.

I'm running into an issue where I've bound a UDP Socket to a specific interface(I understand that you can accomplish this in windows by just providing the network cards existing IP address)

self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind(('10.31.9.0', 6466)) #<<< 10.31.9.0 is address of desired card

I then set the timeout to 5s

self.sock.settimeout(5)

Then I try to send a message out to a server that I can prove exists and works. then wait for a response.

self.destintation = ('10.42.40.34', 62434)

# Send the msg
self.sock.sendto(msg, self.destintation)

# receive data
reply, addr = self.sock.recvfrom(1024)

However a socket.timeout is always thrown. so I open up wire shark to see what is going wrong, and it turns out that my initial message never gets sent on the desired interface.

What I do see is an arp broadcast on a different interface(10.10.10.12 ) from my machine asking who is attached to my desired destination IP:

1   0.000000    IntelCor_8c:6d:97   Broadcast   ARP 42      Who has 10.42.40.34?  Tell 10.10.10.12

Of course there is no response to the broadcast because the 10.42.40.34 Address/machine is not reachable from the 10.10.10.12 interface

How do I tell Python to send the ARP broadcast out on '10.31.9.0' ? What have I done Wrong?

EDIT:

Additional Information> The network for the interface I am using is a Class B (netmask is 255.255.0.0)

The interface IP is : 10.31.9.0 

The target IP is: 10.42.40.34. 

I am wondering if the issue is a result of my target sitting on a separate subnet. However, as described in a related issue here . there is traffic from the server to me... =/

UPDATE:

Results of "route PRINT 10*"

Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
         10.0.0.0        255.0.0.0      10.10.10.12     10.10.10.12   10
      10.10.10.12  255.255.255.255        127.0.0.1       127.0.0.1   10
        10.31.0.0      255.255.0.0        10.31.9.0       10.31.9.0   10
        10.31.9.0  255.255.255.255        127.0.0.1       127.0.0.1   10
   10.255.255.255  255.255.255.255      10.10.10.12     10.10.10.12   10
   10.255.255.255  255.255.255.255        10.31.9.0       10.31.9.0   10
Default Gateway:        153.4.84.1
===========================================================================
Persistent Routes:
  None

UPDATE #2 Full route PRINT

Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0       153.4.84.1     153.4.85.81   10
        10.10.0.0      255.255.0.0      10.10.10.12     10.10.10.12   10
      10.10.10.12  255.255.255.255        127.0.0.1       127.0.0.1   10
        10.31.0.0      255.255.0.0        10.31.9.0       10.31.9.0   10
        10.31.9.0  255.255.255.255        127.0.0.1       127.0.0.1   10
   10.255.255.255  255.255.255.255      10.10.10.12     10.10.10.12   10
   10.255.255.255  255.255.255.255        10.31.9.0       10.31.9.0   10
        127.0.0.0        255.0.0.0        127.0.0.1       127.0.0.1   1
       153.4.84.0    255.255.252.0      153.4.85.81     153.4.85.81   10
      153.4.85.81  255.255.255.255        127.0.0.1       127.0.0.1   10
    153.4.255.255  255.255.255.255      153.4.85.81     153.4.85.81   10
     192.168.56.0    255.255.255.0     192.168.56.1    192.168.56.1   20
     192.168.56.1  255.255.255.255        127.0.0.1       127.0.0.1   20
   192.168.56.255  255.255.255.255     192.168.56.1    192.168.56.1   20
        224.0.0.0        240.0.0.0      10.10.10.12     10.10.10.12   10
        224.0.0.0        240.0.0.0        10.31.9.0       10.31.9.0   10
        224.0.0.0        240.0.0.0      153.4.85.81     153.4.85.81   10
        224.0.0.0        240.0.0.0     192.168.56.1    192.168.56.1   20
  255.255.255.255  255.255.255.255      10.10.10.12     10.10.10.12   1
  255.255.255.255  255.255.255.255        10.31.9.0       10.31.9.0   1
  255.255.255.255  255.255.255.255      153.4.85.81     153.4.85.81   1
  255.255.255.255  255.255.255.255     192.168.56.1    192.168.56.1   1
  255.255.255.255  255.255.255.255     192.168.56.1               5   1
Default Gateway:        153.4.84.1
===========================================================================
Persistent Routes:
  None

Given the output from "route", it looks like you're 10.10.10.12 and 10.31.9.0 interfaces have been configured with overlapping subnets. The OS is choosing to use 10.10.10.12 for all 10.xxx addresses as it's the first rule that applies.

Having overlapping subnets is normally a network configuration error: it's probably intended that 10.10.xx and 10.31.xx are the valid subnets and both should use a netmask of 255.255.0.0, and so the current 255.0.0.0 netmask used by the 10.10.10.12 interface is incorrect.

(It may be possible to 'fudge' a fix, if the intention is to make all 10.xxx requests use the 10.10.10.12 interface except for those in 10.31.xx which should use the 10.31.9.0 address, by changing the 'metric' of the 10.31.0.0 routing rule so that anything for 10.31.xx addresses matches that rule before the 10.xxx rule is checked. You can use the route command to make that change, but it's definitely not recommended! Fixing the overlapping subnets is the proper solution.)

Turns out, the Packets that my "server" was sending where not IP kosher. so they where getting rejected at the network and transport layers. Solution was to not use python socket class, but instead communicate directly to OSI-L2 using winpcap and ctypes

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