#!/usr/bin/python

import libnetc
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=libnetc.name_resolve('192.168.1.3',0)
tgt_ip=libnetc.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 = libnetc.new_interface()
  #libnetc.interface_open_link(ifc,libnetc.select_device(None))
  libnetc.interface_open_link(ifc,'eth0')
  #src_eth = libnetc.interface_get_hwaddr(ifc)
  #print 'source ethernet address:'
  #dumphex (src_eth)

  icmp_pkt = libnetc.build_icmp_echo(libnetc.ICMP_ECHO,
                                    0,
                                    1,
                                    1)
  dumphex(icmp_pkt)

  ip_pkt = libnetc.build_ip(len(icmp_pkt),
                            0,
                            1,
                            0,
                            255,
                            libnetc.IPPROTO_ICMP,
                            src_ip,
                            tgt_ip) + icmp_pkt
                            
  dumphex(ip_pkt)
  libnetc.do_checksum(ip_pkt, libnetc.IPPROTO_ICMP, libnetc.ICMP_H)
  dumphex(ip_pkt)
  libnetc.do_checksum(ip_pkt, libnetc.IPPROTO_IP, libnetc.IP_H)
  dumphex(ip_pkt)

  pkt = libnetc.build_ethernet(tgt_eth,src_eth,
                               libnetc.ETHERTYPE_IP) + ip_pkt


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




  libnetc.interface_write(ifc,pkt)

  libnetc.delete_interface(ifc)


syntax highlighted by Code2HTML, v. 0.9.1