getport.c revision 145519
133965Sjdp/*	$FreeBSD: head/contrib/ipfilter/lib/getport.c 145519 2005-04-25 18:20:15Z darrenr $	*/
233965Sjdp
338889Sjdp#include "ipf.h"
4218822Sdim
5218822Sdimint getport(fr, name, port)
6218822Sdimfrentry_t *fr;
733965Sjdpchar *name;
8218822Sdimu_short *port;
938889Sjdp{
1038889Sjdp	struct protoent *p;
11218822Sdim	struct servent *s;
1238889Sjdp	u_short p1;
1338889Sjdp
1438889Sjdp	if (fr == NULL || fr->fr_type != FR_T_IPF) {
1538889Sjdp		s = getservbyname(name, NULL);
1638889Sjdp		if (s != NULL) {
1738889Sjdp			*port = s->s_port;
1838889Sjdp			return 0;
1938889Sjdp		}
2038889Sjdp		return -1;
2138889Sjdp	}
22218822Sdim
23218822Sdim	if ((fr->fr_flx & FI_TCPUDP) != 0) {
2438889Sjdp		/*
25218822Sdim		 * If a rule is "tcp/udp" then check that both TCP and UDP
26218822Sdim		 * mappings for this protocol name match ports.
2733965Sjdp		 */
2833965Sjdp		s = getservbyname(name, "tcp");
2933965Sjdp		if (s == NULL)
3033965Sjdp			return -1;
3133965Sjdp		p1 = s->s_port;
3233965Sjdp		s = getservbyname(name, "udp");
3333965Sjdp		if (s == NULL || s->s_port != p1)
3433965Sjdp			return -1;
3533965Sjdp		*port = p1;
3633965Sjdp		return 0;
37218822Sdim	}
3833965Sjdp
3933965Sjdp	p = getprotobynumber(fr->fr_proto);
4033965Sjdp	s = getservbyname(name, p ? p->p_name : NULL);
4133965Sjdp	if (s != NULL) {
4233965Sjdp		*port = s->s_port;
4333965Sjdp		return 0;
4433965Sjdp	}
4533965Sjdp	return -1;
4633965Sjdp}
4733965Sjdp