本文整理汇总了Python中ryu.lib.packet.arp.arp函数的典型用法代码示例。如果您正苦于以下问题:Python arp函数的具体用法?Python arp怎么用?Python arp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了arp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_arp
def _handle_arp(self, datapath, port, pkt_ethernet, pkt_arp):
"""Handle ARP reply/request packets.
When controller get an ARP reply packet, it will write into ARP table.
When controller get an ARP request packet,
it will reply someone who want to ask NAT's MAC address.
(Probably under NAT's LAN or WAN)"""
if pkt_arp.opcode != arp.ARP_REQUEST:
#it means ARP_REPLY
if pkt_arp.dst_ip == self.nat_ip:
# 140.92.62.30 is at xx:xx:xx:xx:xx:xx
# Got an ARP Reply and parser it.
#gw_src_mac = pkt_arp.src_mac
#gw_ip = pkt_arp.src_ip
ARP_TABLE[pkt_arp.src_ip] = pkt_arp.src_mac
print "Get ARP reply"
print ARP_TABLE
return
else:
pass
# Handle ARP Request and send an ARP Reply
if pkt_arp.dst_ip == self.nat_intranet_gateway:
# Who has 192.168.9.1 ?
# Tell 192.168.9.20(Host), 192.168.9.1's fake MAC address (eth1)
pkt = packet.Packet()
pkt.add_protocol(ethernet.ethernet(ethertype=pkt_ethernet.ethertype,
dst=pkt_ethernet.src,
src=self.fake_mac_eth1))
pkt.add_protocol(arp.arp(opcode=arp.ARP_REPLY,
src_mac=self.fake_mac_eth1,
src_ip=self.nat_intranet_gateway,
dst_mac=pkt_arp.src_mac,
dst_ip=pkt_arp.src_ip))
#print "Send pkt to %s" % pkt_arp.src_mac
self._send_packet(datapath, port, pkt)
elif pkt_arp.dst_ip == self.nat_ip:
# Who has 140.92.62.235 ?
# Tell 140.92.62.1(Extranet Gateway)
pkt = packet.Packet()
pkt.add_protocol(ethernet.ethernet(ethertype=pkt_ethernet.ethertype,
dst=pkt_ethernet.src,
src=self.fake_mac_eth2))
pkt.add_protocol(arp.arp(opcode=arp.ARP_REPLY,
src_mac=self.fake_mac_eth1,
src_ip=self.nat_ip,
dst_mac=pkt_arp.src_mac,
dst_ip=pkt_arp.src_ip))
self._send_packet(datapath, port, pkt)
else:
return
示例2: _handle_arp
def _handle_arp(self, ev, subname):
msg = ev.msg
datapath = msg.datapath
in_port = msg.match['in_port']
pkt = packet.Packet(data=msg.data)
pkt_ethernet = pkt.get_protocol(ethernet.ethernet)
pkt_arp = pkt.get_protocol(arp.arp)
if pkt_arp.opcode == arp.ARP_REPLY:
self.subnet[datapath.id][subname]['arp_table'][pkt_arp.src_ip]=pkt_arp.src_mac
self.logger.info('arp_table: %s ' % json.dumps(self.subnet))
return
elif pkt_arp.opcode == arp.ARP_REQUEST:
#ingest arp_table
self.subnet[datapath.id][subname]['arp_table'][pkt_arp.src_ip]=pkt_arp.src_mac
self.logger.info('arp_table: %s ' % json.dumps(self.subnet))
this_subnet=self.subnet[datapath.id][subname]
#generating arp packet
pkt = packet.Packet()
pkt.add_protocol(ethernet.ethernet(ethertype=pkt_ethernet.ethertype, dst=pkt_ethernet.src, src=this_subnet['mac']))
pkt.add_protocol(arp.arp(opcode=arp.ARP_REPLY,
src_mac=this_subnet['mac'],
src_ip =this_subnet['ip'],
dst_mac=pkt_arp.src_mac,
dst_ip=pkt_arp.src_ip))
self._send_packet(datapath, in_port, pkt)
self.logger.info("arp reply is sent: %s-%s -> %s-%s via port=%s" % (this_subnet['mac'], this_subnet['ip'], pkt_arp.src_mac, pkt_arp.src_ip, in_port) )
return
else:
return
示例3: discover_router
def discover_router(self, datapath, ofproto, parser):
##arp for the router first, to learn its out port
##form the ARP req
a_hwtype = 1
a_proto = ether.ETH_TYPE_IP
a_hlen = 6
a_plen = 4
a_opcode = 1 #request1
a_srcMAC = self.CONTROLLER_SPECIAL_MAC
a_srcIP = self.CONTROLLER_SPECIAL_IP
a_dstMAC = self.ROUTER_MAC
a_dstIP = self.ROUTER_IP
p = packet.Packet()
e = ethernet.ethernet(a_dstMAC,a_srcMAC,ether.ETH_TYPE_ARP)
a = arp.arp(a_hwtype,a_proto,a_hlen,a_plen,a_opcode,a_srcMAC,a_srcIP,a_dstMAC,a_dstIP)
p.add_protocol(e)
p.add_protocol(a)
p.serialize()
#send packet out
actions = [parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
out = parser.OFPPacketOut(datapath=datapath, buffer_id=ofproto.OFP_NO_BUFFER,in_port=ofproto.OFPP_CONTROLLER, actions=actions, data=p.data)
datapath.send_msg(out)
dpid = datapath.id
self.logger.info("packet out dpid:'%s' src:'%s' dst:'%s' out_port:'OFPP_FLOOD'", dpid, self.CONTROLLER_SPECIAL_MAC, self.ROUTER_MAC)
self.logger.info("[ADMIN] Attempting to discover WAN router... ")
示例4: _handle_arp_overrider
def _handle_arp_overrider(self, datapath, port, pkt_ethernet, pkt_arp):
if pkt_arp.opcode != arp.ARP_REQUEST:
return
#Learn the MAC to IP
mac_lst = []
for each_key in GlobalTables.mac_to_port.keys():
x = GlobalTables.mac_to_port[each_key]
for mac,port in x.iteritems():
mac_lst.append(mac)
if (pkt_arp.src_mac in mac_lst):
self.learn_mac_to_ip(datapath.id, pkt_arp.src_mac, pkt_arp.src_ip)
self.logger.info("INFO: New MAC_TO_IP: %s", GlobalTables.mac_to_ip)
#Generate response
pkt = packet.Packet()
pkt.add_protocol(ethernet.ethernet(ethertype = pkt_ethernet.ethertype,
dst = pkt_ethernet.src,
src = GlobalTables.hw_addr))
pkt.add_protocol(arp.arp(opcode = arp.ARP_REPLY,
src_mac = '02:00:00:00:00:01',
src_ip = pkt_arp.dst_ip,
dst_mac = pkt_arp.src_mac,
dst_ip = pkt_arp.src_ip))
self.logger.info("INFO: Sending ARPOverrider response")
self._send_packet(datapath, port, pkt)
示例5: addARP
def addARP(pkt_out,fields):
pkt_out.add_protocol(arp.arp(opcode=arp.ARP_REPLY,
src_mac=fields['srcmac'],
src_ip=fields['srcip'],
dst_mac=fields['dstmac'],
dst_ip=fields['dstip']))
return pkt_out
示例6: _arp_request
def _arp_request(self, datapath, pkt_ip=None):
"""Sending an ARP request via broadcast"""
# Who has xxx.xxx.xxx.xxx? Tell 140.92.62.235 (NAT's Public IP)
if pkt_ip is None:
target_ip = self.nat_extranet_gateway
else:
target_ip = self.subnet(pkt_ip)
print "Sending ARP Request..."
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
#target_ip = self.subnet(pkt_ip)
pkt = packet.Packet()
pkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_ARP,
dst='ff:ff:ff:ff:ff:ff',
src=self.fake_mac_eth2))
pkt.add_protocol(arp.arp(opcode=arp.ARP_REQUEST,
src_mac=self.fake_mac_eth2,
src_ip=self.nat_ip,
dst_mac='00:00:00:00:00:00',
dst_ip=target_ip))
pkt.serialize()
data = pkt.data
actions = [parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
out = parser.OFPPacketOut(datapath=datapath,
buffer_id=ofproto.OFP_NO_BUFFER,
in_port=ofproto.OFPP_CONTROLLER,
actions=actions,
data=data)
datapath.send_msg(out)
示例7: receive_ip
def receive_ip(self, datapath, packet, etherFrame, inPort, msg): #Función que se usa cuando se recibe un paquete IP
ipPacket = packet.get_protocol(ipv4.ipv4)
if ipPacket.dst == self.ports_to_ips[0][0]: #Si va destinado al router
if ipPacket.proto == inet.IPPROTO_ICMP:
icmpPacket = packet.get_protocol(icmp.icmp)
self.check_icmp(datapath, etherFrame, ipPacket, icmpPacket, inPort) #Se usa una función que trata un paquete ICMP
return 0
else:
send_packet(datapath=datapath,port=inPort,packet=packet) #Se envía el paquete
return 1
else: #Si no va destinado al router
(next_hop, port) = self.find_in_routingTable(ipPacket.dst) #Almacena el puerto y el siguiente salto
#en port y en next_hop
if next_hop in self.ipToMac.keys(): #Si está dentro de la tabla de ips y macs se envía.
match = datapath.ofproto_parser.OFPMatch(eth_dst=self.ipToMac[next_hop])
actions = [datapath.ofproto_parser.OFPActionOutput(port)]
#self.add_flow(datapath, 0, match, actions)
self.insertar_flujo(msg=msg, mac=self.ipToMac[next_hop], port=port, mod=1)
else: #Si no está dentro de la tabla se construye un paquete ARP para averiguar su MAC
# print "---- NEXT_HOP -----", next_hop
e = ethernet.ethernet(src=etherFrame.dst, ethertype=ether.ETH_TYPE_ARP)
a = arp.arp(opcode=arp.ARP_REQUEST,
src_ip=self.ports_to_ips[port-1][0],
src_mac=etherFrame.src,
dst_ip=next_hop)
puerto = etherFrame.src
p = Packet()
p.add_protocol(e)
p.add_protocol(a)
if next_hop not in self.dict_pendientes:
self.dict_pendientes[next_hop] = []
self.dict_pendientes[next_hop] = self.dict_pendientes[next_hop] + [(msg, port,None,None)]
self.send_packet(datapath=datapath, port=port, packet=p)
示例8: _arp_proxy_handler
def _arp_proxy_handler(self, ev):
msg = ev.msg
pkt = ev.pkt
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match["in_port"]
eth_protocol = pkt.get_protocol(ethernet.ethernet)
eth_src = eth_protocol.src
arp_protocol = pkt.get_protocol(arp.arp)
src_ip = arp_protocol.src_ip
dst_ip = arp_protocol.dst_ip
self.arp_table[src_ip] = eth_src
self.dp_to_ip[datapath.id].add(src_ip)
if arp_protocol.opcode == arp.ARP_REPLY:
return
dst_mac = self.arp_table.get(dst_ip)
if dst_mac is None:
# Flood to flood_ports
ports = self.wrapper.get_flood_ports()
for dpid, out_port in ports:
if (dpid, out_port) == (datapath.id, in_port):
continue
self.logger.info("flood to port:%d:%d", dpid, out_port)
dp = self.dpset.get(dpid)
actions = [parser.OFPActionOutput(out_port)]
out = dp.ofproto_parser.OFPPacketOut(datapath=dp,
buffer_id=dp.ofproto.OFP_NO_BUFFER,
in_port=dp.ofproto.OFPP_CONTROLLER,
actions=actions,
data=msg.data)
dp.send_msg(out)
else:
ARP_Reply = packet.Packet()
ARP_Reply.add_protocol(ethernet.ethernet(ethertype=eth_protocol.ethertype,
dst=eth_src,
src=dst_mac))
ARP_Reply.add_protocol(arp.arp(opcode=arp.ARP_REPLY,
src_mac=dst_mac,
src_ip=dst_ip,
dst_mac=eth_src,
dst_ip=src_ip))
ARP_Reply.serialize()
actions = [parser.OFPActionOutput(in_port)]
out = parser.OFPPacketOut(datapath=datapath,
buffer_id=ofproto.OFP_NO_BUFFER,
in_port=ofproto.OFPP_CONTROLLER,
actions=actions,
data=ARP_Reply.data)
datapath.send_msg(out)
self.logger.debug("%s - %s" % (dst_ip, dst_mac))
return True
示例9: _handle_arp_reply
def _handle_arp_reply(self, pkt_arp, port, dpid):
if pkt_arp.opcode == arp.ARP_REPLY and pkt_arp.dst_mac == self.hw_addr:
if pkt_arp.src_ip not in self.active_ips:
print("ARP from " + pkt_arp.src_ip + "\n")
self.active_ips[pkt_arp.src_ip] = [dpid, port]
self.arp_table[pkt_arp.src_ip] = pkt_arp.src_mac
if self.topology:
self.draw_graph(1, draw=True)
if pkt_arp.opcode == arp.ARP_REQUEST and pkt_arp.src_ip != self.ip_addr:
#print("ARP Reqest from: " + pkt_arp.src_mac + " requesting: " + pkt_arp.dst_ip)
if pkt_arp.dst_ip not in self.arp_table: return
#construct and send ARP reply
reply_pkt = packet.Packet()
reply_pkt.add_protocol(ethernet.ethernet(ethertype=0x806,\
dst=pkt_arp.src_mac,\
src=self.arp_table[pkt_arp.dst_ip]))
reply_pkt.add_protocol(arp.arp(opcode=arp.ARP_REPLY,\
src_mac=self.arp_table[pkt_arp.dst_ip],\
src_ip=pkt_arp.dst_ip,\
dst_mac=pkt_arp.src_mac,\
dst_ip=pkt_arp.src_ip))
print("Responded to ARP Request: " )
print("Gave [" + pkt_arp.src_mac + "," + pkt_arp.src_ip + "]" +\
"[" + pkt_arp.dst_ip + "," + self.arp_table[pkt_arp.dst_ip] + "]")
self._send_packet(reply_pkt, self.dpids[int(dpid, 16)])
示例10: find_mac
def find_mac(self, datapath, etherFrame, msg):
#Almacena el puerto y el siguiente salto
(next_hop, port) = self.find_in_routingTable(ipPacket.dst)
if next_hop in self.ipToMac.keys(): #Si está dentro de la tabla de ips y macs se envía.
match = datapath.ofproto_parser.OFPMatch(eth_dst=self.ipToMac[next_hop])
actions = [datapath.ofproto_parser.OFPActionOutput(port)]
self.add_flow(datapath, 0, match, actions)
self.insertar_flujo(msg=msg, mac=self.ipToMac[next_hop], port=port, mod=1)
else: #Si no está dentro de la tabla se construye un paquete ARP para averiguar su MAC
# print "---- NEXT_HOP -----", next_hop
e = ethernet.ethernet(src=etherFrame.dst, ethertype=ether.ETH_TYPE_ARP)
a = arp.arp(opcode=arp.ARP_REQUEST,
src_ip=self.ports_to_ips[port-1][0],
src_mac=etherFrame.src,
dst_ip=next_hop)
puerto = etherFrame.src
p = Packet()
p.add_protocol(e)
p.add_protocol(a)
if next_hop not in self.dict_pendientes:
self.dict_pendientes[next_hop] = []
self.dict_pendientes[next_hop] = self.dict_pendientes[next_hop] + [(msg, port, None, None)]
self.send_packet(datapath=datapath, port=port, packet=p)
示例11: createArpReply
def createArpReply(message, ip):
if not packetIsARP(message):
print("Packet is not ARP")
return
pkt = packet.Packet(message.data)
origarp = pkt.get_protocol(arp.arp)
a = arp.arp(
hwtype=origarp.hwtype,
proto=origarp.proto,
src_mac=origarp.src_mac,
dst_mac=origarp.dst_mac,
hlen=origarp.hlen,
opcode=arp.ARP_REPLY,
plen=origarp.plen,
src_ip=ip,
dst_ip=origarp.dst_ip
)
e = ethernet.ethernet(
dst=origarp.dst_mac,
src=origarp.src_mac,
ethertype=ether.ETH_TYPE_ARP)
p = packet.Packet()
p.add_protocol(e)
p.add_protocol(a)
p.serialize()
return p
示例12: receive_arp
def receive_arp(self, datapath, packet, etherFrame, inPort):
arp_msg = packet.get_protocol(arp.arp)
if arp_msg.opcode == arp.ARP_REQUEST:
if arp_msg.dst_ip == self.ports_to_ips[inPort-1][0]:
e = ethernet.ethernet(dst=etherFrame.src,
src=self.ports_to_ips[inPort-1][2],
ethertype=ether.ETH_TYPE_ARP)
a = arp.arp(opcode=arp.ARP_REPLY,
src_mac=self.ports_to_ips[inPort-1][2],
src_ip=arp_msg.dst_ip,
dst_mac=etherFrame.src,
dst_ip=arp_msg.src_ip)
puerto=inPort
p = Packet()
p.add_protocol(e)
p.add_protocol(a)
self.send_packet(datapath, puerto, p)
elif arp_msg.opcode == arp.ARP_REPLY:
self.ipToMac[arp_msg.src_ip] = arp_msg.src_mac
for (msg,port,nat,puertoNat) in self.dict_pendientes[arp_msg.src_ip]:
if nat == None and puertoNat == None:
self.insertar_flujo(msg=msg, mac=arp_msg.src_mac, port=port, mod=1)
elif nat == 1:
self.insertar_flujo(msg=msg, mod=0, puerto_origen=puertoNat, ip_origen=ip_publica, sentido=1, protoc=1, port=port, mac=arp_msg.src_mac)
self.dict_pendientes[arp_msg.src_ip] = []
示例13: arp_reply
def arp_reply(self,datapath, src, dst, dst_ip, src_ip, eth, msg):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
pkt = packet.Packet()
pkt.add_protocol(ethernet.ethernet(ethertype=eth.ethertype, dst=src, src=dst))
pkt.add_protocol(arp.arp(opcode=2, src_mac=dst, src_ip=dst_ip, dst_mac=src, dst_ip=src_ip))
pkt.serialize()
self.logger.info("packet-out %s", pkt)
actions = [parser.OFPActionOutput(port=msg.in_port)]
data = pkt.data
out = parser.OFPPacketOut(datapath=datapath,
buffer_id=ofproto.OFP_NO_BUFFER,
in_port=ofproto.OFPP_CONTROLLER,
actions=actions,
data=data)
datapath.send_msg(out)
示例14: handle_arp
def handle_arp(self, dp, port, pkt_ethernet, pkt_arp):
if pkt_arp.opcode != arp.ARP_REQUEST:
return
if self.arp_table.get(pkt_arp.dst_ip) == None:
return
get_mac = self.arp_table[pkt_arp.dst_ip]
pkt = packet.Packet()
pkt.add_protocol(
ethernet.ethernet(
ethertype=ether.ETH_TYPE_ARP,
dst = pkt_ethernet.src,
src = get_mac
)
)
pkt.add_protocol(
arp.arp(
opcode=arp.ARP_REPLY,
src_mac= get_mac,
src_ip = pkt_arp.dst_ip,
dst_mac= pkt_arp.src_mac,
dst_ip = pkt_arp.src_ip
)
)
self.send_packet(dp, port, pkt)
示例15: request_generator
def request_generator(self,datapath):
parser = datapath.ofproto_parser
barrier_req = parser.OFPBarrierRequest(datapath)
e = ethernet.ethernet(dst='ff:ff:ff:ff:ff:ff',
src='08:60:6e:7f:74:e7',
ethertype=ether.ETH_TYPE_ARP)
a = arp.arp(hwtype=1, proto=0x0800, hlen=6, plen=4, opcode=2,
src_mac='08:60:6e:7f:74:e7', src_ip='192.0.2.1',
dst_mac='00:00:00:00:00:00', dst_ip='192.0.2.2')
p = packet.Packet()
p.add_protocol(e)
p.add_protocol(a)
p.serialize()
msg1=p.data
actions = [parser.OFPActionOutput(1, 0)]
send_data_req = parser.OFPPacketOut(datapath, buffer_id=0xffffffff, in_port=datapath.ofproto.OFPP_CONTROLLER, actions=actions, data=p.data)
print "send empty request"
datapath.send_msg(barrier_req)
yield
print "send 1st datamsg"
datapath.send_msg(send_data_req)
datapath.send_msg(barrier_req)
yield
print "send 2nd datamsg"
datapath.send_msg(send_data_req)
datapath.send_msg(barrier_req)
yield
print "*** end of request sequence ***"
yield