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