File: ip.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 (149 lines) | stat: -rw-r--r-- 3,471 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**************************************************************************** 
** File: ip.c
**
** Author: Mike Borella
**
** Comments: Dump IP header information
**
*****************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include "config.h"
#include "ip.h"

extern struct arg_t *my_args;

/*----------------------------------------------------------------------------
**
** dump_ip()
**
** Parse IP header and dump fields
**
**----------------------------------------------------------------------------
*/

void dump_ip(u_char *bp, int length)
{
  IPHdr *ip, ip2;
  u_int hlen, len, off;
  u_char *cp = NULL;
  u_int frag_off;
  u_char tos;
  u_int16_t csum;
  u_int16_t my_csum;
  void dump_udp(u_char *, int);
  void dump_tcp(u_char *, int);
  void dump_icmp(u_char *, u_char *);
  u_int16_t in_cksum(u_int16_t *, int);

  /*
   * Dump header announcement
   */

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

  /*
   * View the packet as an IP header
   */

  ip = (IPHdr *) bp;

  /*
   * Check for truncated header and truncated packet
   */
  
  if (length < sizeof(IPHdr))
    {
      printf("Truncated header, length = %d bytes\n", length);
      return;
    }

  len = ntohs(ip->ip_len);
  if (length < len)
    {
      printf("Truncated packet: length field = %d, actual length = %d\n", 
	     len, length);
      return;
    }

  /*
   * Dump header fields
   */
  
  csum = ntohs(ip->ip_csum);
  hlen = ip->ip_hl*4;

  if (!my_args->n)
    {
      printf("Version:                %d\n", ip->ip_v);
      printf("Header length:          %d\n", hlen);
      
      tos = ip->ip_tos;
      printf("Type of service:        %d", tos);
      printf(" (precedence=%d, D=%d, T=%d, R=%d, U=%d)\n",
	     tos >> 5, (tos & 0x10) >> 4, (tos & 0x08) >> 3,
	     (tos & 0x04) >> 2, tos & 0x03);

      printf("Total length:           %d\n", ntohs(ip->ip_len));
      printf("Identification #:       %d\n", ntohs(ip->ip_id));
      
      frag_off = ntohs(ip->ip_off);
      printf("Fragmentation offset:   %d", (frag_off & 0x1fff) * 8);
      frag_off &= 0xe000;
      printf(" (U=%d, DF=%d, MF=%d)\n", (frag_off & 0x8000) >> 15, 
	     (frag_off & 0x4000) >> 14, (frag_off & 0x2000) >> 13);
      
      printf("Time to live:           %d\n", ip->ip_ttl);
      printf("Protocol:               %d\n", ip->ip_p);
      
      printf("Header checksum:        %d ", csum);
      memcpy((void *) &ip2, (void *) ip, sizeof(IPHdr));
      ip2.ip_csum = 0;
      my_csum = ntohs(in_cksum((u_int16_t *) &ip2, sizeof(IPHdr)));
      if (my_csum != csum) printf("(error: should be %d)", my_csum);
      printf("\n");
      
      printf("Source address          %s\n", inet_ntoa(ip->ip_src));
      printf("Destination address     %s\n", inet_ntoa(ip->ip_dst));
    }


  /*
   * If this is fragment zero, hand it to the next higher
   * level protocol.
   */

  len -= hlen;
  off = ntohs(ip->ip_off);
  if ((off & 0x1fff) == 0) 
    {
      cp = (u_char *) ip + hlen;
      switch (ip->ip_p) 
	{
	case IPPROTO_TCP:
	  dump_tcp(cp, len);
	  break;
       
	case IPPROTO_UDP:
	  dump_udp(cp, len);
	  break;
	  /*
	case IPPROTO_ICMP:
	  dump_icmp(cp, (u_char *) ip);
	  break; 
	  */
	default:
	  break;
	}
    }
}