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