我目前正在python中进行一些练习,以发送(UDP)数据包。我对此很陌生,所以我一直在尝试从概念上了解正在发生的事情。
基本上,我正在尝试从Host1->路由器-> Host3发送数据包。根据我的研究,我需要从Host1向路由器发送ARP请求以获取路由器的MAC地址,然后将以太网帧发送到路由器。从这里我从概念上有点迷路。路由器在其IP表上查找以找到Host2以便发送消息?尽管如此,这就是我的代码当前的样子。
我目前也在使用mininet router.py模拟这些主机和路由器。
$(function() {
$(window).on("scroll", function() {
if($(window).scrollTop() > 50) {
$(".header").addClass("active");
} else {
//remove the background property so it comes transparent again (defined in your css)
$(".header").removeClass("active");
}
});
});
如果未提交打印,则会收到错误消息
port = 12345
def get_ip_address(ifname):
ni.ifaddresses(ifname)
ip = ni.ifaddresses(ifname)[ni.AF_INET][0]['addr']
return ip
def sendArpMesage(interfaceName, dst_ip):
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ans, unans = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=dst_ip), timeout=2, iface=interfaceName, inter=0.2)
for send, receive in ans:
return(receive.sprintf(r"%Ether.src%"))
def get_netmask(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x891b, struct.pack('256s',ifname))[20:24])
def sendIPMessage(interfaceName, dst_ip, routerIP, message):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
src_addr = get_mac_address(interface=interfaceName)
my_ip = get_ip_address(interfaceName)
netmask = ipaddress.ip_address(dst_ip) in ipaddress.ip_network(my_ip)
if netmask is True: # if dst is in the same network
arp_MAC = sendArpMesage(interfaceName, dst_ip)
else:
arp_MAC = sendArpMesage(interfaceName, routerIP)
msg = message.encode('utf-8')
packet = send(Ether(src=src_addr, dst=arp_MAC)/IP(src=my_ip, dst=dst_ip)/UDP(dport=port)/msg)
#print(packet.show())
正在运行的print(packet.show())
AttributeError: 'NoneType' object has no attribute 'show'
的输出是
sendIPMessage
wireshark显示向路由器的ARP请求和格式错误的数据包
编辑:当试图显示数据包的内容时,我意识到我使用了错误的功能。但是现在,数据包的校验和似乎出现了问题。