]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/lib/portname.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / lib / portname.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2000-2003 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id: portname.c,v 1.7.2.1 2006/06/16 17:21:09 darrenr Exp $
9  */
10 #include "ipf.h"
11
12
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, *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) sprintf(buf, "%d", port);
41         return buf;
42 }