Exemplo n.º 1
0
    def start(self):
        eth_length = 14
        arp_length = 28
    
        while self.run:
            # receive arp requests
            try:
                packet = self.raw_socket.recvfrom(65565)

                packet = packet[0]
                
                eth_frame = self.parse_eth_frame(packet[0:eth_length])
                arp_packet = self.parse_arp_packet(packet[eth_length:(eth_length+arp_length)])

                arp_type = struct.unpack("!h", arp_packet["oper"])[0]
                if LOG:
                    print "ARP-PROXY: received ARP-" + ("REQUEST" if (arp_type == 1) else "REPLY") +" SRC: "+eth_frame["src_mac"]+" / "+arp_packet["src_ip"]+" "+"DST: "+eth_frame["dst_mac"]+" / "+arp_packet["dst_ip"]

                if arp_type == 1:
                    # check if the arp request stems from one of the participants
                    if eth_frame["src_mac"] in self.xrs.portmac_2_participant:
                        # then craft reply using VNH to VMAC mapping
                        if LOG:
                            print "Crafting REPLY for received Request"
                        vmac_addr = vmac(arp_packet["dst_ip"], self.xrs.portmac_2_participant[eth_frame["src_mac"]], self.xrs)

                        # only send arp request if a vmac exists
                        if vmac_addr <> "":
                            if LOG:
                                print "ARP-PROXY: reply with VMAC "+vmac_addr

                            data = self.craft_arp_packet(arp_packet, vmac_addr)
                            eth_packet = self.craft_eth_frame(eth_frame, vmac_addr, data)

                            self.raw_socket.send(''.join(eth_packet))
                            
            except socket.timeout:
                if LOG:
                    print 'Socket Timeout Occured'
Exemplo n.º 2
0
    def start(self):
        eth_length = 14
        arp_length = 28
    
        while self.run:
            # receive arp requests
            try:
                packet = self.raw_socket.recvfrom(65565)

                packet = packet[0]
                
                eth_frame = self.parse_eth_frame(packet[0:eth_length])
                arp_packet = self.parse_arp_packet(packet[eth_length:(eth_length+arp_length)])

                arp_type = struct.unpack("!h", arp_packet["oper"])[0]
                if LOG:
                    print "ARP-PROXY: received ARP-" + ("REQUEST" if (arp_type == 1) else "REPLY") +" SRC: "+eth_frame["src_mac"]+" / "+arp_packet["src_ip"]+" "+"DST: "+eth_frame["dst_mac"]+" / "+arp_packet["dst_ip"]

                if arp_type == 1:
                    # check if the arp request stems from one of the participants
                    if eth_frame["src_mac"] in self.xrs.portmac_2_participant:
                        # then craft reply using VNH to VMAC mapping
                        print "Crafting REPLY for received Request"
                        vmac_addr = vmac(arp_packet["dst_ip"], self.xrs.portmac_2_participant[eth_frame["src_mac"]], self.xrs)

                        # only send arp request if a vmac exists
                        if vmac_addr <> "":
                            if LOG:
                                print "ARP-PROXY: reply with VMAC "+vmac_addr

                            data = self.craft_arp_packet(arp_packet, vmac_addr)
                            eth_packet = self.craft_eth_frame(eth_frame, vmac_addr, data)

                            self.raw_socket.send(''.join(eth_packet))
                            
            except socket.timeout:
                if LOG:
                    print 'Socket Timeout Occured'
Exemplo n.º 3
0
    def send_gratuitous_arp(self, changes):
        # then craft reply using VNH to VMAC mapping
        vmac_addr = vmac(changes["VNH"], changes["participant"], self.xrs)
        
        dst_mac = vmac_best_path(changes["participant"], self.xrs)

        arp_packet = [
            # HTYPE
            struct.pack("!h", 1),
            # PTYPE (IPv4)
            struct.pack("!h", 0x0800),
            # HLEN
            struct.pack("!B", 6),
            # PLEN
            struct.pack("!B", 4),
            # OPER (reply)
            struct.pack("!h", 2),
            # SHA
            binascii.unhexlify(vmac_addr.replace(':', '')),
            # SPA
            socket.inet_aton(str(changes["VNH"])),
            # THA
            binascii.unhexlify(vmac_addr.replace(':', '')),
            # TPA
            socket.inet_aton(str(changes["VNH"]))
        ]
        eth_frame = [
            # Destination address:
            binascii.unhexlify(dst_mac.replace(':', '')),
            # Source address:
            binascii.unhexlify(vmac_addr.replace(':', '')),
            # Protocol
            struct.pack("!h", ETH_TYPE_ARP),
            # Data
            ''.join(arp_packet)
        ]
                
        self.raw_socket.send(''.join(eth_frame))   
Exemplo n.º 4
0
    def send_gratuitous_arp(self, changes):
        # then craft reply using VNH to VMAC mapping
        vmac_addr = vmac(changes["VNH"], changes["participant"], self.xrs)
        
        dst_mac = vmac_best_path(changes["participant"], self.xrs)

        arp_packet = [
            # HTYPE
            struct.pack("!h", 1),
            # PTYPE (IPv4)
            struct.pack("!h", 0x0800),
            # HLEN
            struct.pack("!B", 6),
            # PLEN
            struct.pack("!B", 4),
            # OPER (reply)
            struct.pack("!h", 2),
            # SHA
            binascii.unhexlify(vmac_addr.replace(':', '')),
            # SPA
            socket.inet_aton(str(changes["VNH"])),
            # THA
            binascii.unhexlify(vmac_addr.replace(':', '')),
            # TPA
            socket.inet_aton(str(changes["VNH"]))
        ]
        eth_frame = [
            # Destination address:
            binascii.unhexlify(dst_mac.replace(':', '')),
            # Source address:
            binascii.unhexlify(vmac_addr.replace(':', '')),
            # Protocol
            struct.pack("!h", ETH_TYPE_ARP),
            # Data
            ''.join(arp_packet)
        ]
                
        self.raw_socket.send(''.join(eth_frame))