xref: /freebsd/sbin/ipf/libipf/portname.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 #include "ipf.h"
10 
11 
12 char *
13 portname(int pr, int port)
14 {
15 	static char buf[32];
16 	struct protoent *p = NULL;
17 	struct servent *sv = NULL;
18 	struct servent *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) snprintf(buf, sizeof(buf), "%d", port);
41 	return (buf);
42 }
43