Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / cmd / ipf / lib / common / portnum.c
blob4079f464c21f6a3292f9268b01ad6483fb13797d
1 /*
2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 * $Id: portnum.c,v 1.6.4.1 2004/12/09 19:41:22 darrenr Exp $
8 */
10 #include <ctype.h>
12 #include "ipf.h"
16 * find the port number given by the name, either from getservbyname() or
17 * straight atoi(). Return 1 on success, 0 on failure
19 int portnum(name, proto, port, linenum)
20 char *name, *proto;
21 u_short *port;
22 int linenum;
24 struct servent *sp, *sp2;
25 u_short p1 = 0;
26 int i;
28 if (ISDIGIT(*name)) {
29 if (ratoi(name, &i, 0, USHRT_MAX)) {
30 *port = (u_short)i;
31 return 1;
33 fprintf(stderr, "%d: unknown port \"%s\"\n", linenum, name);
34 return 0;
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;
42 fprintf(stderr, "%d: unknown service \"%s\".\n", linenum, name);
43 return 0;
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;
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;
60 *port = ntohs(p1);
61 return 1;