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