/*-
 * Copyright (c) 2004 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * [id for your version control system, if any]
 */

#include <sys/types.h>

#include <net/ethernet.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <arpa/inet.h>

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "arp.h"
#include "cksum.h"
#include "ip.h"
#include "stack.h"
#include "tcp.h"

static void
usage(void)
{

	fprintf(stderr, "prog [tap|bpf] ifname myipaddr targetip\n");
	exit(-1);
}

u_char	default_ether_addr[] = { 0x00, 0x04, 0x76, 0x42, 0xf0, 0xcb };

static void
tcp_syn(struct in_addr *myipaddr, u_short myport,
    struct in_addr *targetipaddr, u_short targetport, u_int32_t myseq)
{
	struct tcphdr *th;
	u_int packetlen;
	u_char *packet;
	struct ip *ip;

	packetlen = sizeof(struct ether_header) + sizeof(*ip) + sizeof(*th);
	packet = malloc(packetlen);
	if (packet == NULL) {
		perror("task::malloc");
		return;
	}
	bzero(packet, packetlen);

	ip = (struct ip *)(packet + sizeof(struct ether_header));
	ip->ip_hl = sizeof(*ip) >> 2;
	ip->ip_v = 4;
	ip->ip_tos = 0;
	ip->ip_len = htons(sizeof(*ip) + sizeof(*th));
	ip->ip_id = random();
	ip->ip_off = htons(0);
	ip->ip_ttl = 64;
	ip->ip_p = IPPROTO_TCP;
	ip->ip_sum = htons(0);	/* Will be filled in by ip_output(). */
	ip->ip_src = *myipaddr;
	ip->ip_dst = *targetipaddr;

	th = (struct tpchdr *)(ip + 1);
	th->th_off = sizeof(*th) >> 2;
	th->th_x2 = 0;
	th->th_sport = myport;
	th->th_dport = targetport;
	th->th_seq = myseq;
	th->th_ack = htons(0);
	th->th_flags = TH_SYN;
	th->th_win = htons(65535);
	th->th_urp = htons(0);
	th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
	    htons(sizeof(*th) + IPPROTO_TCP));
	th->th_sum = in_cksum((u_short *)th, sizeof(*th));

	ip_output(packet, packetlen);
	free(packet);
}

/*
 * A generated SYN/ACK as might be received as a recipient of back-scatter.
 */
static void
tcp_synack(struct in_addr *myipaddr, u_short myport,
    struct in_addr *targetipaddr, u_short targetport)
{
	struct tcphdr *th;
	u_int packetlen;
	u_char *packet;
	struct ip *ip;

	packetlen = sizeof(struct ether_header) + sizeof(*ip) + sizeof(*th);
	packet = malloc(packetlen);
	if (packet == NULL) {
		perror("task::malloc");
		return;
	}
	bzero(packet, packetlen);

	ip = (struct ip *)(packet + sizeof(struct ether_header));
	ip->ip_hl = sizeof(*ip) >> 2;
	ip->ip_v = 4;
	ip->ip_tos = 0;
	ip->ip_len = htons(sizeof(*ip) + sizeof(*th));
	ip->ip_id = random();
	ip->ip_off = htons(0);
	ip->ip_ttl = 64;
	ip->ip_p = IPPROTO_TCP;
	ip->ip_sum = htons(0);	/* Will be filled in by ip_output(). */
	ip->ip_src = *myipaddr;
	ip->ip_dst = *targetipaddr;

	th = (struct tpchdr *)(ip + 1);
	th->th_off = sizeof(*th) >> 2;
	th->th_x2 = 0;
	th->th_sport = myport;
	th->th_dport = targetport;
	th->th_seq = random();
	th->th_ack = random();
	th->th_flags = TH_SYN | TH_ACK;
	th->th_win = htons(65535);
	th->th_urp = htons(0);
	th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
	    htons(sizeof(*th) + IPPROTO_TCP));
	th->th_sum = in_cksum((u_short *)th, sizeof(*th));

	ip_output(packet, packetlen);
	free(packet);
}

/*
 * Global arguments to the threads, must be set before launching task
 * threads.
 */
static struct in_addr	task_myipaddr;
static struct in_addr	task_targetipaddr;
static u_short		task_targetport;
static u_int32_t	task_myseq;
static pthread_t	task_thread1, task_thread2;

static void *
task_synack(void *arg)
{
	u_int32_t theirseq;
	u_int16_t myport;

	sleep(5);

	/*
	 * Prime ARP.  Need to add saved packet support to ARP.
	 */
	arp_lookup(&task_targetipaddr, NULL);
	sleep(1);
	arp_lookup(&task_targetipaddr, NULL);
	sleep(1);

	myport = htons((random() % (65535 - 1024)) + 1024);
	theirseq = 0;

	while (1) {
		usleep(10000);
#if 1
		tcp_syn(&task_myipaddr, myport++, &task_targetipaddr,
		    task_targetport, task_myseq);
#else
		tcp_synack(&task_myipaddr, myport, &task_targetipaddr,
		    task_targetport);
#endif
	}
}

static void *
task_receive(void *arg)
{
	struct tcphdr *th;
	struct ip *ip;

	while (1) {
		u_char *packet;
		u_int packetlen;

		tcp_receive(&packet, &packetlen);
		/*
		 * Rely on validation by ip_input() of IP header length.
		 */
		ip = (struct ip *)packet;
		th = (struct tcphdr *)((u_char *)ip + (ip->ip_hl << 2));

		if (ip->ip_dst.s_addr != task_myipaddr.s_addr) {
			free(packet);
			continue;
		}
#if 0
		if (th->th_sport != task_targetport) {
			tcp_sendrst(ip, th);
			free(packet);
			continue;
		}
		printf("Packet is interesting:\n");
		tcp_printhdr(ip, th);
#endif

		if ((th->th_flags & TH_SYN) && (th->th_flags & TH_ACK))
			tcp_sendackseq(ip, th, task_myseq);
		else
			tcp_sendrstseq(ip, th, th->th_ack);

		free(packet);
	}
}

int
main(int argc, char *argv[])
{
	struct in_addr myipaddr, targetipaddr;
	const char *ifname;

	if (argc != 5)
		usage();

	ifname = argv[2];

	if (inet_aton(argv[3], &myipaddr) == 0)
		usage();

	if (inet_aton(argv[4], &targetipaddr) == 0)
		usage();

	if (stack_setup(argv[1], ifname, default_ether_addr, &myipaddr) < 0) {
		perror("stack_setup");
		return (-1);
	}

	srandomdev();

	task_myipaddr = myipaddr;
	task_targetipaddr = targetipaddr;
	task_targetport = htons(22);
	task_myseq = htonl(random());

	assert(pthread_create(&task_thread1, NULL, task_receive, NULL) == 0);
	assert(pthread_create(&task_thread2, NULL, task_synack, NULL) == 0);

	assert(pthread_join(task_thread1, NULL) == 0);
	assert(pthread_join(task_thread2, NULL) == 0);

	stack_shutdown();

	return (0);
}
