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