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

unsigned char *
retrieve_icmp_code(unsigned short type, unsigned short code)
{
    unsigned char *icmp_c;

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

    icmp_c = malloc(sizeof(unsigned char) * 32);

    if(type == ICMP_UNREACH)
    {
        switch(code)
        {
            case ICMP_UNREACH_NET:
                sprintf(icmp_c, "Network");
                break;

            case ICMP_UNREACH_HOST:
                sprintf(icmp_c, "Host");
                break;

            case ICMP_UNREACH_PROTOCOL:
                sprintf(icmp_c, "Protocol");
                break;

            case ICMP_UNREACH_PORT:
                sprintf(icmp_c, "Port");
                break;

            case ICMP_UNREACH_NEEDFRAG:
                sprintf(icmp_c, "Need Fragment");
                break;

            case ICMP_UNREACH_SRCFAIL: 
                sprintf(icmp_c, "Source Fail");
                break;

            case ICMP_UNREACH_NET_UNKNOWN:
                sprintf(icmp_c, "Network Unknown");
                break;

            case ICMP_UNREACH_HOST_UNKNOWN: 
                sprintf(icmp_c, "Host Unknown");
                break;

            case ICMP_UNREACH_ISOLATED: 
                sprintf(icmp_c, "Isolated");
                break;

            case ICMP_UNREACH_NET_PROHIB: 
                sprintf(icmp_c, "Network Prohibited");
                break;

            case ICMP_UNREACH_HOST_PROHIB:
                sprintf(icmp_c, "Host Prohibited");
                break;

            case ICMP_UNREACH_TOSNET: 
                sprintf(icmp_c, "ToS Network");
                break;

            case ICMP_UNREACH_TOSHOST: 
                sprintf(icmp_c, "ToS Host");
                break;

            case ICMP_UNREACH_FILTER_PROHIB: 
                sprintf(icmp_c, "Filter Prohibited");
                break;

            case ICMP_UNREACH_HOST_PRECEDENCE: 
                sprintf(icmp_c, "Host Precedence");
                break;

            case ICMP_UNREACH_PRECEDENCE_CUTOFF: 
                sprintf(icmp_c, "Precedence Cutoff");
                break;
        }
    }
    else if(type == ICMP_REDIRECT)
    {
        switch(code)
        {
            case ICMP_REDIRECT_NET: 
                sprintf(icmp_c, "Network");
                break;

            case ICMP_REDIRECT_HOST: 
                sprintf(icmp_c, "Host");
                break;

            case ICMP_REDIRECT_TOSNET:
                sprintf(icmp_c, "ToS Net");
                break;

            case ICMP_REDIRECT_TOSHOST:
                sprintf(icmp_c, "ToS Host");
                break;
        }
    }
    else if(type == ICMP_TIMXCEED)
    {
        switch(code)
        {
            case ICMP_TIMXCEED_INTRANS: 
                sprintf(icmp_c, "Transit");
                break;

            case ICMP_TIMXCEED_REASS: 
                sprintf(icmp_c, "Reassembly");
                break;
        }
    }
    else if(type == ICMP_PARAMPROB)
    {
        switch(code)
        {
            case ICMP_PARAMPROB_OPTABSENT:
                sprintf(icmp_c, "Missing Option");
                break;
        }
    }
    else
    {
        sprintf(icmp_c, "Unknown");
    }

    return icmp_c;
}

unsigned char *
retrieve_icmp_type(unsigned short type)
{
    unsigned char *icmp_t;

    icmp_t = malloc(sizeof(unsigned char) * 32); 

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

    switch(type)
    {
        case ICMP_ECHOREPLY:
            sprintf(icmp_t, "Echo Reply");
            break;

        case ICMP_UNREACH:
            sprintf(icmp_t, "Unreachable");
            break;

        case ICMP_SOURCEQUENCH:
            sprintf(icmp_t, "Source Quench");
            break;

        case ICMP_REDIRECT:
            sprintf(icmp_t, "Redirect");
            break;

        case ICMP_ECHO:
            sprintf(icmp_t, "Echo Request");
            break;

        case ICMP_ROUTERADVERT:
            sprintf(icmp_t, "Router Advertise");
            break;

        case ICMP_ROUTERSOLICIT:
            sprintf(icmp_t, "Router Selection");
            break;

        case ICMP_TIMXCEED:
            sprintf(icmp_t, "Time Exceeded");
            break;

        case ICMP_PARAMPROB:
            sprintf(icmp_t, "Param Problem");
            break;

        case ICMP_TSTAMP:
            sprintf(icmp_t, "Timestamp");
            break;

        case ICMP_TSTAMPREPLY:
            sprintf(icmp_t, "Timestamp Reply");
            break;

        case ICMP_IREQ:
            sprintf(icmp_t, "Info Request");
            break;

        case ICMP_IREQREPLY:
            sprintf(icmp_t, "Info Reply");
            break;

        case ICMP_MASKREQ:
            sprintf(icmp_t, "Address Mask Request");
            break;

        case ICMP_MASKREPLY:
            sprintf(icmp_t, "Address Mask Reply");
            break;

        default:
            sprintf(icmp_t, "Unknown");
            break;
    }

    return icmp_t;
}


void
print_icmpv4_hdr(unsigned char *packet)
{
    unsigned char *icmp_t = NULL;
    unsigned char *icmp_c = NULL;
    
    struct libnet_icmpv4_hdr *icmphdr;

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

    icmphdr = (struct libnet_icmpv4_hdr *)(packet + PACKIT_IPV4_H + header_len);
    icmp_t = retrieve_icmp_type(icmphdr->icmp_type);

    if(icmphdr->icmp_code)
        icmp_c = retrieve_icmp_code(icmphdr->icmp_type, icmphdr->icmp_code);

    fprintf(stdout, "ICMP header: Type: %s(%d)  ", icmp_t, icmphdr->icmp_type);

    if(icmphdr->icmp_code > 0)
        fprintf(stdout, "Code: %s(%d)  ", icmp_c, icmphdr->icmp_code);

    switch(icmphdr->icmp_type) 
    {
	case ICMP_ECHOREPLY: case ICMP_ECHO: case ICMP_TSTAMP:
            fprintf(stdout, "ID: %d  Seqn: %d", icmphdr->icmp_id, icmphdr->icmp_seq);
	    break;
    }
    
    fprintf(stdout, "\n");

    return;
}
