xref: /titanic_50/usr/src/cmd/ipf/lib/common/portnum.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  *
7*ab25eeb5Syz155240  * $Id: portnum.c,v 1.6.4.1 2004/12/09 19:41:22 darrenr Exp $
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include <ctype.h>
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "ipf.h"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate /*
167c478bd9Sstevel@tonic-gate  * find the port number given by the name, either from getservbyname() or
177c478bd9Sstevel@tonic-gate  * straight atoi(). Return 1 on success, 0 on failure
187c478bd9Sstevel@tonic-gate  */
portnum(name,proto,port,linenum)197c478bd9Sstevel@tonic-gate int	portnum(name, proto, port, linenum)
207c478bd9Sstevel@tonic-gate char	*name, *proto;
217c478bd9Sstevel@tonic-gate u_short	*port;
227c478bd9Sstevel@tonic-gate int     linenum;
237c478bd9Sstevel@tonic-gate {
247c478bd9Sstevel@tonic-gate 	struct	servent	*sp, *sp2;
257c478bd9Sstevel@tonic-gate 	u_short	p1 = 0;
267c478bd9Sstevel@tonic-gate 	int i;
277c478bd9Sstevel@tonic-gate 
28*ab25eeb5Syz155240 	if (ISDIGIT(*name)) {
297c478bd9Sstevel@tonic-gate 		if (ratoi(name, &i, 0, USHRT_MAX)) {
307c478bd9Sstevel@tonic-gate 			*port = (u_short)i;
317c478bd9Sstevel@tonic-gate 			return 1;
327c478bd9Sstevel@tonic-gate 		}
337c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: unknown port \"%s\"\n", linenum, name);
347c478bd9Sstevel@tonic-gate 		return 0;
357c478bd9Sstevel@tonic-gate 	}
367c478bd9Sstevel@tonic-gate 	if (proto != NULL && strcasecmp(proto, "tcp/udp") != 0) {
377c478bd9Sstevel@tonic-gate 		sp = getservbyname(name, proto);
387c478bd9Sstevel@tonic-gate 		if (sp) {
397c478bd9Sstevel@tonic-gate 			*port = ntohs(sp->s_port);
407c478bd9Sstevel@tonic-gate 			return 1;
417c478bd9Sstevel@tonic-gate 		}
427c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: unknown service \"%s\".\n", linenum, name);
437c478bd9Sstevel@tonic-gate 		return 0;
447c478bd9Sstevel@tonic-gate 	}
457c478bd9Sstevel@tonic-gate 	sp = getservbyname(name, "tcp");
467c478bd9Sstevel@tonic-gate 	if (sp)
477c478bd9Sstevel@tonic-gate 		p1 = sp->s_port;
487c478bd9Sstevel@tonic-gate 	sp2 = getservbyname(name, "udp");
497c478bd9Sstevel@tonic-gate 	if (!sp || !sp2) {
507c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: unknown tcp/udp service \"%s\".\n",
517c478bd9Sstevel@tonic-gate 			linenum, name);
527c478bd9Sstevel@tonic-gate 		return 0;
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate 	if (p1 != sp2->s_port) {
557c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: %s %d/tcp is a different port to ",
567c478bd9Sstevel@tonic-gate 			linenum, name, p1);
577c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: %s %d/udp\n", linenum, name, sp->s_port);
587c478bd9Sstevel@tonic-gate 		return 0;
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 	*port = ntohs(p1);
617c478bd9Sstevel@tonic-gate 	return 1;
627c478bd9Sstevel@tonic-gate }
63