portname.c revision 153881
1/*	$FreeBSD: head/contrib/ipfilter/lib/portname.c 153881 2005-12-30 11:52:26Z guido $	*/
2
3/*
4 * Copyright (C) 1993-2001 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 *
8 * $Id: portname.c,v 1.7 2003/08/14 14:27:43 darrenr Exp $
9 */
10#include "ipf.h"
11
12
13char	*portname(pr, port)
14int	pr, port;
15{
16	static	char	buf[32];
17	struct	protoent	*p = NULL;
18	struct	servent	*sv = NULL, *sv1 = NULL;
19
20	if ((opts & OPT_NORESOLVE) == 0) {
21		if (pr == -1) {
22			if ((sv = getservbyport(htons(port), "tcp"))) {
23				strncpy(buf, sv->s_name, sizeof(buf)-1);
24				buf[sizeof(buf)-1] = '\0';
25				sv1 = getservbyport(htons(port), "udp");
26				sv = strncasecmp(buf, sv->s_name, strlen(buf)) ?
27				     NULL : sv1;
28			}
29			if (sv)
30				return buf;
31		} else if ((pr != -2) && (p = getprotobynumber(pr))) {
32			if ((sv = getservbyport(htons(port), p->p_name))) {
33				strncpy(buf, sv->s_name, sizeof(buf)-1);
34				buf[sizeof(buf)-1] = '\0';
35				return buf;
36			}
37		}
38	}
39
40	(void) sprintf(buf, "%d", port);
41	return buf;
42}
43