/*-
 * 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 <sys/socket.h>

#include <net/ethernet.h>

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

#include <stdio.h>

#include "arp.h"
#include "cksum.h"
#include "ether.h"
#include "icmp.h"
#include "tcp.h"

struct in_addr	my_ipaddr;

int
ip_output(u_char *packet, u_int packetlen)
{
	struct ether_header *eh;
	struct ip *ip;

	eh = (struct ether_header *)packet;
	eh->ether_type = htons(ETHERTYPE_IP);
	ip = (struct ip *)(packet + sizeof(*eh));
	ip->ip_sum = in_cksum((u_short *)ip, ip->ip_hl << 2);

	if (arp_lookup((struct in_addr *)&ip->ip_dst, eh->ether_dhost) == -1)
		return (-1);
	return (ether_output(packet, packetlen));
}

void
ip_input(struct ether_header *eh, u_char *payload, u_int payloadlen)
{
	u_int len, hlen;
	struct ip *ip;

	if (payloadlen < sizeof(*ip))
		return;
	ip = (struct ip *)payload;
	if (ip->ip_v != 4) {
		printf("ip_input: not ipv4\n");
		return;
	}
	len = ntohs(ip->ip_len);
	hlen = ip->ip_hl << 2;

	if (payloadlen < len) {
		printf("ip_input: truncated packet (%d, %d, %d)\n",
		    payloadlen, len, hlen);
		return;
	}

	if (hlen > len) {
		printf("ip_input: truncated header in packet\n");
		return;
	}

	if (hlen < sizeof(*ip)) {
		printf("ip_input: offset truncated IP header\n");
		return;
	}

	/*
	 * Re-adjust payload to be the IP payload, not ethernet payload.
	 * Allow for variable-size IP header.
	 */
	switch (ip->ip_p) {
	case IPPROTO_ICMP:
		icmp_input(eh, ip, hlen, payload + hlen, payloadlen - hlen);
		break;

	case IPPROTO_TCP:
		tcp_input(eh, ip, hlen, payload + hlen, payloadlen - hlen);
		break;

	default:
		printf("ip_input: got something\n");
	}
}
