xref: /titanic_41/usr/src/cmd/ipf/lib/common/portname.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
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*ab25eeb5Syz155240  * $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*ab25eeb5Syz155240 	if ((opts & OPT_NORESOLVE) == 0) {
197c478bd9Sstevel@tonic-gate 		if (pr == -1) {
207c478bd9Sstevel@tonic-gate 			if ((sv = getservbyport(htons(port), "tcp"))) {
217c478bd9Sstevel@tonic-gate 				strncpy(buf, sv->s_name, sizeof(buf)-1);
227c478bd9Sstevel@tonic-gate 				buf[sizeof(buf)-1] = '\0';
237c478bd9Sstevel@tonic-gate 				sv1 = getservbyport(htons(port), "udp");
247c478bd9Sstevel@tonic-gate 				sv = strncasecmp(buf, sv->s_name, strlen(buf)) ?
257c478bd9Sstevel@tonic-gate 				     NULL : sv1;
267c478bd9Sstevel@tonic-gate 			}
277c478bd9Sstevel@tonic-gate 			if (sv)
287c478bd9Sstevel@tonic-gate 				return buf;
297c478bd9Sstevel@tonic-gate 		} else if ((pr != -2) && (p = getprotobynumber(pr))) {
307c478bd9Sstevel@tonic-gate 			if ((sv = getservbyport(htons(port), p->p_name))) {
317c478bd9Sstevel@tonic-gate 				strncpy(buf, sv->s_name, sizeof(buf)-1);
327c478bd9Sstevel@tonic-gate 				buf[sizeof(buf)-1] = '\0';
337c478bd9Sstevel@tonic-gate 				return buf;
347c478bd9Sstevel@tonic-gate 			}
357c478bd9Sstevel@tonic-gate 		}
36*ab25eeb5Syz155240 	}
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%d", port);
397c478bd9Sstevel@tonic-gate 	return buf;
407c478bd9Sstevel@tonic-gate }
41