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