env.c revision 1.3 1 /* $NetBSD: env.c,v 1.3 2008/05/06 18:35:38 dyoung Exp $ */
2
3 /*-
4 * Copyright (c)2008 David Young. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <errno.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <util.h>
32
33 #include <net/if.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36
37 #include "env.h"
38 #include "util.h"
39
40 prop_dictionary_t
41 prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top)
42 {
43 prop_object_iterator_t i;
44 prop_dictionary_t d;
45 prop_object_t ko, o;
46 prop_dictionary_keysym_t k;
47 const char *key;
48
49 d = prop_dictionary_copy_mutable(bottom);
50
51 i = prop_dictionary_iterator(top);
52
53 while ((ko = prop_object_iterator_next(i)) != NULL) {
54 k = (prop_dictionary_keysym_t)ko;
55 key = prop_dictionary_keysym_cstring_nocopy(k);
56 o = prop_dictionary_get_keysym(top, k);
57 if (o == NULL || !prop_dictionary_set(d, key, o)) {
58 prop_object_release((prop_object_t)d);
59 d = NULL;
60 break;
61 }
62 }
63 prop_object_iterator_release(i);
64 prop_dictionary_make_immutable(d);
65 return d;
66 }
67
68 int
69 getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
70 unsigned short *flagsp)
71 {
72 struct ifreq ifr;
73 const char *ifname;
74 uint64_t ifflags;
75 int s;
76
77 if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
78 *flagsp = (unsigned short)ifflags;
79 return 0;
80 }
81
82 if ((s = getsock(AF_UNSPEC)) == -1)
83 return -1;
84
85 if ((ifname = getifname(env)) == NULL)
86 return -1;
87
88 memset(&ifr, 0, sizeof(ifr));
89 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
90 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
91 return -1;
92
93 *flagsp = (unsigned short)ifr.ifr_flags;
94
95 prop_dictionary_set_uint64(oenv, "ifflags",
96 (unsigned short)ifr.ifr_flags);
97
98 return 0;
99 }
100
101 const char *
102 getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
103 {
104 if (getifflags(env, oenv, flagsp) == -1)
105 return NULL;
106
107 return getifname(env);
108 }
109
110 const char *
111 getifname(prop_dictionary_t env)
112 {
113 const char *s;
114
115 return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
116 }
117
118 ssize_t
119 getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
120 {
121 prop_data_t data;
122 size_t datalen;
123
124 data = (prop_data_t)prop_dictionary_get(env, key);
125 if (data == NULL) {
126 errno = ENOENT;
127 return -1;
128 }
129 datalen = prop_data_size(data);
130 if (datalen > buflen) {
131 errno = ENAMETOOLONG;
132 return -1;
133 }
134 memset(buf, 0, buflen);
135 memcpy(buf, prop_data_data_nocopy(data), datalen);
136 return datalen;
137 }
138
139 ssize_t
140 getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
141 {
142 prop_data_t data;
143 size_t datalen;
144
145 data = (prop_data_t)prop_dictionary_get(env, "bssid");
146 if (data == NULL) {
147 errno = ENOENT;
148 return -1;
149 }
150 datalen = prop_data_size(data);
151 if (datalen >= buflen) {
152 errno = ENAMETOOLONG;
153 return -1;
154 }
155 memset(buf, 0, buflen);
156 memcpy(buf, prop_data_data_nocopy(data), datalen);
157 return datalen;
158 }
159
160 int
161 getaf(prop_dictionary_t env)
162 {
163 int64_t af;
164
165 if (!prop_dictionary_get_int64(env, "af", &af)) {
166 errno = ENOENT;
167 return -1;
168 }
169 return (int)af;
170 }
171