/*
 * 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_arp_type(unsigned short optype)
{
    unsigned char *arp_t;

#ifdef __DEBUG
    fprintf(stderr, "DEBUG: retrieve_arp_type() OPTYPE: %d\n", optype);
#endif

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

    switch(optype)
    {
        case ARPOP_REQUEST:
            sprintf(arp_t, "Request");
            break;

        case ARPOP_REPLY:
            sprintf(arp_t, "Reply");
            break;

        case ARPOP_REVREQUEST:
            sprintf(arp_t, "Reverse Request");
            break;

        case ARPOP_REVREPLY:
            sprintf(arp_t, "Reverse Reply");
	    break;

	case ARPOP_INVREQUEST:
	    sprintf(arp_t, "Inverse Request");
	    break;

        case ARPOP_INVREPLY:
            sprintf(arp_t, "Inverse Reply");
            break;

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

#ifdef __DEBUG
    fprintf(stderr, "DEBUG: ARP type: %s\n", arp_t);
#endif

    return arp_t;  
}


void 
print_arp_hdr(unsigned char *packet)
{
    unsigned char *ip_addr;
    unsigned char *arp_t;
    
    struct libnet_arp_hdr *ahdr;

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

    ahdr = (struct libnet_arp_hdr *)(packet + header_len);
    ip_addr = ((unsigned char *)((ahdr + 1)) + 2 *(ahdr)->ar_hln + (ahdr)->ar_pln);
    arp_t = retrieve_arp_type(htons(ahdr->ar_op));

    fprintf(stdout, "ARP header:  Type: %s(%d)\n", arp_t, htons(ahdr->ar_op));
    fprintf(stdout, "\t     Hardware Format: %d  Length: %d\n", 
        ahdr->ar_hrd, 
	ahdr->ar_pro);
    
    fprintf(stdout, "\t     Protocol Format: %d  Length: %d\n", 
       ahdr->ar_pro, 
	ahdr->ar_pln);

    return;
}
