10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (C) 1993-2001 by Darren Reed. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 50Sstevel@tonic-gate * 6*2393Syz155240 * $Id: icmpcode.c,v 1.7.2.1 2004/12/09 19:41:20 darrenr Exp $ 70Sstevel@tonic-gate */ 80Sstevel@tonic-gate 90Sstevel@tonic-gate #include <ctype.h> 100Sstevel@tonic-gate 110Sstevel@tonic-gate #include "ipf.h" 120Sstevel@tonic-gate 130Sstevel@tonic-gate #ifndef MIN 140Sstevel@tonic-gate # define MIN(a,b) ((a) > (b) ? (b) : (a)) 150Sstevel@tonic-gate #endif 160Sstevel@tonic-gate 170Sstevel@tonic-gate 180Sstevel@tonic-gate char *icmpcodes[MAX_ICMPCODE + 1] = { 190Sstevel@tonic-gate "net-unr", "host-unr", "proto-unr", "port-unr", "needfrag", "srcfail", 200Sstevel@tonic-gate "net-unk", "host-unk", "isolate", "net-prohib", "host-prohib", 21*2393Syz155240 "net-tos", "host-tos", "filter-prohib", "host-preced", "preced-cutoff", 220Sstevel@tonic-gate NULL }; 230Sstevel@tonic-gate 240Sstevel@tonic-gate /* 250Sstevel@tonic-gate * Return the number for the associated ICMP unreachable code. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate int icmpcode(str) 280Sstevel@tonic-gate char *str; 290Sstevel@tonic-gate { 300Sstevel@tonic-gate char *s; 310Sstevel@tonic-gate int i, len; 320Sstevel@tonic-gate 330Sstevel@tonic-gate if ((s = strrchr(str, ')'))) 340Sstevel@tonic-gate *s = '\0'; 35*2393Syz155240 if (ISDIGIT(*str)) { 360Sstevel@tonic-gate if (!ratoi(str, &i, 0, 255)) 370Sstevel@tonic-gate return -1; 380Sstevel@tonic-gate else 390Sstevel@tonic-gate return i; 400Sstevel@tonic-gate } 410Sstevel@tonic-gate len = strlen(str); 420Sstevel@tonic-gate for (i = 0; icmpcodes[i]; i++) 430Sstevel@tonic-gate if (!strncasecmp(str, icmpcodes[i], MIN(len, 440Sstevel@tonic-gate strlen(icmpcodes[i])) )) 450Sstevel@tonic-gate return i; 460Sstevel@tonic-gate return -1; 470Sstevel@tonic-gate } 48