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