xref: /onnv-gate/usr/src/cmd/ipf/lib/common/portnum.c (revision 2393:76e0289ce525)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  *
7*2393Syz155240  * $Id: portnum.c,v 1.6.4.1 2004/12/09 19:41:22 darrenr Exp $
80Sstevel@tonic-gate  */
90Sstevel@tonic-gate 
100Sstevel@tonic-gate #include <ctype.h>
110Sstevel@tonic-gate 
120Sstevel@tonic-gate #include "ipf.h"
130Sstevel@tonic-gate 
140Sstevel@tonic-gate 
150Sstevel@tonic-gate /*
160Sstevel@tonic-gate  * find the port number given by the name, either from getservbyname() or
170Sstevel@tonic-gate  * straight atoi(). Return 1 on success, 0 on failure
180Sstevel@tonic-gate  */
portnum(name,proto,port,linenum)190Sstevel@tonic-gate int	portnum(name, proto, port, linenum)
200Sstevel@tonic-gate char	*name, *proto;
210Sstevel@tonic-gate u_short	*port;
220Sstevel@tonic-gate int     linenum;
230Sstevel@tonic-gate {
240Sstevel@tonic-gate 	struct	servent	*sp, *sp2;
250Sstevel@tonic-gate 	u_short	p1 = 0;
260Sstevel@tonic-gate 	int i;
270Sstevel@tonic-gate 
28*2393Syz155240 	if (ISDIGIT(*name)) {
290Sstevel@tonic-gate 		if (ratoi(name, &i, 0, USHRT_MAX)) {
300Sstevel@tonic-gate 			*port = (u_short)i;
310Sstevel@tonic-gate 			return 1;
320Sstevel@tonic-gate 		}
330Sstevel@tonic-gate 		fprintf(stderr, "%d: unknown port \"%s\"\n", linenum, name);
340Sstevel@tonic-gate 		return 0;
350Sstevel@tonic-gate 	}
360Sstevel@tonic-gate 	if (proto != NULL && strcasecmp(proto, "tcp/udp") != 0) {
370Sstevel@tonic-gate 		sp = getservbyname(name, proto);
380Sstevel@tonic-gate 		if (sp) {
390Sstevel@tonic-gate 			*port = ntohs(sp->s_port);
400Sstevel@tonic-gate 			return 1;
410Sstevel@tonic-gate 		}
420Sstevel@tonic-gate 		fprintf(stderr, "%d: unknown service \"%s\".\n", linenum, name);
430Sstevel@tonic-gate 		return 0;
440Sstevel@tonic-gate 	}
450Sstevel@tonic-gate 	sp = getservbyname(name, "tcp");
460Sstevel@tonic-gate 	if (sp)
470Sstevel@tonic-gate 		p1 = sp->s_port;
480Sstevel@tonic-gate 	sp2 = getservbyname(name, "udp");
490Sstevel@tonic-gate 	if (!sp || !sp2) {
500Sstevel@tonic-gate 		fprintf(stderr, "%d: unknown tcp/udp service \"%s\".\n",
510Sstevel@tonic-gate 			linenum, name);
520Sstevel@tonic-gate 		return 0;
530Sstevel@tonic-gate 	}
540Sstevel@tonic-gate 	if (p1 != sp2->s_port) {
550Sstevel@tonic-gate 		fprintf(stderr, "%d: %s %d/tcp is a different port to ",
560Sstevel@tonic-gate 			linenum, name, p1);
570Sstevel@tonic-gate 		fprintf(stderr, "%d: %s %d/udp\n", linenum, name, sp->s_port);
580Sstevel@tonic-gate 		return 0;
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 	*port = ntohs(p1);
610Sstevel@tonic-gate 	return 1;
620Sstevel@tonic-gate }
63