def parse_ether_header(frm): etherdata = frm[0:14] etherh = tcplib.etherhdr() etherh.parsehdr(etherdata) payload = frm[14:len(frm)] return etherh, payload
def pcap_send_segments(ifname, pkts, smacaddr=0, dmacaddr=0, sent=None): # Open pcap descripter plib = pcaplib.pcaplib() if plib.lib == None: print "Error: failed to load libpcap" return -1 descrip = plib.Pcap_open_live(ifname) if descrip == None: print "Error: failed to open pcap descripter" return -1 # Compose packets including datalink header frms = [] dltype = plib.Pcap_datalink(descrip) for pkt in pkts: frm = "" if dltype == pcaplib.DLT_EN10MB: etherh = tcplib.etherhdr(dmacaddr, smacaddr, 0x0800) frm = etherh.bin() elif dltype == pcaplib.DLT_PPP or (dltype == 0 and re.match('ng[0-9]', ifname)): frm = struct.pack('!BBBB', 0xff, 0x03, 0x00, 0x21) else: print "Error: unsupported link type" return -1 frm += pkt frms.append(frm) # Send frames for frm in frms: err = plib.Pcap_inject(descrip, frm, len(frm)) if err < 0: plib.Pcap_close(descrip) print "Error: pcap_inject failed" return -1 if sent != None: sent.set() plib.Pcap_close(descrip) return 0