2 * Copyright (c) 1990, 1991, 1992, 1993, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 * @(#) Copyright (c) 1990, 1991, 1992, 1993, 1996 The Regents of the University of California. All rights reserved.
17 * $FreeBSD: src/usr.sbin/rarpd/rarpd.c,v 1.41 2004/08/07 04:28:54 imp Exp $
18 * $DragonFly: src/usr.sbin/rarpd/rarpd.c,v 1.4 2004/12/18 22:48:05 swildner Exp $
22 * rarpd - Reverse ARP Daemon
24 * Usage: rarpd -a [-dfsv] [-t directory] [hostname]
25 * rarpd [-dfsv] [-t directory] interface [hostname]
27 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
28 * Currently, the argument is ignored.
30 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
37 #include <net/ethernet.h>
39 #include <net/if_types.h>
40 #include <net/if_dl.h>
41 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
46 #include <arpa/inet.h>
59 /* Cast a struct sockaddr to a struct sockaddr_in */
60 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
63 #define TFTP_DIR "/tftpboot"
66 #define ARPSECS (20 * 60) /* as per code in netinet/if_ether.c */
67 #define REVARP_REQUEST ARPOP_REVREQUEST
68 #define REVARP_REPLY ARPOP_REVREPLY
71 * The structure for each interface.
74 struct if_info
*ii_next
;
75 int ii_fd
; /* BPF file descriptor */
76 in_addr_t ii_ipaddr
; /* IP address */
77 in_addr_t ii_netmask
; /* subnet or net mask */
78 u_char ii_eaddr
[ETHER_ADDR_LEN
]; /* ethernet address */
79 char ii_ifname
[IF_NAMESIZE
];
83 * The list of all interfaces that are being listened to. rarp_loop()
84 * "selects" on the descriptors in this list.
86 struct if_info
*iflist
;
88 int verbose
; /* verbose messages */
89 const char *tftp_dir
= TFTP_DIR
; /* tftp directory */
91 int dflag
; /* messages to stdout/stderr, not syslog(3) */
92 int sflag
; /* ignore /tftpboot */
94 static u_char zero
[6];
96 static int bpf_open(void);
97 static in_addr_t
choose_ipaddr(in_addr_t
**, in_addr_t
, in_addr_t
);
98 static char *eatoa(u_char
*);
99 static int expand_syslog_m(const char *fmt
, char **newfmt
);
100 static void init(char *);
101 static void init_one(struct ifaddrs
*, char *, int);
102 static char *intoa(in_addr_t
);
103 static in_addr_t
ipaddrtonetmask(in_addr_t
);
104 static void logmsg(int, const char *, ...) __printflike(2, 3);
105 static int rarp_bootable(in_addr_t
);
106 static int rarp_check(u_char
*, u_int
);
107 static void rarp_loop(void);
108 static int rarp_open(char *);
109 static void rarp_process(struct if_info
*, u_char
*, u_int
);
110 static void rarp_reply(struct if_info
*, struct ether_header
*,
112 static void update_arptab(u_char
*, in_addr_t
);
113 static void usage(void);
116 main(int argc
, char *argv
[])
119 char *ifname
, *hostname
, *name
;
121 int aflag
= 0; /* listen on "all" interfaces */
122 int fflag
= 0; /* don't fork */
124 if ((name
= strrchr(argv
[0], '/')) != NULL
)
132 * All error reporting is done through syslog, unless -d is specified
134 openlog(name
, LOG_PID
| LOG_CONS
, LOG_DAEMON
);
137 while ((op
= getopt(argc
, argv
, "adfst:v")) != -1)
170 ifname
= (aflag
== 0) ? argv
[0] : NULL
;
171 hostname
= ifname
? argv
[1] : argv
[0];
173 if ((aflag
&& ifname
) || (!aflag
&& ifname
== NULL
))
180 logmsg(LOG_ERR
, "cannot fork");
189 * Add to the interface list.
192 init_one(struct ifaddrs
*ifa
, char *target
, int pass1
)
194 struct if_info
*ii
, *ii2
;
195 struct sockaddr_dl
*ll
;
198 family
= ifa
->ifa_addr
->sa_family
;
202 /* Consider only AF_LINK during pass1. */
206 if (!(ifa
->ifa_flags
& IFF_UP
) ||
207 (ifa
->ifa_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)))
214 /* Don't bother going any further if not the target interface */
215 if (target
!= NULL
&& strcmp(ifa
->ifa_name
, target
) != 0)
218 /* Look for interface in list */
219 for (ii
= iflist
; ii
!= NULL
; ii
= ii
->ii_next
)
220 if (strcmp(ifa
->ifa_name
, ii
->ii_ifname
) == 0)
223 if (pass1
&& ii
!= NULL
)
224 /* We've already seen that interface once. */
227 /* Allocate a new one if not found */
229 ii
= (struct if_info
*)malloc(sizeof(*ii
));
231 logmsg(LOG_ERR
, "malloc: %m");
234 bzero(ii
, sizeof(*ii
));
236 strlcpy(ii
->ii_ifname
, ifa
->ifa_name
, sizeof(ii
->ii_ifname
));
237 ii
->ii_next
= iflist
;
239 } else if (!pass1
&& ii
->ii_ipaddr
!= 0) {
241 * Second AF_INET definition for that interface: clone
242 * the existing one, and work on that cloned one.
243 * This must be another IP address for this interface,
244 * so avoid killing the previous configuration.
246 ii2
= (struct if_info
*)malloc(sizeof(*ii2
));
248 logmsg(LOG_ERR
, "malloc: %m");
251 memcpy(ii2
, ii
, sizeof(*ii2
));
253 ii2
->ii_next
= iflist
;
261 ii
->ii_ipaddr
= SATOSIN(ifa
->ifa_addr
)->sin_addr
.s_addr
;
262 ii
->ii_netmask
= SATOSIN(ifa
->ifa_netmask
)->sin_addr
.s_addr
;
263 if (ii
->ii_netmask
== 0)
264 ii
->ii_netmask
= ipaddrtonetmask(ii
->ii_ipaddr
);
266 ii
->ii_fd
= rarp_open(ii
->ii_ifname
);
270 ll
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
271 if (ll
->sdl_type
== IFT_ETHER
)
272 bcopy(LLADDR(ll
), ii
->ii_eaddr
, 6);
277 * Initialize all "candidate" interfaces that are in the system
278 * configuration list. A "candidate" is up, not loopback and not
284 struct if_info
*ii
, *nii
, *lii
;
285 struct ifaddrs
*ifhead
, *ifa
;
288 error
= getifaddrs(&ifhead
);
290 logmsg(LOG_ERR
, "getifaddrs: %m");
294 * We make two passes over the list we have got. In the first
295 * one, we only collect AF_LINK interfaces, and initialize our
296 * list of interfaces from them. In the second pass, we
297 * collect the actual IP addresses from the AF_INET
298 * interfaces, and allow for the same interface name to appear
299 * multiple times (in case of more than one IP address).
301 for (ifa
= ifhead
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
302 init_one(ifa
, target
, 1);
303 for (ifa
= ifhead
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
304 init_one(ifa
, target
, 0);
307 /* Throw away incomplete interfaces */
309 for (ii
= iflist
; ii
!= NULL
; ii
= nii
) {
311 if (ii
->ii_ipaddr
== 0 ||
312 bcmp(ii
->ii_eaddr
, zero
, 6) == 0) {
327 for (ii
= iflist
; ii
!= NULL
; ii
= ii
->ii_next
)
328 logmsg(LOG_DEBUG
, "%s %s 0x%08x %s",
329 ii
->ii_ifname
, intoa(ntohl(ii
->ii_ipaddr
)),
330 (in_addr_t
)ntohl(ii
->ii_netmask
), eatoa(ii
->ii_eaddr
));
336 fprintf(stderr
, "%s\n%s\n",
337 "usage: rarpd -a [-dfsv] [-t directory]",
338 " rarpd [-dfsv] [-t directory] interface");
347 char device
[sizeof "/dev/bpf000"];
350 * Go through all the minors and find one that isn't in use.
353 sprintf(device
, "/dev/bpf%d", n
++);
354 fd
= open(device
, O_RDWR
);
355 } while ((fd
== -1) && (errno
== EBUSY
));
358 logmsg(LOG_ERR
, "%s: %m", device
);
365 * Open a BPF file and attach it to the interface named 'device'.
366 * Set immediate mode, and set a filter that accepts only RARP requests.
369 rarp_open(char *device
)
376 static struct bpf_insn insns
[] = {
377 BPF_STMT(BPF_LD
|BPF_H
|BPF_ABS
, 12),
378 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, ETHERTYPE_REVARP
, 0, 3),
379 BPF_STMT(BPF_LD
|BPF_H
|BPF_ABS
, 20),
380 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, REVARP_REQUEST
, 0, 1),
381 BPF_STMT(BPF_RET
|BPF_K
, sizeof(struct ether_arp
) +
382 sizeof(struct ether_header
)),
383 BPF_STMT(BPF_RET
|BPF_K
, 0),
385 static struct bpf_program filter
= {
386 sizeof insns
/ sizeof(insns
[0]),
392 * Set immediate mode so packets are processed as they arrive.
395 if (ioctl(fd
, BIOCIMMEDIATE
, &immediate
) == -1) {
396 logmsg(LOG_ERR
, "BIOCIMMEDIATE: %m");
399 strlcpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
400 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) == -1) {
401 logmsg(LOG_ERR
, "BIOCSETIF: %m");
405 * Check that the data link layer is an Ethernet; this code won't
406 * work with anything else.
408 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&dlt
) == -1) {
409 logmsg(LOG_ERR
, "BIOCGDLT: %m");
412 if (dlt
!= DLT_EN10MB
) {
413 logmsg(LOG_ERR
, "%s is not an ethernet", device
);
417 * Set filter program.
419 if (ioctl(fd
, BIOCSETF
, (caddr_t
)&filter
) == -1) {
420 logmsg(LOG_ERR
, "BIOCSETF: %m");
427 * Perform various sanity checks on the RARP request packet. Return
428 * false on failure and log the reason.
431 rarp_check(u_char
*p
, u_int len
)
433 struct ether_header
*ep
= (struct ether_header
*)p
;
434 struct ether_arp
*ap
= (struct ether_arp
*)(p
+ sizeof(*ep
));
436 if (len
< sizeof(*ep
) + sizeof(*ap
)) {
437 logmsg(LOG_ERR
, "truncated request, got %u, expected %lu",
438 len
, (u_long
)(sizeof(*ep
) + sizeof(*ap
)));
442 * XXX This test might be better off broken out...
444 if (ntohs(ep
->ether_type
) != ETHERTYPE_REVARP
||
445 ntohs(ap
->arp_hrd
) != ARPHRD_ETHER
||
446 ntohs(ap
->arp_op
) != REVARP_REQUEST
||
447 ntohs(ap
->arp_pro
) != ETHERTYPE_IP
||
448 ap
->arp_hln
!= 6 || ap
->arp_pln
!= 4) {
449 logmsg(LOG_DEBUG
, "request fails sanity check");
452 if (bcmp((char *)&ep
->ether_shost
, (char *)&ap
->arp_sha
, 6) != 0) {
453 logmsg(LOG_DEBUG
, "ether/arp sender address mismatch");
456 if (bcmp((char *)&ap
->arp_sha
, (char *)&ap
->arp_tha
, 6) != 0) {
457 logmsg(LOG_DEBUG
, "ether/arp target address mismatch");
464 * Loop indefinitely listening for RARP requests on the
465 * interfaces in 'iflist'.
470 u_char
*buf
, *bp
, *ep
;
472 fd_set fds
, listeners
;
473 int bufsize
, maxfd
= 0;
476 if (iflist
== NULL
) {
477 logmsg(LOG_ERR
, "no interfaces");
480 if (ioctl(iflist
->ii_fd
, BIOCGBLEN
, (caddr_t
)&bufsize
) == -1) {
481 logmsg(LOG_ERR
, "BIOCGBLEN: %m");
484 buf
= malloc(bufsize
);
486 logmsg(LOG_ERR
, "malloc: %m");
492 * Find the highest numbered file descriptor for select().
493 * Initialize the set of descriptors to listen to.
496 for (ii
= iflist
; ii
!= NULL
; ii
= ii
->ii_next
) {
497 FD_SET(ii
->ii_fd
, &fds
);
498 if (ii
->ii_fd
> maxfd
)
502 if (select(maxfd
+ 1, &listeners
, NULL
, NULL
, NULL
) == -1) {
503 /* Don't choke when we get ptraced */
506 logmsg(LOG_ERR
, "select: %m");
509 for (ii
= iflist
; ii
!= NULL
; ii
= ii
->ii_next
) {
511 if (!FD_ISSET(fd
, &listeners
))
514 cc
= read(fd
, (char *)buf
, bufsize
);
515 /* Don't choke when we get ptraced */
516 if ((cc
== -1) && (errno
== EINTR
))
519 /* Loop through the packet(s) */
520 #define bhp ((struct bpf_hdr *)bp)
524 u_int caplen
, hdrlen
;
526 caplen
= bhp
->bh_caplen
;
527 hdrlen
= bhp
->bh_hdrlen
;
528 if (rarp_check(bp
+ hdrlen
, caplen
))
529 rarp_process(ii
, bp
+ hdrlen
, caplen
);
530 bp
+= BPF_WORDALIGN(hdrlen
+ caplen
);
538 * True if this server can boot the host whose IP address is 'addr'.
539 * This check is made by looking in the tftp directory for the
540 * configuration file.
543 rarp_bootable(in_addr_t addr
)
548 static DIR *dd
= NULL
;
550 sprintf(ipname
, "%08X", (in_addr_t
)ntohl(addr
));
553 * If directory is already open, rewind it. Otherwise, open it.
555 if ((d
= dd
) != NULL
)
558 if (chdir(tftp_dir
) == -1) {
559 logmsg(LOG_ERR
, "chdir: %s: %m", tftp_dir
);
564 logmsg(LOG_ERR
, "opendir: %m");
569 while ((dent
= readdir(d
)) != NULL
)
570 if (strncmp(dent
->d_name
, ipname
, 8) == 0)
576 * Given a list of IP addresses, 'alist', return the first address that
577 * is on network 'net'; 'netmask' is a mask indicating the network portion
581 choose_ipaddr(in_addr_t
**alist
, in_addr_t net
, in_addr_t netmask
)
583 for (; *alist
; ++alist
)
584 if ((**alist
& netmask
) == net
)
590 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
591 * already been checked for validity. The reply is overlaid on the request.
594 rarp_process(struct if_info
*ii
, u_char
*pkt
, u_int len
)
596 struct ether_header
*ep
;
598 in_addr_t target_ipaddr
;
601 ep
= (struct ether_header
*)pkt
;
602 /* should this be arp_tha? */
603 if (ether_ntohost(ename
, (struct ether_addr
*)&ep
->ether_shost
) != 0) {
604 logmsg(LOG_ERR
, "cannot map %s to name",
605 eatoa(ep
->ether_shost
));
609 if ((hp
= gethostbyname(ename
)) == NULL
) {
610 logmsg(LOG_ERR
, "cannot map %s to IP address", ename
);
615 * Choose correct address from list.
617 if (hp
->h_addrtype
!= AF_INET
) {
618 logmsg(LOG_ERR
, "cannot handle non IP addresses for %s",
622 target_ipaddr
= choose_ipaddr((in_addr_t
**)hp
->h_addr_list
,
623 ii
->ii_ipaddr
& ii
->ii_netmask
,
625 if (target_ipaddr
== 0) {
626 logmsg(LOG_ERR
, "cannot find %s on net %s",
627 ename
, intoa(ntohl(ii
->ii_ipaddr
& ii
->ii_netmask
)));
630 if (sflag
|| rarp_bootable(target_ipaddr
))
631 rarp_reply(ii
, ep
, target_ipaddr
, len
);
632 else if (verbose
> 1)
633 logmsg(LOG_INFO
, "%s %s at %s DENIED (not bootable)",
635 eatoa(ep
->ether_shost
),
636 intoa(ntohl(target_ipaddr
)));
640 * Poke the kernel arp tables with the ethernet/ip address combinataion
641 * given. When processing a reply, we must do this so that the booting
642 * host (i.e. the guy running rarpd), won't try to ARP for the hardware
643 * address of the guy being booted (he cannot answer the ARP).
645 struct sockaddr_inarp sin_inarp
= {
646 sizeof(struct sockaddr_inarp
), AF_INET
, 0,
651 struct sockaddr_dl sin_dl
= {
652 sizeof(struct sockaddr_dl
), AF_LINK
, 0, IFT_ETHER
, 0, 6,
656 struct rt_msghdr rthdr
;
661 update_arptab(u_char
*ep
, in_addr_t ipaddr
)
664 struct sockaddr_inarp
*ar
, *ar2
;
665 struct sockaddr_dl
*ll
, *ll2
;
666 struct rt_msghdr
*rt
;
672 r
= socket(PF_ROUTE
, SOCK_RAW
, 0);
674 logmsg(LOG_ERR
, "raw route socket: %m");
680 ar
->sin_addr
.s_addr
= ipaddr
;
682 bcopy(ep
, LLADDR(ll
), 6);
684 /* Get the type and interface index */
686 bzero(rt
, sizeof(rtmsg
));
687 rt
->rtm_version
= RTM_VERSION
;
688 rt
->rtm_addrs
= RTA_DST
;
689 rt
->rtm_type
= RTM_GET
;
691 ar2
= (struct sockaddr_inarp
*)rtmsg
.rtspace
;
692 bcopy(ar
, ar2
, sizeof(*ar
));
693 rt
->rtm_msglen
= sizeof(*rt
) + sizeof(*ar
);
695 if ((write(r
, rt
, rt
->rtm_msglen
) == -1) && (errno
!= ESRCH
)) {
696 logmsg(LOG_ERR
, "rtmsg get write: %m");
701 cc
= read(r
, rt
, sizeof(rtmsg
));
702 } while (cc
> 0 && (rt
->rtm_seq
!= seq
|| rt
->rtm_pid
!= pid
));
704 logmsg(LOG_ERR
, "rtmsg get read: %m");
708 ll2
= (struct sockaddr_dl
*)((u_char
*)ar2
+ ar2
->sin_len
);
709 if (ll2
->sdl_family
!= AF_LINK
) {
711 * XXX I think this means the ip address is not on a
712 * directly connected network (the family is AF_INET in
715 logmsg(LOG_ERR
, "bogus link family (%d) wrong net for %08X?\n",
716 ll2
->sdl_family
, ipaddr
);
720 xtype
= ll2
->sdl_type
;
721 xindex
= ll2
->sdl_index
;
723 /* Set the new arp entry */
724 bzero(rt
, sizeof(rtmsg
));
725 rt
->rtm_version
= RTM_VERSION
;
726 rt
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
727 rt
->rtm_inits
= RTV_EXPIRE
;
728 rt
->rtm_rmx
.rmx_expire
= time(0) + ARPSECS
;
729 rt
->rtm_flags
= RTF_HOST
| RTF_STATIC
;
730 rt
->rtm_type
= RTM_ADD
;
733 bcopy(ar
, ar2
, sizeof(*ar
));
735 ll2
= (struct sockaddr_dl
*)((u_char
*)ar2
+ sizeof(*ar2
));
736 bcopy(ll
, ll2
, sizeof(*ll
));
737 ll2
->sdl_type
= xtype
;
738 ll2
->sdl_index
= xindex
;
740 rt
->rtm_msglen
= sizeof(*rt
) + sizeof(*ar2
) + sizeof(*ll2
);
742 if ((write(r
, rt
, rt
->rtm_msglen
) == -1) && (errno
!= EEXIST
)) {
743 logmsg(LOG_ERR
, "rtmsg add write: %m");
748 cc
= read(r
, rt
, sizeof(rtmsg
));
749 } while (cc
> 0 && (rt
->rtm_seq
!= seq
|| rt
->rtm_pid
!= pid
));
752 logmsg(LOG_ERR
, "rtmsg add read: %m");
758 * Build a reverse ARP packet and sent it out on the interface.
759 * 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built
760 * on top of the request, then written to the network.
762 * RFC 903 defines the ether_arp fields as follows. The following comments
763 * are taken (more or less) straight from this document.
767 * arp_sha is the hardware address of the sender of the packet.
768 * arp_spa is undefined.
769 * arp_tha is the 'target' hardware address.
770 * In the case where the sender wishes to determine his own
771 * protocol address, this, like arp_sha, will be the hardware
772 * address of the sender.
773 * arp_tpa is undefined.
777 * arp_sha is the hardware address of the responder (the sender of the
779 * arp_spa is the protocol address of the responder (see the note below).
780 * arp_tha is the hardware address of the target, and should be the same as
781 * that which was given in the request.
782 * arp_tpa is the protocol address of the target, that is, the desired address.
784 * Note that the requirement that arp_spa be filled in with the responder's
785 * protocol is purely for convenience. For instance, if a system were to use
786 * both ARP and RARP, then the inclusion of the valid protocol-hardware
787 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
791 rarp_reply(struct if_info
*ii
, struct ether_header
*ep
, in_addr_t ipaddr
,
795 struct ether_arp
*ap
= (struct ether_arp
*)(ep
+ 1);
797 update_arptab((u_char
*)&ap
->arp_sha
, ipaddr
);
800 * Build the rarp reply by modifying the rarp request in place.
802 ap
->arp_op
= htons(REVARP_REPLY
);
805 ep
->ether_type
= ETHERTYPE_REVARP
;
807 bcopy((char *)&ap
->arp_sha
, (char *)&ep
->ether_dhost
, 6);
808 bcopy((char *)ii
->ii_eaddr
, (char *)&ep
->ether_shost
, 6);
809 bcopy((char *)ii
->ii_eaddr
, (char *)&ap
->arp_sha
, 6);
811 bcopy((char *)&ipaddr
, (char *)ap
->arp_tpa
, 4);
812 /* Target hardware is unchanged. */
813 bcopy((char *)&ii
->ii_ipaddr
, (char *)ap
->arp_spa
, 4);
815 /* Zero possible garbage after packet. */
816 bzero((char *)ep
+ (sizeof(*ep
) + sizeof(*ap
)),
817 len
- (sizeof(*ep
) + sizeof(*ap
)));
818 n
= write(ii
->ii_fd
, (char *)ep
, len
);
820 logmsg(LOG_ERR
, "write: only %d of %d bytes written", n
, len
);
822 logmsg(LOG_INFO
, "%s %s at %s REPLIED", ii
->ii_ifname
,
824 intoa(ntohl(ipaddr
)));
828 * Get the netmask of an IP address. This routine is used if
829 * SIOCGIFNETMASK doesn't work.
832 ipaddrtonetmask(in_addr_t addr
)
836 return htonl(IN_CLASSA_NET
);
838 return htonl(IN_CLASSB_NET
);
840 return htonl(IN_CLASSC_NET
);
841 logmsg(LOG_DEBUG
, "unknown IP address class: %08X", addr
);
842 return htonl(0xffffffff);
846 * A faster replacement for inet_ntoa().
849 intoa(in_addr_t addr
)
854 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
856 cp
= &buf
[sizeof buf
];
862 *--cp
= byte
% 10 + '0';
865 *--cp
= byte
% 10 + '0';
880 static char buf
[sizeof("xx:xx:xx:xx:xx:xx")];
882 sprintf(buf
, "%x:%x:%x:%x:%x:%x",
883 ea
[0], ea
[1], ea
[2], ea
[3], ea
[4], ea
[5]);
888 logmsg(int pri
, const char *fmt
, ...)
900 if (expand_syslog_m(fmt
, &newfmt
) == -1) {
901 vfprintf(fp
, fmt
, v
);
903 vfprintf(fp
, newfmt
, v
);
909 vsyslog(pri
, fmt
, v
);
915 expand_syslog_m(const char *fmt
, char **newfmt
)
922 while ((m
= strstr(str
, "%m")) != NULL
) {
923 asprintf(&np
, "%s%.*s%s", p
, (int)(m
- str
),
924 str
, strerror(errno
));
935 asprintf(&np
, "%s%s", p
, str
);