]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-str.c
don't need if_ppp.h
[ppp.git] / pppd / sys-str.c
1 /*
2  * sys-str.c - System-dependent procedures for setting up
3  * PPP interfaces on systems which use the STREAMS ppp interface.
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by Carnegie Mellon University.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20
21 /*
22  * TODO:
23  */
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <syslog.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <time.h>
31 #include <utmp.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/time.h>
36 #include <sys/stream.h>
37 #include <sys/stropts.h>
38
39 #include <net/if.h>
40 #include <net/route.h>
41 #include <net/if_arp.h>
42 #include <netinet/in.h>
43
44 #include "pppd.h"
45 #include "ppp.h"
46 #include <net/ppp_str.h>
47
48 #ifndef ifr_mtu
49 #define ifr_mtu         ifr_metric
50 #endif
51
52 #define MAXMODULES      10      /* max number of module names to save */
53 static struct   modlist {
54     char        modname[FMNAMESZ+1];
55 } str_modules[MAXMODULES];
56 static int      str_module_count = 0;
57 static int      pushed_ppp;
58
59 extern int hungup;              /* has the physical layer been disconnected? */
60 extern int kdebugflag;
61
62 #define PAI_FLAGS_B7_0          0x100
63 #define PAI_FLAGS_B7_1          0x200
64 #define PAI_FLAGS_PAR_EVEN      0x400
65 #define PAI_FLAGS_PAR_ODD       0x800
66 #define PAI_FLAGS_HIBITS        0xF00
67
68 /*
69  * ppp_available - check if this kernel supports PPP.
70  */
71 int
72 ppp_available()
73 {
74     int fd, ret;
75
76     fd = open("/dev/tty", O_RDONLY, 0);
77     if (fd < 0)
78         return 1;               /* can't find out - assume we have ppp */
79     ret = ioctl(fd, I_FIND, "pppasync") >= 0;
80     close(fd);
81     return ret;
82 }
83
84
85 /*
86  * establish_ppp - Turn the serial port into a ppp interface.
87  */
88 void
89 establish_ppp()
90 {
91     /* go through and save the name of all the modules, then pop em */
92     for (;;) { 
93         if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
94             ioctl(fd, I_POP, 0) < 0)
95             break;
96         MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
97                    str_modules[str_module_count].modname));
98         str_module_count++;
99     }
100
101     /* now push the async/fcs module */
102     if (ioctl(fd, I_PUSH, "pppasync") < 0) {
103         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
104         die(1);
105     }
106     /* finally, push the ppp_if module that actually handles the */
107     /* network interface */ 
108     if (ioctl(fd, I_PUSH, "pppif") < 0) {
109         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
110         die(1);
111     }
112     pushed_ppp = 1;
113     if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
114         syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
115         die(1);
116     }
117     /* read mode, message non-discard mode */
118     if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
119         syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
120         die(1);
121     }
122     /* Flush any waiting messages, or we'll never get SIGPOLL */
123     if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
124         syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
125         die(1);
126     }
127     /*
128      * Find out which interface we were given.
129      * (ppp_if handles this ioctl)
130      */
131     if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
132         syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
133         die(1);
134     }
135
136     /* Set debug flags in driver */
137     if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
138         syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
139     }
140 }
141
142 /*
143  * disestablish_ppp - Restore the serial port to normal operation.
144  * It attempts to reconstruct the stream with the previously popped
145  * modules.  This shouldn't call die() because it's called from die().
146  */
147 void
148 disestablish_ppp()
149 {
150     int flags;
151     char *s;
152
153     if (hungup) {
154         /* we can't push or pop modules after the stream has hung up */
155         str_module_count = 0;
156         return;
157     }
158
159     if (pushed_ppp) {
160         /*
161          * Check whether the link seems not to be 8-bit clean.
162          */
163         if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
164             s = NULL;
165             switch (~flags & PAI_FLAGS_HIBITS) {
166             case PAI_FLAGS_B7_0:
167                 s = "bit 7 set to 1";
168                 break;
169             case PAI_FLAGS_B7_1:
170                 s = "bit 7 set to 0";
171                 break;
172             case PAI_FLAGS_PAR_EVEN:
173                 s = "odd parity";
174                 break;
175             case PAI_FLAGS_PAR_ODD:
176                 s = "even parity";
177                 break;
178             }
179             if (s != NULL) {
180                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
181                 syslog(LOG_WARNING, "All received characters had %s", s);
182             }
183         }
184     }
185
186     while (ioctl(fd, I_POP, 0) == 0)    /* pop any we pushed */
187         ;
188     pushed_ppp = 0;
189   
190     for (; str_module_count > 0; str_module_count--) {
191         if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
192             syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
193                    str_modules[str_module_count-1].modname);
194         } else {
195             MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
196                        str_modules[str_module_count-1].modname));
197         }
198     }
199 }
200
201
202 /*
203  * output - Output PPP packet.
204  */
205 void
206 output(unit, p, len)
207     int unit;
208     u_char *p;
209     int len;
210 {
211     struct strbuf       str;
212
213     if (unit != 0)
214         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
215     if (debug)
216         log_packet(p, len, "sent ");
217
218     str.len = len;
219     str.buf = (caddr_t) p;
220     if(putmsg(fd, NULL, &str, 0) < 0) {
221         syslog(LOG_ERR, "putmsg: %m");
222         die(1);
223     }
224 }
225
226
227 /*
228  * read_packet - get a PPP packet from the serial device.
229  */
230 int
231 read_packet(buf)
232     u_char *buf;
233 {
234     struct strbuf str;
235     int len, i;
236
237     str.maxlen = MTU+DLLHEADERLEN;
238     str.buf = (caddr_t) buf;
239     i = 0;
240     len = getmsg(fd, NULL, &str, &i);
241     if (len < 0) {
242         if (errno == EAGAIN || errno == EWOULDBLOCK) {
243             return -1;
244         }
245         syslog(LOG_ERR, "getmsg(fd) %m");
246         die(1);
247     }
248     if (len) 
249         MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
250
251     if (str.len < 0) {
252         MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
253         return -1;
254     }
255
256     return str.len;
257 }
258
259
260 /*
261  * ppp_send_config - configure the transmit characteristics of
262  * the ppp interface.
263  */
264 void
265 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
266     int unit, mtu;
267     u_long asyncmap;
268     int pcomp, accomp;
269 {
270     char c;
271     struct ifreq ifr;
272
273     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
274     ifr.ifr_mtu = mtu;
275     if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
276         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
277         quit();
278     }
279
280     if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
281         syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
282         quit();
283     }
284
285     c = (pcomp? 1: 0);
286     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
287         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
288         quit();
289     }
290
291     c = (accomp? 1: 0);
292     if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
293         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
294         quit();
295     }
296 }
297
298
299 /*
300  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
301  */
302 void
303 ppp_set_xaccm(unit, accm)
304     int unit;
305     ext_accm accm;
306 {
307     if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
308         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
309 }
310
311
312 /*
313  * ppp_recv_config - configure the receive-side characteristics of
314  * the ppp interface.
315  */
316 void
317 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
318     int unit, mru;
319     u_long asyncmap;
320     int pcomp, accomp;
321 {
322     char c;
323
324     if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
325         syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
326     }
327
328     if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
329         syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
330     }
331
332     c = 2 + (pcomp? 1: 0);
333     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
334         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
335     }
336
337     c = 2 + (accomp? 1: 0);
338     if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
339         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
340     }
341 }
342
343 /*
344  * sifvjcomp - config tcp header compression
345  */
346 int
347 sifvjcomp(u, vjcomp, cidcomp, maxcid)
348     int u, vjcomp, cidcomp, maxcid;
349 {
350     char x;
351
352     x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
353     if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
354         syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
355         return 0;
356     }
357     return 1;
358 }
359
360 /*
361  * sifup - Config the interface up.
362  */
363 int
364 sifup(u)
365     int u;
366 {
367     struct ifreq ifr;
368
369     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
370     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
371         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
372         return 0;
373     }
374     ifr.ifr_flags |= IFF_UP;
375     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
376         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
377         return 0;
378     }
379     return 1;
380 }
381
382 /*
383  * sifdown - Config the interface down.
384  */
385 int
386 sifdown(u)
387     int u;
388 {
389     struct ifreq ifr;
390     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
391     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
392         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
393         return 0;
394     }
395     ifr.ifr_flags &= ~IFF_UP;
396     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
397         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
398         return 0;
399     }
400     return 1;
401 }
402
403 /*
404  * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
405  */
406 #define SET_SA_FAMILY(addr, family)             \
407     BZERO((char *) &(addr), sizeof(addr));      \
408     addr.sa_family = (family);
409
410 /*
411  * sifaddr - Config the interface IP addresses and netmask.
412  */
413 int
414 sifaddr(u, o, h, m)
415     int u;
416     u_long o, h, m;
417 {
418     int ret;
419     struct ifreq ifr;
420
421     ret = 1;
422     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
423     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
424     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
425     if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
426         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
427         ret = 0;
428     }
429     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
430     if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
431         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
432         ret = 0;
433     }
434     if (m != 0) {
435         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
436         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
437         if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
438             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
439             ret = 0;
440         }
441     }
442     return ret;
443 }
444
445 /*
446  * cifaddr - Clear the interface IP addresses, and delete routes
447  * through the interface if possible.
448  */
449 int
450 cifaddr(u, o, h)
451     int u;
452     u_long o, h;
453 {
454     struct rtentry rt;
455
456     SET_SA_FAMILY(rt.rt_dst, AF_INET);
457     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
458     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
459     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
460     rt.rt_flags = RTF_HOST;
461     if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
462         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
463         return 0;
464     }
465     return 1;
466 }
467
468 /*
469  * sifdefaultroute - assign a default route through the address given.
470  */
471 int
472 sifdefaultroute(u, g)
473     int u;
474     u_long g;
475 {
476     struct rtentry rt;
477
478     SET_SA_FAMILY(rt.rt_dst, AF_INET);
479     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
480     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
481     rt.rt_flags = RTF_GATEWAY;
482     if (ioctl(s, SIOCADDRT, &rt) < 0) {
483         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
484         return 0;
485     }
486     return 1;
487 }
488
489 /*
490  * cifdefaultroute - delete a default route through the address given.
491  */
492 int
493 cifdefaultroute(u, g)
494     int u;
495     u_long g;
496 {
497     struct rtentry rt;
498
499     SET_SA_FAMILY(rt.rt_dst, AF_INET);
500     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
501     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
502     rt.rt_flags = RTF_GATEWAY;
503     if (ioctl(s, SIOCDELRT, &rt) < 0) {
504         syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
505         return 0;
506     }
507     return 1;
508 }
509
510 /*
511  * sifproxyarp - Make a proxy ARP entry for the peer.
512  */
513 int
514 sifproxyarp(unit, hisaddr)
515     int unit;
516     u_long hisaddr;
517 {
518     struct arpreq arpreq;
519
520     BZERO(&arpreq, sizeof(arpreq));
521
522     /*
523      * Get the hardware address of an interface on the same subnet
524      * as our local address.
525      */
526     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
527         syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
528         return 0;
529     }
530
531     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
532     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
533     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
534     if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
535         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
536         return 0;
537     }
538
539     return 1;
540 }
541
542 /*
543  * cifproxyarp - Delete the proxy ARP entry for the peer.
544  */
545 int
546 cifproxyarp(unit, hisaddr)
547     int unit;
548     u_long hisaddr;
549 {
550     struct arpreq arpreq;
551
552     BZERO(&arpreq, sizeof(arpreq));
553     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
554     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
555     if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
556         syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
557         return 0;
558     }
559     return 1;
560 }
561
562 /*
563  * get_ether_addr - get the hardware address of an interface on the
564  * the same subnet as ipaddr.  Code borrowed from myetheraddr.c
565  * in the cslip-2.6 distribution, which is subject to the following
566  * copyright notice (which also applies to logwtmp below):
567  *
568  * Copyright (c) 1990, 1992 The Regents of the University of California.
569  * All rights reserved.
570  *
571  * Redistribution and use in source and binary forms, with or without
572  * modification, are permitted provided that: (1) source code distributions
573  * retain the above copyright notice and this paragraph in its entirety, (2)
574  * distributions including binary code include the above copyright notice and
575  * this paragraph in its entirety in the documentation or other materials
576  * provided with the distribution, and (3) all advertising materials mentioning
577  * features or use of this software display the following acknowledgement:
578  * ``This product includes software developed by the University of California,
579  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
580  * the University nor the names of its contributors may be used to endorse
581  * or promote products derived from this software without specific prior
582  * written permission.
583  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
584  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
585  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
586  */
587
588 #include <fcntl.h>
589 #include <nlist.h>
590 #include <kvm.h>
591 #include <arpa/inet.h>
592
593 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
594 #ifdef _nlist_h
595 #define SUNOS4
596 #endif
597
598 #ifdef SUNOS4
599 #include <netinet/in_var.h>
600 #endif
601 #include <netinet/if_ether.h>
602
603 /* Cast a struct sockaddr to a structaddr_in */
604 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
605
606 /* Determine if "bits" is set in "flag" */
607 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
608
609 static struct nlist nl[] = {
610 #define N_IFNET 0
611         { "_ifnet" },
612         { 0 }
613 };
614
615 static void kread();
616
617 int
618 get_ether_addr(ipaddr, hwaddr)
619     u_long ipaddr;
620     struct sockaddr *hwaddr;
621 {
622     register kvm_t *kd;
623     register struct ifnet *ifp;
624     register struct arpcom *ac;
625     struct arpcom arpcom;
626     struct in_addr *inp;
627 #ifdef SUNOS4
628     register struct ifaddr *ifa;
629     register struct in_ifaddr *in;
630     union {
631         struct ifaddr ifa;
632         struct in_ifaddr in;
633     } ifaddr;
634 #endif
635     u_long addr, mask;
636
637     /* Open kernel memory for reading */
638     kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
639     if (kd == 0) {
640         syslog(LOG_ERR, "kvm_open: %m");
641         return 0;
642     }
643
644     /* Fetch namelist */
645     if (kvm_nlist(kd, nl) != 0) {
646         syslog(LOG_ERR, "kvm_nlist failed");
647         return 0;
648     }
649
650     ac = &arpcom;
651     ifp = &arpcom.ac_if;
652 #ifdef SUNOS4
653     ifa = &ifaddr.ifa;
654     in = &ifaddr.in;
655 #endif
656
657     if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
658         != sizeof(addr)) {
659         syslog(LOG_ERR, "error reading ifnet addr");
660         return 0;
661     }
662     for ( ; addr; addr = (u_long)ifp->if_next) {
663         if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
664             syslog(LOG_ERR, "error reading ifnet");
665             return 0;
666         }
667
668         /* Only look at configured, broadcast interfaces */
669         if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
670             continue;
671 #ifdef SUNOS4
672         /* This probably can't happen... */
673         if (ifp->if_addrlist == 0)
674             continue;
675 #endif
676
677         /* Get interface ip address */
678 #ifdef SUNOS4
679         if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
680                      sizeof(ifaddr)) != sizeof(ifaddr)) {
681             syslog(LOG_ERR, "error reading ifaddr");
682             return 0;
683         }
684         inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
685 #else
686         inp = &SATOSIN(&ifp->if_addr)->sin_addr;
687 #endif
688
689         /* Check if this interface on the right subnet */
690 #ifdef SUNOS4
691         mask = in->ia_subnetmask;
692 #else
693         mask = ifp->if_subnetmask;
694 #endif
695         if ((ipaddr & mask) != (inp->s_addr & mask))
696             continue;
697
698         /* Copy out the local ethernet address */
699         hwaddr->sa_family = AF_UNSPEC;
700         BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
701               sizeof(arpcom.ac_enaddr));
702         return 1;               /* success! */
703     }
704
705     /* couldn't find one */
706     return 0;
707 }
708
709 #define WTMPFILE        "/usr/adm/wtmp"
710
711 int
712 logwtmp(line, name, host)
713     char *line, *name, *host;
714 {
715     int fd;
716     struct stat buf;
717     struct utmp ut;
718
719     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
720         return;
721     if (!fstat(fd, &buf)) {
722         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
723         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
724         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
725         (void)time(&ut.ut_time);
726         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
727             (void)ftruncate(fd, buf.st_size);
728     }
729     close(fd);
730 }