Sun Nov 8 00:35:21 2015 UTC ()
Pull up following revision(s) (requested by christos in ticket #1012):
	lib/libc/net/getnetnamadr.c: revision 1.43
	lib/libc/net/getnetnamadr.c: revision 1.44
	external/gpl3/binutils/dist/binutils/arsup.c: revision 1.2
put the state back after it is used.
PR/50367: Stefan Schaeckeler: Apply fix to obey RES_CHECKNAME to getnetbyaddr
and getnetbyname.
use asprintf(3)


(riz)
diff -r1.1.1.2 -r1.1.1.2.22.1 src/external/gpl3/binutils/dist/binutils/arsup.c
diff -r1.42 -r1.42.12.1 src/lib/libc/net/getnetnamadr.c

cvs diff -r1.1.1.2 -r1.1.1.2.22.1 src/external/gpl3/binutils/dist/binutils/arsup.c (switch to unified diff)

--- src/external/gpl3/binutils/dist/binutils/arsup.c 2011/09/24 22:39:13 1.1.1.2
+++ src/external/gpl3/binutils/dist/binutils/arsup.c 2015/11/08 00:35:21 1.1.1.2.22.1
@@ -1,481 +1,488 @@ @@ -1,481 +1,488 @@
1/* arsup.c - Archive support for MRI compatibility 1/* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2 Copyright 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2007, 2008 Free Software Foundation, Inc. 3 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
4 4
5 This file is part of GNU Binutils. 5 This file is part of GNU Binutils.
6 6
7 This program is free software; you can redistribute it and/or modify 7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or 9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version. 10 (at your option) any later version.
11 11
12 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */ 20 MA 02110-1301, USA. */
21 21
22 22
23/* Contributed by Steve Chamberlain 23/* Contributed by Steve Chamberlain
24 sac@cygnus.com 24 sac@cygnus.com
25 25
26 This file looks after requests from arparse.y, to provide the MRI 26 This file looks after requests from arparse.y, to provide the MRI
27 style librarian command syntax + 1 word LIST. */ 27 style librarian command syntax + 1 word LIST. */
28 28
29#include "sysdep.h" 29#include "sysdep.h"
30#include "bfd.h" 30#include "bfd.h"
31#include "libiberty.h" 31#include "libiberty.h"
32#include "filenames.h" 32#include "filenames.h"
33#include "bucomm.h" 33#include "bucomm.h"
34#include "arsup.h" 34#include "arsup.h"
35 35
36static void map_over_list 36static void map_over_list
37 (bfd *, void (*function) (bfd *, bfd *), struct list *); 37 (bfd *, void (*function) (bfd *, bfd *), struct list *);
38static void ar_directory_doer (bfd *, bfd *); 38static void ar_directory_doer (bfd *, bfd *);
39static void ar_addlib_doer (bfd *, bfd *); 39static void ar_addlib_doer (bfd *, bfd *);
40 40
41extern int verbose; 41extern int verbose;
42 42
43static bfd *obfd; 43static bfd *obfd;
44static char *real_name; 44static char *real_name;
45static FILE *outfile; 45static FILE *outfile;
46 46
47static void 47static void
48map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list) 48map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
49{ 49{
50 bfd *head; 50 bfd *head;
51 51
52 if (list == NULL) 52 if (list == NULL)
53 { 53 {
54 bfd *next; 54 bfd *next;
55 55
56 head = arch->archive_next; 56 head = arch->archive_next;
57 while (head != NULL) 57 while (head != NULL)
58 { 58 {
59 next = head->archive_next; 59 next = head->archive_next;
60 function (head, (bfd *) NULL); 60 function (head, (bfd *) NULL);
61 head = next; 61 head = next;
62 } 62 }
63 } 63 }
64 else 64 else
65 { 65 {
66 struct list *ptr; 66 struct list *ptr;
67 67
68 /* This may appear to be a baroque way of accomplishing what we 68 /* This may appear to be a baroque way of accomplishing what we
69 want. however we have to iterate over the filenames in order 69 want. however we have to iterate over the filenames in order
70 to notice where a filename is requested but does not exist in 70 to notice where a filename is requested but does not exist in
71 the archive. Ditto mapping over each file each time -- we 71 the archive. Ditto mapping over each file each time -- we
72 want to hack multiple references. */ 72 want to hack multiple references. */
73 for (ptr = list; ptr; ptr = ptr->next) 73 for (ptr = list; ptr; ptr = ptr->next)
74 { 74 {
75 bfd_boolean found = FALSE; 75 bfd_boolean found = FALSE;
76 bfd *prev = arch; 76 bfd *prev = arch;
77 77
78 for (head = arch->archive_next; head; head = head->archive_next) 78 for (head = arch->archive_next; head; head = head->archive_next)
79 { 79 {
80 if (head->filename != NULL 80 if (head->filename != NULL
81 && FILENAME_CMP (ptr->name, head->filename) == 0) 81 && FILENAME_CMP (ptr->name, head->filename) == 0)
82 { 82 {
83 found = TRUE; 83 found = TRUE;
84 function (head, prev); 84 function (head, prev);
85 } 85 }
86 prev = head; 86 prev = head;
87 } 87 }
88 if (! found) 88 if (! found)
89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name); 89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
90 } 90 }
91 } 91 }
92} 92}
93 93
94 94
95 95
96static void 96static void
97ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED) 97ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
98{ 98{
99 print_arelt_descr(outfile, abfd, verbose); 99 print_arelt_descr(outfile, abfd, verbose);
100} 100}
101 101
102void 102void
103ar_directory (char *ar_name, struct list *list, char *output) 103ar_directory (char *ar_name, struct list *list, char *output)
104{ 104{
105 bfd *arch; 105 bfd *arch;
106 106
107 arch = open_inarch (ar_name, (char *) NULL); 107 arch = open_inarch (ar_name, (char *) NULL);
108 if (output) 108 if (output)
109 { 109 {
110 outfile = fopen(output,"w"); 110 outfile = fopen(output,"w");
111 if (outfile == 0) 111 if (outfile == 0)
112 { 112 {
113 outfile = stdout; 113 outfile = stdout;
114 fprintf (stderr,_("Can't open file %s\n"), output); 114 fprintf (stderr,_("Can't open file %s\n"), output);
115 output = 0; 115 output = 0;
116 } 116 }
117 } 117 }
118 else 118 else
119 outfile = stdout; 119 outfile = stdout;
120 120
121 map_over_list (arch, ar_directory_doer, list); 121 map_over_list (arch, ar_directory_doer, list);
122 122
123 bfd_close (arch); 123 bfd_close (arch);
124 124
125 if (output) 125 if (output)
126 fclose (outfile); 126 fclose (outfile);
127} 127}
128 128
129void 129void
130prompt (void) 130prompt (void)
131{ 131{
132 extern int interactive; 132 extern int interactive;
133 133
134 if (interactive) 134 if (interactive)
135 { 135 {
136 printf ("AR >"); 136 printf ("AR >");
137 fflush (stdout); 137 fflush (stdout);
138 } 138 }
139} 139}
140 140
141void 141void
142maybequit (void) 142maybequit (void)
143{ 143{
144 if (! interactive) 144 if (! interactive)
145 xexit (9); 145 xexit (9);
146} 146}
147 147
148 148
149void 149void
150ar_open (char *name, int t) 150ar_open (char *name, int t)
151{ 151{
152 char *tname = (char *) xmalloc (strlen (name) + 10); 152 char *tname;
153 const char *bname = lbasename (name); 153 const char *bname = lbasename (name);
154 real_name = name; 154 real_name = name;
155 155
156 /* Prepend tmp- to the beginning, to avoid file-name clashes after 156 /* Prepend tmp- to the beginning, to avoid file-name clashes after
157 truncation on filesystems with limited namespaces (DOS). */ 157 truncation on filesystems with limited namespaces (DOS). */
158 sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname); 158 if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
 159 {
 160 fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
 161 program_name, strerror(errno));
 162 maybequit ();
 163 return;
 164 }
 165
159 obfd = bfd_openw (tname, NULL); 166 obfd = bfd_openw (tname, NULL);
160 167
161 if (!obfd) 168 if (!obfd)
162 { 169 {
163 fprintf (stderr, 170 fprintf (stderr,
164 _("%s: Can't open output archive %s\n"), 171 _("%s: Can't open output archive %s\n"),
165 program_name, tname); 172 program_name, tname);
166 173
167 maybequit (); 174 maybequit ();
168 } 175 }
169 else 176 else
170 { 177 {
171 if (!t) 178 if (!t)
172 { 179 {
173 bfd **ptr; 180 bfd **ptr;
174 bfd *element; 181 bfd *element;
175 bfd *ibfd; 182 bfd *ibfd;
176 183
177 ibfd = bfd_openr (name, NULL); 184 ibfd = bfd_openr (name, NULL);
178 185
179 if (!ibfd) 186 if (!ibfd)
180 { 187 {
181 fprintf (stderr,_("%s: Can't open input archive %s\n"), 188 fprintf (stderr,_("%s: Can't open input archive %s\n"),
182 program_name, name); 189 program_name, name);
183 maybequit (); 190 maybequit ();
184 return; 191 return;
185 } 192 }
186 193
187 if (!bfd_check_format(ibfd, bfd_archive)) 194 if (!bfd_check_format(ibfd, bfd_archive))
188 { 195 {
189 fprintf (stderr, 196 fprintf (stderr,
190 _("%s: file %s is not an archive\n"), 197 _("%s: file %s is not an archive\n"),
191 program_name, name); 198 program_name, name);
192 maybequit (); 199 maybequit ();
193 return; 200 return;
194 } 201 }
195 202
196 ptr = &(obfd->archive_head); 203 ptr = &(obfd->archive_head);
197 element = bfd_openr_next_archived_file (ibfd, NULL); 204 element = bfd_openr_next_archived_file (ibfd, NULL);
198 205
199 while (element) 206 while (element)
200 { 207 {
201 *ptr = element; 208 *ptr = element;
202 ptr = &element->archive_next; 209 ptr = &element->archive_next;
203 element = bfd_openr_next_archived_file (ibfd, element); 210 element = bfd_openr_next_archived_file (ibfd, element);
204 } 211 }
205 } 212 }
206 213
207 bfd_set_format (obfd, bfd_archive); 214 bfd_set_format (obfd, bfd_archive);
208 215
209 obfd->has_armap = 1; 216 obfd->has_armap = 1;
210 obfd->is_thin_archive = 0; 217 obfd->is_thin_archive = 0;
211 } 218 }
212} 219}
213 220
214static void 221static void
215ar_addlib_doer (bfd *abfd, bfd *prev) 222ar_addlib_doer (bfd *abfd, bfd *prev)
216{ 223{
217 /* Add this module to the output bfd. */ 224 /* Add this module to the output bfd. */
218 if (prev != NULL) 225 if (prev != NULL)
219 prev->archive_next = abfd->archive_next; 226 prev->archive_next = abfd->archive_next;
220 227
221 abfd->archive_next = obfd->archive_head; 228 abfd->archive_next = obfd->archive_head;
222 obfd->archive_head = abfd; 229 obfd->archive_head = abfd;
223} 230}
224 231
225void 232void
226ar_addlib (char *name, struct list *list) 233ar_addlib (char *name, struct list *list)
227{ 234{
228 if (obfd == NULL) 235 if (obfd == NULL)
229 { 236 {
230 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name); 237 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
231 maybequit (); 238 maybequit ();
232 } 239 }
233 else 240 else
234 { 241 {
235 bfd *arch; 242 bfd *arch;
236 243
237 arch = open_inarch (name, (char *) NULL); 244 arch = open_inarch (name, (char *) NULL);
238 if (arch != NULL) 245 if (arch != NULL)
239 map_over_list (arch, ar_addlib_doer, list); 246 map_over_list (arch, ar_addlib_doer, list);
240 247
241 /* Don't close the bfd, since it will make the elements disappear. */ 248 /* Don't close the bfd, since it will make the elements disappear. */
242 } 249 }
243} 250}
244 251
245void 252void
246ar_addmod (struct list *list) 253ar_addmod (struct list *list)
247{ 254{
248 if (!obfd) 255 if (!obfd)
249 { 256 {
250 fprintf (stderr, _("%s: no open output archive\n"), program_name); 257 fprintf (stderr, _("%s: no open output archive\n"), program_name);
251 maybequit (); 258 maybequit ();
252 } 259 }
253 else 260 else
254 { 261 {
255 while (list) 262 while (list)
256 { 263 {
257 bfd *abfd = bfd_openr (list->name, NULL); 264 bfd *abfd = bfd_openr (list->name, NULL);
258 265
259 if (!abfd) 266 if (!abfd)
260 { 267 {
261 fprintf (stderr, _("%s: can't open file %s\n"), 268 fprintf (stderr, _("%s: can't open file %s\n"),
262 program_name, list->name); 269 program_name, list->name);
263 maybequit (); 270 maybequit ();
264 } 271 }
265 else 272 else
266 { 273 {
267 abfd->archive_next = obfd->archive_head; 274 abfd->archive_next = obfd->archive_head;
268 obfd->archive_head = abfd; 275 obfd->archive_head = abfd;
269 } 276 }
270 list = list->next; 277 list = list->next;
271 } 278 }
272 } 279 }
273} 280}
274 281
275 282
276void 283void
277ar_clear (void) 284ar_clear (void)
278{ 285{
279 if (obfd) 286 if (obfd)
280 obfd->archive_head = 0; 287 obfd->archive_head = 0;
281} 288}
282 289
283void 290void
284ar_delete (struct list *list) 291ar_delete (struct list *list)
285{ 292{
286 if (!obfd) 293 if (!obfd)
287 { 294 {
288 fprintf (stderr, _("%s: no open output archive\n"), program_name); 295 fprintf (stderr, _("%s: no open output archive\n"), program_name);
289 maybequit (); 296 maybequit ();
290 } 297 }
291 else 298 else
292 { 299 {
293 while (list) 300 while (list)
294 { 301 {
295 /* Find this name in the archive. */ 302 /* Find this name in the archive. */
296 bfd *member = obfd->archive_head; 303 bfd *member = obfd->archive_head;
297 bfd **prev = &(obfd->archive_head); 304 bfd **prev = &(obfd->archive_head);
298 int found = 0; 305 int found = 0;
299 306
300 while (member) 307 while (member)
301 { 308 {
302 if (FILENAME_CMP(member->filename, list->name) == 0) 309 if (FILENAME_CMP(member->filename, list->name) == 0)
303 { 310 {
304 *prev = member->archive_next; 311 *prev = member->archive_next;
305 found = 1; 312 found = 1;
306 } 313 }
307 else 314 else
308 prev = &(member->archive_next); 315 prev = &(member->archive_next);
309 316
310 member = member->archive_next; 317 member = member->archive_next;
311 } 318 }
312 319
313 if (!found) 320 if (!found)
314 { 321 {
315 fprintf (stderr, _("%s: can't find module file %s\n"), 322 fprintf (stderr, _("%s: can't find module file %s\n"),
316 program_name, list->name); 323 program_name, list->name);
317 maybequit (); 324 maybequit ();
318 } 325 }
319 326
320 list = list->next; 327 list = list->next;
321 } 328 }
322 } 329 }
323} 330}
324 331
325void 332void
326ar_save (void) 333ar_save (void)
327{ 334{
328 if (!obfd) 335 if (!obfd)
329 { 336 {
330 fprintf (stderr, _("%s: no open output archive\n"), program_name); 337 fprintf (stderr, _("%s: no open output archive\n"), program_name);
331 maybequit (); 338 maybequit ();
332 } 339 }
333 else 340 else
334 { 341 {
335 char *ofilename = xstrdup (bfd_get_filename (obfd)); 342 char *ofilename = xstrdup (bfd_get_filename (obfd));
336 343
337 bfd_close (obfd); 344 bfd_close (obfd);
338 345
339 smart_rename (ofilename, real_name, 0); 346 smart_rename (ofilename, real_name, 0);
340 obfd = 0; 347 obfd = 0;
341 free (ofilename); 348 free (ofilename);
342 } 349 }
343} 350}
344 351
345void 352void
346ar_replace (struct list *list) 353ar_replace (struct list *list)
347{ 354{
348 if (!obfd) 355 if (!obfd)
349 { 356 {
350 fprintf (stderr, _("%s: no open output archive\n"), program_name); 357 fprintf (stderr, _("%s: no open output archive\n"), program_name);
351 maybequit (); 358 maybequit ();
352 } 359 }
353 else 360 else
354 { 361 {
355 while (list) 362 while (list)
356 { 363 {
357 /* Find this name in the archive. */ 364 /* Find this name in the archive. */
358 bfd *member = obfd->archive_head; 365 bfd *member = obfd->archive_head;
359 bfd **prev = &(obfd->archive_head); 366 bfd **prev = &(obfd->archive_head);
360 int found = 0; 367 int found = 0;
361 368
362 while (member) 369 while (member)
363 { 370 {
364 if (FILENAME_CMP (member->filename, list->name) == 0) 371 if (FILENAME_CMP (member->filename, list->name) == 0)
365 { 372 {
366 /* Found the one to replace. */ 373 /* Found the one to replace. */
367 bfd *abfd = bfd_openr (list->name, 0); 374 bfd *abfd = bfd_openr (list->name, 0);
368 375
369 if (!abfd) 376 if (!abfd)
370 { 377 {
371 fprintf (stderr, _("%s: can't open file %s\n"), 378 fprintf (stderr, _("%s: can't open file %s\n"),
372 program_name, list->name); 379 program_name, list->name);
373 maybequit (); 380 maybequit ();
374 } 381 }
375 else 382 else
376 { 383 {
377 *prev = abfd; 384 *prev = abfd;
378 abfd->archive_next = member->archive_next; 385 abfd->archive_next = member->archive_next;
379 found = 1; 386 found = 1;
380 } 387 }
381 } 388 }
382 else 389 else
383 { 390 {
384 prev = &(member->archive_next); 391 prev = &(member->archive_next);
385 } 392 }
386 member = member->archive_next; 393 member = member->archive_next;
387 } 394 }
388 395
389 if (!found) 396 if (!found)
390 { 397 {
391 bfd *abfd = bfd_openr (list->name, 0); 398 bfd *abfd = bfd_openr (list->name, 0);
392 399
393 fprintf (stderr,_("%s: can't find module file %s\n"), 400 fprintf (stderr,_("%s: can't find module file %s\n"),
394 program_name, list->name); 401 program_name, list->name);
395 if (!abfd) 402 if (!abfd)
396 { 403 {
397 fprintf (stderr, _("%s: can't open file %s\n"), 404 fprintf (stderr, _("%s: can't open file %s\n"),
398 program_name, list->name); 405 program_name, list->name);
399 maybequit (); 406 maybequit ();
400 } 407 }
401 else 408 else
402 *prev = abfd; 409 *prev = abfd;
403 } 410 }
404 411
405 list = list->next; 412 list = list->next;
406 } 413 }
407 } 414 }
408} 415}
409 416
410/* And I added this one. */ 417/* And I added this one. */
411void 418void
412ar_list (void) 419ar_list (void)
413{ 420{
414 if (!obfd) 421 if (!obfd)
415 { 422 {
416 fprintf (stderr, _("%s: no open output archive\n"), program_name); 423 fprintf (stderr, _("%s: no open output archive\n"), program_name);
417 maybequit (); 424 maybequit ();
418 } 425 }
419 else 426 else
420 { 427 {
421 bfd *abfd; 428 bfd *abfd;
422 429
423 outfile = stdout; 430 outfile = stdout;
424 verbose =1 ; 431 verbose =1 ;
425 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd)); 432 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
426 433
427 for (abfd = obfd->archive_head; 434 for (abfd = obfd->archive_head;
428 abfd != (bfd *)NULL; 435 abfd != (bfd *)NULL;
429 abfd = abfd->archive_next) 436 abfd = abfd->archive_next)
430 ar_directory_doer (abfd, (bfd *) NULL); 437 ar_directory_doer (abfd, (bfd *) NULL);
431 } 438 }
432} 439}
433 440
434void 441void
435ar_end (void) 442ar_end (void)
436{ 443{
437 if (obfd) 444 if (obfd)
438 { 445 {
439 bfd_cache_close (obfd); 446 bfd_cache_close (obfd);
440 unlink (bfd_get_filename (obfd)); 447 unlink (bfd_get_filename (obfd));
441 } 448 }
442} 449}
443 450
444void 451void
445ar_extract (struct list *list) 452ar_extract (struct list *list)
446{ 453{
447 if (!obfd) 454 if (!obfd)
448 { 455 {
449 fprintf (stderr, _("%s: no open archive\n"), program_name); 456 fprintf (stderr, _("%s: no open archive\n"), program_name);
450 maybequit (); 457 maybequit ();
451 } 458 }
452 else 459 else
453 { 460 {
454 while (list) 461 while (list)
455 { 462 {
456 /* Find this name in the archive. */ 463 /* Find this name in the archive. */
457 bfd *member = obfd->archive_head; 464 bfd *member = obfd->archive_head;
458 int found = 0; 465 int found = 0;
459 466
460 while (member && !found) 467 while (member && !found)
461 { 468 {
462 if (FILENAME_CMP (member->filename, list->name) == 0) 469 if (FILENAME_CMP (member->filename, list->name) == 0)
463 { 470 {
464 extract_file (member); 471 extract_file (member);
465 found = 1; 472 found = 1;
466 } 473 }
467 474
468 member = member->archive_next; 475 member = member->archive_next;
469 } 476 }
470 477
471 if (!found) 478 if (!found)
472 { 479 {
473 bfd_openr (list->name, 0); 480 bfd_openr (list->name, 0);
474 fprintf (stderr, _("%s: can't find module file %s\n"), 481 fprintf (stderr, _("%s: can't find module file %s\n"),
475 program_name, list->name); 482 program_name, list->name);
476 } 483 }
477 484
478 list = list->next; 485 list = list->next;
479 } 486 }
480 } 487 }
481} 488}

cvs diff -r1.42 -r1.42.12.1 src/lib/libc/net/getnetnamadr.c (switch to unified diff)

--- src/lib/libc/net/getnetnamadr.c 2012/03/13 21:13:41 1.42
+++ src/lib/libc/net/getnetnamadr.c 2015/11/08 00:35:21 1.42.12.1
@@ -1,648 +1,655 @@ @@ -1,648 +1,655 @@
1/* $NetBSD: getnetnamadr.c,v 1.42 2012/03/13 21:13:41 christos Exp $ */ 1/* $NetBSD: getnetnamadr.c,v 1.42.12.1 2015/11/08 00:35:21 riz Exp $ */
2 2
3/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro 3/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
4 * Dep. Matematica Universidade de Coimbra, Portugal, Europe 4 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
5 * 5 *
6 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 */ 9 */
10/* 10/*
11 * Copyright (c) 1983, 1993 11 * Copyright (c) 1983, 1993
12 * The Regents of the University of California. All rights reserved. 12 * The Regents of the University of California. All rights reserved.
13 * 13 *
14 * Redistribution and use in source and binary forms, with or without 14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions 15 * modification, are permitted provided that the following conditions
16 * are met: 16 * are met:
17 * 1. Redistributions of source code must retain the above copyright 17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer. 18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright 19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the 20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution. 21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors 22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software 23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission. 24 * without specific prior written permission.
25 * 25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE. 36 * SUCH DAMAGE.
37 */ 37 */
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(LIBC_SCCS) && !defined(lint) 40#if defined(LIBC_SCCS) && !defined(lint)
41#if 0 41#if 0
42static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; 42static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
43static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03"; 43static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
44static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp "; 44static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
45#else 45#else
46__RCSID("$NetBSD: getnetnamadr.c,v 1.42 2012/03/13 21:13:41 christos Exp $"); 46__RCSID("$NetBSD: getnetnamadr.c,v 1.42.12.1 2015/11/08 00:35:21 riz Exp $");
47#endif 47#endif
48#endif /* LIBC_SCCS and not lint */ 48#endif /* LIBC_SCCS and not lint */
49 49
50#include "namespace.h" 50#include "namespace.h"
51#include <sys/types.h> 51#include <sys/types.h>
52#include <sys/param.h> 52#include <sys/param.h>
53#include <sys/socket.h> 53#include <sys/socket.h>
54#include <netinet/in.h> 54#include <netinet/in.h>
55#include <arpa/inet.h> 55#include <arpa/inet.h>
56#include <arpa/nameser.h> 56#include <arpa/nameser.h>
57 57
58#include <assert.h> 58#include <assert.h>
59#include <ctype.h> 59#include <ctype.h>
60#include <errno.h> 60#include <errno.h>
61#include <netdb.h> 61#include <netdb.h>
62#include <nsswitch.h> 62#include <nsswitch.h>
63#include <resolv.h> 63#include <resolv.h>
64#include <stdarg.h> 64#include <stdarg.h>
65#include <stdio.h> 65#include <stdio.h>
66#include <stdlib.h> 66#include <stdlib.h>
67#include <string.h> 67#include <string.h>
68 68
69#ifdef YP 69#ifdef YP
70#include <rpc/rpc.h> 70#include <rpc/rpc.h>
71#include <rpcsvc/yp_prot.h> 71#include <rpcsvc/yp_prot.h>
72#include <rpcsvc/ypclnt.h> 72#include <rpcsvc/ypclnt.h>
73#endif 73#endif
74 74
75#ifdef __weak_alias 75#ifdef __weak_alias
76__weak_alias(getnetbyaddr,_getnetbyaddr) 76__weak_alias(getnetbyaddr,_getnetbyaddr)
77__weak_alias(getnetbyname,_getnetbyname) 77__weak_alias(getnetbyname,_getnetbyname)
78#endif 78#endif
79 79
 80#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
 81 (ok)(nm) != 0)
 82#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
 83#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
 84
 85
80extern int _net_stayopen; 86extern int _net_stayopen;
81 87
82#define BYADDR 0 88#define BYADDR 0
83#define BYNAME 1 89#define BYNAME 1
84#define MAXALIASES 35 90#define MAXALIASES 35
85 91
86#define MAXPACKET (64*1024) 92#define MAXPACKET (64*1024)
87 93
88typedef union { 94typedef union {
89 HEADER hdr; 95 HEADER hdr;
90 u_char buf[MAXPACKET]; 96 u_char buf[MAXPACKET];
91} querybuf; 97} querybuf;
92 98
93typedef union { 99typedef union {
94 long al; 100 long al;
95 char ac; 101 char ac;
96} align; 102} align;
97 103
98#ifdef YP 104#ifdef YP
99static char *__ypdomain; 105static char *__ypdomain;
100static char *__ypcurrent; 106static char *__ypcurrent;
101static int __ypcurrentlen; 107static int __ypcurrentlen;
102#endif 108#endif
103 109
104static struct netent net_entry; 110static struct netent net_entry;
105static char *net_aliases[MAXALIASES]; 111static char *net_aliases[MAXALIASES];
106 112
107static int parse_reversed_addr(const char *, in_addr_t *); 113static int parse_reversed_addr(const char *, in_addr_t *);
108static struct netent *getnetanswer(querybuf *, int, int); 114static struct netent *getnetanswer(res_state, querybuf *, int, int);
109static int _files_getnetbyaddr(void *, void *, va_list); 115static int _files_getnetbyaddr(void *, void *, va_list);
110static int _files_getnetbyname(void *, void *, va_list); 116static int _files_getnetbyname(void *, void *, va_list);
111static int _dns_getnetbyaddr(void *, void *, va_list); 117static int _dns_getnetbyaddr(void *, void *, va_list);
112static int _dns_getnetbyname(void *, void *, va_list); 118static int _dns_getnetbyname(void *, void *, va_list);
113#ifdef YP 119#ifdef YP
114static int _yp_getnetbyaddr(void *, void *, va_list); 120static int _yp_getnetbyaddr(void *, void *, va_list);
115static int _yp_getnetbyname(void *, void *, va_list); 121static int _yp_getnetbyname(void *, void *, va_list);
116static struct netent *_ypnetent(char *); 122static struct netent *_ypnetent(char *);
117#endif 123#endif
118 124
119/* 125/*
120 * parse_reversed_addr -- 126 * parse_reversed_addr --
121 * parse str, which should be of the form 'd.c.b.a.IN-ADDR.ARPA' 127 * parse str, which should be of the form 'd.c.b.a.IN-ADDR.ARPA'
122 * (a PTR as per RFC 1101) and convert into an in_addr_t of the 128 * (a PTR as per RFC 1101) and convert into an in_addr_t of the
123 * address 'a.b.c.d'. 129 * address 'a.b.c.d'.
124 * returns 0 on success (storing in *result), or -1 on error. 130 * returns 0 on success (storing in *result), or -1 on error.
125 */ 131 */
126static int 132static int
127parse_reversed_addr(const char *str, in_addr_t *result) 133parse_reversed_addr(const char *str, in_addr_t *result)
128{ 134{
129 unsigned long octet[4]; 135 unsigned long octet[4];
130 const char *sp; 136 const char *sp;
131 char *ep; 137 char *ep;
132 int octidx; 138 int octidx;
133 139
134 sp = str; 140 sp = str;
135 /* find the four octets 'd.b.c.a.' */ 141 /* find the four octets 'd.b.c.a.' */
136 for (octidx = 0; octidx < 4; octidx++) { 142 for (octidx = 0; octidx < 4; octidx++) {
137 /* ensure it's a number */ 143 /* ensure it's a number */
138 if (!isdigit((unsigned char)*sp)) 144 if (!isdigit((unsigned char)*sp))
139 return -1; 145 return -1;
140 octet[octidx] = strtoul(sp, &ep, 10); 146 octet[octidx] = strtoul(sp, &ep, 10);
141 /* with a trailing '.' */ 147 /* with a trailing '.' */
142 if (*ep != '.') 148 if (*ep != '.')
143 return -1; 149 return -1;
144 /* and is 0 <= octet <= 255 */ 150 /* and is 0 <= octet <= 255 */
145 if (octet[octidx] > 255) 151 if (octet[octidx] > 255)
146 return -1; 152 return -1;
147 sp = ep + 1; 153 sp = ep + 1;
148 } 154 }
149 /* ensure trailer is correct */ 155 /* ensure trailer is correct */
150 if (strcasecmp(sp, "IN-ADDR.ARPA") != 0) 156 if (strcasecmp(sp, "IN-ADDR.ARPA") != 0)
151 return -1; 157 return -1;
152 *result = 0; 158 *result = 0;
153 /* build result from octets in reverse */ 159 /* build result from octets in reverse */
154 for (octidx = 3; octidx >= 0; octidx--) { 160 for (octidx = 3; octidx >= 0; octidx--) {
155 *result <<= 8; 161 *result <<= 8;
156 *result |= (in_addr_t)(octet[octidx] & 0xff); 162 *result |= (in_addr_t)(octet[octidx] & 0xff);
157 } 163 }
158 return 0; 164 return 0;
159} 165}
160 166
161static struct netent * 167static struct netent *
162getnetanswer(querybuf *answer, int anslen, int net_i) 168getnetanswer(res_state res, querybuf *answer, int anslen, int net_i)
163{ 169{
164 static char n_name[MAXDNAME]; 170 static char n_name[MAXDNAME];
165 static char netbuf[PACKETSZ]; 171 static char netbuf[PACKETSZ];
166 172
167 HEADER *hp; 173 HEADER *hp;
168 u_char *cp; 174 u_char *cp;
169 int n; 175 int n;
170 u_char *eom; 176 u_char *eom;
171 int type, class, ancount, qdcount, haveanswer; 177 int type, class, ancount, qdcount, haveanswer;
172 char *in, *bp, **ap, *ep; 178 char *in, *bp, **ap, *ep;
173 179
174 _DIAGASSERT(answer != NULL); 180 _DIAGASSERT(answer != NULL);
 181 _DIAGASSERT(res != NULL);
175 182
176 /* 183 /*
177 * find first satisfactory answer 184 * find first satisfactory answer
178 * 185 *
179 * answer --> +------------+ ( MESSAGE ) 186 * answer --> +------------+ ( MESSAGE )
180 * | Header | 187 * | Header |
181 * +------------+ 188 * +------------+
182 * | Question | the question for the name server 189 * | Question | the question for the name server
183 * +------------+ 190 * +------------+
184 * | Answer | RRs answering the question 191 * | Answer | RRs answering the question
185 * +------------+ 192 * +------------+
186 * | Authority | RRs pointing toward an authority 193 * | Authority | RRs pointing toward an authority
187 * | Additional | RRs holding additional information 194 * | Additional | RRs holding additional information
188 * +------------+ 195 * +------------+
189 */ 196 */
190 eom = answer->buf + anslen; 197 eom = answer->buf + anslen;
191 hp = &answer->hdr; 198 hp = &answer->hdr;
192 ancount = ntohs(hp->ancount); /* #/records in the answer section */ 199 ancount = ntohs(hp->ancount); /* #/records in the answer section */
193 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 200 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
194 bp = netbuf; 201 bp = netbuf;
195 ep = netbuf + sizeof(netbuf); 202 ep = netbuf + sizeof(netbuf);
196 cp = answer->buf + HFIXEDSZ; 203 cp = answer->buf + HFIXEDSZ;
197 if (!qdcount) { 204 if (!qdcount) {
198 if (hp->aa) 205 if (hp->aa)
199 h_errno = HOST_NOT_FOUND; 206 h_errno = HOST_NOT_FOUND;
200 else 207 else
201 h_errno = TRY_AGAIN; 208 h_errno = TRY_AGAIN;
202 return NULL; 209 return NULL;
203 } 210 }
204 while (qdcount-- > 0) { 211 while (qdcount-- > 0) {
205 n = __dn_skipname(cp, eom); 212 n = __dn_skipname(cp, eom);
206 if (n < 0 || (cp + n + QFIXEDSZ) > eom) { 213 if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
207 h_errno = NO_RECOVERY; 214 h_errno = NO_RECOVERY;
208 return(NULL); 215 return(NULL);
209 } 216 }
210 cp += n + QFIXEDSZ; 217 cp += n + QFIXEDSZ;
211 } 218 }
212 ap = net_aliases; 219 ap = net_aliases;
213 *ap = NULL; 220 *ap = NULL;
214 net_entry.n_aliases = net_aliases; 221 net_entry.n_aliases = net_aliases;
215 haveanswer = 0; 222 haveanswer = 0;
216 n_name[0] = '\0'; 223 n_name[0] = '\0';
217 while (--ancount >= 0 && cp < eom) { 224 while (--ancount >= 0 && cp < eom) {
218 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); 225 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
219 if ((n < 0) || !res_dnok(bp)) 226 if ((n < 0) || !maybe_dnok(res, bp))
220 break; 227 break;
221 cp += n; 228 cp += n;
222 (void)strlcpy(n_name, bp, sizeof(n_name)); 229 (void)strlcpy(n_name, bp, sizeof(n_name));
223 GETSHORT(type, cp); 230 GETSHORT(type, cp);
224 GETSHORT(class, cp); 231 GETSHORT(class, cp);
225 cp += INT32SZ; /* TTL */ 232 cp += INT32SZ; /* TTL */
226 GETSHORT(n, cp); 233 GETSHORT(n, cp);
227 if (class == C_IN && type == T_PTR) { 234 if (class == C_IN && type == T_PTR) {
228 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); 235 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
229 if ((n < 0) || !res_hnok(bp)) { 236 if ((n < 0) || !maybe_hnok(res, bp)) {
230 cp += n; 237 cp += n;
231 return NULL; 238 return NULL;
232 } 239 }
233 cp += n; 240 cp += n;
234 *ap++ = bp; 241 *ap++ = bp;
235 bp += strlen(bp) + 1; 242 bp += strlen(bp) + 1;
236 net_entry.n_addrtype = 243 net_entry.n_addrtype =
237 (class == C_IN) ? AF_INET : AF_UNSPEC; 244 (class == C_IN) ? AF_INET : AF_UNSPEC;
238 haveanswer++; 245 haveanswer++;
239 } 246 }
240 } 247 }
241 if (haveanswer) { 248 if (haveanswer) {
242 *ap = NULL; 249 *ap = NULL;
243 switch (net_i) { 250 switch (net_i) {
244 case BYADDR: 251 case BYADDR:
245 net_entry.n_name = *net_entry.n_aliases; 252 net_entry.n_name = *net_entry.n_aliases;
246 net_entry.n_net = 0L; 253 net_entry.n_net = 0L;
247 break; 254 break;
248 case BYNAME: 255 case BYNAME:
249 ap = net_entry.n_aliases; 256 ap = net_entry.n_aliases;
250 next_alias: 257 next_alias:
251 in = *ap++; 258 in = *ap++;
252 if (in == NULL) { 259 if (in == NULL) {
253 h_errno = HOST_NOT_FOUND; 260 h_errno = HOST_NOT_FOUND;
254 return NULL; 261 return NULL;
255 } 262 }
256 net_entry.n_name = n_name; 263 net_entry.n_name = n_name;
257 if (parse_reversed_addr(in, &net_entry.n_net) == -1) 264 if (parse_reversed_addr(in, &net_entry.n_net) == -1)
258 goto next_alias; 265 goto next_alias;
259 break; 266 break;
260 } 267 }
261 net_entry.n_aliases++; 268 net_entry.n_aliases++;
262#if (defined(__sparc__) && defined(_LP64)) || \ 269#if (defined(__sparc__) && defined(_LP64)) || \
263 defined(__alpha__) || \ 270 defined(__alpha__) || \
264 (defined(__i386__) && defined(_LP64)) || \ 271 (defined(__i386__) && defined(_LP64)) || \
265 (defined(__sh__) && defined(_LP64)) 272 (defined(__sh__) && defined(_LP64))
266 net_entry.__n_pad0 = 0; 273 net_entry.__n_pad0 = 0;
267#endif 274#endif
268 return &net_entry; 275 return &net_entry;
269 } 276 }
270 h_errno = TRY_AGAIN; 277 h_errno = TRY_AGAIN;
271 return NULL; 278 return NULL;
272} 279}
273 280
274/*ARGSUSED*/ 281/*ARGSUSED*/
275static int 282static int
276_files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap) 283_files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
277{ 284{
278 struct netent **retval = va_arg(ap, struct netent **); 285 struct netent **retval = va_arg(ap, struct netent **);
279 uint32_t net = va_arg(ap, uint32_t); 286 uint32_t net = va_arg(ap, uint32_t);
280 int type = va_arg(ap, int); 287 int type = va_arg(ap, int);
281 288
282 struct netent *np; 289 struct netent *np;
283 290
284 setnetent(_net_stayopen); 291 setnetent(_net_stayopen);
285 while ((np = getnetent()) != NULL) 292 while ((np = getnetent()) != NULL)
286 if (np->n_addrtype == type && np->n_net == net) 293 if (np->n_addrtype == type && np->n_net == net)
287 break; 294 break;
288 if (!_net_stayopen) 295 if (!_net_stayopen)
289 endnetent(); 296 endnetent();
290 297
291 if (np != NULL) { 298 if (np != NULL) {
292 *retval = np; 299 *retval = np;
293 return NS_SUCCESS; 300 return NS_SUCCESS;
294 } else { 301 } else {
295 h_errno = HOST_NOT_FOUND; 302 h_errno = HOST_NOT_FOUND;
296 return NS_NOTFOUND; 303 return NS_NOTFOUND;
297 } 304 }
298} 305}
299 306
300/*ARGSUSED*/ 307/*ARGSUSED*/
301static int 308static int
302_dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap) 309_dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
303{ 310{
304 struct netent **retval = va_arg(ap, struct netent **); 311 struct netent **retval = va_arg(ap, struct netent **);
305 uint32_t net = va_arg(ap, uint32_t); 312 uint32_t net = va_arg(ap, uint32_t);
306 int type = va_arg(ap, int); 313 int type = va_arg(ap, int);
307 314
308 unsigned int netbr[4]; 315 unsigned int netbr[4];
309 int nn, anslen; 316 int nn, anslen;
310 querybuf *buf; 317 querybuf *buf;
311 char qbuf[MAXDNAME]; 318 char qbuf[MAXDNAME];
312 uint32_t net2; 319 uint32_t net2;
313 struct netent *np; 320 struct netent *np;
314 res_state res; 321 res_state res;
315 322
316 if (type != AF_INET) 323 if (type != AF_INET)
317 return NS_UNAVAIL; 324 return NS_UNAVAIL;
318 325
319 for (nn = 4, net2 = net; net2; net2 >>= 8) 326 for (nn = 4, net2 = net; net2; net2 >>= 8)
320 netbr[--nn] = (unsigned int)(net2 & 0xff); 327 netbr[--nn] = (unsigned int)(net2 & 0xff);
321 switch (nn) { 328 switch (nn) {
322 default: 329 default:
323 return NS_UNAVAIL; 330 return NS_UNAVAIL;
324 case 3: /* Class A */ 331 case 3: /* Class A */
325 snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]); 332 snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
326 break; 333 break;
327 case 2: /* Class B */ 334 case 2: /* Class B */
328 snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa", 335 snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
329 netbr[3], netbr[2]); 336 netbr[3], netbr[2]);
330 break; 337 break;
331 case 1: /* Class C */ 338 case 1: /* Class C */
332 snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa", 339 snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
333 netbr[3], netbr[2], netbr[1]); 340 netbr[3], netbr[2], netbr[1]);
334 break; 341 break;
335 case 0: /* Class D - E */ 342 case 0: /* Class D - E */
336 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", 343 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
337 netbr[3], netbr[2], netbr[1], netbr[0]); 344 netbr[3], netbr[2], netbr[1], netbr[0]);
338 break; 345 break;
339 } 346 }
340 buf = malloc(sizeof(*buf)); 347 buf = malloc(sizeof(*buf));
341 if (buf == NULL) { 348 if (buf == NULL) {
342 h_errno = NETDB_INTERNAL; 349 h_errno = NETDB_INTERNAL;
343 return NS_NOTFOUND; 350 return NS_NOTFOUND;
344 } 351 }
345 res = __res_get_state(); 352 res = __res_get_state();
346 if (res == NULL) { 353 if (res == NULL) {
347 free(buf); 354 free(buf);
348 return NS_NOTFOUND; 355 return NS_NOTFOUND;
349 } 356 }
350 anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, 357 anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf,
351 (int)sizeof(buf->buf)); 358 (int)sizeof(buf->buf));
352 if (anslen < 0) { 359 if (anslen < 0) {
353 free(buf); 360 free(buf);
354#ifdef DEBUG 361#ifdef DEBUG
355 if (res->options & RES_DEBUG) 362 if (res->options & RES_DEBUG)
356 printf("res_query failed\n"); 363 printf("res_query failed\n");
357#endif 364#endif
358 __res_put_state(res); 365 __res_put_state(res);
359 return NS_NOTFOUND; 366 return NS_NOTFOUND;
360 } 367 }
 368 np = getnetanswer(res, buf, anslen, BYADDR);
361 __res_put_state(res); 369 __res_put_state(res);
362 np = getnetanswer(buf, anslen, BYADDR); 
363 free(buf); 370 free(buf);
364 if (np) { 371 if (np) {
365 /* maybe net should be unsigned? */ 372 /* maybe net should be unsigned? */
366 uint32_t u_net = net; 373 uint32_t u_net = net;
367 374
368 /* Strip trailing zeros */ 375 /* Strip trailing zeros */
369 while ((u_net & 0xff) == 0 && u_net != 0) 376 while ((u_net & 0xff) == 0 && u_net != 0)
370 u_net >>= 8; 377 u_net >>= 8;
371 np->n_net = u_net; 378 np->n_net = u_net;
372 } 379 }
373 380
374 if (np != NULL) { 381 if (np != NULL) {
375 *retval = np; 382 *retval = np;
376 return NS_SUCCESS; 383 return NS_SUCCESS;
377 } else { 384 } else {
378 h_errno = HOST_NOT_FOUND; 385 h_errno = HOST_NOT_FOUND;
379 return NS_NOTFOUND; 386 return NS_NOTFOUND;
380 } 387 }
381} 388}
382 389
383struct netent * 390struct netent *
384getnetbyaddr(uint32_t net, int net_type) 391getnetbyaddr(uint32_t net, int net_type)
385{ 392{
386 int rv; 393 int rv;
387 struct netent *retval; 394 struct netent *retval;
388 395
389 static const ns_dtab dtab[] = { 396 static const ns_dtab dtab[] = {
390 NS_FILES_CB(_files_getnetbyaddr, NULL) 397 NS_FILES_CB(_files_getnetbyaddr, NULL)
391 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */ 398 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */
392 NS_NIS_CB(_yp_getnetbyaddr, NULL) 399 NS_NIS_CB(_yp_getnetbyaddr, NULL)
393 NS_NULL_CB 400 NS_NULL_CB
394 }; 401 };
395 402
396 retval = NULL; 403 retval = NULL;
397 h_errno = NETDB_INTERNAL; 404 h_errno = NETDB_INTERNAL;
398 rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr", 405 rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr",
399 __nsdefaultsrc, &retval, net, net_type); 406 __nsdefaultsrc, &retval, net, net_type);
400 if (rv == NS_SUCCESS) { 407 if (rv == NS_SUCCESS) {
401 h_errno = NETDB_SUCCESS; 408 h_errno = NETDB_SUCCESS;
402 return retval; 409 return retval;
403 } 410 }
404 return NULL; 411 return NULL;
405} 412}
406 413
407/*ARGSUSED*/ 414/*ARGSUSED*/
408static int 415static int
409_files_getnetbyname(void *cbrv, void *cbdata, va_list ap) 416_files_getnetbyname(void *cbrv, void *cbdata, va_list ap)
410{ 417{
411 struct netent **retval = va_arg(ap, struct netent **); 418 struct netent **retval = va_arg(ap, struct netent **);
412 const char *name = va_arg(ap, const char *); 419 const char *name = va_arg(ap, const char *);
413 420
414 struct netent *np; 421 struct netent *np;
415 char **cp; 422 char **cp;
416 423
417 setnetent(_net_stayopen); 424 setnetent(_net_stayopen);
418 while ((np = getnetent()) != NULL) { 425 while ((np = getnetent()) != NULL) {
419 if (strcasecmp(np->n_name, name) == 0) 426 if (strcasecmp(np->n_name, name) == 0)
420 break; 427 break;
421 for (cp = np->n_aliases; *cp != 0; cp++) 428 for (cp = np->n_aliases; *cp != 0; cp++)
422 if (strcasecmp(*cp, name) == 0) 429 if (strcasecmp(*cp, name) == 0)
423 goto found; 430 goto found;
424 } 431 }
425found: 432found:
426 if (!_net_stayopen) 433 if (!_net_stayopen)
427 endnetent(); 434 endnetent();
428 435
429 if (np != NULL) { 436 if (np != NULL) {
430 *retval = np; 437 *retval = np;
431 return NS_SUCCESS; 438 return NS_SUCCESS;
432 } else { 439 } else {
433 h_errno = HOST_NOT_FOUND; 440 h_errno = HOST_NOT_FOUND;
434 return NS_NOTFOUND; 441 return NS_NOTFOUND;
435 } 442 }
436} 443}
437 444
438/*ARGSUSED*/ 445/*ARGSUSED*/
439static int 446static int
440_dns_getnetbyname(void *cbrv, void *cbdata, va_list ap) 447_dns_getnetbyname(void *cbrv, void *cbdata, va_list ap)
441{ 448{
442 struct netent **retval = va_arg(ap, struct netent **); 449 struct netent **retval = va_arg(ap, struct netent **);
443 const char *name = va_arg(ap, const char *); 450 const char *name = va_arg(ap, const char *);
444 451
445 int anslen; 452 int anslen;
446 querybuf *buf; 453 querybuf *buf;
447 char qbuf[MAXDNAME]; 454 char qbuf[MAXDNAME];
448 struct netent *np; 455 struct netent *np;
449 res_state res; 456 res_state res;
450 457
451 strlcpy(&qbuf[0], name, sizeof(qbuf)); 458 strlcpy(&qbuf[0], name, sizeof(qbuf));
452 buf = malloc(sizeof(*buf)); 459 buf = malloc(sizeof(*buf));
453 if (buf == NULL) { 460 if (buf == NULL) {
454 h_errno = NETDB_INTERNAL; 461 h_errno = NETDB_INTERNAL;
455 return NS_NOTFOUND; 462 return NS_NOTFOUND;
456 } 463 }
457 res = __res_get_state(); 464 res = __res_get_state();
458 if (res == NULL) { 465 if (res == NULL) {
459 free(buf); 466 free(buf);
460 return NS_NOTFOUND; 467 return NS_NOTFOUND;
461 } 468 }
462 anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf, 469 anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf,
463 (int)sizeof(buf->buf)); 470 (int)sizeof(buf->buf));
464 if (anslen < 0) { 471 if (anslen < 0) {
465 free(buf); 472 free(buf);
466#ifdef DEBUG 473#ifdef DEBUG
467 if (res->options & RES_DEBUG) 474 if (res->options & RES_DEBUG)
468 printf("res_search failed\n"); 475 printf("res_search failed\n");
469#endif 476#endif
470 __res_put_state(res); 477 __res_put_state(res);
471 return NS_NOTFOUND; 478 return NS_NOTFOUND;
472 } 479 }
 480 np = getnetanswer(res, buf, anslen, BYNAME);
473 __res_put_state(res); 481 __res_put_state(res);
474 np = getnetanswer(buf, anslen, BYNAME); 
475 free(buf); 482 free(buf);
476 483
477 if (np != NULL) { 484 if (np != NULL) {
478 *retval = np; 485 *retval = np;
479 return NS_SUCCESS; 486 return NS_SUCCESS;
480 } else { 487 } else {
481 h_errno = HOST_NOT_FOUND; 488 h_errno = HOST_NOT_FOUND;
482 return NS_NOTFOUND; 489 return NS_NOTFOUND;
483 } 490 }
484} 491}
485 492
486struct netent * 493struct netent *
487getnetbyname(const char *name) 494getnetbyname(const char *name)
488{ 495{
489 int rv; 496 int rv;
490 struct netent *retval; 497 struct netent *retval;
491 498
492 static const ns_dtab dtab[] = { 499 static const ns_dtab dtab[] = {
493 NS_FILES_CB(_files_getnetbyname, NULL) 500 NS_FILES_CB(_files_getnetbyname, NULL)
494 { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */ 501 { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */
495 NS_NIS_CB(_yp_getnetbyname, NULL) 502 NS_NIS_CB(_yp_getnetbyname, NULL)
496 NS_NULL_CB 503 NS_NULL_CB
497 }; 504 };
498 505
499 _DIAGASSERT(name != NULL); 506 _DIAGASSERT(name != NULL);
500 507
501 retval = NULL; 508 retval = NULL;
502 h_errno = NETDB_INTERNAL; 509 h_errno = NETDB_INTERNAL;
503 rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname", 510 rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname",
504 __nsdefaultsrc, &retval, name); 511 __nsdefaultsrc, &retval, name);
505 if (rv == NS_SUCCESS) { 512 if (rv == NS_SUCCESS) {
506 h_errno = NETDB_SUCCESS; 513 h_errno = NETDB_SUCCESS;
507 return retval; 514 return retval;
508 } 515 }
509 return NULL; 516 return NULL;
510} 517}
511 518
512#ifdef YP 519#ifdef YP
513/*ARGSUSED*/ 520/*ARGSUSED*/
514static int 521static int
515_yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap) 522_yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap)
516{ 523{
517 struct netent **retval = va_arg(ap, struct netent **); 524 struct netent **retval = va_arg(ap, struct netent **);
518 uint32_t net = va_arg(ap, uint32_t); 525 uint32_t net = va_arg(ap, uint32_t);
519 int type = va_arg(ap, int); 526 int type = va_arg(ap, int);
520 527
521 struct netent *np; 528 struct netent *np;
522 char qbuf[MAXDNAME]; 529 char qbuf[MAXDNAME];
523 unsigned int netbr[4]; 530 unsigned int netbr[4];
524 uint32_t net2; 531 uint32_t net2;
525 int r; 532 int r;
526 533
527 if (type != AF_INET) 534 if (type != AF_INET)
528 return NS_UNAVAIL; 535 return NS_UNAVAIL;
529 536
530 if (!__ypdomain) { 537 if (!__ypdomain) {
531 if (_yp_check(&__ypdomain) == 0) 538 if (_yp_check(&__ypdomain) == 0)
532 return NS_UNAVAIL; 539 return NS_UNAVAIL;
533 } 540 }
534 np = NULL; 541 np = NULL;
535 if (__ypcurrent) 542 if (__ypcurrent)
536 free(__ypcurrent); 543 free(__ypcurrent);
537 __ypcurrent = NULL; 544 __ypcurrent = NULL;
538 for (r = 4, net2 = net; net2; net2 >>= 8) 545 for (r = 4, net2 = net; net2; net2 >>= 8)
539 netbr[--r] = (unsigned int)(net2 & 0xff); 546 netbr[--r] = (unsigned int)(net2 & 0xff);
540 switch (r) { 547 switch (r) {
541 default: 548 default:
542 return NS_UNAVAIL; 549 return NS_UNAVAIL;
543 case 3: /* Class A */ 550 case 3: /* Class A */
544 snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]); 551 snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
545 break; 552 break;
546 case 2: /* Class B */ 553 case 2: /* Class B */
547 snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]); 554 snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
548 break; 555 break;
549 case 1: /* Class C */ 556 case 1: /* Class C */
550 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2], 557 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
551 netbr[3]); 558 netbr[3]);
552 break; 559 break;
553 case 0: /* Class D - E */ 560 case 0: /* Class D - E */
554 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1], 561 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
555 netbr[2], netbr[3]); 562 netbr[2], netbr[3]);
556 break; 563 break;
557 } 564 }
558 r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf), 565 r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
559 &__ypcurrent, &__ypcurrentlen); 566 &__ypcurrent, &__ypcurrentlen);
560 if (r == 0) 567 if (r == 0)
561 np = _ypnetent(__ypcurrent); 568 np = _ypnetent(__ypcurrent);
562 569
563 if (np != NULL) { 570 if (np != NULL) {
564 *retval = np; 571 *retval = np;
565 return NS_SUCCESS; 572 return NS_SUCCESS;
566 } else { 573 } else {
567 h_errno = HOST_NOT_FOUND; 574 h_errno = HOST_NOT_FOUND;
568 return NS_NOTFOUND; 575 return NS_NOTFOUND;
569 } 576 }
570} 577}
571 578
572/*ARGSUSED*/ 579/*ARGSUSED*/
573static int 580static int
574_yp_getnetbyname(void *cbrv, void *cbdata, va_list ap) 581_yp_getnetbyname(void *cbrv, void *cbdata, va_list ap)
575{ 582{
576 struct netent **retval = va_arg(ap, struct netent **); 583 struct netent **retval = va_arg(ap, struct netent **);
577 const char *name = va_arg(ap, const char *); 584 const char *name = va_arg(ap, const char *);
578 585
579 struct netent *np; 586 struct netent *np;
580 int r; 587 int r;
581 588
582 if (!__ypdomain) { 589 if (!__ypdomain) {
583 if (_yp_check(&__ypdomain) == 0) 590 if (_yp_check(&__ypdomain) == 0)
584 return NS_UNAVAIL; 591 return NS_UNAVAIL;
585 } 592 }
586 np = NULL; 593 np = NULL;
587 if (__ypcurrent) 594 if (__ypcurrent)
588 free(__ypcurrent); 595 free(__ypcurrent);
589 __ypcurrent = NULL; 596 __ypcurrent = NULL;
590 r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name), 597 r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
591 &__ypcurrent, &__ypcurrentlen); 598 &__ypcurrent, &__ypcurrentlen);
592 if (r == 0) 599 if (r == 0)
593 np = _ypnetent(__ypcurrent); 600 np = _ypnetent(__ypcurrent);
594 601
595 if (np != NULL) { 602 if (np != NULL) {
596 *retval = np; 603 *retval = np;
597 return NS_SUCCESS; 604 return NS_SUCCESS;
598 } else { 605 } else {
599 h_errno = HOST_NOT_FOUND; 606 h_errno = HOST_NOT_FOUND;
600 return NS_NOTFOUND; 607 return NS_NOTFOUND;
601 } 608 }
602} 609}
603 610
604static struct netent * 611static struct netent *
605_ypnetent(char *line) 612_ypnetent(char *line)
606{ 613{
607 char *cp, *p, **q; 614 char *cp, *p, **q;
608 615
609 _DIAGASSERT(line != NULL); 616 _DIAGASSERT(line != NULL);
610 617
611 net_entry.n_name = line; 618 net_entry.n_name = line;
612 cp = strpbrk(line, " \t"); 619 cp = strpbrk(line, " \t");
613 if (cp == NULL) 620 if (cp == NULL)
614 return NULL; 621 return NULL;
615 *cp++ = '\0'; 622 *cp++ = '\0';
616 while (*cp == ' ' || *cp == '\t') 623 while (*cp == ' ' || *cp == '\t')
617 cp++; 624 cp++;
618 p = strpbrk(cp, " \t"); 625 p = strpbrk(cp, " \t");
619 if (p != NULL) 626 if (p != NULL)
620 *p++ = '\0'; 627 *p++ = '\0';
621 net_entry.n_net = inet_network(cp); 628 net_entry.n_net = inet_network(cp);
622#if (defined(__sparc__) && defined(_LP64)) || \ 629#if (defined(__sparc__) && defined(_LP64)) || \
623 defined(__alpha__) || \ 630 defined(__alpha__) || \
624 (defined(__i386__) && defined(_LP64)) || \ 631 (defined(__i386__) && defined(_LP64)) || \
625 (defined(__sh__) && defined(_LP64)) 632 (defined(__sh__) && defined(_LP64))
626 net_entry.__n_pad0 = 0; 633 net_entry.__n_pad0 = 0;
627#endif 634#endif
628 net_entry.n_addrtype = AF_INET; 635 net_entry.n_addrtype = AF_INET;
629 q = net_entry.n_aliases = net_aliases; 636 q = net_entry.n_aliases = net_aliases;
630 if (p != NULL) { 637 if (p != NULL) {
631 cp = p; 638 cp = p;
632 while (cp && *cp) { 639 while (cp && *cp) {
633 if (*cp == ' ' || *cp == '\t') { 640 if (*cp == ' ' || *cp == '\t') {
634 cp++; 641 cp++;
635 continue; 642 continue;
636 } 643 }
637 if (q < &net_aliases[MAXALIASES - 1]) 644 if (q < &net_aliases[MAXALIASES - 1])
638 *q++ = cp; 645 *q++ = cp;
639 cp = strpbrk(cp, " \t"); 646 cp = strpbrk(cp, " \t");
640 if (cp != NULL) 647 if (cp != NULL)
641 *cp++ = '\0'; 648 *cp++ = '\0';
642 } 649 }
643 } 650 }
644 *q = NULL; 651 *q = NULL;
645 652
646 return &net_entry; 653 return &net_entry;
647} 654}
648#endif 655#endif