/*
 * author: Darren Bounds <dbounds@intrusense.com>
 * copyright: Copyright (C) 2002 by Darren Bounds
 * license: This software is under GPL version 2 of license
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * packit official page at http://packit.sourceforge.net
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../include/packit.h" 
#include "../include/inject.h"
#include "../include/capture.h"
#include "../include/error.h"

libnet_t *packet_d;

int
reset_udp_stream(unsigned char *packet, struct libnet_ipv4_hdr *iphdr)
{
    unsigned char *s_addr, *d_addr;
    unsigned char payload[8];
    unsigned int payload_len = 8;

    struct libnet_udp_hdr *udphdr;

#ifdef __DEBUG
    fprintf(stderr, "DEBUG: reset_udp_stream()\n");
#endif

    udphdr = (struct libnet_udp_hdr *)(packet + PACKIT_IPV4_H + header_len);

    if((packet_d = libnet_init(0x01, device, errbuf)) == NULL)
    {
        fprintf(stderr, "\nError: Unable to initialize packet injection: %s\n", errbuf);
        exit(FAILURE);
    }

    payload[0] = (htons(udphdr->uh_sport) >> 8) & 0xff;
    payload[1] = htons(udphdr->uh_sport) & 0xff;
    payload[2] = (htons(udphdr->uh_dport) >> 8) & 0xff;
    payload[3] = htons(udphdr->uh_dport) & 0xff;
    payload[4] = (ntohs(udphdr->uh_ulen) >> 8) & 0xff;
    payload[5] = ntohs(udphdr->uh_ulen) & 0xff;
    payload[6] = (udphdr->uh_sum >> 8) & 0xff;
    payload[7] = (udphdr->uh_sum) & 0xff;

    payload_len = 8;

    if(libnet_build_icmpv4_unreach(
        ICMP_UNREACH, 
        ICMP_UNREACH_PORT, 
        0, 
        PACKIT_IPV4_H + ntohs(udphdr->uh_ulen), 
        iphdr->ip_tos, 
        ntohs(iphdr->ip_id), 
        ntohs(iphdr->ip_off), 
        iphdr->ip_ttl, 
        IPPROTO_UDP, 
        iphdr->ip_sum, 
        iphdr->ip_src.s_addr, 
        iphdr->ip_dst.s_addr,
	payload,
	payload_len,
        packet_d, 
        0) == -1)
    {
        fprintf(stderr, "\nError: Unable to build ICMP header: %s\n", libnet_geterror(packet_d)); 
        exit(FAILURE);
    } 
    
    if(libnet_build_ipv4(
        PACKIT_IPV4_H + PACKIT_ICMPV4_UNREACH_H + PACKIT_IPV4_H,
        IPTOS_LOWDELAY | IPTOS_THROUGHPUT,
        libnet_get_prand(LIBNET_PRu16),
        IP_DF,
        255,
        IPPROTO_ICMP,
        0,
        iphdr->ip_dst.s_addr,
        iphdr->ip_src.s_addr,
        NULL,
        0,
        packet_d,
        0) == -1)
    {
        fprintf(stderr, "\nError: Unable to build IP header: %s\n", libnet_geterror(packet_d));
        exit(FAILURE);
    }

    if(libnet_write(packet_d) == -1)
    {
        fprintf(stderr, "\nError: Unable to write packet to network: %s\n", libnet_geterror(packet_d));
        exit(FAILURE);
    }

    s_addr = libnet_addr2name4(iphdr->ip_src.s_addr, 0);
    d_addr = libnet_addr2name4(iphdr->ip_dst.s_addr, 0);

    fprintf(stdout, "Src Address %s > Dst Address %s: Port Unreachable: %s:%d > %s:%d\n", 
        d_addr,
	s_addr,
	s_addr,
	htons(udphdr->uh_sport),
        d_addr,
	htons(udphdr->uh_dport));

    libnet_clear_packet(packet_d);

    return 1;
}
