1 /* $NetBSD: env.c,v 1.9 2013/02/07 13:20:51 apb Exp $ */
4 * Copyright (c) 2008 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: env.c,v 1.9 2013/02/07 13:20:51 apb Exp $");
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
47 prop_dictionary_augment(prop_dictionary_t bottom
, prop_dictionary_t top
)
49 prop_object_iterator_t i
;
52 prop_dictionary_keysym_t k
;
55 d
= prop_dictionary_copy_mutable(bottom
);
59 i
= prop_dictionary_iterator(top
);
61 while (i
!= NULL
&& (ko
= prop_object_iterator_next(i
)) != NULL
) {
62 k
= (prop_dictionary_keysym_t
)ko
;
63 key
= prop_dictionary_keysym_cstring_nocopy(k
);
64 o
= prop_dictionary_get_keysym(top
, k
);
65 if (o
== NULL
|| !prop_dictionary_set(d
, key
, o
)) {
66 prop_object_release((prop_object_t
)d
);
72 prop_object_iterator_release(i
);
74 prop_dictionary_make_immutable(d
);
79 getifflags(prop_dictionary_t env
, prop_dictionary_t oenv
,
80 unsigned short *flagsp
)
87 if (prop_dictionary_get_uint64(env
, "ifflags", &ifflags
)) {
88 *flagsp
= (unsigned short)ifflags
;
92 if ((s
= getsock(AF_UNSPEC
)) == -1)
95 if ((ifname
= getifname(env
)) == NULL
)
98 memset(&ifr
, 0, sizeof(ifr
));
99 estrlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
100 if (prog_ioctl(s
, SIOCGIFFLAGS
, &ifr
) == -1)
103 *flagsp
= (unsigned short)ifr
.ifr_flags
;
105 prop_dictionary_set_uint64(oenv
, "ifflags",
106 (unsigned short)ifr
.ifr_flags
);
112 getifinfo(prop_dictionary_t env
, prop_dictionary_t oenv
, unsigned short *flagsp
)
114 if (getifflags(env
, oenv
, flagsp
) == -1)
117 return getifname(env
);
121 getifname(prop_dictionary_t env
)
125 return prop_dictionary_get_cstring_nocopy(env
, "if", &s
) ? s
: NULL
;
129 getargdata(prop_dictionary_t env
, const char *key
, uint8_t *buf
, size_t buflen
)
134 data
= (prop_data_t
)prop_dictionary_get(env
, key
);
139 datalen
= prop_data_size(data
);
140 if (datalen
> buflen
) {
141 errno
= ENAMETOOLONG
;
144 memset(buf
, 0, buflen
);
145 memcpy(buf
, prop_data_data_nocopy(data
), datalen
);
150 getargstr(prop_dictionary_t env
, const char *key
, char *buf
, size_t buflen
)
155 data
= (prop_data_t
)prop_dictionary_get(env
, key
);
160 datalen
= prop_data_size(data
);
161 if (datalen
>= buflen
) {
162 errno
= ENAMETOOLONG
;
165 memset(buf
, 0, buflen
);
166 memcpy(buf
, prop_data_data_nocopy(data
), datalen
);
171 getaf(prop_dictionary_t env
)
175 if (!prop_dictionary_get_int64(env
, "af", &af
)) {