support.c revision 1.1 1 /* $NetBSD: support.c,v 1.1 2025/02/06 19:35:28 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: support.c,v 1.1 2025/02/06 19:35:28 christos Exp $");
34
35 #include <assert.h>
36 #include <limits.h>
37 #include <errno.h>
38 #include <netdb.h>
39 #include <stdbool.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "tables.h"
46 #include "support.h"
47
48 static bool
49 parse_numeric(const char *string, int *rv)
50 {
51 char *end;
52 long value;
53
54 errno = 0;
55 value = strtol(string, &end, 0);
56 if ((string[0] == '\0') || (*end != '\0'))
57 return false;
58 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
59 return false;
60 if ((value > INT_MAX) || (value < INT_MIN))
61 return false;
62 *rv = (int)value;
63 return true;
64 }
65
66 bool
67 parse_af(const char *string, int *afp)
68 {
69
70 return parse_numeric_tabular(string, afp, address_families,
71 __arraycount(address_families));
72 }
73
74 bool
75 parse_protocol(const char *string, int *protop)
76 {
77 struct protoent *protoent;
78
79 if (parse_numeric(string, protop))
80 return true;
81
82 protoent = getprotobyname(string);
83 if (protoent == NULL)
84 return false;
85
86 *protop = protoent->p_proto;
87 return true;
88 }
89
90 bool
91 parse_socktype(const char *string, int *typep)
92 {
93
94 return parse_numeric_tabular(string, typep, socket_types,
95 __arraycount(socket_types));
96 }
97
98 bool
99 parse_numeric_tabular(const char *string, int *valuep,
100 const char *const *table, size_t n)
101 {
102
103 assert((uintmax_t)n <= (uintmax_t)INT_MAX);
104
105 if (parse_numeric(string, valuep))
106 return true;
107
108 for (size_t i = 0; i < n; i++)
109 if ((table[i] != NULL) && (strcmp(string, table[i]) == 0)) {
110 *valuep = (int)i;
111 return true;
112 }
113 return false;
114 }
115