File: icmp.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 (178 lines) | stat: -rw-r--r-- 3,909 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**************************************************************************** 
** File: icmp.c
**
** Author: Mike Borella
**
** Comments: Dump ICMP header information
**
*****************************************************************************/

#include "config.h"
#include <stdio.h>
#include "ip_icmp.h"

/* rfc1191 */
struct mtu_discovery 
{
  short unused;
  short nexthopmtu;
};

struct tok 
{
  int v;                  /* value */
  char *s;                /* string */
};

static struct tok icmp2str[] = 
{
  { ICMP_ECHOREPLY,               "echo reply" },
  { ICMP_SOURCEQUENCH,            "source quench" },
  { ICMP_ECHO,                    "echo request" },
  { ICMP_ROUTERSOLICIT,           "router solicitation" },
  { ICMP_TSTAMP,                  "time stamp request" },
  { ICMP_TSTAMPREPLY,             "time stamp reply" },
  { ICMP_IREQ,                    "information request" },
  { ICMP_IREQREPLY,               "information reply" },
  { ICMP_MASKREQ,                 "address mask request" },
  { 0,                            NULL }
};

extern u_char *packet_end;

/*----------------------------------------------------------------------------
**
** dump_icmp()
**
** Parse ICMP header and dump fields
**
**----------------------------------------------------------------------------
*/

void dump_icmp(u_char *bp, u_char *bp2)
{
  char *cp;
  struct icmp *dp;
  struct ip *ip;
  char *str, *fmt;
  struct ip *oip;
  struct udphdr *ouh;
  u_int hlen, dport, mtu;
  char buf[256];

  dp = (struct icmp *)bp;
  ip = (struct ip *) bp2;
  str = buf;

  printf("----------------------------------------------------------\n");
  printf("                        ICMP Header\n");
  printf("----------------------------------------------------------\n");
  printf("Type:                   %d ", dp->icmp_type);
  
  switch (dp->icmp_type) 
    {
    case ICMP_UNREACH:
      printf("(unreachable)\n");
      printf("Code:                   %d ", dp->icmp_code);

      switch (dp->icmp_code) 
	{
	  
	case ICMP_UNREACH_PROTOCOL:
	  printf("(protocol)\n");
	  printf("Destination IP:         %s\n");
	  break;

	case ICMP_UNREACH_PORT:
	  printf("(port)\n");
	  printf("Destination IP:         %s\n");
	  
	  oip = &dp->icmp_ip;
	  hlen = oip->ip_hl * 4;
	  ouh = (struct udphdr *)(((u_char *)oip) + hlen);
	  dport = ntohs(ouh->dest);
	  switch (oip->ip_p) 
	    {

	    case IPPROTO_TCP:
	      printf("tcp port %s unreachable\n", tcpport_string(dport));
	      break;
	      
	    case IPPROTO_UDP:
	      printf("udp port %s unreachable\n", udpport_string(dport));
	      break;
	      
	    default:
	      printf("Unknown protocol\n");
	      break;
	    }
	  break;
	  
	case ICMP_UNREACH_NEEDFRAG:
	  {
	    struct mtu_discovery *mp;
	    
	    mp = (struct mtu_discovery *)&dp->icmp_void;
	    mtu = EXTRACT_16BITS(&mp->nexthopmtu);

	    printf("(fragmentation needed)\n");
	    printf("Destination IP:         %s\n");
	    
	    if (mtu) printf("MTU = %d\n", mtu);
	  }
	break;
	
	default:
	  printf("(unknown)\n");
	  break;
	}
      
      break;
            
    case ICMP_TIMXCEED:
      switch (dp->icmp_code) 
	{
	case ICMP_TIMXCEED_INTRANS:
	  printf("(time exceeded in transit)\n");
	  break;

	case ICMP_TIMXCEED_REASS:
	  printf("(time exceeded for IP reassembly)\n");
	  break;

	default:
	  printf("Time exceeded-#%d", dp->icmp_code);
	  break;
	}
      break;

    default:
      str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
      break;
    }
  
  printf("icmp: %s", str);
  return;
  
}

  
/*
 * Convert a token value to a string; use "fmt" if not found.
 */
const char *tok2str(register const struct tok *lp, register const char *fmt,
		     register int v)
{
  static char buf[128];
  
  while (lp->s != NULL) 
    {
      if (lp->v == v) return (lp->s);
      ++lp;
    }
  
  if (fmt == NULL) fmt = "#%d";
  sprintf(buf, fmt, v);
  
  return (buf);
}