xref: /illumos-gate/usr/src/cmd/ipf/lib/common/portname.c (revision ab25eeb5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6*ab25eeb5Syz  * $Id: portname.c,v 1.7 2003/08/14 14:27:43 darrenr Exp $
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate #include "ipf.h"
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate 
portname(pr,port)117c478bd9Sstevel@tonic-gate char	*portname(pr, port)
127c478bd9Sstevel@tonic-gate int	pr, port;
137c478bd9Sstevel@tonic-gate {
147c478bd9Sstevel@tonic-gate 	static	char	buf[32];
157c478bd9Sstevel@tonic-gate 	struct	protoent	*p = NULL;
167c478bd9Sstevel@tonic-gate 	struct	servent	*sv = NULL, *sv1 = NULL;
177c478bd9Sstevel@tonic-gate 
18*ab25eeb5Syz 	if ((opts & OPT_NORESOLVE) == 0) {
19*ab25eeb5Syz 		if (pr == -1) {
20*ab25eeb5Syz 			if ((sv = getservbyport(htons(port), "tcp"))) {
21*ab25eeb5Syz 				strncpy(buf, sv->s_name, sizeof(buf)-1);
22*ab25eeb5Syz 				buf[sizeof(buf)-1] = '\0';
23*ab25eeb5Syz 				sv1 = getservbyport(htons(port), "udp");
24*ab25eeb5Syz 				sv = strncasecmp(buf, sv->s_name, strlen(buf)) ?
25*ab25eeb5Syz 				     NULL : sv1;
26*ab25eeb5Syz 			}
27*ab25eeb5Syz 			if (sv)
28*ab25eeb5Syz 				return buf;
29*ab25eeb5Syz 		} else if ((pr != -2) && (p = getprotobynumber(pr))) {
30*ab25eeb5Syz 			if ((sv = getservbyport(htons(port), p->p_name))) {
31*ab25eeb5Syz 				strncpy(buf, sv->s_name, sizeof(buf)-1);
32*ab25eeb5Syz 				buf[sizeof(buf)-1] = '\0';
33*ab25eeb5Syz 				return buf;
34*ab25eeb5Syz 			}
357c478bd9Sstevel@tonic-gate 		}
367c478bd9Sstevel@tonic-gate 	}
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%d", port);
397c478bd9Sstevel@tonic-gate 	return buf;
407c478bd9Sstevel@tonic-gate }
41