1*5bbd2a12Schristos /* $NetBSD: subr.c,v 1.1.1.2 2012/09/09 16:08:11 christos Exp $ */
2b5677b36Schristos
3b5677b36Schristos /*
4b5677b36Schristos * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos *
6b5677b36Schristos * Permission to use, copy, modify, and/or distribute this software for any
7b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
8b5677b36Schristos * copyright notice and this permission notice appear in all copies.
9b5677b36Schristos *
10b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11b5677b36Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12b5677b36Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13b5677b36Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14b5677b36Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15b5677b36Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16b5677b36Schristos * PERFORMANCE OF THIS SOFTWARE.
17b5677b36Schristos */
18b5677b36Schristos
19b5677b36Schristos /*
20b5677b36Schristos * Portions Copyright (c) 1993 by Digital Equipment Corporation.
21b5677b36Schristos *
22b5677b36Schristos * Permission to use, copy, modify, and distribute this software for any
23b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
24b5677b36Schristos * copyright notice and this permission notice appear in all copies, and that
25b5677b36Schristos * the name of Digital Equipment Corporation not be used in advertising or
26b5677b36Schristos * publicity pertaining to distribution of the document or software without
27b5677b36Schristos * specific, written prior permission.
28b5677b36Schristos *
29b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
30b5677b36Schristos * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
31b5677b36Schristos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
32b5677b36Schristos * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
33b5677b36Schristos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
34b5677b36Schristos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
35b5677b36Schristos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
36b5677b36Schristos * SOFTWARE.
37b5677b36Schristos */
38b5677b36Schristos
39b5677b36Schristos #ifndef lint
40b5677b36Schristos static const char sccsid[] = "@(#)subr.c 5.24 (Berkeley) 3/2/91";
41b5677b36Schristos static const char rcsid[] = "Id: subr.c,v 1.3 2009/03/03 23:49:07 tbox Exp ";
42b5677b36Schristos #endif /* not lint */
43b5677b36Schristos
44b5677b36Schristos /*
45b5677b36Schristos *******************************************************************************
46b5677b36Schristos *
47b5677b36Schristos * subr.c --
48b5677b36Schristos *
49b5677b36Schristos * Miscellaneous subroutines for the name server
50b5677b36Schristos * lookup program.
51b5677b36Schristos *
52b5677b36Schristos * Copyright (c) 1985
53b5677b36Schristos * Andrew Cherenson
54b5677b36Schristos * U.C. Berkeley
55b5677b36Schristos * CS298-26 Fall 1985
56b5677b36Schristos *
57b5677b36Schristos *******************************************************************************
58b5677b36Schristos */
59b5677b36Schristos
60b5677b36Schristos #include "port_before.h"
61b5677b36Schristos
62b5677b36Schristos #include <sys/types.h>
63b5677b36Schristos #include <sys/param.h>
64b5677b36Schristos #include <sys/socket.h>
65b5677b36Schristos
66b5677b36Schristos #include <netinet/in.h>
67b5677b36Schristos #include <arpa/nameser.h>
68b5677b36Schristos #include <arpa/inet.h>
69b5677b36Schristos
70b5677b36Schristos #include <ctype.h>
71b5677b36Schristos #include <netdb.h>
72b5677b36Schristos #include <setjmp.h>
73b5677b36Schristos #include <signal.h>
74b5677b36Schristos #include <stdio.h>
75b5677b36Schristos #include <stdlib.h>
76b5677b36Schristos #include <string.h>
77b5677b36Schristos
78b5677b36Schristos #include "port_after.h"
79b5677b36Schristos
80b5677b36Schristos #include "resolv.h"
81b5677b36Schristos #include "res.h"
82b5677b36Schristos
83b5677b36Schristos
84b5677b36Schristos /*
85b5677b36Schristos *******************************************************************************
86b5677b36Schristos *
87b5677b36Schristos * StringToClass --
88b5677b36Schristos *
89b5677b36Schristos * Converts a string form of a query class name to its
90b5677b36Schristos * corresponding integer value.
91b5677b36Schristos *
92b5677b36Schristos *******************************************************************************
93b5677b36Schristos */
94b5677b36Schristos int
StringToClass(class,dflt,errorfile)95b5677b36Schristos StringToClass(class, dflt, errorfile)
96b5677b36Schristos char *class;
97b5677b36Schristos int dflt;
98b5677b36Schristos FILE *errorfile;
99b5677b36Schristos {
100b5677b36Schristos int result, success;
101b5677b36Schristos
102b5677b36Schristos result = sym_ston(__p_class_syms, class, &success);
103b5677b36Schristos if (success)
104b5677b36Schristos return result;
105b5677b36Schristos
106b5677b36Schristos if (errorfile)
107b5677b36Schristos fprintf(errorfile, "unknown query class: %s\n", class);
108b5677b36Schristos return(dflt);
109b5677b36Schristos }
110b5677b36Schristos
111b5677b36Schristos
112b5677b36Schristos /*
113b5677b36Schristos *******************************************************************************
114b5677b36Schristos *
115b5677b36Schristos * StringToType --
116b5677b36Schristos *
117b5677b36Schristos * Converts a string form of a query type name to its
118b5677b36Schristos * corresponding integer value.
119b5677b36Schristos *
120b5677b36Schristos *******************************************************************************
121b5677b36Schristos */
122b5677b36Schristos
123b5677b36Schristos int
StringToType(type,dflt,errorfile)124b5677b36Schristos StringToType(type, dflt, errorfile)
125b5677b36Schristos char *type;
126b5677b36Schristos int dflt;
127b5677b36Schristos FILE *errorfile;
128b5677b36Schristos {
129b5677b36Schristos int result, success;
130b5677b36Schristos
131b5677b36Schristos result = sym_ston(__p_type_syms, type, &success);
132b5677b36Schristos if (success)
133b5677b36Schristos return (result);
134b5677b36Schristos
135b5677b36Schristos if (errorfile)
136b5677b36Schristos fprintf(errorfile, "unknown query type: %s\n", type);
137b5677b36Schristos return (dflt);
138b5677b36Schristos }
139b5677b36Schristos
140b5677b36Schristos /*
141b5677b36Schristos * Skip over leading white space in SRC and then copy the next sequence of
142b5677b36Schristos * non-whitespace characters into DEST. No more than (DEST_SIZE - 1)
143b5677b36Schristos * characters are copied. DEST is always null-terminated. Returns 0 if no
144b5677b36Schristos * characters could be copied into DEST. Returns the number of characters
145b5677b36Schristos * in SRC that were processed (i.e. the count of characters in the leading
146b5677b36Schristos * white space and the first non-whitespace sequence).
147b5677b36Schristos *
148b5677b36Schristos * int i;
149b5677b36Schristos * char *p = " foo bar ", *q;
150b5677b36Schristos * char buf[100];
151b5677b36Schristos *
152b5677b36Schristos * q = p + pickString(p, buf, sizeof buff);
153b5677b36Schristos * assert (strcmp (q, " bar ") == 0) ;
154b5677b36Schristos *
155b5677b36Schristos */
156b5677b36Schristos
157b5677b36Schristos int
pickString(const char * src,char * dest,size_t dest_size)158b5677b36Schristos pickString(const char *src, char *dest, size_t dest_size) {
159b5677b36Schristos const char *start;
160b5677b36Schristos const char *end ;
161b5677b36Schristos size_t sublen ;
162b5677b36Schristos
163b5677b36Schristos if (dest_size == 0 || dest == NULL || src == NULL)
164b5677b36Schristos return 0;
165b5677b36Schristos
166b5677b36Schristos for (start = src ; isspace((unsigned char)*start) ; start++)
167b5677b36Schristos /* nada */ ;
168b5677b36Schristos
169b5677b36Schristos for (end = start ; *end != '\0' && !isspace((unsigned char)*end) ; end++)
170b5677b36Schristos /* nada */ ;
171b5677b36Schristos
172b5677b36Schristos sublen = end - start ;
173b5677b36Schristos
174b5677b36Schristos if (sublen == 0 || sublen > (dest_size - 1))
175b5677b36Schristos return 0;
176b5677b36Schristos
177b5677b36Schristos strncpy (dest, start, sublen);
178b5677b36Schristos
179b5677b36Schristos dest[sublen] = '\0' ;
180b5677b36Schristos
181b5677b36Schristos return (end - src);
182b5677b36Schristos }
183b5677b36Schristos
184b5677b36Schristos
185b5677b36Schristos
186b5677b36Schristos
187b5677b36Schristos /*
188b5677b36Schristos * match the string FORMAT against the string SRC. Leading whitespace in
189b5677b36Schristos * FORMAT will match any amount of (including no) leading whitespace in
190b5677b36Schristos * SRC. Any amount of whitespace inside FORMAT matches any non-zero amount
191b5677b36Schristos * of whitespace in SRC. Value returned is 0 if match didn't occur, or the
192b5677b36Schristos * amount of characters in SRC that did match
193b5677b36Schristos *
194b5677b36Schristos * int i ;
195b5677b36Schristos *
196b5677b36Schristos * i = matchString(" a b c", "a b c") ;
197b5677b36Schristos * assert (i == 5) ;
198b5677b36Schristos * i = matchString("a b c", " a b c");
199b5677b36Schristos * assert (i == 0) ; becasue no leading white space in format
200b5677b36Schristos * i = matchString(" a b c", " a b c");
201b5677b36Schristos * assert(i == 12);
202b5677b36Schristos * i = matchString("aa bb ", "aa bb ddd sd");
203b5677b36Schristos * assert(i == 16);
204b5677b36Schristos */
205b5677b36Schristos int
matchString(const char * format,const char * src)206b5677b36Schristos matchString (const char *format, const char *src) {
207b5677b36Schristos const char *f = format;
208b5677b36Schristos const char *s = src;
209b5677b36Schristos
210b5677b36Schristos if (f == NULL || s == NULL)
211b5677b36Schristos goto notfound;
212b5677b36Schristos
213b5677b36Schristos if (isspace((unsigned char)*f)) {
214b5677b36Schristos while (isspace((unsigned char)*f))
215b5677b36Schristos f++ ;
216b5677b36Schristos while (isspace((unsigned char)*s))
217b5677b36Schristos s++ ;
218b5677b36Schristos }
219b5677b36Schristos
220b5677b36Schristos while (1) {
221b5677b36Schristos if (isspace((unsigned char)*f)) {
222b5677b36Schristos if (!isspace((unsigned char)*s))
223b5677b36Schristos goto notfound;
224b5677b36Schristos while(isspace((unsigned char)*s))
225b5677b36Schristos s++;
226b5677b36Schristos /* any amount of whitespace in the format string
227b5677b36Schristos will match any amount of space in the source
228b5677b36Schristos string. */
229b5677b36Schristos while (isspace((unsigned char)*f))
230b5677b36Schristos f++;
231b5677b36Schristos } else if (*f == '\0') {
232b5677b36Schristos return (s - src);
233b5677b36Schristos } else if (*f != *s) {
234b5677b36Schristos goto notfound;
235b5677b36Schristos } else {
236b5677b36Schristos s++ ;
237b5677b36Schristos f++ ;
238b5677b36Schristos }
239b5677b36Schristos }
240b5677b36Schristos notfound:
241b5677b36Schristos return 0 ;
242b5677b36Schristos }
243