summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/wol.c
blob: 8960db195fb86bae5442041bb1e6edd5af3a960f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* send wake-on-lan magic ethernet packet */
#include <u.h>
#include <libc.h>
#include <ip.h>

enum {
	Eaddrlen = 6,	/* 48 bits */
};

typedef struct Wolpack Wolpack;
struct Wolpack{
	uchar	magic[6];
	uchar	macs[16][Eaddrlen];
	char	pass[6+1];
};

int verbose;

void
usage(void)
{
	fprint(2, "usage: wol [-v] [-a dialstr] [-c password] macaddr\n");
	exits("usage");
}

void
fillmac(Wolpack *w, uchar *mac)
{
	int i;

	for(i = 0; i < nelem(w->macs); i++)
		memmove(w->macs[i], mac, Eaddrlen);
}

void
dumppack(Wolpack *w)
{
	int i;

	print("packet: [\n");
	print("\t%E\n", w->magic);
	for(i = 0; i < nelem(w->macs); i++)
		print("\t%E\n", w->macs[i]);
	print("\t%6s\n", w->pass);
	print("]\n");
}

void
main(int argc, char* argv[])
{
	int fd, nw;
	char *argmac, *pass, *address;
	uchar mac[Eaddrlen];
	static Wolpack w = {
		.magic { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }
	};

	address = pass = nil;
	fmtinstall('E', eipfmt);

	ARGBEGIN{
	case 'a':
		address = EARGF(usage());
		break;
	case 'c':
		pass = EARGF(usage());
		break;
	case 'v':
		verbose++;
		break;
	default:
		usage();
	}ARGEND

	if(argc != 1)
		usage();
	argmac = argv[0];
	if(verbose)
		print("mac is %s, pass is %s\n", argmac, pass);

	parseether(mac, argmac);
	fillmac(&w, mac);
	if(pass){
		if(strlen(pass) > 6)
			sysfatal("password greater than 6 bytes");
		strcpy(w.pass, pass);
	}
	if(verbose)
		dumppack(&w);

	if(!address)
		address = "udp!255.255.255.255!0";

	fd = dial(address, nil, nil, nil);
	if(fd < 0)
		sysfatal("%s: %r", address);
	nw = write(fd, &w, sizeof w);
	if(nw != sizeof w)
		sysfatal("error sending: %r");
	exits(0);
}