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