1*ab25eeb5Syz /*
2*ab25eeb5Syz  * Copyright (C) 1993-2005  by Darren Reed.
3*ab25eeb5Syz  * See the IPFILTER.LICENCE file for details on licencing.
4*ab25eeb5Syz  */
5*ab25eeb5Syz 
67c478bd9Sstevel@tonic-gate #include <ctype.h>
77c478bd9Sstevel@tonic-gate #include "ipf.h"
87c478bd9Sstevel@tonic-gate 
getportproto(name,proto)97c478bd9Sstevel@tonic-gate int getportproto(name, proto)
107c478bd9Sstevel@tonic-gate char *name;
117c478bd9Sstevel@tonic-gate int proto;
127c478bd9Sstevel@tonic-gate {
137c478bd9Sstevel@tonic-gate 	struct servent *s;
147c478bd9Sstevel@tonic-gate 	struct protoent *p;
157c478bd9Sstevel@tonic-gate 
16*ab25eeb5Syz 	if (ISDIGIT(*name)) {
17*ab25eeb5Syz 		int number;
18*ab25eeb5Syz 		char *s;
197c478bd9Sstevel@tonic-gate 
20*ab25eeb5Syz 		for (s = name; *s != '\0'; s++)
21*ab25eeb5Syz 			if (!ISDIGIT(*s))
22*ab25eeb5Syz 				return -1;
23*ab25eeb5Syz 
24*ab25eeb5Syz 		number = atoi(name);
25*ab25eeb5Syz 		if (number < 0 || number > 65535)
26*ab25eeb5Syz 			return -1;
27*ab25eeb5Syz 		return htons(number);
287c478bd9Sstevel@tonic-gate 	}
29*ab25eeb5Syz 
30*ab25eeb5Syz 	p = getprotobynumber(proto);
31*ab25eeb5Syz 	s = getservbyname(name, p ? p->p_name : NULL);
32*ab25eeb5Syz 	if (s != NULL)
33*ab25eeb5Syz 		return s->s_port;
34*ab25eeb5Syz 	return -1;
357c478bd9Sstevel@tonic-gate }
36