1 /* $OpenBSD: getnetnamadr.c,v 1.5 2013/05/27 17:31:01 eric Exp $ */
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <netinet/in.h>
28 static void _fillnetent(const struct netent
*, struct netent
*, char *buf
,
31 static struct netent _netent
;
32 static char _entbuf
[4096];
34 static char *_empty
[] = { NULL
, };
37 _fillnetent(const struct netent
*e
, struct netent
*r
, char *buf
, size_t len
)
39 char **ptr
, *end
, *pos
;
45 r
->n_aliases
= _empty
;
48 ptr
= (char **)ALIGN(buf
);
50 if ((char *)ptr
>= end
)
53 for (naliases
= 0; e
->n_aliases
[naliases
]; naliases
++)
57 r
->n_addrtype
= e
->n_addrtype
;
61 pos
= (char *)(ptr
+ (naliases
+ 1));
63 r
->n_aliases
= _empty
;
65 n
= strlcpy(pos
, e
->n_name
, end
- pos
);
71 for (i
= 0; i
< naliases
; i
++) {
72 n
= strlcpy(pos
, e
->n_aliases
[i
], end
- pos
);
75 r
->n_aliases
[i
] = pos
;
81 getnetbyname(const char *name
)
88 as
= getnetbyname_async(name
, NULL
);
90 h_errno
= NETDB_INTERNAL
;
94 async_run_sync(as
, &ar
);
97 h_errno
= ar
.ar_h_errno
;
98 if (ar
.ar_netent
== NULL
)
101 _fillnetent(ar
.ar_netent
, &_netent
, _entbuf
, sizeof(_entbuf
));
108 getnetbyaddr(in_addr_t net
, int type
)
115 as
= getnetbyaddr_async(net
, type
, NULL
);
117 h_errno
= NETDB_INTERNAL
;
121 async_run_sync(as
, &ar
);
124 h_errno
= ar
.ar_h_errno
;
125 if (ar
.ar_netent
== NULL
)
128 _fillnetent(ar
.ar_netent
, &_netent
, _entbuf
, sizeof(_entbuf
));