xref: /illumos-gate/usr/src/cmd/ipf/lib/icmpcode.c (revision 30fdefb82f3ce27d19928cb02b9a265b2c321a49)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: icmpcode.c,v 1.7.2.1 2004/12/09 19:41:20 darrenr Exp $
7  */
8 
9 #include <ctype.h>
10 
11 #include "ipf.h"
12 
13 #ifndef	MIN
14 # define	MIN(a,b)	((a) > (b) ? (b) : (a))
15 #endif
16 
17 
18 char	*icmpcodes[MAX_ICMPCODE + 1] = {
19 	"net-unr", "host-unr", "proto-unr", "port-unr", "needfrag", "srcfail",
20 	"net-unk", "host-unk", "isolate", "net-prohib", "host-prohib",
21 	"net-tos", "host-tos", "filter-prohib", "host-preced", "preced-cutoff",
22 	NULL };
23 
24 /*
25  * Return the number for the associated ICMP unreachable code.
26  */
27 int icmpcode(str)
28 char *str;
29 {
30 	char	*s;
31 	int	i, len;
32 
33 	if ((s = strrchr(str, ')')))
34 		*s = '\0';
35 	if (ISDIGIT(*str)) {
36 		if (!ratoi(str, &i, 0, 255))
37 			return -1;
38 		else
39 			return i;
40 	}
41 	len = strlen(str);
42 	for (i = 0; icmpcodes[i]; i++)
43 		if (!strncasecmp(str, icmpcodes[i], MIN(len,
44 				 strlen(icmpcodes[i])) ))
45 			return i;
46 	return -1;
47 }
48