Home | History | Annotate | Line # | Download | only in ifconfig
agr.c revision 1.9
      1  1.9   dyoung /*	$NetBSD: agr.c,v 1.9 2008/05/07 19:55:24 dyoung Exp $	*/
      2  1.1     yamt 
      3  1.1     yamt /*-
      4  1.1     yamt  * Copyright (c)2005 YAMAMOTO Takashi,
      5  1.1     yamt  * All rights reserved.
      6  1.1     yamt  *
      7  1.1     yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1     yamt  * modification, are permitted provided that the following conditions
      9  1.1     yamt  * are met:
     10  1.1     yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1     yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1     yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1     yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1     yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1     yamt  *
     16  1.1     yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1     yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1     yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1     yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1     yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1     yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1     yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1     yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1     yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1     yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1     yamt  * SUCH DAMAGE.
     27  1.1     yamt  */
     28  1.1     yamt 
     29  1.1     yamt #include <sys/cdefs.h>
     30  1.1     yamt #if !defined(lint)
     31  1.9   dyoung __RCSID("$NetBSD: agr.c,v 1.9 2008/05/07 19:55:24 dyoung Exp $");
     32  1.1     yamt #endif /* !defined(lint) */
     33  1.1     yamt 
     34  1.1     yamt #include <sys/param.h>
     35  1.1     yamt #include <sys/ioctl.h>
     36  1.1     yamt 
     37  1.1     yamt #include <net/if.h>
     38  1.1     yamt #include <net/agr/if_agrioctl.h>
     39  1.1     yamt 
     40  1.1     yamt #include <ctype.h>
     41  1.1     yamt #include <err.h>
     42  1.1     yamt #include <errno.h>
     43  1.1     yamt #include <string.h>
     44  1.5   dyoung #include <stdio.h>
     45  1.1     yamt #include <stdlib.h>
     46  1.1     yamt #include <util.h>
     47  1.1     yamt 
     48  1.5   dyoung #include "env.h"
     49  1.5   dyoung #include "parse.h"
     50  1.2  thorpej #include "extern.h"
     51  1.1     yamt #include "agr.h"
     52  1.1     yamt 
     53  1.1     yamt static int checkifname(const char *);
     54  1.1     yamt static void assertifname(const char *);
     55  1.1     yamt 
     56  1.9   dyoung static const struct kwinst agrkw[] = {
     57  1.9   dyoung 	  {.k_word = "agrport", .k_type = KW_T_NUM, .k_neg = true,
     58  1.9   dyoung 	   .k_num = AGRCMD_ADDPORT, .k_negnum = AGRCMD_REMPORT,
     59  1.9   dyoung 	   .k_exec = agrsetport}
     60  1.9   dyoung };
     61  1.9   dyoung 
     62  1.9   dyoung struct pkw agr = PKW_INITIALIZER(&agr, "agr", NULL, NULL,
     63  1.9   dyoung     agrkw, __arraycount(agrkw), NULL);
     64  1.9   dyoung 
     65  1.1     yamt static int
     66  1.3  thorpej checkifname(const char *ifname)
     67  1.1     yamt {
     68  1.1     yamt 
     69  1.3  thorpej 	return strncmp(ifname, "agr", 3) != 0 ||
     70  1.3  thorpej 	    !isdigit((unsigned char)ifname[3]);
     71  1.1     yamt }
     72  1.1     yamt 
     73  1.1     yamt static void
     74  1.3  thorpej assertifname(const char *ifname)
     75  1.1     yamt {
     76  1.1     yamt 
     77  1.3  thorpej 	if (checkifname(ifname)) {
     78  1.1     yamt 		errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
     79  1.1     yamt 	}
     80  1.1     yamt }
     81  1.1     yamt 
     82  1.5   dyoung int
     83  1.5   dyoung agrsetport(prop_dictionary_t env, prop_dictionary_t xenv)
     84  1.1     yamt {
     85  1.4   dyoung 	char buf[IFNAMSIZ];
     86  1.1     yamt 	struct agrreq ar;
     87  1.5   dyoung 	int s;
     88  1.7   dyoung 	const char *port;
     89  1.5   dyoung 	const char *ifname;
     90  1.5   dyoung 	struct ifreq ifr;
     91  1.7   dyoung 	int64_t cmd;
     92  1.5   dyoung 
     93  1.5   dyoung 	if ((s = getsock(AF_UNSPEC)) == -1)
     94  1.5   dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
     95  1.1     yamt 
     96  1.7   dyoung 	if (!prop_dictionary_get_int64(env, "agrcmd", &cmd)) {
     97  1.5   dyoung 		warnx("%s.%d", __func__, __LINE__);
     98  1.5   dyoung 		errno = ENOENT;
     99  1.5   dyoung 		return -1;
    100  1.5   dyoung 	}
    101  1.4   dyoung 
    102  1.7   dyoung 	if (!prop_dictionary_get_cstring_nocopy(env, "agrport", &port)) {
    103  1.5   dyoung 		warnx("%s.%d", __func__, __LINE__);
    104  1.5   dyoung 		errno = ENOENT;
    105  1.5   dyoung 		return -1;
    106  1.5   dyoung 	}
    107  1.7   dyoung 	strlcpy(buf, port, sizeof(buf));
    108  1.4   dyoung 
    109  1.5   dyoung 	memset(&ifr, 0, sizeof(ifr));
    110  1.5   dyoung 	if ((ifname = getifname(env)) == NULL)
    111  1.5   dyoung 		err(EXIT_FAILURE, "%s: getifname", __func__);
    112  1.8     yamt 	assertifname(ifname);
    113  1.5   dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    114  1.1     yamt 	memset(&ar, 0, sizeof(ar));
    115  1.1     yamt 	ar.ar_version = AGRREQ_VERSION;
    116  1.7   dyoung 	ar.ar_cmd = cmd;
    117  1.4   dyoung 	ar.ar_buf = buf;
    118  1.4   dyoung 	ar.ar_buflen = strlen(buf);
    119  1.4   dyoung 	ifr.ifr_data = &ar;
    120  1.1     yamt 
    121  1.4   dyoung 	if (ioctl(s, SIOCSETAGR, &ifr) == -1)
    122  1.1     yamt 		err(EXIT_FAILURE, "SIOCSETAGR");
    123  1.5   dyoung 	return 0;
    124  1.1     yamt }
    125  1.1     yamt 
    126  1.1     yamt void
    127  1.6   dyoung agr_status(prop_dictionary_t env, prop_dictionary_t oenv)
    128  1.1     yamt {
    129  1.1     yamt 	struct agrreq ar;
    130  1.1     yamt 	void *buf = NULL;
    131  1.1     yamt 	size_t buflen = 0;
    132  1.1     yamt 	struct agrportlist *apl;
    133  1.1     yamt 	struct agrportinfo *api;
    134  1.1     yamt 	int i;
    135  1.5   dyoung 	int s;
    136  1.5   dyoung 	const char *ifname;
    137  1.5   dyoung 	struct ifreq ifr;
    138  1.5   dyoung 
    139  1.5   dyoung 	if ((s = getsock(AF_UNSPEC)) == -1)
    140  1.5   dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
    141  1.5   dyoung 
    142  1.5   dyoung 	memset(&ifr, 0, sizeof(ifr));
    143  1.5   dyoung 	if ((ifname = getifname(env)) == NULL)
    144  1.5   dyoung 		err(EXIT_FAILURE, "%s: getifname", __func__);
    145  1.5   dyoung 	if (checkifname(ifname))
    146  1.1     yamt 		return;
    147  1.5   dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    148  1.1     yamt 
    149  1.1     yamt again:
    150  1.1     yamt 	memset(&ar, 0, sizeof(ar));
    151  1.1     yamt 	ar.ar_version = AGRREQ_VERSION;
    152  1.1     yamt 	ar.ar_cmd = AGRCMD_PORTLIST;
    153  1.1     yamt 	ar.ar_buf = buf;
    154  1.1     yamt 	ar.ar_buflen = buflen;
    155  1.5   dyoung 	ifr.ifr_data = &ar;
    156  1.1     yamt 
    157  1.1     yamt 	if (ioctl(s, SIOCGETAGR, &ifr) == -1) {
    158  1.1     yamt 		if (errno != E2BIG) {
    159  1.1     yamt 			warn("SIOCGETAGR");
    160  1.1     yamt 			return;
    161  1.1     yamt 		}
    162  1.1     yamt 
    163  1.1     yamt 		free(buf);
    164  1.1     yamt 		buf = NULL;
    165  1.1     yamt 		buflen = 0;
    166  1.1     yamt 		goto again;
    167  1.1     yamt 	}
    168  1.1     yamt 
    169  1.1     yamt 	if (buf == NULL) {
    170  1.1     yamt 		buflen = ar.ar_buflen;
    171  1.1     yamt 		buf = malloc(buflen);
    172  1.1     yamt 		if (buf == NULL) {
    173  1.1     yamt 			err(EXIT_FAILURE, "agr_status");
    174  1.1     yamt 		}
    175  1.1     yamt 		goto again;
    176  1.1     yamt 	}
    177  1.1     yamt 
    178  1.1     yamt 	apl = buf;
    179  1.1     yamt 	api = (void *)(apl + 1);
    180  1.1     yamt 
    181  1.1     yamt 	for (i = 0; i < apl->apl_nports; i++) {
    182  1.1     yamt 		char tmp[256];
    183  1.1     yamt 
    184  1.1     yamt 		snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
    185  1.1     yamt 		printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
    186  1.1     yamt 		api++;
    187  1.1     yamt 	}
    188  1.1     yamt }
    189