xref: /titanic_41/usr/src/cmd/ipf/lib/common/portname.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
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.7 2003/08/14 14:27:43 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 ((opts & OPT_NORESOLVE) == 0) {
19 		if (pr == -1) {
20 			if ((sv = getservbyport(htons(port), "tcp"))) {
21 				strncpy(buf, sv->s_name, sizeof(buf)-1);
22 				buf[sizeof(buf)-1] = '\0';
23 				sv1 = getservbyport(htons(port), "udp");
24 				sv = strncasecmp(buf, sv->s_name, strlen(buf)) ?
25 				     NULL : sv1;
26 			}
27 			if (sv)
28 				return buf;
29 		} else if ((pr != -2) && (p = getprotobynumber(pr))) {
30 			if ((sv = getservbyport(htons(port), p->p_name))) {
31 				strncpy(buf, sv->s_name, sizeof(buf)-1);
32 				buf[sizeof(buf)-1] = '\0';
33 				return buf;
34 			}
35 		}
36 	}
37 
38 	(void) sprintf(buf, "%d", port);
39 	return buf;
40 }
41