af_atalk.c revision 1.1 1 1.1 thorpej /* $NetBSD: af_atalk.c,v 1.1 2005/03/19 23:32:55 thorpej 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.1 thorpej __RCSID("$NetBSD: af_atalk.c,v 1.1 2005/03/19 23:32:55 thorpej 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.1 thorpej
53 1.1 thorpej #include "extern.h"
54 1.1 thorpej #include "af_atalk.h"
55 1.1 thorpej
56 1.1 thorpej static struct netrange at_nr; /* AppleTalk net range */
57 1.1 thorpej
58 1.1 thorpej void
59 1.1 thorpej at_getaddr(const char *addr, int which)
60 1.1 thorpej {
61 1.1 thorpej struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
62 1.1 thorpej u_int net, node;
63 1.1 thorpej
64 1.1 thorpej sat->sat_family = AF_APPLETALK;
65 1.1 thorpej sat->sat_len = sizeof(*sat);
66 1.1 thorpej if (which == MASK)
67 1.1 thorpej errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
68 1.1 thorpej if (sscanf(addr, "%u.%u", &net, &node) != 2
69 1.1 thorpej || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
70 1.1 thorpej errx(EXIT_FAILURE, "%s: illegal address", addr);
71 1.1 thorpej sat->sat_addr.s_net = htons(net);
72 1.1 thorpej sat->sat_addr.s_node = node;
73 1.1 thorpej }
74 1.1 thorpej
75 1.1 thorpej void
76 1.1 thorpej setatrange(const char *range, int d)
77 1.1 thorpej {
78 1.1 thorpej u_short first = 123, last = 123;
79 1.1 thorpej
80 1.1 thorpej if (sscanf(range, "%hu-%hu", &first, &last) != 2
81 1.1 thorpej || first == 0 /* || first > 0xffff */
82 1.1 thorpej || last == 0 /* || last > 0xffff */ || first > last)
83 1.1 thorpej errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
84 1.1 thorpej first, last);
85 1.1 thorpej at_nr.nr_firstnet = htons(first);
86 1.1 thorpej at_nr.nr_lastnet = htons(last);
87 1.1 thorpej }
88 1.1 thorpej
89 1.1 thorpej void
90 1.1 thorpej setatphase(const char *phase, int d)
91 1.1 thorpej {
92 1.1 thorpej if (!strcmp(phase, "1"))
93 1.1 thorpej at_nr.nr_phase = 1;
94 1.1 thorpej else if (!strcmp(phase, "2"))
95 1.1 thorpej at_nr.nr_phase = 2;
96 1.1 thorpej else
97 1.1 thorpej errx(EXIT_FAILURE, "%s: illegal phase", phase);
98 1.1 thorpej }
99 1.1 thorpej
100 1.1 thorpej void
101 1.1 thorpej checkatrange(struct sockaddr *sa)
102 1.1 thorpej {
103 1.1 thorpej struct sockaddr_at *sat = (struct sockaddr_at *) sa;
104 1.1 thorpej
105 1.1 thorpej if (at_nr.nr_phase == 0)
106 1.1 thorpej at_nr.nr_phase = 2; /* Default phase 2 */
107 1.1 thorpej if (at_nr.nr_firstnet == 0)
108 1.1 thorpej at_nr.nr_firstnet = /* Default range of one */
109 1.1 thorpej at_nr.nr_lastnet = sat->sat_addr.s_net;
110 1.1 thorpej printf("\tatalk %d.%d range %d-%d phase %d\n",
111 1.1 thorpej ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
112 1.1 thorpej ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
113 1.1 thorpej if ((u_short) ntohs(at_nr.nr_firstnet) >
114 1.1 thorpej (u_short) ntohs(sat->sat_addr.s_net)
115 1.1 thorpej || (u_short) ntohs(at_nr.nr_lastnet) <
116 1.1 thorpej (u_short) ntohs(sat->sat_addr.s_net))
117 1.1 thorpej errx(EXIT_FAILURE, "AppleTalk address is not in range");
118 1.1 thorpej *((struct netrange *) &sat->sat_zero) = at_nr;
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej void
122 1.1 thorpej at_status(int force)
123 1.1 thorpej {
124 1.1 thorpej struct sockaddr_at *sat, null_sat;
125 1.1 thorpej struct netrange *nr;
126 1.1 thorpej
127 1.1 thorpej getsock(AF_APPLETALK);
128 1.1 thorpej if (s < 0) {
129 1.1 thorpej if (errno == EPROTONOSUPPORT)
130 1.1 thorpej return;
131 1.1 thorpej err(EXIT_FAILURE, "socket");
132 1.1 thorpej }
133 1.1 thorpej (void) memset(&ifr, 0, sizeof(ifr));
134 1.1 thorpej (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
135 1.1 thorpej if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
136 1.1 thorpej if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
137 1.1 thorpej if (!force)
138 1.1 thorpej return;
139 1.1 thorpej (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
140 1.1 thorpej } else
141 1.1 thorpej warn("SIOCGIFADDR");
142 1.1 thorpej }
143 1.1 thorpej (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
144 1.1 thorpej sat = (struct sockaddr_at *)&ifr.ifr_addr;
145 1.1 thorpej
146 1.1 thorpej (void) memset(&null_sat, 0, sizeof(null_sat));
147 1.1 thorpej
148 1.1 thorpej nr = (struct netrange *) &sat->sat_zero;
149 1.1 thorpej printf("\tatalk %d.%d range %d-%d phase %d",
150 1.1 thorpej ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
151 1.1 thorpej ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
152 1.1 thorpej if (flags & IFF_POINTOPOINT) {
153 1.1 thorpej if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
154 1.1 thorpej if (errno == EADDRNOTAVAIL)
155 1.1 thorpej (void) memset(&ifr.ifr_addr, 0,
156 1.1 thorpej sizeof(ifr.ifr_addr));
157 1.1 thorpej else
158 1.1 thorpej warn("SIOCGIFDSTADDR");
159 1.1 thorpej }
160 1.1 thorpej (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
161 1.1 thorpej sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
162 1.1 thorpej if (!sat)
163 1.1 thorpej sat = &null_sat;
164 1.1 thorpej printf("--> %d.%d",
165 1.1 thorpej ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
166 1.1 thorpej }
167 1.1 thorpej if (flags & IFF_BROADCAST) {
168 1.1 thorpej /* note RTAX_BRD overlap with IFF_POINTOPOINT */
169 1.1 thorpej sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
170 1.1 thorpej if (sat)
171 1.1 thorpej printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
172 1.1 thorpej sat->sat_addr.s_node);
173 1.1 thorpej }
174 1.1 thorpej printf("\n");
175 1.1 thorpej }
176 1.1 thorpej
177 1.1 thorpej #endif /* ! INET_ONLY */
178