xref: /illumos-gate/usr/src/cmd/ipf/lib/common/icmpcode.c (revision ab25eeb5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6*ab25eeb5Syz  * $Id: icmpcode.c,v 1.7.2.1 2004/12/09 19:41:20 darrenr Exp $
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #include <ctype.h>
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #include "ipf.h"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #ifndef	MIN
147c478bd9Sstevel@tonic-gate # define	MIN(a,b)	((a) > (b) ? (b) : (a))
157c478bd9Sstevel@tonic-gate #endif
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate char	*icmpcodes[MAX_ICMPCODE + 1] = {
197c478bd9Sstevel@tonic-gate 	"net-unr", "host-unr", "proto-unr", "port-unr", "needfrag", "srcfail",
207c478bd9Sstevel@tonic-gate 	"net-unk", "host-unk", "isolate", "net-prohib", "host-prohib",
21*ab25eeb5Syz 	"net-tos", "host-tos", "filter-prohib", "host-preced", "preced-cutoff",
227c478bd9Sstevel@tonic-gate 	NULL };
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * Return the number for the associated ICMP unreachable code.
267c478bd9Sstevel@tonic-gate  */
icmpcode(str)277c478bd9Sstevel@tonic-gate int icmpcode(str)
287c478bd9Sstevel@tonic-gate char *str;
297c478bd9Sstevel@tonic-gate {
307c478bd9Sstevel@tonic-gate 	char	*s;
317c478bd9Sstevel@tonic-gate 	int	i, len;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 	if ((s = strrchr(str, ')')))
347c478bd9Sstevel@tonic-gate 		*s = '\0';
35*ab25eeb5Syz 	if (ISDIGIT(*str)) {
367c478bd9Sstevel@tonic-gate 		if (!ratoi(str, &i, 0, 255))
377c478bd9Sstevel@tonic-gate 			return -1;
387c478bd9Sstevel@tonic-gate 		else
397c478bd9Sstevel@tonic-gate 			return i;
407c478bd9Sstevel@tonic-gate 	}
417c478bd9Sstevel@tonic-gate 	len = strlen(str);
427c478bd9Sstevel@tonic-gate 	for (i = 0; icmpcodes[i]; i++)
437c478bd9Sstevel@tonic-gate 		if (!strncasecmp(str, icmpcodes[i], MIN(len,
447c478bd9Sstevel@tonic-gate 				 strlen(icmpcodes[i])) ))
457c478bd9Sstevel@tonic-gate 			return i;
467c478bd9Sstevel@tonic-gate 	return -1;
477c478bd9Sstevel@tonic-gate }
48