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