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