xref: /netbsd-src/external/bsd/ipf/dist/lib/getproto.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: getproto.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: getproto.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos #include <ctype.h>
13bc4097aaSchristos 
getproto(name)14bc4097aaSchristos int getproto(name)
15bc4097aaSchristos 	char *name;
16bc4097aaSchristos {
17bc4097aaSchristos 	struct protoent *p;
18bc4097aaSchristos 	char *s;
19bc4097aaSchristos 
20bc4097aaSchristos 	for (s = name; *s != '\0'; s++)
21bc4097aaSchristos 		if (!ISDIGIT(*s))
22bc4097aaSchristos 			break;
23bc4097aaSchristos 	if (*s == '\0')
24bc4097aaSchristos 		return atoi(name);
25bc4097aaSchristos 
26bc4097aaSchristos #ifdef _AIX51
27bc4097aaSchristos 	/*
28bc4097aaSchristos 	 * For some bogus reason, "ip" is 252 in /etc/protocols on AIX 5
29bc4097aaSchristos 	 * The IANA has doubled up on the definition of 0 - it is now also
30bc4097aaSchristos 	 * used for IPv6 hop-opts, so we can no longer rely on /etc/protocols
31bc4097aaSchristos 	 * providing the correct name->number mapping
32bc4097aaSchristos 	 */
33bc4097aaSchristos #endif
34bc4097aaSchristos 	if (!strcasecmp(name, "ip"))
35bc4097aaSchristos 		return 0;
36bc4097aaSchristos 
37bc4097aaSchristos 	p = getprotobyname(name);
38bc4097aaSchristos 	if (p != NULL)
39bc4097aaSchristos 		return p->p_proto;
40bc4097aaSchristos 	return -1;
41bc4097aaSchristos }
42