Home | History | Annotate | Line # | Download | only in ifconfig
env.c revision 1.7.12.1
      1  1.7.12.1     tls /*	$NetBSD: env.c,v 1.7.12.1 2013/02/25 00:28:08 tls 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.7.12.1     tls __RCSID("$NetBSD: env.c,v 1.7.12.1 2013/02/25 00:28:08 tls 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.7.12.1     tls 	if (d == NULL)
     57  1.7.12.1     tls 		return NULL;
     58       1.1  dyoung 
     59       1.1  dyoung 	i = prop_dictionary_iterator(top);
     60       1.1  dyoung 
     61  1.7.12.1     tls 	while (i != NULL && (ko = prop_object_iterator_next(i)) != NULL) {
     62       1.1  dyoung 		k = (prop_dictionary_keysym_t)ko;
     63       1.1  dyoung 		key = prop_dictionary_keysym_cstring_nocopy(k);
     64       1.1  dyoung 		o = prop_dictionary_get_keysym(top, k);
     65       1.1  dyoung 		if (o == NULL || !prop_dictionary_set(d, key, o)) {
     66       1.1  dyoung 			prop_object_release((prop_object_t)d);
     67       1.1  dyoung 			d = NULL;
     68       1.1  dyoung 			break;
     69       1.1  dyoung 		}
     70       1.1  dyoung 	}
     71  1.7.12.1     tls 	if (i != NULL)
     72  1.7.12.1     tls 		prop_object_iterator_release(i);
     73  1.7.12.1     tls 	if (d != NULL)
     74  1.7.12.1     tls 		prop_dictionary_make_immutable(d);
     75       1.1  dyoung 	return d;
     76       1.1  dyoung }
     77       1.1  dyoung 
     78       1.1  dyoung int
     79       1.1  dyoung getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
     80       1.1  dyoung     unsigned short *flagsp)
     81       1.1  dyoung {
     82       1.1  dyoung 	struct ifreq ifr;
     83       1.1  dyoung 	const char *ifname;
     84       1.3  dyoung 	uint64_t ifflags;
     85       1.1  dyoung 	int s;
     86       1.1  dyoung 
     87       1.3  dyoung 	if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
     88       1.3  dyoung 		*flagsp = (unsigned short)ifflags;
     89       1.1  dyoung 		return 0;
     90       1.1  dyoung 	}
     91       1.1  dyoung 
     92       1.1  dyoung 	if ((s = getsock(AF_UNSPEC)) == -1)
     93       1.1  dyoung 		return -1;
     94       1.1  dyoung 
     95       1.1  dyoung 	if ((ifname = getifname(env)) == NULL)
     96       1.1  dyoung 		return -1;
     97       1.1  dyoung 
     98       1.1  dyoung 	memset(&ifr, 0, sizeof(ifr));
     99       1.1  dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    100       1.7   pooka 	if (prog_ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
    101       1.1  dyoung 		return -1;
    102       1.1  dyoung 
    103       1.1  dyoung 	*flagsp = (unsigned short)ifr.ifr_flags;
    104       1.1  dyoung 
    105       1.3  dyoung 	prop_dictionary_set_uint64(oenv, "ifflags",
    106       1.1  dyoung 	    (unsigned short)ifr.ifr_flags);
    107       1.1  dyoung 
    108       1.1  dyoung 	return 0;
    109       1.1  dyoung }
    110       1.1  dyoung 
    111       1.1  dyoung const char *
    112       1.1  dyoung getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
    113       1.1  dyoung {
    114       1.1  dyoung 	if (getifflags(env, oenv, flagsp) == -1)
    115       1.1  dyoung 		return NULL;
    116       1.1  dyoung 
    117       1.1  dyoung 	return getifname(env);
    118       1.1  dyoung }
    119       1.1  dyoung 
    120       1.1  dyoung const char *
    121       1.1  dyoung getifname(prop_dictionary_t env)
    122       1.1  dyoung {
    123       1.3  dyoung 	const char *s;
    124       1.1  dyoung 
    125       1.3  dyoung 	return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
    126       1.1  dyoung }
    127       1.1  dyoung 
    128       1.1  dyoung ssize_t
    129       1.1  dyoung getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
    130       1.1  dyoung {
    131       1.1  dyoung 	prop_data_t data;
    132       1.1  dyoung 	size_t datalen;
    133       1.1  dyoung 
    134       1.1  dyoung 	data = (prop_data_t)prop_dictionary_get(env, key);
    135       1.1  dyoung 	if (data == NULL) {
    136       1.1  dyoung 		errno = ENOENT;
    137       1.1  dyoung 		return -1;
    138       1.1  dyoung 	}
    139       1.1  dyoung 	datalen = prop_data_size(data);
    140       1.1  dyoung 	if (datalen > buflen) {
    141       1.1  dyoung 		errno = ENAMETOOLONG;
    142       1.1  dyoung 		return -1;
    143       1.1  dyoung 	}
    144       1.1  dyoung 	memset(buf, 0, buflen);
    145       1.1  dyoung 	memcpy(buf, prop_data_data_nocopy(data), datalen);
    146       1.1  dyoung 	return datalen;
    147       1.1  dyoung }
    148       1.1  dyoung 
    149       1.1  dyoung ssize_t
    150       1.1  dyoung getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
    151       1.1  dyoung {
    152       1.1  dyoung 	prop_data_t data;
    153       1.1  dyoung 	size_t datalen;
    154       1.1  dyoung 
    155       1.4  dyoung 	data = (prop_data_t)prop_dictionary_get(env, key);
    156       1.1  dyoung 	if (data == NULL) {
    157       1.1  dyoung 		errno = ENOENT;
    158       1.1  dyoung 		return -1;
    159       1.1  dyoung 	}
    160       1.1  dyoung 	datalen = prop_data_size(data);
    161       1.1  dyoung 	if (datalen >= buflen) {
    162       1.1  dyoung 		errno = ENAMETOOLONG;
    163       1.1  dyoung 		return -1;
    164       1.1  dyoung 	}
    165       1.1  dyoung 	memset(buf, 0, buflen);
    166       1.1  dyoung 	memcpy(buf, prop_data_data_nocopy(data), datalen);
    167       1.1  dyoung 	return datalen;
    168       1.1  dyoung }
    169       1.1  dyoung 
    170       1.1  dyoung int
    171       1.1  dyoung getaf(prop_dictionary_t env)
    172       1.1  dyoung {
    173       1.3  dyoung 	int64_t af;
    174       1.1  dyoung 
    175       1.3  dyoung 	if (!prop_dictionary_get_int64(env, "af", &af)) {
    176       1.1  dyoung 		errno = ENOENT;
    177       1.1  dyoung 		return -1;
    178       1.1  dyoung 	}
    179       1.3  dyoung 	return (int)af;
    180       1.1  dyoung }
    181