1 /*        $NetBSD: getportproto.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $       */
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * Id: getportproto.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9  */
10 
11 #include <ctype.h>
12 #include "ipf.h"
13 
getportproto(name,proto)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