env.c revision 1.8 1 1.8 apb /* $NetBSD: env.c,v 1.8 2013/02/07 11:24:15 apb Exp $ */
2 1.2 dyoung
3 1.2 dyoung /*-
4 1.5 dyoung * Copyright (c) 2008 David Young. All rights reserved.
5 1.2 dyoung *
6 1.2 dyoung * Redistribution and use in source and binary forms, with or without
7 1.2 dyoung * modification, are permitted provided that the following conditions
8 1.2 dyoung * are met:
9 1.2 dyoung * 1. Redistributions of source code must retain the above copyright
10 1.2 dyoung * notice, this list of conditions and the following disclaimer.
11 1.2 dyoung * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 dyoung * notice, this list of conditions and the following disclaimer in the
13 1.2 dyoung * documentation and/or other materials provided with the distribution.
14 1.2 dyoung *
15 1.2 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.2 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.2 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.2 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.2 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.2 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.2 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.2 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.2 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.2 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.2 dyoung * SUCH DAMAGE.
26 1.2 dyoung */
27 1.2 dyoung
28 1.6 dyoung #include <sys/cdefs.h>
29 1.6 dyoung #ifndef lint
30 1.8 apb __RCSID("$NetBSD: env.c,v 1.8 2013/02/07 11:24:15 apb Exp $");
31 1.6 dyoung #endif /* not lint */
32 1.6 dyoung
33 1.1 dyoung #include <errno.h>
34 1.1 dyoung #include <string.h>
35 1.1 dyoung #include <stdlib.h>
36 1.1 dyoung #include <util.h>
37 1.1 dyoung
38 1.1 dyoung #include <net/if.h>
39 1.1 dyoung #include <sys/socket.h>
40 1.1 dyoung #include <sys/ioctl.h>
41 1.1 dyoung
42 1.1 dyoung #include "env.h"
43 1.1 dyoung #include "util.h"
44 1.7 pooka #include "prog_ops.h"
45 1.1 dyoung
46 1.1 dyoung prop_dictionary_t
47 1.1 dyoung prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top)
48 1.1 dyoung {
49 1.1 dyoung prop_object_iterator_t i;
50 1.1 dyoung prop_dictionary_t d;
51 1.1 dyoung prop_object_t ko, o;
52 1.1 dyoung prop_dictionary_keysym_t k;
53 1.1 dyoung const char *key;
54 1.1 dyoung
55 1.1 dyoung d = prop_dictionary_copy_mutable(bottom);
56 1.1 dyoung
57 1.1 dyoung i = prop_dictionary_iterator(top);
58 1.1 dyoung
59 1.1 dyoung while ((ko = prop_object_iterator_next(i)) != NULL) {
60 1.1 dyoung k = (prop_dictionary_keysym_t)ko;
61 1.1 dyoung key = prop_dictionary_keysym_cstring_nocopy(k);
62 1.1 dyoung o = prop_dictionary_get_keysym(top, k);
63 1.1 dyoung if (o == NULL || !prop_dictionary_set(d, key, o)) {
64 1.1 dyoung prop_object_release((prop_object_t)d);
65 1.1 dyoung d = NULL;
66 1.1 dyoung break;
67 1.1 dyoung }
68 1.1 dyoung }
69 1.1 dyoung prop_object_iterator_release(i);
70 1.8 apb if (d !== NULL)
71 1.8 apb prop_dictionary_make_immutable(d);
72 1.1 dyoung return d;
73 1.1 dyoung }
74 1.1 dyoung
75 1.1 dyoung int
76 1.1 dyoung getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
77 1.1 dyoung unsigned short *flagsp)
78 1.1 dyoung {
79 1.1 dyoung struct ifreq ifr;
80 1.1 dyoung const char *ifname;
81 1.3 dyoung uint64_t ifflags;
82 1.1 dyoung int s;
83 1.1 dyoung
84 1.3 dyoung if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
85 1.3 dyoung *flagsp = (unsigned short)ifflags;
86 1.1 dyoung return 0;
87 1.1 dyoung }
88 1.1 dyoung
89 1.1 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
90 1.1 dyoung return -1;
91 1.1 dyoung
92 1.1 dyoung if ((ifname = getifname(env)) == NULL)
93 1.1 dyoung return -1;
94 1.1 dyoung
95 1.1 dyoung memset(&ifr, 0, sizeof(ifr));
96 1.1 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
97 1.7 pooka if (prog_ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
98 1.1 dyoung return -1;
99 1.1 dyoung
100 1.1 dyoung *flagsp = (unsigned short)ifr.ifr_flags;
101 1.1 dyoung
102 1.3 dyoung prop_dictionary_set_uint64(oenv, "ifflags",
103 1.1 dyoung (unsigned short)ifr.ifr_flags);
104 1.1 dyoung
105 1.1 dyoung return 0;
106 1.1 dyoung }
107 1.1 dyoung
108 1.1 dyoung const char *
109 1.1 dyoung getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
110 1.1 dyoung {
111 1.1 dyoung if (getifflags(env, oenv, flagsp) == -1)
112 1.1 dyoung return NULL;
113 1.1 dyoung
114 1.1 dyoung return getifname(env);
115 1.1 dyoung }
116 1.1 dyoung
117 1.1 dyoung const char *
118 1.1 dyoung getifname(prop_dictionary_t env)
119 1.1 dyoung {
120 1.3 dyoung const char *s;
121 1.1 dyoung
122 1.3 dyoung return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
123 1.1 dyoung }
124 1.1 dyoung
125 1.1 dyoung ssize_t
126 1.1 dyoung getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
127 1.1 dyoung {
128 1.1 dyoung prop_data_t data;
129 1.1 dyoung size_t datalen;
130 1.1 dyoung
131 1.1 dyoung data = (prop_data_t)prop_dictionary_get(env, key);
132 1.1 dyoung if (data == NULL) {
133 1.1 dyoung errno = ENOENT;
134 1.1 dyoung return -1;
135 1.1 dyoung }
136 1.1 dyoung datalen = prop_data_size(data);
137 1.1 dyoung if (datalen > buflen) {
138 1.1 dyoung errno = ENAMETOOLONG;
139 1.1 dyoung return -1;
140 1.1 dyoung }
141 1.1 dyoung memset(buf, 0, buflen);
142 1.1 dyoung memcpy(buf, prop_data_data_nocopy(data), datalen);
143 1.1 dyoung return datalen;
144 1.1 dyoung }
145 1.1 dyoung
146 1.1 dyoung ssize_t
147 1.1 dyoung getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
148 1.1 dyoung {
149 1.1 dyoung prop_data_t data;
150 1.1 dyoung size_t datalen;
151 1.1 dyoung
152 1.4 dyoung data = (prop_data_t)prop_dictionary_get(env, key);
153 1.1 dyoung if (data == NULL) {
154 1.1 dyoung errno = ENOENT;
155 1.1 dyoung return -1;
156 1.1 dyoung }
157 1.1 dyoung datalen = prop_data_size(data);
158 1.1 dyoung if (datalen >= buflen) {
159 1.1 dyoung errno = ENAMETOOLONG;
160 1.1 dyoung return -1;
161 1.1 dyoung }
162 1.1 dyoung memset(buf, 0, buflen);
163 1.1 dyoung memcpy(buf, prop_data_data_nocopy(data), datalen);
164 1.1 dyoung return datalen;
165 1.1 dyoung }
166 1.1 dyoung
167 1.1 dyoung int
168 1.1 dyoung getaf(prop_dictionary_t env)
169 1.1 dyoung {
170 1.3 dyoung int64_t af;
171 1.1 dyoung
172 1.3 dyoung if (!prop_dictionary_get_int64(env, "af", &af)) {
173 1.1 dyoung errno = ENOENT;
174 1.1 dyoung return -1;
175 1.1 dyoung }
176 1.3 dyoung return (int)af;
177 1.1 dyoung }
178