agr.c revision 1.5
1/* $NetBSD: agr.c,v 1.5 2008/05/06 04:33:42 dyoung 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.5 2008/05/06 04:33:42 dyoung 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 53static int checkifname(const char *); 54static void assertifname(const char *); 55 56static int 57checkifname(const char *ifname) 58{ 59 60 return strncmp(ifname, "agr", 3) != 0 || 61 !isdigit((unsigned char)ifname[3]); 62} 63 64static void 65assertifname(const char *ifname) 66{ 67 68 if (checkifname(ifname)) { 69 errx(EXIT_FAILURE, "valid only with agr(4) interfaces"); 70 } 71} 72 73int 74agrsetport(prop_dictionary_t env, prop_dictionary_t xenv) 75{ 76 char buf[IFNAMSIZ]; 77 struct agrreq ar; 78 int s; 79 prop_string_t str; 80 prop_number_t num; 81 const char *ifname; 82 struct ifreq ifr; 83 84 if ((s = getsock(AF_UNSPEC)) == -1) 85 err(EXIT_FAILURE, "%s: getsock", __func__); 86 87 assertifname(ifr.ifr_name); 88 89 num = (prop_number_t)prop_dictionary_get(env, "agrcmd"); 90 if (num == NULL) { 91 warnx("%s.%d", __func__, __LINE__); 92 errno = ENOENT; 93 return -1; 94 } 95 96 str = (prop_string_t)prop_dictionary_get(env, "agrport"); 97 if (str == NULL) { 98 warnx("%s.%d", __func__, __LINE__); 99 errno = ENOENT; 100 return -1; 101 } 102 strlcpy(buf, prop_string_cstring_nocopy(str), sizeof(buf)); 103 104 memset(&ifr, 0, sizeof(ifr)); 105 if ((ifname = getifname(env)) == NULL) 106 err(EXIT_FAILURE, "%s: getifname", __func__); 107 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 108 memset(&ar, 0, sizeof(ar)); 109 ar.ar_version = AGRREQ_VERSION; 110 ar.ar_cmd = (int)prop_number_integer_value(num); 111 ar.ar_buf = buf; 112 ar.ar_buflen = strlen(buf); 113 ifr.ifr_data = &ar; 114 115 if (ioctl(s, SIOCSETAGR, &ifr) == -1) 116 err(EXIT_FAILURE, "SIOCSETAGR"); 117 return 0; 118} 119 120void 121agr_status(prop_dictionary_t env) 122{ 123 struct agrreq ar; 124 void *buf = NULL; 125 size_t buflen = 0; 126 struct agrportlist *apl; 127 struct agrportinfo *api; 128 int i; 129 int s; 130 const char *ifname; 131 struct ifreq ifr; 132 133 if ((s = getsock(AF_UNSPEC)) == -1) 134 err(EXIT_FAILURE, "%s: getsock", __func__); 135 136 memset(&ifr, 0, sizeof(ifr)); 137 if ((ifname = getifname(env)) == NULL) 138 err(EXIT_FAILURE, "%s: getifname", __func__); 139 if (checkifname(ifname)) 140 return; 141 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 142 143again: 144 memset(&ar, 0, sizeof(ar)); 145 ar.ar_version = AGRREQ_VERSION; 146 ar.ar_cmd = AGRCMD_PORTLIST; 147 ar.ar_buf = buf; 148 ar.ar_buflen = buflen; 149 ifr.ifr_data = &ar; 150 151 if (ioctl(s, SIOCGETAGR, &ifr) == -1) { 152 if (errno != E2BIG) { 153 warn("SIOCGETAGR"); 154 return; 155 } 156 157 free(buf); 158 buf = NULL; 159 buflen = 0; 160 goto again; 161 } 162 163 if (buf == NULL) { 164 buflen = ar.ar_buflen; 165 buf = malloc(buflen); 166 if (buf == NULL) { 167 err(EXIT_FAILURE, "agr_status"); 168 } 169 goto again; 170 } 171 172 apl = buf; 173 api = (void *)(apl + 1); 174 175 for (i = 0; i < apl->apl_nports; i++) { 176 char tmp[256]; 177 178 snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags); 179 printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp); 180 api++; 181 } 182} 183