1 /* $NetBSD: env.c,v 1.5 2008/05/12 21:53:49 dyoung 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>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
46 prop_dictionary_augment(prop_dictionary_t bottom
, prop_dictionary_t top
)
48 prop_object_iterator_t i
;
51 prop_dictionary_keysym_t k
;
54 d
= prop_dictionary_copy_mutable(bottom
);
56 i
= prop_dictionary_iterator(top
);
58 while ((ko
= prop_object_iterator_next(i
)) != NULL
) {
59 k
= (prop_dictionary_keysym_t
)ko
;
60 key
= prop_dictionary_keysym_cstring_nocopy(k
);
61 o
= prop_dictionary_get_keysym(top
, k
);
62 if (o
== NULL
|| !prop_dictionary_set(d
, key
, o
)) {
63 prop_object_release((prop_object_t
)d
);
68 prop_object_iterator_release(i
);
69 prop_dictionary_make_immutable(d
);
74 getifflags(prop_dictionary_t env
, prop_dictionary_t oenv
,
75 unsigned short *flagsp
)
82 if (prop_dictionary_get_uint64(env
, "ifflags", &ifflags
)) {
83 *flagsp
= (unsigned short)ifflags
;
87 if ((s
= getsock(AF_UNSPEC
)) == -1)
90 if ((ifname
= getifname(env
)) == NULL
)
93 memset(&ifr
, 0, sizeof(ifr
));
94 estrlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
95 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) == -1)
98 *flagsp
= (unsigned short)ifr
.ifr_flags
;
100 prop_dictionary_set_uint64(oenv
, "ifflags",
101 (unsigned short)ifr
.ifr_flags
);
107 getifinfo(prop_dictionary_t env
, prop_dictionary_t oenv
, unsigned short *flagsp
)
109 if (getifflags(env
, oenv
, flagsp
) == -1)
112 return getifname(env
);
116 getifname(prop_dictionary_t env
)
120 return prop_dictionary_get_cstring_nocopy(env
, "if", &s
) ? s
: NULL
;
124 getargdata(prop_dictionary_t env
, const char *key
, uint8_t *buf
, size_t buflen
)
129 data
= (prop_data_t
)prop_dictionary_get(env
, key
);
134 datalen
= prop_data_size(data
);
135 if (datalen
> buflen
) {
136 errno
= ENAMETOOLONG
;
139 memset(buf
, 0, buflen
);
140 memcpy(buf
, prop_data_data_nocopy(data
), datalen
);
145 getargstr(prop_dictionary_t env
, const char *key
, char *buf
, size_t buflen
)
150 data
= (prop_data_t
)prop_dictionary_get(env
, key
);
155 datalen
= prop_data_size(data
);
156 if (datalen
>= buflen
) {
157 errno
= ENAMETOOLONG
;
160 memset(buf
, 0, buflen
);
161 memcpy(buf
, prop_data_data_nocopy(data
), datalen
);
166 getaf(prop_dictionary_t env
)
170 if (!prop_dictionary_get_int64(env
, "af", &af
)) {