Eu tenho um sistema com uma interface de rede virtual eth0: 1 e desejo enviar pacotes multicast que tenham um IP de origem dessa interface. No entanto, meus pacotes acabam com o IP de origem para eth0. Como obtenho o IP de origem correto nos meus pacotes multicast?
Os comandos usados para criar o iface / route
sudo ifconfig eth0:1 plumb
sudo ifconfig eth0:1 192.168.123.123 netmask 255.255.255.0 up
sudo ip route add 224.1.1.0/24 dev eth0:1 src 192.168.123.123
Depois do qual as interfaces ficam assim:
eth0 Link encap:Ethernet HWaddr fa:16:3e:9d:94:c2
inet addr:14.0.0.2 Bcast:14.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:1 Link encap:Ethernet HWaddr fa:16:3e:9d:94:c2
inet addr:192.168.123.123 Bcast:192.168.123.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
as rotas são assim:
default via 14.0.0.1 metric 100
14.0.0.0/24 proto kernel scope link src 14.0.0.2
192.168.123.0/24 proto kernel scope link src 192.168.123.123
224.1.1.0/24 dev eth0 scope link src 192.168.123.123
Um pacote udp dirigido enviado para 192.168.123.123:5007 obtém o IP de origem correto de 192.168.123.123, mas um pacote multicast para 224.1.1.1:5007 obtém um IP de origem de 14.0.0.2.
O script python que estou usando para gerar os pacotes é este:
MCAST_GRP = '224.1.1.1'
MCAST_PORT = 5007
MY_IP = '192.168.123.123'
def send_mcast():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
pass # Some systems don't support SO_REUSEPORT
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 2)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(MY_IP))
print "Sending: robot"
sock.sendto("robot", (MCAST_GRP, MCAST_PORT))
def send_direct():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
pass # Some systems don't support SO_REUSEPORT
print "Sending: robot"
sock.sendto("robot", (MY_IP, MCAST_PORT))