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