xref: /titanic_50/usr/src/cmd/ipf/lib/common/getportproto.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /*
2  * Copyright (C) 1993-2005  by Darren Reed.
3  * See the IPFILTER.LICENCE file for details on licencing.
4  */
5 
6 #include <ctype.h>
7 #include "ipf.h"
8 
9 int getportproto(name, proto)
10 char *name;
11 int proto;
12 {
13 	struct servent *s;
14 	struct protoent *p;
15 
16 	if (ISDIGIT(*name)) {
17 		int number;
18 		char *s;
19 
20 		for (s = name; *s != '\0'; s++)
21 			if (!ISDIGIT(*s))
22 				return -1;
23 
24 		number = atoi(name);
25 		if (number < 0 || number > 65535)
26 			return -1;
27 		return htons(number);
28 	}
29 
30 	p = getprotobynumber(proto);
31 	s = getservbyname(name, p ? p->p_name : NULL);
32 	if (s != NULL)
33 		return s->s_port;
34 	return -1;
35 }
36