我很可能(尽管完全正确;-))为以下代码公开处以私刑,但作为一个网络工程师对另一个检查下面的代码。
还要记住几位 - 您需要先“建立邻接”(交换一些 hello 数据包),否则上游路由器将忽略您的 PIM 加入(因此添加了虚拟 hello 功能)。
此外,在发送 PIM 加入时,“上游邻居 IP”也是必需的。
import socket
from struct import pack, iter_unpack
from functools import reduce
def checksum(data):
sum = reduce(lambda x, y: x + y, iter_unpack("!H",data))
s = reduce(lambda x, y: x + y, sum)
while (s >> 16):
s = (s & 0xFFFF) + (s >> 16)
s = ~s & 0xffff
return s
def send_dummy_hello():
PIM_GROUP = '224.0.0.13'
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_PIM)
my_packet=pack('!BBH3H4H4H4H', 32, 0, 24707, 1, 2, 105, 20, 4, 16121,
16078, 19, 4, 0, 1, 21, 4, 256, 0)
sock.sendto(my_packet, (PIM_GROUP,0))
def send_dummy_join(group, rp_addr, neighbor_ip):
PIM_GROUP = '224.0.0.13'
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_PIM)
pim_ver = 35
pim_res = 0
pktchksum = 0
pim_opts = 256
neighbor = socket.inet_aton(neighbor_ip)
ngroups = 1
holdtime = 210
subnet = socket.inet_aton('1.0.0.32')
group_a = socket.inet_aton(group)
num_joins = 1
num_prunes = 0
rrp_subnet = socket.inet_aton('1.0.7.32')
rp_address = socket.inet_aton(rp_addr)
my_packet = pack('!BB2H4sHH4s4sHH4s4s', pim_ver, pim_res, pktchksum, pim_opts,
neighbor, ngroups, holdtime, subnet, group_a, num_joins, num_prunes,
rrp_subnet, rp_address)
pktchksum = checksum(my_packet)
my_packet = pack('!BB2H4sHH4s4sHH4s4s', pim_ver, pim_res, pktchksum, pim_opts,
neighbor, ngroups, holdtime, subnet, group_a, num_joins, num_prunes,
rrp_subnet, rp_address)
sock.sendto(my_packet, (PIM_GROUP,0))
print (f'PIM Join sent to {neighbor_ip} for {group} & RP {rp_addr}')
>>>send_dummy_hello()
>>>send_dummy_join('239.10.20.30','192.168.0.1','10.0.0.102')
我用 EVE-NG 测试了它,它(神奇地!)工作:-)
编辑:
现在编辑代码以支持标头校验和计算。我想这已经完成了!:-)