Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / wake / wake.c
blob3306f5a1e80613d4193c6c6c5afa9acdc4d1dbf3
1 /* $NetBSD: wake.c,v 1.10 2010/01/03 17:58:14 mbalmer Exp $ */
3 /*
4 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc@msys.ch>
5 * Copyright (C) 2000 Eugene M. Kim. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Author's name may not be used endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 /* Send Wake on LAN packets to hosts on a local Ethernet network */
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
38 #include <net/bpf.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <ifaddrs.h>
50 #include <limits.h>
51 #include <paths.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
59 #ifndef SYNC_LEN
60 #define SYNC_LEN 6
61 #endif
63 #ifndef DESTADDR_COUNT
64 #define DESTADDR_COUNT 16
65 #endif
67 static void usage(void);
68 static int wake(int, const char *);
69 static int bind_if_to_bpf(char const *, int);
70 static int find_ether(char *, size_t);
71 static int get_ether(char const *, struct ether_addr *);
72 static int send_wakeup(int, struct ether_addr const *);
74 static void
75 usage(void)
77 (void)fprintf(stderr, "usage: %s [interface] lladdr [lladdr ...]\n",
78 getprogname());
79 exit(EXIT_FAILURE);
82 static int
83 wake(int bpf, const char *host)
85 struct ether_addr macaddr;
87 if (get_ether(host, &macaddr) == -1)
88 return -1;
90 return send_wakeup(bpf, &macaddr);
93 static int
94 bind_if_to_bpf(char const *ifname, int bpf)
96 struct ifreq ifr;
97 u_int dlt;
99 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
100 sizeof(ifr.ifr_name)) {
101 errno = ENAMETOOLONG;
102 return -1;
104 if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
105 return -1;
106 if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
107 return -1;
108 if (dlt != DLT_EN10MB) {
109 errno = EOPNOTSUPP;
110 return -1;
112 return 0;
115 static int
116 find_ether(char *dst, size_t len)
118 struct ifaddrs *ifap, *ifa;
119 struct sockaddr_dl *sdl = NULL;
120 int nifs;
122 if (dst == NULL || len == 0)
123 return 0;
125 if (getifaddrs(&ifap) != 0)
126 return -1;
128 /* XXX also check the link state */
129 for (nifs = 0, ifa = ifap; ifa; ifa = ifa->ifa_next)
130 if (ifa->ifa_addr->sa_family == AF_LINK &&
131 ifa->ifa_flags & IFF_UP && ifa->ifa_flags & IFF_RUNNING) {
132 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
133 if (sdl->sdl_type == IFT_ETHER) {
134 strlcpy(dst, ifa->ifa_name, len);
135 nifs++;
139 freeifaddrs(ifap);
140 return nifs == 1 ? 0 : -1;
143 static int
144 get_ether(char const *text, struct ether_addr *addr)
146 struct ether_addr *paddr;
148 paddr = ether_aton(text);
149 if (paddr != NULL) {
150 *addr = *paddr;
151 return 0;
153 if (ether_hostton(text, addr))
154 return -1;
155 return 0;
158 static int
159 send_wakeup(int bpf, struct ether_addr const *addr)
161 struct {
162 struct ether_header hdr;
163 u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
164 } pkt;
165 u_char *p;
166 int i;
167 ssize_t bw, len;
169 (void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
170 pkt.hdr.ether_type = htons(0);
171 (void)memset(pkt.data, 0xff, SYNC_LEN);
172 for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
173 p += ETHER_ADDR_LEN, i++)
174 (void)memcpy(p, addr->ether_addr_octet, ETHER_ADDR_LEN);
175 p = (u_char *)(void *)&pkt;
176 len = sizeof(pkt);
177 bw = 0;
178 while (len) {
179 if ((bw = write(bpf, p, len)) == -1)
180 return -1;
181 len -= bw;
182 p += bw;
184 return 0;
188 main(int argc, char *argv[])
190 int bpf, n;
191 char ifname[IF_NAMESIZE];
193 if (argc < 2)
194 usage();
196 if ((bpf = open(_PATH_BPF, O_RDWR)) == -1)
197 err(EXIT_FAILURE, "Cannot open bpf interface");
199 n = 2;
200 if (bind_if_to_bpf(argv[1], bpf) == -1) {
201 if (find_ether(ifname, sizeof(ifname)))
202 err(EXIT_FAILURE, "Failed to determine ethernet "
203 "interface");
204 if (bind_if_to_bpf(ifname, bpf) == -1)
205 err(EXIT_FAILURE, "Cannot bind to interface `%s'",
206 ifname);
207 --n;
208 } else
209 strlcpy(ifname, argv[1], sizeof(ifname));
211 if (n >= argc)
212 usage();
213 for (; n < argc; n++)
214 if (wake(bpf, argv[n]))
215 warn("Cannot send Wake on LAN frame over `%s' to `%s'",
216 ifname, argv[n]);
217 return EXIT_SUCCESS;