Module Name:    src
Committed By:   mbalmer
Date:           Thu Jun 25 18:41:22 UTC 2009

Modified Files:
        src/distrib/sets/lists/base: mi
        src/distrib/sets/lists/man: mi
        src/usr.sbin: Makefile
Added Files:
        src/usr.sbin/wake: Makefile wake.8 wake.c

Log Message:
wake(8) is a command to send Wake-on-LAN packets to machines on the local
ethernet, allowing machines to be powered up without physical access to them.

tonnerre@ and tron@ reviewed it and decided that the bin directories
are not to full for this small and useful command.


To generate a diff of this commit:
cvs rdiff -u -r1.820 -r1.821 src/distrib/sets/lists/base/mi
cvs rdiff -u -r1.1142 -r1.1143 src/distrib/sets/lists/man/mi
cvs rdiff -u -r1.241 -r1.242 src/usr.sbin/Makefile
cvs rdiff -u -r0 -r1.1 src/usr.sbin/wake/Makefile src/usr.sbin/wake/wake.8 \
    src/usr.sbin/wake/wake.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/base/mi
diff -u src/distrib/sets/lists/base/mi:1.820 src/distrib/sets/lists/base/mi:1.821
--- src/distrib/sets/lists/base/mi:1.820	Tue Jun 23 17:58:22 2009
+++ src/distrib/sets/lists/base/mi	Thu Jun 25 18:41:22 2009
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.820 2009/06/23 17:58:22 tron Exp $
+# $NetBSD: mi,v 1.821 2009/06/25 18:41:22 mbalmer Exp $
 #
 # Note:	Don't delete entries from here - mark them as "obsolete" instead,
 #	unless otherwise stated below.
@@ -1667,6 +1667,7 @@
 ./usr/sbin/veriexecgen				base-sysutil-bin
 ./usr/sbin/vipw					base-sysutil-bin
 ./usr/sbin/vnconfig				base-sysutil-bin
+./usr/sbin/wake					base-netutil-bin
 ./usr/sbin/wiconfig				base-sysutil-bin
 ./usr/sbin/wire-test				base-netutil-bin
 ./usr/sbin/wlanctl				base-sysutil-bin

Index: src/distrib/sets/lists/man/mi
diff -u src/distrib/sets/lists/man/mi:1.1142 src/distrib/sets/lists/man/mi:1.1143
--- src/distrib/sets/lists/man/mi:1.1142	Tue Jun 23 17:58:23 2009
+++ src/distrib/sets/lists/man/mi	Thu Jun 25 18:41:22 2009
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1142 2009/06/23 17:58:23 tron Exp $
+# $NetBSD: mi,v 1.1143 2009/06/25 18:41:22 mbalmer Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2681,6 +2681,7 @@
 ./usr/share/man/cat8/virtual.0			man-postfix-catman	postfix,.cat
 ./usr/share/man/cat8/vmstat.0			man-obsolete		obsolete
 ./usr/share/man/cat8/vnconfig.0			man-sysutil-catman	.cat
+./usr/share/man/cat8/wake.0			man-netutil-catman	.cat
 ./usr/share/man/cat8/walnut/MAKEDEV.0		man-obsolete		obsolete
 ./usr/share/man/cat8/walnut/makedev.0		man-obsolete		obsolete
 ./usr/share/man/cat8/wdogctl.0			man-sysutil-catman	.cat
@@ -4989,6 +4990,7 @@
 ./usr/share/man/html8/vgscan.html		man-lvm-htmlman		lvm,html
 ./usr/share/man/html8/vgsplit.html		man-lvm-htmlman		lvm,html
 ./usr/share/man/html8/vnconfig.html		man-sysutil-htmlman	html
+./usr/share/man/html8/wake.html			man-netutil-htmlman	html
 ./usr/share/man/html8/wdogctl.html		man-sysutil-htmlman	html
 ./usr/share/man/html8/wiconfig.html		man-netutil-htmlman	html
 ./usr/share/man/html8/wire-test.html		man-netutil-htmlman	html
@@ -7665,6 +7667,7 @@
 ./usr/share/man/man8/virtual.8			man-postfix-man		postfix,.man
 ./usr/share/man/man8/vmstat.8			man-obsolete		obsolete
 ./usr/share/man/man8/vnconfig.8			man-sysutil-man		.man
+./usr/share/man/man8/wake.8			man-netutil-man		.man
 ./usr/share/man/man8/walnut/MAKEDEV.8		man-obsolete		obsolete
 ./usr/share/man/man8/walnut/makedev.8		man-obsolete		obsolete
 ./usr/share/man/man8/wdogctl.8			man-sysutil-man		.man

Index: src/usr.sbin/Makefile
diff -u src/usr.sbin/Makefile:1.241 src/usr.sbin/Makefile:1.242
--- src/usr.sbin/Makefile:1.241	Thu Jun 25 13:59:00 2009
+++ src/usr.sbin/Makefile	Thu Jun 25 18:41:21 2009
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.241 2009/06/25 13:59:00 agc Exp $
+#	$NetBSD: Makefile,v 1.242 2009/06/25 18:41:21 mbalmer Exp $
 #	from: @(#)Makefile	5.20 (Berkeley) 6/12/93
 
 .include <bsd.own.mk>
@@ -21,7 +21,8 @@
 	schedctl sliplogin sntp \
 	spray srtconfig sti sunlabel sup syslogd tadpolectl tcpdchk \
 	tcpdmatch tcpdump tcpdrop timed tpctl traceroute trpt unlink \
-	usbdevs user videomode vipw veriexecgen vnconfig wiconfig wlanctl wpa \
+	usbdevs user videomode vipw veriexecgen vnconfig wake wiconfig \
+	wlanctl wpa \
 	wsconscfg wsfontload wsmoused wsmuxctl zdump zic
 
 .if (${MKKERBEROS} != "no")

Added files:

Index: src/usr.sbin/wake/Makefile
diff -u /dev/null src/usr.sbin/wake/Makefile:1.1
--- /dev/null	Thu Jun 25 18:41:23 2009
+++ src/usr.sbin/wake/Makefile	Thu Jun 25 18:41:22 2009
@@ -0,0 +1,9 @@
+# $NetBSD: Makefile,v 1.1 2009/06/25 18:41:22 mbalmer Exp $
+
+PROG=		wake
+SRCS=		wake.c
+
+BINDIR=		/usr/sbin
+MAN=		wake.8
+
+.include <bsd.prog.mk>
Index: src/usr.sbin/wake/wake.8
diff -u /dev/null src/usr.sbin/wake/wake.8:1.1
--- /dev/null	Thu Jun 25 18:41:23 2009
+++ src/usr.sbin/wake/wake.8	Thu Jun 25 18:41:22 2009
@@ -0,0 +1,60 @@
+.\" $NetBSD: wake.8,v 1.1 2009/06/25 18:41:22 mbalmer Exp $
+.\"
+.\" Copyright (c) 2009 Marc Balmer <[email protected]>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd February 2 2009
+.Dt WAKE 8
+.Os
+.Sh NAME
+.Nm wake
+.Nd "send Wake on LAN frames to hosts on a local Ethernet network"
+.Sh SYNOPSIS
+.Nm
+.Ar interface
+.Op Ar lladdr ...
+.Sh DESCRIPTION
+The
+.Nm
+program is used to send Wake on LAN (WoL) frames over a local
+Ethernet network to one or more hosts using their link layer (hardware)
+addresses.
+WoL functionality is generally enabled in a machine's BIOS
+and can be used to power on machines from a remote system without
+having physical access to them.
+.Pp
+.Ar interface
+is the network interface of the local machine.
+.Ar lladdr
+are the link layer addresses of the remote machines
+and can be specified as the actual hardware address
+(six hexadecimal numbers separated by colons)
+or a hostname entry in
+.Pa /etc/ethers .
+Link layer addresses can be determined and set on
+.Nx
+machines using
+.Xr ifconfig 8 .
+.Sh FILES
+.Bl -tag -width "/etc/ethers" -compact
+.It /etc/ethers
+Ethernet host name database.
+.El
+.Sh SEE ALSO
+.Xr ethers 5 ,
+.Xr ifconfig 8
+.Sh AUTHORS
+.Nm
+was written by
+.Aq [email protected] .
Index: src/usr.sbin/wake/wake.c
diff -u /dev/null src/usr.sbin/wake/wake.c:1.1
--- /dev/null	Thu Jun 25 18:41:23 2009
+++ src/usr.sbin/wake/wake.c	Thu Jun 25 18:41:22 2009
@@ -0,0 +1,197 @@
+/* $NetBSD: wake.c,v 1.1 2009/06/25 18:41:22 mbalmer Exp $ */
+/*
+ * Copyright (C) 2006, 2007, 2008, 2009 Marc Balmer <[email protected]>
+ * Copyright (C) 2000 Eugene M. Kim.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Author's name may not be used endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <net/bpf.h>
+#include <net/if.h>
+
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#ifndef BPF_PATH_FORMAT
+#define BPF_PATH_FORMAT "/dev/bpf%u"
+#endif
+
+#ifndef SYNC_LEN
+#define SYNC_LEN 6
+#endif
+
+#ifndef DESTADDR_COUNT
+#define DESTADDR_COUNT 16
+#endif
+
+__dead void usage(void);
+
+int wake(const char *iface, const char *host);
+int get_bpf(void);
+int bind_if_to_bpf(char const *ifname, int bpf);
+int get_ether(char const *text, struct ether_addr *addr);
+int send_wakeup(int bpf, struct ether_addr const *addr);
+
+void
+usage(void)
+{
+	extern char *__progname;
+
+	(void)fprintf(stderr, "usage: %s interface lladdr\n", __progname);
+	exit(0);
+}
+
+int
+wake(const char *iface, const char *host)
+{
+	int res, bpf;
+	struct ether_addr macaddr;
+
+	bpf = get_bpf();
+	if (bpf == -1) {
+		printf("no bpf\n");
+		return -1;
+	}
+	if (bind_if_to_bpf(iface, bpf) == -1 ||
+	    get_ether(host, &macaddr) == -1) {
+		(void)close(bpf);
+		return -1;
+	}
+	res = send_wakeup(bpf, &macaddr);
+	(void)close(bpf);
+	return res;
+}
+
+int
+get_bpf(void)
+{
+	int i, fd;
+	char path[MAXPATHLEN];
+
+	for (i = 0;; i++) {
+		if (snprintf(path, sizeof(path), BPF_PATH_FORMAT, i) == -1)
+			return -1;
+
+		fd = open(path, O_RDWR);
+		if (fd != -1)
+			return fd;
+		if (errno == EBUSY)
+			continue;
+		break;
+	}
+	return -1;
+}
+
+int
+bind_if_to_bpf(char const *ifname, int bpf)
+{
+	struct ifreq ifr;
+	u_int dlt;
+
+	if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
+	    sizeof(ifr.ifr_name))
+		return -1;
+	if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
+		return -1;
+	if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
+		return -1;
+	if (dlt != DLT_EN10MB)
+		return -1;
+	return 0;
+}
+
+int
+get_ether(char const *text, struct ether_addr *addr)
+{
+	struct ether_addr *paddr;
+
+	paddr = ether_aton(text);
+	if (paddr != NULL) {
+		*addr = *paddr;
+		return 0;
+	}
+	if (ether_hostton(text, addr))
+		return -1;
+	return 0;
+}
+
+int
+send_wakeup(int bpf, struct ether_addr const *addr)
+{
+	struct {
+		struct ether_header hdr;
+		u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
+	} pkt;
+	u_char *p;
+	int i;
+	ssize_t bw;
+	ssize_t len;
+
+	(void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
+	pkt.hdr.ether_type = htons(0);
+	(void)memset(pkt.data, 0xff, SYNC_LEN);
+	for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
+	    p += ETHER_ADDR_LEN, i++)
+		bcopy(addr->ether_addr_octet, p, ETHER_ADDR_LEN);
+	p = (u_char *)&pkt;
+	len = sizeof(pkt);
+	bw = 0;
+	while (len) {
+		if ((bw = write(bpf, &pkt, sizeof(pkt))) == -1)
+			return -1;
+		len -= bw;
+		p += bw;
+	}
+	return 0;
+}
+
+int
+main(int argc, char *argv[])
+{
+	int n;
+
+	if (argc < 3)
+		usage();
+
+	for (n = 2; n < argc; n++)
+		if (wake(argv[1], argv[n]))
+			warnx("error sending Wake on LAN frame over %s to %s",
+			    argv[1], argv[n]);
+	return 0;
+}

Reply via email to