/* This was taken from /afs/sipb/user/jhawk/src/fixpain/fixpain.c,
   some code to restart pain.lcs.mit.edu */
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

#include <sys/param.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/ioctl.h>

#include <net/bpf.h>
#include <net/if.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <string.h>

void main() {
  char packet[1000]; int plen; char *p;
  struct ifreq ifr;

  struct ether_addr esrc, edst;
  struct ether_header ehead;
  struct ip iphead;
  struct tcphdr thead;

  /* get a device, ie fd = open("/dev/bpf0", O_WRONLY);
     ioctl(fd, BIOCSETIF, (caddr_t)&ifr);
     ioctl(fd, BIOCGBLEN, (caddr_t)&n); */

  p=packet;
  
  /* This is a packet going from granola to pain.lcs.mit.edu */

  bcopy((char*)ether_aton("8:0:20:75:6c:8a"), &ehead.ether_shost,
	sizeof(struct ether_addr));	/* should be granola */
  bcopy((char*)ether_aton("0:0:c:5:a2:33"), &ehead.ether_dhost,
	sizeof(struct ether_addr));	/* w20rtr */
  ehead.ether_type = htons(ETHERTYPE_IP);
  fprintf(stdout, "From %s", ether_ntoa(ehead.ether_shost));
  fprintf(stdout, " to %s\n", ether_ntoa(ehead.ether_dhost));
  bcopy(&ehead, p, sizeof(ehead)); p+=sizeof(ehead);
  
  /* IP */
  
  iphead.ip_hl=5;
  iphead.ip_v=4;
  iphead.ip_tos=0;
  iphead.ip_len=htons(54-14);	/* XXX */
  iphead.ip_id= htons(0x1234);
  iphead.ip_off=0;
  iphead.ip_ttl= 30;
  iphead.ip_p = IPPROTO_TCP;
  iphead.ip_sum = 0; /* XXX */
#ifdef ITSREAL
  if (inet_aton("128.52.46.239", &iphead.ip_dst) < 0)
#else
    if (inet_aton("18.70.0.228", &iphead.ip_dst) < 0)
#endif
      err("dst");
  
  if (inet_aton("18.70.0.25", &iphead.ip_src) < 0)
    err("src");
  
  /* Set checksum */
  
  /*    iphead.ip_sum = in_cksum(&iphead, iphead.ip_hl << 4); */

  bcopy(&iphead, p, sizeof(iphead)); p+=sizeof(iphead);

  /* TCP */
    
  thead.th_sport = htons(1040); /* XXX */
  thead.th_dport = htons(23);
  thead.th_seq = htons(4); /* XXX */
  thead.th_ack = htons(0); /* XXX */
  thead.th_off = 6;
  thead.th_flags = TH_RST;
  thead.th_win = 0;
  thead.th_sum = 0; /* XXX */
  thead.th_urp = 0;
  
  bcopy(&thead, p, sizeof(thead)); p+=sizeof(thead);
  
  *p++='A';
  *p++=0;
  
  plen=p-packet;
  printf("Packet length is %d.\n", plen);

  /* Then you'd write this packet directly to a filter ie:
     write(fd, packet, plen); */

}
