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