xref: /freebsd/sbin/ipf/libipf/portname.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id$
9  */
10 #include "ipf.h"
11 
12 
13 char *
14 portname(int pr, int 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) snprintf(buf, sizeof(buf), "%d", port);
42 	return (buf);
43 }
44