xref: /netbsd-src/external/bsd/ipf/dist/lib/getportproto.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: getportproto.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: getportproto.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include <ctype.h>
12bc4097aaSchristos #include "ipf.h"
13bc4097aaSchristos 
getportproto(name,proto)14bc4097aaSchristos int getportproto(name, proto)
15bc4097aaSchristos 	char *name;
16bc4097aaSchristos 	int proto;
17bc4097aaSchristos {
18bc4097aaSchristos 	struct servent *s;
19bc4097aaSchristos 	struct protoent *p;
20bc4097aaSchristos 
21bc4097aaSchristos 	if (ISDIGIT(*name)) {
22bc4097aaSchristos 		int number;
23bc4097aaSchristos 		char *s;
24bc4097aaSchristos 
25bc4097aaSchristos 		for (s = name; *s != '\0'; s++)
26bc4097aaSchristos 			if (!ISDIGIT(*s))
27bc4097aaSchristos 				return -1;
28bc4097aaSchristos 
29bc4097aaSchristos 		number = atoi(name);
30bc4097aaSchristos 		if (number < 0 || number > 65535)
31bc4097aaSchristos 			return -1;
32bc4097aaSchristos 		return htons(number);
33bc4097aaSchristos 	}
34bc4097aaSchristos 
35bc4097aaSchristos 	p = getprotobynumber(proto);
36bc4097aaSchristos 	s = getservbyname(name, p ? p->p_name : NULL);
37bc4097aaSchristos 	if (s != NULL)
38bc4097aaSchristos 		return s->s_port;
39bc4097aaSchristos 	return -1;
40bc4097aaSchristos }
41