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