2 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3 * Distributed under the terms of the MIT License.
8 #include <sys/socket.h>
9 #include <netinet/in.h>
12 main(int argc
, char *argv
[])
15 printf("usage: %s <MAC address>\n", argv
[0]);
20 if (sscanf(argv
[1], "%2x%*c%2x%*c%2x%*c%2x%*c%2x%*c%2x", &mac
[0], &mac
[1],
21 &mac
[2], &mac
[3], &mac
[4], &mac
[5]) != 6) {
22 printf("unrecognized MAC format\n");
27 memset(buffer
, 0xff, 6);
28 for (int i
= 1; i
<= 16; i
++)
29 memcpy(buffer
+ i
* 6, mac
, sizeof(mac
));
31 int sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
33 printf("failed to create socket: %s\n", strerror(sock
));
38 int result
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &value
,
41 printf("failed to set broadcast socket option: %s\n", strerror(result
));
46 address
.sin_family
= AF_INET
;
47 address
.sin_addr
.s_addr
= 0xffffffff;
50 result
= sendto(sock
, buffer
, sizeof(buffer
), 0,
51 (struct sockaddr
*)&address
, sizeof(address
));
53 printf("failed to send magic packet: %s\n", strerror(result
));
57 printf("magic packet sent to %02x:%02x:%02x:%02x:%02x:%02x\n", mac
[0],
58 mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);