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

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


unsigned char *
retrieve_rand_ipv4_addr()
{
    int oct, oct_count; 
    static int seed;
    unsigned char *ip;

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

    ip = malloc(sizeof(unsigned char) * 16);

    for(oct_count = 1; oct_count < 5; oct_count++)
    {
	seed++;
        srand(time(0) ^ getpid() * seed);
        oct = rand() % 254;

        if(oct_count != 1)
            sprintf(ip, "%s.%d", ip, oct);
        else
            sprintf(ip, "%d", oct);
    }

    return ip;
}

libnet_t *
build_ipv4_hdr(libnet_t *packet_d)
{
#ifdef __DEBUG
    fprintf(stderr, "DEBUG: build_ipv4_hdr()\n");
#endif

    if(ip4hdr_o.ip_rand_saddr)
        ip4hdr_o.ip_saddr = retrieve_rand_ipv4_addr();

    if(ip4hdr_o.ip_rand_daddr)
	ip4hdr_o.ip_daddr = retrieve_rand_ipv4_addr();
    
    if(ip4hdr_o.ip_daddr == NULL)
    {
        fprintf(stderr, "\nError: No destination IP address defined\n");
        exit(FAILURE);
    }

    if(ip4hdr_o.ip_saddr == NULL)
    {
        if((ip4hdr_o.ipn_saddr = libnet_get_ipaddr4(packet_d)) == -1)
        {
            fprintf(stderr, "\nError: Unable to retreive local IP address: %s\n", libnet_geterror(packet_d));
            exit(FAILURE);
        }

        ip4hdr_o.ip_saddr = libnet_addr2name4(ip4hdr_o.ipn_saddr, 0);
    }
    else 
    {
        if((ip4hdr_o.ipn_saddr = libnet_name2addr4(packet_d, ip4hdr_o.ip_saddr, 1)) == -1)
        {
            fprintf(stderr, "\nError: Invalid source IP address: %s\n", ip4hdr_o.ip_saddr);
            exit(FAILURE);
        }
    }
	
    if((ip4hdr_o.ipn_daddr = libnet_name2addr4(packet_d, ip4hdr_o.ip_daddr, 1)) == -1)
    {
        fprintf(stderr, "\nError: Invalid source IP address: %s\n", ip4hdr_o.ip_daddr);
        exit(FAILURE);
    }

    if(ip4hdr_o.ip_rand_id)
        ip4hdr_o.ip_id = libnet_get_prand(LIBNET_PRu16);

    if(libnet_build_ipv4(
        PACKIT_IPV4_H + header_len + payload_len,
        ip4hdr_o.ip_tos,
        ip4hdr_o.ip_id,
        ip4hdr_o.ip_frag,
        ip4hdr_o.ip_ttl,
        ip4hdr_o.ip_proto,
        ip4hdr_o.ip_sum,
        ip4hdr_o.ipn_saddr,
        ip4hdr_o.ipn_daddr,
        NULL,
        0,
        packet_d,
        0) == -1)
    {
        fprintf(stderr, "\nError: Unable to build IP header: %s\n", libnet_geterror(packet_d));
        exit(FAILURE);
    }
  
    return packet_d;
}
