Support new token ring ARP type
[platform/upstream/net-tools.git] / lib / hw.c
1 /*
2  * lib/hw.c   This file contains the top-level part of the hardware
3  *              support functions module.
4  *
5  * Version:     $Id: hw.c,v 1.15 1999/12/11 12:09:23 philip Exp $
6  *
7  * Maintainer:  Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
8  *
9  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10  *              Copyright 1993 MicroWalt Corporation
11  *
12  *980701 {1.21}  Arnaldo C. Melo       GNU gettext instead of catgets
13  *
14  *              This program is free software; you can redistribute it
15  *              and/or  modify it under  the terms of  the GNU General
16  *              Public  License as  published  by  the  Free  Software
17  *              Foundation;  either  version 2 of the License, or  (at
18  *              your option) any later version.
19  */
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/if_arp.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include "config.h"
30 #include "net-support.h"
31 #include "pathnames.h"
32 #include "intl.h"
33
34 extern struct hwtype unspec_hwtype;
35 extern struct hwtype loop_hwtype;
36
37 extern struct hwtype slip_hwtype;
38 extern struct hwtype cslip_hwtype;
39 extern struct hwtype slip6_hwtype;
40 extern struct hwtype cslip6_hwtype;
41 extern struct hwtype adaptive_hwtype;
42
43 extern struct hwtype ether_hwtype;
44 extern struct hwtype fddi_hwtype;
45 extern struct hwtype hippi_hwtype;
46 extern struct hwtype tr_hwtype;
47 #ifdef ARPHRD_IEEE802_TR
48 extern struct hwtype tr_hwtype1;
49 #endif
50
51 extern struct hwtype ax25_hwtype;
52 extern struct hwtype rose_hwtype;
53 extern struct hwtype netrom_hwtype;
54 extern struct hwtype tunnel_hwtype;
55
56 extern struct hwtype ash_hwtype;
57
58 extern struct hwtype ppp_hwtype;
59
60 extern struct hwtype arcnet_hwtype;
61
62 extern struct hwtype dlci_hwtype;
63 extern struct hwtype frad_hwtype;
64
65 extern struct hwtype hdlc_hwtype;
66 extern struct hwtype lapb_hwtype;
67
68 extern struct hwtype sit_hwtype;
69
70 extern struct hwtype irda_hwtype;
71
72 extern struct hwtype ec_hwtype;
73
74 static struct hwtype *hwtypes[] =
75 {
76
77     &loop_hwtype,
78
79 #if HAVE_HWSLIP
80     &slip_hwtype,
81     &cslip_hwtype,
82     &slip6_hwtype,
83     &cslip6_hwtype,
84     &adaptive_hwtype,
85 #endif
86 #if HAVE_HWASH
87     &ash_hwtype,
88 #endif
89 #if HAVE_HWETHER
90     &ether_hwtype,
91 #endif
92 #if HAVE_HWTR
93     &tr_hwtype,
94 #ifdef ARPHRD_IEEE802_TR
95     &tr_hwtype1, 
96 #endif
97 #endif
98 #if HAVE_HWAX25
99     &ax25_hwtype,
100 #endif
101 #if HAVE_HWNETROM
102     &netrom_hwtype,
103 #endif
104 #if HAVE_HWROSE
105     &rose_hwtype,
106 #endif
107 #if HAVE_HWTUNNEL
108     &tunnel_hwtype,
109 #endif
110 #if HAVE_HWPPP
111     &ppp_hwtype,
112 #endif
113 #if HAVE_HWHDLCLAPB
114     &hdlc_hwtype,
115     &lapb_hwtype,
116 #endif
117 #if HAVE_HWARC
118     &arcnet_hwtype,
119 #endif
120 #if HAVE_HWFR
121     &dlci_hwtype,
122     &frad_hwtype,
123 #endif
124 #if HAVE_HWSIT
125     &sit_hwtype,
126 #endif
127 #if HAVE_HWFDDI
128     &fddi_hwtype,
129 #endif
130 #if HAVE_HWHIPPI
131     &hippi_hwtype,
132 #endif
133 #if HAVE_HWIRDA
134     &irda_hwtype,
135 #endif
136 #if HAVE_HWEC
137     &ec_hwtype,
138 #endif
139     &unspec_hwtype,
140     NULL
141 };
142
143 static short sVhwinit = 0;
144
145 void hwinit()
146 {
147     loop_hwtype.title = _("Local Loopback");
148     unspec_hwtype.title = _("UNSPEC");
149 #if HAVE_HWSLIP
150     slip_hwtype.title = _("Serial Line IP");
151     cslip_hwtype.title = _("VJ Serial Line IP");
152     slip6_hwtype.title = _("6-bit Serial Line IP");
153     cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
154     adaptive_hwtype.title = _("Adaptive Serial Line IP");
155 #endif
156 #if HAVE_HWETHER
157     ether_hwtype.title = _("Ethernet");
158 #endif
159 #if HAVE_HWASH
160     ash_hwtype.title = _("Ash");
161 #endif
162 #if HAVE_HWFDDI
163     fddi_hwtype.title = _("Fiber Distributed Data Interface");
164 #endif
165 #if HAVE_HWHIPPI
166     hippi_hwtype.title = _("HIPPI");
167 #endif
168 #if HAVE_HWAX25
169     ax25_hwtype.title = _("AMPR AX.25");
170 #endif
171 #if HAVE_HWROSE
172     rose_hwtype.title = _("AMPR ROSE");
173 #endif
174 #if HAVE_HWNETROM
175     netrom_hwtype.title = _("AMPR NET/ROM");
176 #endif
177 #if HAVE_HWTUNNEL
178     tunnel_hwtype.title = _("IPIP Tunnel");
179 #endif
180 #if HAVE_HWPPP
181     ppp_hwtype.title = _("Point-to-Point Protocol");
182 #endif
183 #if HAVE_HWHDLCLAPB
184     hdlc_hwtype.title = _("(Cisco)-HDLC");
185     lapb_hwtype.title = _("LAPB");
186 #endif
187 #if HAVE_HWARC
188     arcnet_hwtype.title = _("ARCnet");
189 #endif
190 #if HAVE_HWFR
191     dlci_hwtype.title = _("Frame Relay DLCI");
192     frad_hwtype.title = _("Frame Relay Access Device");
193 #endif
194 #if HAVE_HWSIT
195     sit_hwtype.title = _("IPv6-in-IPv4");
196 #endif
197 #if HAVE_HWIRDA
198     irda_hwtype.title = _("IrLAP");
199 #endif
200 #if HAVE_HWTR
201     tr_hwtype.title = _("16/4 Mbps Token Ring");
202 #ifdef ARPHRD_IEEE802_TR
203     tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ; 
204 #endif
205 #endif
206 #if HAVE_HWEC
207     ec_hwtype.title = _("Econet");
208 #endif
209     sVhwinit = 1;
210 }
211
212 /* Check our hardware type table for this type. */
213 struct hwtype *get_hwtype(const char *name)
214 {
215     struct hwtype **hwp;
216
217     if (!sVhwinit)
218         hwinit();
219
220     hwp = hwtypes;
221     while (*hwp != NULL) {
222         if (!strcmp((*hwp)->name, name))
223             return (*hwp);
224         hwp++;
225     }
226     return (NULL);
227 }
228
229
230 /* Check our hardware type table for this type. */
231 struct hwtype *get_hwntype(int type)
232 {
233     struct hwtype **hwp;
234
235     if (!sVhwinit)
236         hwinit();
237
238     hwp = hwtypes;
239     while (*hwp != NULL) {
240         if ((*hwp)->type == type)
241             return (*hwp);
242         hwp++;
243     }
244     return (NULL);
245 }
246
247 /* type: 0=all, 1=ARPable */
248 void print_hwlist(int type) {
249     int count = 0;
250     char * txt;
251     struct hwtype **hwp;
252
253     if (!sVhwinit)
254         hwinit();
255
256     hwp = hwtypes;
257     while (*hwp != NULL) {
258         if (((type == 1) && ((*hwp)->alen == 0)) || ((*hwp)->type == -1)) {
259                 hwp++; continue;
260         }
261         if ((count % 3) == 0) fprintf(stderr,count?"\n    ":"    "); 
262         txt = (*hwp)->name; if (!txt) txt = "..";
263         fprintf(stderr,"%s (%s) ",txt,(*hwp)->title);
264         count++;
265         hwp++;
266     }
267     fprintf(stderr,"\n");
268 }
269
270 /* return 1 if address is all zeros */
271 int hw_null_address(struct hwtype *hw, void *ap)
272 {
273     unsigned int i;
274     unsigned char *address = (unsigned char *)ap;
275     for (i = 0; i < hw->alen; i++)
276         if (address[i])
277             return 0;
278     return 1;
279 }