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