Home | History | Annotate | Line # | Download | only in ifconfig
af_atalk.c revision 1.5
      1 /*	$NetBSD: af_atalk.c,v 1.5 2008/04/11 00:55:41 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *      The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #ifndef INET_ONLY
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: af_atalk.c,v 1.5 2008/04/11 00:55:41 dyoung Exp $");
     37 #endif /* not lint */
     38 
     39 #include <sys/param.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/socket.h>
     42 
     43 #include <net/if.h>
     44 
     45 #include <netatalk/at.h>
     46 
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <string.h>
     50 #include <stdlib.h>
     51 #include <stdio.h>
     52 #include <util.h>
     53 
     54 #include "extern.h"
     55 #include "af_atalk.h"
     56 
     57 static struct netrange at_nr;	/* AppleTalk net range */
     58 
     59 void
     60 at_getaddr(const char *addr, int which)
     61 {
     62 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
     63 	u_int net, node;
     64 
     65 	sat->sat_family = AF_APPLETALK;
     66 	sat->sat_len = sizeof(*sat);
     67 	if (which == MASK)
     68 		errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
     69 	if (sscanf(addr, "%u.%u", &net, &node) != 2
     70 	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
     71 		errx(EXIT_FAILURE, "%s: illegal address", addr);
     72 	sat->sat_addr.s_net = htons(net);
     73 	sat->sat_addr.s_node = node;
     74 }
     75 
     76 void
     77 setatrange(const char *range, int d)
     78 {
     79 	u_short	first = 123, last = 123;
     80 
     81 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
     82 	    || first == 0 /* || first > 0xffff */
     83 	    || last == 0 /* || last > 0xffff */ || first > last)
     84 		errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
     85 		    first, last);
     86 	at_nr.nr_firstnet = htons(first);
     87 	at_nr.nr_lastnet = htons(last);
     88 }
     89 
     90 void
     91 setatphase(const char *phase, int d)
     92 {
     93 	if (!strcmp(phase, "1"))
     94 		at_nr.nr_phase = 1;
     95 	else if (!strcmp(phase, "2"))
     96 		at_nr.nr_phase = 2;
     97 	else
     98 		errx(EXIT_FAILURE, "%s: illegal phase", phase);
     99 }
    100 
    101 void
    102 checkatrange(struct sockaddr *sa)
    103 {
    104 	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
    105 
    106 	if (at_nr.nr_phase == 0)
    107 		at_nr.nr_phase = 2;	/* Default phase 2 */
    108 	if (at_nr.nr_firstnet == 0)
    109 		at_nr.nr_firstnet =	/* Default range of one */
    110 		at_nr.nr_lastnet = sat->sat_addr.s_net;
    111 	printf("\tatalk %d.%d range %d-%d phase %d\n",
    112 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    113 	    ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
    114 	if (ntohs(at_nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
    115 	    ntohs(at_nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
    116 		errx(EXIT_FAILURE, "AppleTalk address is not in range");
    117 	*((struct netrange *)&sat->sat_zero) = at_nr;
    118 }
    119 
    120 void
    121 at_status(int force)
    122 {
    123 	struct sockaddr_at *sat, null_sat;
    124 	struct netrange *nr;
    125 
    126 	getsock(AF_APPLETALK);
    127 	if (s < 0) {
    128 		if (errno == EAFNOSUPPORT)
    129 			return;
    130 		err(EXIT_FAILURE, "socket");
    131 	}
    132 	(void) memset(&ifr, 0, sizeof(ifr));
    133 	estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    134 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
    135 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    136 			if (!force)
    137 				return;
    138 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    139 		} else
    140 			warn("SIOCGIFADDR");
    141 	}
    142 	estrlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
    143 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
    144 
    145 	(void) memset(&null_sat, 0, sizeof(null_sat));
    146 
    147 	nr = (struct netrange *) &sat->sat_zero;
    148 	printf("\tatalk %d.%d range %d-%d phase %d",
    149 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    150 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
    151 	if (flags & IFF_POINTOPOINT) {
    152 		if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
    153 			if (errno == EADDRNOTAVAIL)
    154 			    (void) memset(&ifr.ifr_addr, 0,
    155 				sizeof(ifr.ifr_addr));
    156 			else
    157 			    warn("SIOCGIFDSTADDR");
    158 		}
    159 		estrlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
    160 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
    161 		if (!sat)
    162 			sat = &null_sat;
    163 		printf("--> %d.%d",
    164 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
    165 	}
    166 	if (flags & IFF_BROADCAST) {
    167 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
    168 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
    169 		if (sat)
    170 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
    171 			    sat->sat_addr.s_node);
    172 	}
    173 	printf("\n");
    174 }
    175 
    176 #endif /* ! INET_ONLY */
    177