xref: /freebsd/sbin/ipf/libipf/getportproto.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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
15 getportproto(char *name, int proto)
16 {
17 	struct servent *s;
18 	struct protoent *p;
19 
20 	if (ISDIGIT(*name)) {
21 		int number;
22 		char *s;
23 
24 		for (s = name; *s != '\0'; s++)
25 			if (!ISDIGIT(*s))
26 				return (-1);
27 
28 		number = atoi(name);
29 		if (number < 0 || number > 65535)
30 			return (-1);
31 		return (htons(number));
32 	}
33 
34 	p = getprotobynumber(proto);
35 	s = getservbyname(name, p ? p->p_name : NULL);
36 	if (s != NULL)
37 		return (s->s_port);
38 	return (-1);
39 }
40