#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "ars.h"

int main(int argc, char **argv)
{
	struct ars_packet p;
	struct ars_iphdr *ip, *ip2;
	struct ars_tcphdr *tcp;
	struct ars_icmphdr *icmp;
	int s, j;

	ars_init(&p);

	s = ars_open_rawsocket(&p);
	if (s == -ARS_ERROR) {
		perror("Opening raw socket");
		exit(1);
	}

	/* IP layer */
	ip = ars_add_iphdr(&p, 0);
	ip->id = htons(1);
	ip->ttl = 64;
	ars_resolve(&p, &ip->saddr, "192.168.1.1");
	ars_resolve(&p, &ip->daddr, "192.168.1.5");

	/* ICMP layer */
	icmp = ars_add_icmphdr(&p, 0);
	icmp->type = ARS_ICMP_DEST_UNREACH;
	icmp->code = ARS_ICMP_UNR_HOST;

	/* Second IP layer */
	ip2 = ars_add_iphdr(&p, 0);
	ip2->id = htons(1);
	ip2->ttl = 64;
	ars_resolve(&p, &ip2->saddr, "192.168.1.5");
	ars_resolve(&p, &ip2->daddr, "192.168.1.1");

	/* TCP layer */
	tcp = ars_add_tcphdr(&p, 0);
	tcp->th_win = htons(512);
	tcp->th_flags = ARS_TCP_TH_ACK | ARS_TCP_TH_PUSH;
	tcp->th_sport = htons(1);
	tcp->th_dport = htons(22);

	for (j = 1; j < 65535; j++) {
		tcp->th_sport = htons(1443);
		if (ars_compile(&p) != -ARS_OK) {
			printf("Error compiling\n");
			exit(0);
		}
		ars_send(s, &p, NULL, 0);
	}

	/* Print some information about the layers */
	for (j = 0; j < ARS_MAX_LAYER; j++) {
		if (p.p_layer[j].l_type == ARS_TYPE_NULL)
			break;
		printf("layer %d rel_size: %d type: %s\n", j, ars_relative_size(&p, j), ars_linfo[p.p_layer[j].l_type].li_name);
	}
	return 0;
}
