File: arp.c

package info (click to toggle)
ipgrab 0.5-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 268 kB
  • ctags: 169
  • sloc: sh: 1,507; ansic: 1,234; makefile: 56
file content (101 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**************************************************************************** 
** File: arp.c
**
** Author: Mike Borella
**
** Comments: Dump ICMP header information
**
*****************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "config.h"
#include "arp.h"

extern u_char *packet_end;

void dump_arp(u_char *bp, int length, int caplen)
{
  EtherARP *ap;
  u_short pro, hrd, op;
  struct in_addr spa, tpa;
  char *etheraddr_string(u_char *ep);
  
  /*
   * ARP header announcement
   */

  printf("----------------------------------------------------------\n");
  printf("			ARP Header\n");
  printf("----------------------------------------------------------\n");

  /*
   * Check for truncated packet
   */
  
  ap = (EtherARP *) bp;
  if (length < sizeof(EtherARP)) 
    {
      printf("Truncated packet\n");
      return;
    }

  /*
   * Dump ARP header fields 
   */

  hrd = ntohs(ap->ea_hdr.ar_hrd);
  pro = ntohs(ap->ea_hdr.ar_pro);
  op = ntohs(ap->ea_hdr.ar_op);
  
  printf("Hardware type:          %d\n", hrd);
  printf("Protocol:               %d\n", pro);
  printf("Operation:              %d ", op);

  /*
   * Figure out type of ARP
   */

  switch (op) 
    {    
    case ARPOP_REQUEST:
      printf("(ARP request)\n");
      break;
      
    case ARPOP_REPLY:
      printf("(ARP reply)\n");
      break;
      
    case ARPOP_RREQUEST:
      printf("(RARP request)\n");
      break;
      
    case ARPOP_RREPLY:
      printf("(RARP reply)\n");
      break;
      
    default:
      printf("(unknown)\n");
      return;
    }
  
  /*
   * Dump hardware and IP addresses
   */
  
  memcpy((void *) &spa, (void *) &ap->arp_spa, sizeof (struct in_addr));
  memcpy((void *) &tpa, (void *) &ap->arp_tpa, sizeof (struct in_addr));

  printf("Sender hardware:        %s\n", etheraddr_string(ap->arp_sha));
  printf("Sender IP:              %s\n", inet_ntoa(spa));
  printf("Target hardware:        %s\n", etheraddr_string(ap->arp_tha));
  printf("Target IP:              %s\n", inet_ntoa(tpa));
  
}