env.c revision 1.8 1 /* $NetBSD: env.c,v 1.8 2013/02/07 11:24:15 apb 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 <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: env.c,v 1.8 2013/02/07 11:24:15 apb Exp $");
31 #endif /* not lint */
32
33 #include <errno.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <util.h>
37
38 #include <net/if.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41
42 #include "env.h"
43 #include "util.h"
44 #include "prog_ops.h"
45
46 prop_dictionary_t
47 prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top)
48 {
49 prop_object_iterator_t i;
50 prop_dictionary_t d;
51 prop_object_t ko, o;
52 prop_dictionary_keysym_t k;
53 const char *key;
54
55 d = prop_dictionary_copy_mutable(bottom);
56
57 i = prop_dictionary_iterator(top);
58
59 while ((ko = prop_object_iterator_next(i)) != NULL) {
60 k = (prop_dictionary_keysym_t)ko;
61 key = prop_dictionary_keysym_cstring_nocopy(k);
62 o = prop_dictionary_get_keysym(top, k);
63 if (o == NULL || !prop_dictionary_set(d, key, o)) {
64 prop_object_release((prop_object_t)d);
65 d = NULL;
66 break;
67 }
68 }
69 prop_object_iterator_release(i);
70 if (d !== NULL)
71 prop_dictionary_make_immutable(d);
72 return d;
73 }
74
75 int
76 getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
77 unsigned short *flagsp)
78 {
79 struct ifreq ifr;
80 const char *ifname;
81 uint64_t ifflags;
82 int s;
83
84 if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
85 *flagsp = (unsigned short)ifflags;
86 return 0;
87 }
88
89 if ((s = getsock(AF_UNSPEC)) == -1)
90 return -1;
91
92 if ((ifname = getifname(env)) == NULL)
93 return -1;
94
95 memset(&ifr, 0, sizeof(ifr));
96 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
97 if (prog_ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
98 return -1;
99
100 *flagsp = (unsigned short)ifr.ifr_flags;
101
102 prop_dictionary_set_uint64(oenv, "ifflags",
103 (unsigned short)ifr.ifr_flags);
104
105 return 0;
106 }
107
108 const char *
109 getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
110 {
111 if (getifflags(env, oenv, flagsp) == -1)
112 return NULL;
113
114 return getifname(env);
115 }
116
117 const char *
118 getifname(prop_dictionary_t env)
119 {
120 const char *s;
121
122 return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
123 }
124
125 ssize_t
126 getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
127 {
128 prop_data_t data;
129 size_t datalen;
130
131 data = (prop_data_t)prop_dictionary_get(env, key);
132 if (data == NULL) {
133 errno = ENOENT;
134 return -1;
135 }
136 datalen = prop_data_size(data);
137 if (datalen > buflen) {
138 errno = ENAMETOOLONG;
139 return -1;
140 }
141 memset(buf, 0, buflen);
142 memcpy(buf, prop_data_data_nocopy(data), datalen);
143 return datalen;
144 }
145
146 ssize_t
147 getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
148 {
149 prop_data_t data;
150 size_t datalen;
151
152 data = (prop_data_t)prop_dictionary_get(env, key);
153 if (data == NULL) {
154 errno = ENOENT;
155 return -1;
156 }
157 datalen = prop_data_size(data);
158 if (datalen >= buflen) {
159 errno = ENAMETOOLONG;
160 return -1;
161 }
162 memset(buf, 0, buflen);
163 memcpy(buf, prop_data_data_nocopy(data), datalen);
164 return datalen;
165 }
166
167 int
168 getaf(prop_dictionary_t env)
169 {
170 int64_t af;
171
172 if (!prop_dictionary_get_int64(env, "af", &af)) {
173 errno = ENOENT;
174 return -1;
175 }
176 return (int)af;
177 }
178