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