xref: /titanic_51/usr/src/cmd/ipf/lib/common/portname.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: portname.c,v 1.6 2002/01/28 06:50:47 darrenr Exp $
7  */
8 #include "ipf.h"
9 
10 
11 char	*portname(pr, port)
12 int	pr, port;
13 {
14 	static	char	buf[32];
15 	struct	protoent	*p = NULL;
16 	struct	servent	*sv = NULL, *sv1 = NULL;
17 
18 	if (pr == -1) {
19 		if ((sv = getservbyport(htons(port), "tcp"))) {
20 			strncpy(buf, sv->s_name, sizeof(buf)-1);
21 			buf[sizeof(buf)-1] = '\0';
22 			sv1 = getservbyport(htons(port), "udp");
23 			sv = strncasecmp(buf, sv->s_name, strlen(buf)) ?
24 			     NULL : sv1;
25 		}
26 		if (sv)
27 			return buf;
28 	} else if ((pr != -2) && (p = getprotobynumber(pr))) {
29 		if ((sv = getservbyport(htons(port), p->p_name))) {
30 			strncpy(buf, sv->s_name, sizeof(buf)-1);
31 			buf[sizeof(buf)-1] = '\0';
32 			return buf;
33 		}
34 	}
35 
36 	(void) sprintf(buf, "%d", port);
37 	return buf;
38 }
39