]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/portname.c
Import IPFilter 4.1.23 to vendor branch.
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / portname.c
1 /*
2  * Copyright (C) 2000-2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: portname.c,v 1.7.2.1 2006/06/16 17:21:09 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 }