Home | History | Annotate | Line # | Download | only in ifconfig
env.c revision 1.5.4.2
      1  1.5.4.2  mjf /*	$NetBSD: env.c,v 1.5.4.2 2008/06/02 13:21:22 mjf Exp $	*/
      2  1.5.4.2  mjf 
      3  1.5.4.2  mjf /*-
      4  1.5.4.2  mjf  * Copyright (c) 2008 David Young.  All rights reserved.
      5  1.5.4.2  mjf  *
      6  1.5.4.2  mjf  * Redistribution and use in source and binary forms, with or without
      7  1.5.4.2  mjf  * modification, are permitted provided that the following conditions
      8  1.5.4.2  mjf  * are met:
      9  1.5.4.2  mjf  * 1. Redistributions of source code must retain the above copyright
     10  1.5.4.2  mjf  *    notice, this list of conditions and the following disclaimer.
     11  1.5.4.2  mjf  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.5.4.2  mjf  *    notice, this list of conditions and the following disclaimer in the
     13  1.5.4.2  mjf  *    documentation and/or other materials provided with the distribution.
     14  1.5.4.2  mjf  *
     15  1.5.4.2  mjf  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.5.4.2  mjf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.5.4.2  mjf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.5.4.2  mjf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.5.4.2  mjf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.5.4.2  mjf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.5.4.2  mjf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.5.4.2  mjf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.5.4.2  mjf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.5.4.2  mjf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.5.4.2  mjf  * SUCH DAMAGE.
     26  1.5.4.2  mjf  */
     27  1.5.4.2  mjf 
     28  1.5.4.2  mjf #include <errno.h>
     29  1.5.4.2  mjf #include <string.h>
     30  1.5.4.2  mjf #include <stdlib.h>
     31  1.5.4.2  mjf #include <util.h>
     32  1.5.4.2  mjf 
     33  1.5.4.2  mjf #include <net/if.h>
     34  1.5.4.2  mjf #include <sys/socket.h>
     35  1.5.4.2  mjf #include <sys/ioctl.h>
     36  1.5.4.2  mjf 
     37  1.5.4.2  mjf #include "env.h"
     38  1.5.4.2  mjf #include "util.h"
     39  1.5.4.2  mjf 
     40  1.5.4.2  mjf prop_dictionary_t
     41  1.5.4.2  mjf prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top)
     42  1.5.4.2  mjf {
     43  1.5.4.2  mjf 	prop_object_iterator_t i;
     44  1.5.4.2  mjf 	prop_dictionary_t d;
     45  1.5.4.2  mjf 	prop_object_t ko, o;
     46  1.5.4.2  mjf 	prop_dictionary_keysym_t k;
     47  1.5.4.2  mjf 	const char *key;
     48  1.5.4.2  mjf 
     49  1.5.4.2  mjf 	d = prop_dictionary_copy_mutable(bottom);
     50  1.5.4.2  mjf 
     51  1.5.4.2  mjf 	i = prop_dictionary_iterator(top);
     52  1.5.4.2  mjf 
     53  1.5.4.2  mjf 	while ((ko = prop_object_iterator_next(i)) != NULL) {
     54  1.5.4.2  mjf 		k = (prop_dictionary_keysym_t)ko;
     55  1.5.4.2  mjf 		key = prop_dictionary_keysym_cstring_nocopy(k);
     56  1.5.4.2  mjf 		o = prop_dictionary_get_keysym(top, k);
     57  1.5.4.2  mjf 		if (o == NULL || !prop_dictionary_set(d, key, o)) {
     58  1.5.4.2  mjf 			prop_object_release((prop_object_t)d);
     59  1.5.4.2  mjf 			d = NULL;
     60  1.5.4.2  mjf 			break;
     61  1.5.4.2  mjf 		}
     62  1.5.4.2  mjf 	}
     63  1.5.4.2  mjf 	prop_object_iterator_release(i);
     64  1.5.4.2  mjf 	prop_dictionary_make_immutable(d);
     65  1.5.4.2  mjf 	return d;
     66  1.5.4.2  mjf }
     67  1.5.4.2  mjf 
     68  1.5.4.2  mjf int
     69  1.5.4.2  mjf getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
     70  1.5.4.2  mjf     unsigned short *flagsp)
     71  1.5.4.2  mjf {
     72  1.5.4.2  mjf 	struct ifreq ifr;
     73  1.5.4.2  mjf 	const char *ifname;
     74  1.5.4.2  mjf 	uint64_t ifflags;
     75  1.5.4.2  mjf 	int s;
     76  1.5.4.2  mjf 
     77  1.5.4.2  mjf 	if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
     78  1.5.4.2  mjf 		*flagsp = (unsigned short)ifflags;
     79  1.5.4.2  mjf 		return 0;
     80  1.5.4.2  mjf 	}
     81  1.5.4.2  mjf 
     82  1.5.4.2  mjf 	if ((s = getsock(AF_UNSPEC)) == -1)
     83  1.5.4.2  mjf 		return -1;
     84  1.5.4.2  mjf 
     85  1.5.4.2  mjf 	if ((ifname = getifname(env)) == NULL)
     86  1.5.4.2  mjf 		return -1;
     87  1.5.4.2  mjf 
     88  1.5.4.2  mjf 	memset(&ifr, 0, sizeof(ifr));
     89  1.5.4.2  mjf 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     90  1.5.4.2  mjf 	if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
     91  1.5.4.2  mjf 		return -1;
     92  1.5.4.2  mjf 
     93  1.5.4.2  mjf 	*flagsp = (unsigned short)ifr.ifr_flags;
     94  1.5.4.2  mjf 
     95  1.5.4.2  mjf 	prop_dictionary_set_uint64(oenv, "ifflags",
     96  1.5.4.2  mjf 	    (unsigned short)ifr.ifr_flags);
     97  1.5.4.2  mjf 
     98  1.5.4.2  mjf 	return 0;
     99  1.5.4.2  mjf }
    100  1.5.4.2  mjf 
    101  1.5.4.2  mjf const char *
    102  1.5.4.2  mjf getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
    103  1.5.4.2  mjf {
    104  1.5.4.2  mjf 	if (getifflags(env, oenv, flagsp) == -1)
    105  1.5.4.2  mjf 		return NULL;
    106  1.5.4.2  mjf 
    107  1.5.4.2  mjf 	return getifname(env);
    108  1.5.4.2  mjf }
    109  1.5.4.2  mjf 
    110  1.5.4.2  mjf const char *
    111  1.5.4.2  mjf getifname(prop_dictionary_t env)
    112  1.5.4.2  mjf {
    113  1.5.4.2  mjf 	const char *s;
    114  1.5.4.2  mjf 
    115  1.5.4.2  mjf 	return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
    116  1.5.4.2  mjf }
    117  1.5.4.2  mjf 
    118  1.5.4.2  mjf ssize_t
    119  1.5.4.2  mjf getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
    120  1.5.4.2  mjf {
    121  1.5.4.2  mjf 	prop_data_t data;
    122  1.5.4.2  mjf 	size_t datalen;
    123  1.5.4.2  mjf 
    124  1.5.4.2  mjf 	data = (prop_data_t)prop_dictionary_get(env, key);
    125  1.5.4.2  mjf 	if (data == NULL) {
    126  1.5.4.2  mjf 		errno = ENOENT;
    127  1.5.4.2  mjf 		return -1;
    128  1.5.4.2  mjf 	}
    129  1.5.4.2  mjf 	datalen = prop_data_size(data);
    130  1.5.4.2  mjf 	if (datalen > buflen) {
    131  1.5.4.2  mjf 		errno = ENAMETOOLONG;
    132  1.5.4.2  mjf 		return -1;
    133  1.5.4.2  mjf 	}
    134  1.5.4.2  mjf 	memset(buf, 0, buflen);
    135  1.5.4.2  mjf 	memcpy(buf, prop_data_data_nocopy(data), datalen);
    136  1.5.4.2  mjf 	return datalen;
    137  1.5.4.2  mjf }
    138  1.5.4.2  mjf 
    139  1.5.4.2  mjf ssize_t
    140  1.5.4.2  mjf getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
    141  1.5.4.2  mjf {
    142  1.5.4.2  mjf 	prop_data_t data;
    143  1.5.4.2  mjf 	size_t datalen;
    144  1.5.4.2  mjf 
    145  1.5.4.2  mjf 	data = (prop_data_t)prop_dictionary_get(env, key);
    146  1.5.4.2  mjf 	if (data == NULL) {
    147  1.5.4.2  mjf 		errno = ENOENT;
    148  1.5.4.2  mjf 		return -1;
    149  1.5.4.2  mjf 	}
    150  1.5.4.2  mjf 	datalen = prop_data_size(data);
    151  1.5.4.2  mjf 	if (datalen >= buflen) {
    152  1.5.4.2  mjf 		errno = ENAMETOOLONG;
    153  1.5.4.2  mjf 		return -1;
    154  1.5.4.2  mjf 	}
    155  1.5.4.2  mjf 	memset(buf, 0, buflen);
    156  1.5.4.2  mjf 	memcpy(buf, prop_data_data_nocopy(data), datalen);
    157  1.5.4.2  mjf 	return datalen;
    158  1.5.4.2  mjf }
    159  1.5.4.2  mjf 
    160  1.5.4.2  mjf int
    161  1.5.4.2  mjf getaf(prop_dictionary_t env)
    162  1.5.4.2  mjf {
    163  1.5.4.2  mjf 	int64_t af;
    164  1.5.4.2  mjf 
    165  1.5.4.2  mjf 	if (!prop_dictionary_get_int64(env, "af", &af)) {
    166  1.5.4.2  mjf 		errno = ENOENT;
    167  1.5.4.2  mjf 		return -1;
    168  1.5.4.2  mjf 	}
    169  1.5.4.2  mjf 	return (int)af;
    170  1.5.4.2  mjf }
    171