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