/*
 * 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 "../include/packit.h"
#include "../include/inject.h"
#include "../include/capture.h"
#include "../include/error.h"

void
print_injection(short init_type, unsigned int p_count)
{
    unsigned char *arp_t;
    unsigned char *icmp_t;
    unsigned char *icmp_c = NULL;
    char proto[5], flags[7];

#ifdef __DEBUG
    fprintf(stderr, "DEBUG: print_injection() INIT_TYPE: %d  P_COUNT: %d\n", init_type, p_count);
#endif

    proto[0] = '\0';
    flags[0] = '\0';

    if(injection_type == ETHERTYPE_IP)
    {
#ifdef __DEBUG
        fprintf(stderr, "DEBUG: Ethertype: IP\n");
#endif
		
        if(ip4hdr_o.ip_proto == IPPROTO_TCP)
        {
            if(thdr_o.th_syn)
                strcat(flags, "S");

            if(thdr_o.th_ack)
                strcat(flags, "A");

            if(thdr_o.th_fin)
                strcat(flags, "F");

            if(thdr_o.th_rst)
                strcat(flags, "R");

            if(thdr_o.th_psh)
                strcat(flags, "P");

            if(thdr_o.th_urg)
                strcat(flags, "P");

            if(strlen(flags) < 1)
                strcpy(flags, "None");

	    fprintf(stdout, "TCP header:  Src Port: %d  Dst Port(s): %s  Flag(s): %s\n",
	        sport,
	        s_dport,
	        flags);

	    fprintf(stdout, "\t     Window: %d  ", thdr_o.th_win);

            if(thdr_o.th_seqn)
                fprintf(stdout, "Seqn: %ld  ", thdr_o.th_seqn);

            if(thdr_o.th_ackn)
                fprintf(stdout, "Ackn: %ld  ", thdr_o.th_ackn);

            if(thdr_o.th_urp)
                fprintf(stdout, "Urg: %d ", thdr_o.th_urp);
        }
        else if(ip4hdr_o.ip_proto == IPPROTO_UDP)
        {
            fprintf(stdout, "UDP header:  Src Port: %d  Dst Port(s): %s",
                sport,
                s_dport);
        }
        else if(ip4hdr_o.ip_proto == IPPROTO_ICMP)
        {
	    icmp_t = retrieve_icmp_type(i4hdr_o.ih_type);

	    fprintf(stdout, "ICMP header: Type: %s(%d)  ", icmp_t, i4hdr_o.ih_type);

	    if(i4hdr_o.ih_code > 0)
	    {
                icmp_c = retrieve_icmp_code(i4hdr_o.ih_type, i4hdr_o.ih_code);

	        fprintf(stdout, "Code: %s(%d) ", icmp_c, i4hdr_o.ih_code);
	    }

	    switch(i4hdr_o.ih_type)
	    {
		case 0: case 8: 
	            fprintf(stdout, "ID: %d  Seqn: %d  ", i4hdr_o.ih_id, i4hdr_o.ih_seq); 
		    break;

		case 3: case 5: case 11:
		    fprintf(stdout, "\n\t     Original Datagram\n");

		    if(i4hdr_o.ih_proto == 17)
		        fprintf(stdout, "\t     Protocol: UDP(%d)  ", i4hdr_o.ih_proto);
		    else if(i4hdr_o.ih_proto == 6)
			fprintf(stdout, "\t     Protocol: TCP(%d)  ", i4hdr_o.ih_proto);
		    else if(i4hdr_o.ih_proto == 1)
			fprintf(stdout, "\t     Protocol: ICMP(%d)  ", i4hdr_o.ih_proto);

		    fprintf(stdout, "Src Port: %d  Dst port: %d\n", 
	                i4hdr_o.ih_sport, i4hdr_o.ih_dport);

		    fprintf(stdout, "\t     Src Address: %s  Dst Address: %s\n",
	                i4hdr_o.ih_saddr, i4hdr_o.ih_daddr);

	            fprintf(stdout, "\t     TTL: %d  ID: %d  TOS: 0x%X  ",
			i4hdr_o.ih_ttl, i4hdr_o.ih_id, i4hdr_o.ih_tos);

		    break;

		case 14:
		    fprintf(stdout, "ID: %d  Seqn: %d\n", i4hdr_o.ih_id, i4hdr_o.ih_seq);
	            fprintf(stderr, "\t     Original Timestamp: %ld\n", i4hdr_o.ih_otime);
		    fprintf(stderr, "\t     Recieved Timestamp: %ld\n", i4hdr_o.ih_rtime);
		    fprintf(stderr, "\t     Transmit Timestamp: %ld", i4hdr_o.ih_ttime);

                    break;

		case 17: case 18:
		    fprintf(stdout, "ID: %d  Seqn: %d\n", i4hdr_o.ih_id, i4hdr_o.ih_seq);

	            if(i4hdr_o.ih_mask != NULL)
		        fprintf(stdout, "\t     Address Mask: %s", i4hdr_o.ih_mask);

		    break;
            }
        }

	fprintf(stdout, "\n");

	fprintf(stdout, "IP header:   Src Address: %s  Dst Address: %s\n", 
	    ip4hdr_o.ip_saddr, ip4hdr_o.ip_daddr); 
	
	fprintf(stdout, "\t     TTL: %d  ID: %d  TOS: 0x%X  ", 
	    ip4hdr_o.ip_ttl, ip4hdr_o.ip_id, (unsigned int)ip4hdr_o.ip_tos);

	if(ip4hdr_o.ip_frag == 0x4000)
	    fprintf(stdout, "(DF)");

	fprintf(stdout, "\n");
    }
    else if(injection_type == ETHERTYPE_ARP)
    {
#ifdef __DEBUG
       fprintf(stderr, "DEBUG: Ethertype: ARP\n");
#endif

	arp_t = retrieve_arp_type(ahdr_o.optype);

	fprintf(stdout, "ARP header:  Type: %s(%d)\n", arp_t, ahdr_o.optype);
	fprintf(stdout, "\t     Sender:  Protocol Address: %s  Hardware Address: %s\n",
	    ahdr_o.s_paddr,
	    ahdr_o.shw_addr); 
	
	fprintf(stdout, "\t     Target:  Protocol Address: %s  Hardware Address: %s\n",
	    ahdr_o.r_paddr,
	    ahdr_o.rhw_addr); 
    }

    if(ehdr_o.s_addr || ehdr_o.d_addr)
        fprintf(stdout, "Eth header:  Src Address: %s  Dst Address: %s\n", 
	    ehdr_o.shw_addr,
	    ehdr_o.dhw_addr);

    fprintf(stdout, "\n");
    fprintf(stderr, "Writing packet(s) (%d): ", p_count);

    return;
}
  
