/*-
 * 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bpf.h"
#include "ether.h"
#include "arp.h"
#include "ip.h"
#include "tap.h"
#include "tcp.h"

/*
 * Using BPF, bind to the user-specified interface and implement basic IP
 * stack functionality on that interface, including responding to ARP for a
 * specific IP address.  Deliver received IP packets to a queue if destined
 * for the local IP address (ignore broadcast for now), and offer an API to
 * send.
 */

#define	IF_TYPE_TAP	1
#define	IF_TYPE_BPF	2

static int stack_online;
static char *my_interface;
static u_char stack_etheraddr[ETHER_ADDR_LEN];
static int my_iftype;

int
stack_setup(const char *iftype, const char *interface,
    u_char *etheraddr, struct in_addr *addr)
{

	if (stack_online) {
		fprintf(stderr, "stack_setup: already online\n");
		errno = EBUSY;
		return (-1);
	}

	if (strcmp(iftype, "tap") == 0)
		my_iftype = IF_TYPE_TAP;
	else if (strcmp(iftype, "bpf") == 0)
		my_iftype = IF_TYPE_BPF;
	else {
		fprintf(stderr, "stack_setup: unknown interface type\n");
		errno = EINVAL;
		return (-1);
	}

	bcopy(etheraddr, stack_etheraddr, ETHER_ADDR_LEN);
	my_ipaddr = *addr;
	my_interface = strdup(interface);

	switch (my_iftype) {
	case IF_TYPE_TAP:
		if (tap_start(interface) < 0) {
			perror("stack_setup: TAP start failure\n");
			return (-1);
		}
		break;

	case IF_TYPE_BPF:
		if (bpf_start(interface) < 0) {
			perror("stack_setup: BPF start failure\n");
			return (-1);
		}
		break;
	}

	arp_init();
	tcp_init();

	arp_ipaddrset();

	stack_online = 1;

	return (0);
}

void
stack_shutdown(void)
{

	if (!stack_online) {
		fprintf(stderr, "stack_shutdown: not started\n");
		return;
	}
	stack_online = 0;

	bpf_stop();
	free(my_interface);
	my_interface = NULL;
}

int
stack_output(u_char *packet, u_int packetlen)
{

	switch (my_iftype) {
	case IF_TYPE_BPF:
		return (bpf_output(packet, packetlen));
		break;
	case IF_TYPE_TAP:
		return (tap_output(packet, packetlen));
		break;
	}
	fprintf(stderr, "stack_output: no iftype set\n");
	return (-1);
}
