]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/portnum.c
This commit was generated by cvs2svn to compensate for changes in r157184,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / portnum.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 1993-2001 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  *
9  * $Id: portnum.c,v 1.6.4.1 2004/12/09 19:41:22 darrenr Exp $
10  */
11
12 #include <ctype.h>
13
14 #include "ipf.h"
15
16
17 /*
18  * find the port number given by the name, either from getservbyname() or
19  * straight atoi(). Return 1 on success, 0 on failure
20  */
21 int     portnum(name, proto, port, linenum)
22 char    *name, *proto;
23 u_short *port;
24 int     linenum;
25 {
26         struct  servent *sp, *sp2;
27         u_short p1 = 0;
28         int i;
29
30         if (ISDIGIT(*name)) {
31                 if (ratoi(name, &i, 0, USHRT_MAX)) {
32                         *port = (u_short)i;
33                         return 1;
34                 }
35                 fprintf(stderr, "%d: unknown port \"%s\"\n", linenum, name);
36                 return 0;
37         }
38         if (proto != NULL && strcasecmp(proto, "tcp/udp") != 0) {
39                 sp = getservbyname(name, proto);
40                 if (sp) {
41                         *port = ntohs(sp->s_port);
42                         return 1;
43                 }
44                 fprintf(stderr, "%d: unknown service \"%s\".\n", linenum, name);
45                 return 0;
46         }
47         sp = getservbyname(name, "tcp");
48         if (sp)
49                 p1 = sp->s_port;
50         sp2 = getservbyname(name, "udp");
51         if (!sp || !sp2) {
52                 fprintf(stderr, "%d: unknown tcp/udp service \"%s\".\n",
53                         linenum, name);
54                 return 0;
55         }
56         if (p1 != sp2->s_port) {
57                 fprintf(stderr, "%d: %s %d/tcp is a different port to ",
58                         linenum, name, p1);
59                 fprintf(stderr, "%d: %s %d/udp\n", linenum, name, sp->s_port);
60                 return 0;
61         }
62         *port = ntohs(p1);
63         return 1;
64 }