source: trunk/third/sysinfo/macinfo.c @ 12269

Revision 12269, 7.5 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12268, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (c) 1992-1998 Michael A. Cooper.
3 * This software may be freely used and distributed provided it is not
4 * sold for profit or used in part or in whole for commercial gain
5 * without prior written agreement, and the author is credited
6 * appropriately.
7 */
8
9#ifndef lint
10static char *RCSid = "$Revision: 1.1.1.3 $";
11#endif
12
13/*
14 * Media Access Control (MAC) info routines
15 */
16
17#include "defs.h"
18
19#if     GETMAC_TYPE == GETMAC_IFREQ_ENADDR
20
21#include <sys/time.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/sockio.h>
25#include <net/if.h>
26
27/*
28 * Find and set the MAC info using ifreq.ifr_enaddr
29 */
30extern void SetMacInfo(DevInfo, NetIf)
31     DevInfo_t                 *DevInfo;
32     NetIF_t                   *NetIf;
33{
34    struct ifreq                ifreq;
35    struct ether_addr          *EtherAddr;
36    struct ether_addr          *ether_aton();
37    static char                 Buff[256];
38    static int                  Sock = -1;
39
40    if (!NetIf || !DevInfo || !DevInfo->Name)
41        return;
42
43    Sock = socket(AF_INET, SOCK_STREAM, 0);
44    if (Sock < 0) {
45        SImsg(SIM_GERR, "Create AF_INET SOCK_STREAM failed: %s", SYSERR);
46        return;
47    }
48
49    strncpy(ifreq.ifr_name, DevInfo->Name, sizeof(ifreq.ifr_name));
50    if (ioctl(Sock, SIOCGENADDR, &ifreq) < 0) {
51        SImsg(SIM_GERR, "%s: ioctl SIOCGENADDR failed: %s.",
52                         ifreq.ifr_name, SYSERR);
53        close(Sock);
54        return;
55    }
56    close(Sock);
57
58    NetIf->MACaddr = strdup(ifreq.ifr_enaddr);
59    EtherAddr = ether_aton(ifreq.ifr_enaddr);
60    if (EtherAddr && ether_ntohost(Buff, EtherAddr) == 0)
61        NetIf->MACname = strdup(Buff);
62}
63#endif  /* HAVE_IFREQ_ENADDR */
64
65#if     GETMAC_TYPE == GETMAC_NIT
66
67#include <sys/time.h>
68#include <sys/types.h>
69#include <sys/socket.h>
70#include <net/if.h>
71#include <net/nit_if.h>
72
73/*
74 * Find and set the MAC info using the Network Interface Tap (NIT)
75 */
76extern void SetMacInfo(DevInfo, NetIf)
77     DevInfo_t                 *DevInfo;
78     NetIF_t                   *NetIf;
79{
80    register struct sockaddr   *SockAddr;
81    struct ifreq                ifreq;
82    char                       *ether_ntoa(), Buf[MAXHOSTNAMLEN+1];
83    int                         Desc;
84
85    if (!NetIf || !DevInfo || !DevInfo->Name)
86        return;
87
88    if ((Desc = open("/dev/nit", O_RDONLY)) == SYSFAIL) {
89        SImsg(SIM_GERR, "open /dev/nit failed");
90        return;
91    }
92
93    /*
94     * Bind to NIT for DevName
95     */
96    strncpy(ifreq.ifr_name, DevInfo->Name, sizeof ifreq.ifr_name);
97    if (ioctl(Desc, NIOCBIND, (caddr_t) &ifreq) < 0) {
98        SImsg(SIM_GERR, "ioctl NIOCBIND failed: %s", SYSERR);
99        return;
100    }
101
102    /*
103     * Get address
104     */
105    if (ioctl(Desc, SIOCGIFADDR, (caddr_t)&ifreq) < 0) {
106        SImsg(SIM_GERR, "ioctl SIOCGIFADDR failed: %s", SYSERR);
107        return;
108    }
109
110    (void) close(Desc);
111
112    SockAddr = (struct sockaddr *)&ifreq.ifr_addr;
113    NetIf->MACaddr = strdup(ether_ntoa((struct ether_addr *)
114                                          SockAddr->sa_data));
115
116    if (ether_ntohost(Buf, (struct ether_addr *) SockAddr->sa_data) == 0)
117        NetIf->MACname = strdup(Buf);
118}
119#endif  /* HAVE_NIT */
120
121#if     GETMAC_TYPE == GETMAC_DLPI
122
123#include <sys/time.h>
124#include <sys/types.h>
125#include <sys/socket.h>
126#include <net/if.h>
127#include <sys/dlpi.h>
128#include <netinet/in.h>
129#include <netinet/if_ether.h>
130#include "dlpi.h"
131
132/*
133 * Find and set the MAC info using the Data Link Provider Interface (DLPI)
134 */
135extern void SetMacInfo(DevInfo, NetIf)
136     DevInfo_t                 *DevInfo;
137     NetIF_t                   *NetIf;
138{
139    char                        buff[MAXHOSTNAMLEN+1];
140    int                         fd;
141    long                        dlbuf[MAXDLBUF];
142    static char                 devname[MAXPATHLEN];
143    static struct ether_addr    ether_addr;
144    register char              *cp;
145    union DL_primitives        *dlp;
146    u_char                      addr[MAXDLADDR];
147
148    if (!NetIf || !DevInfo || !DevInfo->Name)
149        return;
150
151    dlp = (union DL_primitives *) dlbuf;
152    (void) snprintf(devname, sizeof(devname),  "%s/%s", _PATH_DEV, DevInfo->Name);
153    /*
154     * Remove unit part of name from the device name.
155     */
156    if (cp = strrchr(devname, '/')) {
157        ++cp;
158        while (!isdigit(*cp) && ++cp);
159        if (isdigit(*cp))
160            *cp = CNULL;
161    }
162
163    if ((fd = open(devname, O_RDWR)) == SYSFAIL) {
164        SImsg(SIM_GERR, "Cannot open %s: %s.", devname, SYSERR);
165        return;
166    }
167
168    /*
169     * Setup
170     */
171    dlattachreq(fd, DevInfo->Unit);
172    dlokack(fd, dlbuf);
173    dlbindreq(fd, 0, 0, DL_CLDLS, 0, 0);
174    dlbindack(fd, dlbuf);
175
176    /*
177     * Get current physical address
178     */
179    dlphysaddrreq(fd, DL_CURR_PHYS_ADDR);
180    dlphysaddrack(fd, dlbuf);
181
182    addrtostring(OFFADDR(dlp, dlp->physaddr_ack.dl_addr_offset),
183                 dlp->physaddr_ack.dl_addr_length, addr);
184
185    NetIf->MACaddr = strdup((char *)addr);
186
187    memcpy((char *) ether_addr.ether_addr_octet,
188           (char *) OFFADDR(dlp, dlp->physaddr_ack.dl_addr_offset),
189           dlp->physaddr_ack.dl_addr_length);
190    if (ether_ntohost(buff, &ether_addr) == 0)
191        NetIf->MACname = strdup(buff);
192
193    /*
194     * Get factory physical address
195     */
196    dlphysaddrreq(fd, DL_FACT_PHYS_ADDR);
197    dlphysaddrack(fd, dlbuf);
198
199    addrtostring(OFFADDR(dlp, dlp->physaddr_ack.dl_addr_offset),
200                 dlp->physaddr_ack.dl_addr_length, addr);
201
202    NetIf->FacMACaddr = strdup((char *)addr);
203
204    memcpy((char *) ether_addr.ether_addr_octet,
205           (char *) OFFADDR(dlp, dlp->physaddr_ack.dl_addr_offset),
206           dlp->physaddr_ack.dl_addr_length);
207    if (ether_ntohost(buff, &ether_addr) == 0)
208        NetIf->FacMACname = strdup(buff);
209
210    /*
211     * We're done
212     */
213    dlunbindreq(fd);
214    dldetachreq(fd);
215    (void) close(fd);
216}
217#endif  /* HAVE_DLPI */
218
219#if     defined(HAVE_PACKETFILTER)
220
221#include <sys/time.h>
222#include <net/pfilt.h>
223
224#include <fcntl.h>
225#include <sys/socket.h>
226#include <net/if.h>
227#include <netinet/in.h>
228#include <netinet/if_ether.h>
229
230#if     defined(NEED_ETHER_ADDR)
231/*
232 * This didn't appear in <netinet/if_ether.h> until Ultrix 4.2 (4.1?)
233 */
234struct ether_addr {
235        u_char  ether_addr_octet[6];
236};
237#endif  /* NEED_ETHER_ADDR */
238
239/*
240 * Get network type information
241 */
242static char *GetNetType(type)
243    int                         type;
244{
245    register int                i;
246    Define_t                   *Define;
247
248    Define = DefGet(DL_NETTYPE, NULL, (long) type, 0);
249    if (Define && Define->ValStr1)
250        return(Define->ValStr1);
251
252    return((char *) NULL);
253}
254
255/*
256 * Find and set the MAC info using the Packet Filter
257 */
258extern void SetMacInfo(DevInfo, NetIf)
259     DevInfo_t                 *DevInfo;
260     NetIF_t                   *NetIf;
261{
262    struct endevp               endevp;
263    struct ether_addr           ether_addr;
264    char                       *ether_ntoa(), HostBuf[MAXHOSTNAMLEN+1];
265    char                       *p;
266    int                         Desc;
267    char                       *DevName;
268
269    if (!NetIf || !DevInfo || !DevInfo->Name)
270        return;
271
272    DevName = DevInfo->Name;
273
274    /*
275     * Open this device using the packet filter
276     */
277    if ((Desc = pfopen(DevName, O_RDONLY)) < 0) {
278        SImsg(SIM_GERR, "pfopen %s failed: %s.", DevName, SYSERR);
279        return;
280    }
281
282    /*
283     * Retrieve info
284     */
285    if (ioctl(Desc, EIOCDEVP, &endevp) < 0) {
286        SImsg(SIM_GERR, "ioctl EIOCDEVP of %s failed: %s.", DevName, SYSERR);
287        return;
288    }
289
290    close(Desc);
291
292    /*
293     * Convert address into ethers(5) format
294     */
295    memcpy((char *) ether_addr.ether_addr_octet, (char *) endevp.end_addr,
296           endevp.end_addr_len);
297
298    /*
299     * Set what we now know.
300     */
301    if (p = ether_ntoa(&ether_addr))
302        Netif->MACaddr = strdup(p);
303
304    if (ether_ntohost(HostBuf, &ether_addr) == 0)
305        Netif->MACname = strdup(HostBuf);
306
307    if (DevInfo && (p = GetNetType(endevp.end_dev_type)))
308        AddDevDesc(DevInfo, p, NULL, DA_INSERT|DA_PRIME);
309}
310#endif  /* HAVE_PACKETFILTER */
311
312#if     !defined(GETMAC_TYPE)
313extern void SetMacInfo(DevInfo, NetIf)
314    /*ARGSUSED*/
315     DevInfo_t                 *DevInfo;
316     NetIF_t                   *NetIf;
317{
318#if     defined(SETMACINFO_FUNC)
319    SETMACINFO_FUNC(DevInfo, NetIf);
320#else
321    /* Do Nothing */
322#endif
323}
324#endif  /* !GETMAC_TYPE */
Note: See TracBrowser for help on using the repository browser.