Home | History | Annotate | Line # | Download | only in ifconfig
agr.c revision 1.1
      1  1.1  yamt /*	$NetBSD: agr.c,v 1.1 2005/03/18 11:11:51 yamt 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.1  yamt __RCSID("$NetBSD: agr.c,v 1.1 2005/03/18 11:11:51 yamt 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.1  yamt #include <stdlib.h>
     45  1.1  yamt #include <util.h>
     46  1.1  yamt 
     47  1.1  yamt #include "agr.h"
     48  1.1  yamt 
     49  1.1  yamt extern struct ifreq ifr;
     50  1.1  yamt extern int s;
     51  1.1  yamt 
     52  1.1  yamt static int checkifname(const char *);
     53  1.1  yamt static void assertifname(const char *);
     54  1.1  yamt 
     55  1.1  yamt static int
     56  1.1  yamt checkifname(const char *name)
     57  1.1  yamt {
     58  1.1  yamt 
     59  1.1  yamt 	return strncmp(name, "agr", 3) != 0 ||
     60  1.1  yamt 	    !isdigit((unsigned char)name[3]);
     61  1.1  yamt }
     62  1.1  yamt 
     63  1.1  yamt static void
     64  1.1  yamt assertifname(const char *name)
     65  1.1  yamt {
     66  1.1  yamt 
     67  1.1  yamt 	if (checkifname(name)) {
     68  1.1  yamt 		errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
     69  1.1  yamt 	}
     70  1.1  yamt }
     71  1.1  yamt 
     72  1.1  yamt void
     73  1.1  yamt agraddport(const char *val, int d)
     74  1.1  yamt {
     75  1.1  yamt 	struct agrreq ar;
     76  1.1  yamt 
     77  1.1  yamt 	assertifname(ifr.ifr_name);
     78  1.1  yamt 
     79  1.1  yamt 	memset(&ar, 0, sizeof(ar));
     80  1.1  yamt 	ar.ar_version = AGRREQ_VERSION;
     81  1.1  yamt 	ar.ar_cmd = AGRCMD_ADDPORT;
     82  1.1  yamt 	ar.ar_buf = __UNCONST(val);
     83  1.1  yamt 	ar.ar_buflen = strlen(val);
     84  1.1  yamt 	ifr.ifr_data = (void *)&ar;
     85  1.1  yamt 
     86  1.1  yamt 	if (ioctl(s, SIOCSETAGR, &ifr) == -1) {
     87  1.1  yamt 		err(EXIT_FAILURE, "SIOCSETAGR");
     88  1.1  yamt 	}
     89  1.1  yamt }
     90  1.1  yamt 
     91  1.1  yamt void
     92  1.1  yamt agrremport(const char *val, int d)
     93  1.1  yamt {
     94  1.1  yamt 	struct agrreq ar;
     95  1.1  yamt 
     96  1.1  yamt 	assertifname(ifr.ifr_name);
     97  1.1  yamt 
     98  1.1  yamt 	memset(&ar, 0, sizeof(ar));
     99  1.1  yamt 	ar.ar_version = AGRREQ_VERSION;
    100  1.1  yamt 	ar.ar_cmd = AGRCMD_REMPORT;
    101  1.1  yamt 	ar.ar_buf = __UNCONST(val);
    102  1.1  yamt 	ar.ar_buflen = strlen(val);
    103  1.1  yamt 	ifr.ifr_data = (void *)&ar;
    104  1.1  yamt 
    105  1.1  yamt 	if (ioctl(s, SIOCSETAGR, &ifr) == -1) {
    106  1.1  yamt 		err(EXIT_FAILURE, "SIOCSETAGR");
    107  1.1  yamt 	}
    108  1.1  yamt }
    109  1.1  yamt 
    110  1.1  yamt void
    111  1.1  yamt agr_status()
    112  1.1  yamt {
    113  1.1  yamt 	struct agrreq ar;
    114  1.1  yamt 	void *buf = NULL;
    115  1.1  yamt 	size_t buflen = 0;
    116  1.1  yamt 	struct agrportlist *apl;
    117  1.1  yamt 	struct agrportinfo *api;
    118  1.1  yamt 	int i;
    119  1.1  yamt 
    120  1.1  yamt 	if (checkifname(ifr.ifr_name)) {
    121  1.1  yamt 		return;
    122  1.1  yamt 	}
    123  1.1  yamt 
    124  1.1  yamt again:
    125  1.1  yamt 	memset(&ar, 0, sizeof(ar));
    126  1.1  yamt 	ar.ar_version = AGRREQ_VERSION;
    127  1.1  yamt 	ar.ar_cmd = AGRCMD_PORTLIST;
    128  1.1  yamt 	ar.ar_buf = buf;
    129  1.1  yamt 	ar.ar_buflen = buflen;
    130  1.1  yamt 	ifr.ifr_data = (void *)&ar;
    131  1.1  yamt 
    132  1.1  yamt 	if (ioctl(s, SIOCGETAGR, &ifr) == -1) {
    133  1.1  yamt 		if (errno != E2BIG) {
    134  1.1  yamt 			warn("SIOCGETAGR");
    135  1.1  yamt 			return;
    136  1.1  yamt 		}
    137  1.1  yamt 
    138  1.1  yamt 		free(buf);
    139  1.1  yamt 		buf = NULL;
    140  1.1  yamt 		buflen = 0;
    141  1.1  yamt 		goto again;
    142  1.1  yamt 	}
    143  1.1  yamt 
    144  1.1  yamt 	if (buf == NULL) {
    145  1.1  yamt 		buflen = ar.ar_buflen;
    146  1.1  yamt 		buf = malloc(buflen);
    147  1.1  yamt 		if (buf == NULL) {
    148  1.1  yamt 			err(EXIT_FAILURE, "agr_status");
    149  1.1  yamt 		}
    150  1.1  yamt 		goto again;
    151  1.1  yamt 	}
    152  1.1  yamt 
    153  1.1  yamt 	apl = buf;
    154  1.1  yamt 	api = (void *)(apl + 1);
    155  1.1  yamt 
    156  1.1  yamt 	for (i = 0; i < apl->apl_nports; i++) {
    157  1.1  yamt 		char tmp[256];
    158  1.1  yamt 
    159  1.1  yamt 		snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
    160  1.1  yamt 		printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
    161  1.1  yamt 		api++;
    162  1.1  yamt 	}
    163  1.1  yamt }
    164