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