17030SN/A/*	$NetBSD: getport.c,v 1.1 2004/10/16 02:03:54 christos Exp $	*/
214349Stvaleev
37030SN/A/*-
47030SN/A * Copyright (c) 2004 The NetBSD Foundation, Inc.
57030SN/A * All rights reserved.
67030SN/A *
77030SN/A * This code is derived from software contributed to The NetBSD Foundation
87030SN/A * by Christos Zoulas.
97030SN/A *
107030SN/A * Redistribution and use in source and binary forms, with or without
117030SN/A * modification, are permitted provided that the following conditions
127030SN/A * are met:
137030SN/A * 1. Redistributions of source code must retain the above copyright
147030SN/A *    notice, this list of conditions and the following disclaimer.
157030SN/A * 2. Redistributions in binary form must reproduce the above copyright
167030SN/A *    notice, this list of conditions and the following disclaimer in the
177030SN/A *    documentation and/or other materials provided with the distribution.
187030SN/A *
197030SN/A * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
207030SN/A * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
217030SN/A * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
227030SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
237030SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
247030SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257030SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
267030SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
277030SN/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287030SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
297030SN/A * POSSIBILITY OF SUCH DAMAGE.
307030SN/A */
317030SN/A
327030SN/A#include <sys/cdefs.h>
337030SN/A__RCSID("$NetBSD: getport.c,v 1.1 2004/10/16 02:03:54 christos Exp $");
347030SN/A
357030SN/A#include <stdlib.h>
367030SN/A#include <err.h>
377030SN/A#include <netdb.h>
387030SN/A#include <limits.h>
397030SN/A#include <errno.h>
407030SN/A#include <netinet/in.h>
417030SN/A
427030SN/A#include "getport.h"
437030SN/A
447030SN/Astruct servent *
457030SN/Agetport(const char *service, const char *protocol)
467030SN/A{
477030SN/A	long port;
487030SN/A	char *ep;
497030SN/A	struct servent *sp = getservbyname(service, protocol);
507030SN/A	if (sp != NULL)
517030SN/A		return sp;
527030SN/A
537030SN/A	if ((sp = calloc(1, sizeof(*sp))) == NULL)
547030SN/A		err(1, "malloc");
557030SN/A	sp->s_name = __UNCONST(service);
567030SN/A	sp->s_proto = __UNCONST(protocol);
577030SN/A	port = strtol(service, &ep, 0);
587030SN/A	if ((service[0] != '\0' && *ep != '\0') ||
597030SN/A	    (errno == ERANGE &&
607030SN/A	    (port == LONG_MAX || port == LONG_MIN)) ||
617030SN/A	    (port <= 0 || port >= IPPORT_ANONMAX))
627030SN/A		errx(1,"port must be between 1 and %d",
637030SN/A		    IPPORT_ANONMAX);
647030SN/A	sp->s_port = htons((uint16_t)port);
657030SN/A	return sp;
667030SN/A}
677030SN/A