1*ce099b40Smartin /* $NetBSD: getport.c,v 1.2 2008/04/28 20:24:14 martin Exp $ */
2d9d5b792Schristos
3d9d5b792Schristos /*-
4d9d5b792Schristos * Copyright (c) 2004 The NetBSD Foundation, Inc.
5d9d5b792Schristos * All rights reserved.
6d9d5b792Schristos *
7d9d5b792Schristos * This code is derived from software contributed to The NetBSD Foundation
8d9d5b792Schristos * by Christos Zoulas.
9d9d5b792Schristos *
10d9d5b792Schristos * Redistribution and use in source and binary forms, with or without
11d9d5b792Schristos * modification, are permitted provided that the following conditions
12d9d5b792Schristos * are met:
13d9d5b792Schristos * 1. Redistributions of source code must retain the above copyright
14d9d5b792Schristos * notice, this list of conditions and the following disclaimer.
15d9d5b792Schristos * 2. Redistributions in binary form must reproduce the above copyright
16d9d5b792Schristos * notice, this list of conditions and the following disclaimer in the
17d9d5b792Schristos * documentation and/or other materials provided with the distribution.
18d9d5b792Schristos *
19d9d5b792Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d9d5b792Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d9d5b792Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d9d5b792Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d9d5b792Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d9d5b792Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d9d5b792Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d9d5b792Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d9d5b792Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d9d5b792Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d9d5b792Schristos * POSSIBILITY OF SUCH DAMAGE.
30d9d5b792Schristos */
31d9d5b792Schristos
32d9d5b792Schristos #include <sys/cdefs.h>
33*ce099b40Smartin __RCSID("$NetBSD: getport.c,v 1.2 2008/04/28 20:24:14 martin Exp $");
34d9d5b792Schristos
35d9d5b792Schristos #include <stdlib.h>
36d9d5b792Schristos #include <err.h>
37d9d5b792Schristos #include <netdb.h>
38d9d5b792Schristos #include <limits.h>
39d9d5b792Schristos #include <errno.h>
40d9d5b792Schristos #include <netinet/in.h>
41d9d5b792Schristos
42d9d5b792Schristos #include "getport.h"
43d9d5b792Schristos
44d9d5b792Schristos struct servent *
getport(const char * service,const char * protocol)45d9d5b792Schristos getport(const char *service, const char *protocol)
46d9d5b792Schristos {
47d9d5b792Schristos long port;
48d9d5b792Schristos char *ep;
49d9d5b792Schristos struct servent *sp = getservbyname(service, protocol);
50d9d5b792Schristos if (sp != NULL)
51d9d5b792Schristos return sp;
52d9d5b792Schristos
53d9d5b792Schristos if ((sp = calloc(1, sizeof(*sp))) == NULL)
54d9d5b792Schristos err(1, "malloc");
55d9d5b792Schristos sp->s_name = __UNCONST(service);
56d9d5b792Schristos sp->s_proto = __UNCONST(protocol);
57d9d5b792Schristos port = strtol(service, &ep, 0);
58d9d5b792Schristos if ((service[0] != '\0' && *ep != '\0') ||
59d9d5b792Schristos (errno == ERANGE &&
60d9d5b792Schristos (port == LONG_MAX || port == LONG_MIN)) ||
61d9d5b792Schristos (port <= 0 || port >= IPPORT_ANONMAX))
62d9d5b792Schristos errx(1,"port must be between 1 and %d",
63d9d5b792Schristos IPPORT_ANONMAX);
64d9d5b792Schristos sp->s_port = htons((uint16_t)port);
65d9d5b792Schristos return sp;
66d9d5b792Schristos }
67