xref: /titanic_51/usr/src/cmd/ipf/lib/common/getport.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
1*ab25eeb5Syz155240 /*
2*ab25eeb5Syz155240  * Copyright (C) 1993-2005  by Darren Reed.
3*ab25eeb5Syz155240  * See the IPFILTER.LICENCE file for details on licencing.
4*ab25eeb5Syz155240  */
5*ab25eeb5Syz155240 
67c478bd9Sstevel@tonic-gate #include "ipf.h"
77c478bd9Sstevel@tonic-gate 
8*ab25eeb5Syz155240 int getport(fr, name, port)
97c478bd9Sstevel@tonic-gate frentry_t *fr;
107c478bd9Sstevel@tonic-gate char *name;
11*ab25eeb5Syz155240 u_short *port;
127c478bd9Sstevel@tonic-gate {
137c478bd9Sstevel@tonic-gate 	struct protoent *p;
147c478bd9Sstevel@tonic-gate 	struct servent *s;
157c478bd9Sstevel@tonic-gate 	u_short p1;
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate 	if (fr == NULL || fr->fr_type != FR_T_IPF) {
187c478bd9Sstevel@tonic-gate 		s = getservbyname(name, NULL);
19*ab25eeb5Syz155240 		if (s != NULL) {
20*ab25eeb5Syz155240 			*port = s->s_port;
21*ab25eeb5Syz155240 			return 0;
22*ab25eeb5Syz155240 		}
237c478bd9Sstevel@tonic-gate 		return -1;
247c478bd9Sstevel@tonic-gate 	}
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 	if ((fr->fr_flx & FI_TCPUDP) != 0) {
277c478bd9Sstevel@tonic-gate 		/*
287c478bd9Sstevel@tonic-gate 		 * If a rule is "tcp/udp" then check that both TCP and UDP
297c478bd9Sstevel@tonic-gate 		 * mappings for this protocol name match ports.
307c478bd9Sstevel@tonic-gate 		 */
317c478bd9Sstevel@tonic-gate 		s = getservbyname(name, "tcp");
327c478bd9Sstevel@tonic-gate 		if (s == NULL)
337c478bd9Sstevel@tonic-gate 			return -1;
347c478bd9Sstevel@tonic-gate 		p1 = s->s_port;
357c478bd9Sstevel@tonic-gate 		s = getservbyname(name, "udp");
367c478bd9Sstevel@tonic-gate 		if (s == NULL || s->s_port != p1)
377c478bd9Sstevel@tonic-gate 			return -1;
38*ab25eeb5Syz155240 		*port = p1;
39*ab25eeb5Syz155240 		return 0;
407c478bd9Sstevel@tonic-gate 	}
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	p = getprotobynumber(fr->fr_proto);
437c478bd9Sstevel@tonic-gate 	s = getservbyname(name, p ? p->p_name : NULL);
44*ab25eeb5Syz155240 	if (s != NULL) {
45*ab25eeb5Syz155240 		*port = s->s_port;
46*ab25eeb5Syz155240 		return 0;
47*ab25eeb5Syz155240 	}
487c478bd9Sstevel@tonic-gate 	return -1;
497c478bd9Sstevel@tonic-gate }
50