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