#!/usr/bin/python

import libnet
import string

tgt_eth='\x00\xc0\x7b\x6c\x8d\x80'
src_eth='\x00\xc0\x7b\x6c\x8d\x80'
broadcast_eth='\xff\xff\xff\xff\xff\xff'
src_ip=libnet.name_resolve('192.168.1.3',0)
tgt_ip=libnet.name_resolve('192.168.1.1',0)
#payload='arp packets can carry data?  who knew!'
payload=None


def dumphex(s):
    bytes = map(lambda x: '%.2x ' % x, map(ord, s))
    if len(s)>16:
      for i in range((len(s)+16)/16*16, 15, -16):
        bytes.insert(i,'\n')
    print string.join(bytes, '')


if __name__=="__main__":

  ifc = libnet.interface()
  ifc.open_link(libnet.select_device(None))
  src_eth = ifc.get_hwaddr()
  print 'source ethernet address:'
  dumphex (src_eth)

  pkt = libnet.packet()
  pkt.build_icmp_echo(libnet.ICMP_ECHO,
                      0,
                      1,
                      1)

  pkt.build_ip(len(pkt),
                            0,
                            1,
                            0,
                            255,
                            libnet.IPPROTO_ICMP,
                            src_ip,
                            tgt_ip)
                            
  dumphex(pkt.getvalue())
  pkt.do_checksum(libnet.IPPROTO_ICMP, libnet.ICMP_H)
  dumphex(pkt.getvalue())
  pkt.do_checksum(libnet.IPPROTO_IP, libnet.IP_H)
  dumphex(pkt.getvalue())

  pkt.build_ethernet(tgt_eth,src_eth,
                               libnet.ETHERTYPE_IP)


  print 'icmp request with ethernet header:'
  dumphex(pkt.getvalue())

  ifc.write(pkt)


syntax highlighted by Code2HTML, v. 0.9.1