-
Notifications
You must be signed in to change notification settings - Fork 1
/
wow.c
188 lines (157 loc) · 5.52 KB
/
wow.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#define MAC_LEN 17 /* aa:bb:cc:dd:ee:ff */
struct magic_packet_info {
char ip_str[15]; /* aaa.bbb.ccc.ddd */
unsigned char mac[6]; /* 6 bytes */
unsigned char magic_packet[102]; /* first 6 bytes:FF, and MACx16, 6*17 = 102 */
uint16_t port;
bool broadcast;
};
#define myassert(expr, fmt, ...) \
do { \
if (!(expr)) { \
fprintf(stderr, "%s:%d in %s(): %s: "fmt, __FILE__, __LINE__, __func__, strerror(errno), ## __VA_ARGS__); \
exit(EXIT_FAILURE); \
} \
} while(0)
int check_mac(struct magic_packet_info *minfo, const char *mac)
{
char *mac_dup = strdup(mac);
/* check the length */
if (strlen(mac_dup) != MAC_LEN)
goto err;
/* check if '-' or ':' exists and its count is 5 */
char *p;
int cnt = 0;
for (p=mac_dup+2; *p; p+=3) {
switch(*p) {
case '-':
case ':':
cnt++;
break;
default:
goto err;
}
}
if (cnt != 5) goto err;
char *mac_str[6];
for (p=mac_dup; *p; p++) {
switch(*p) {
case '-':
case ':':
*p = '\0';
default:
break;
}
}
int i = 0;
for (p=mac_dup; *p; p+=3) {
mac_str[i] = p;
i++;
}
for (i=0; i<6; i++)
sscanf(mac_str[i], "%hhx", &minfo->mac[i]);
#ifdef _DEBUG
printf("debug: mac:\n");
for (i=0; i<6; i++)
printf("%02x ", minfo->mac[i]);
printf("\n");
#endif
free(mac_dup);
return 0;
err:
fprintf(stderr, "MAC address is invalid.\n");
if (mac_dup) free(mac_dup);
exit(EXIT_FAILURE);
}
int set_magic_packet(struct magic_packet_info *minfo)
{
int i;
for (i=0; i<6; i++)
minfo->magic_packet[i] = (unsigned char)(-1);
for (i=6; i<102; i++)
minfo->magic_packet[i] = minfo->mac[i%6];
return 0;
}
int resolv_name(struct magic_packet_info *minfo, const char *node)
{
struct in_addr addr;
struct addrinfo *res = NULL;
int err;
err = getaddrinfo(node, NULL, NULL, &res);
myassert(err == 0, "getaddrinfo(), %s\n", gai_strerror(err));
addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
memset(minfo->ip_str, '\0', sizeof(minfo->ip_str));
myassert(inet_ntop(AF_INET, &addr, minfo->ip_str, 16) != 0, "inet_ntop()\n");
freeaddrinfo(res);
printf("%s -> %s\n", node, minfo->ip_str);
return 0;
}
int send_magic_packet(struct magic_packet_info *minfo)
{
int sockfd;
struct in_addr ip;
struct sockaddr_in client_addr, server_addr;
myassert(inet_aton(minfo->ip_str, &ip) != 0, "inet_aton()\n");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
myassert(sockfd >= 0, "socket()\n");
printf("using UDP protocol...\n");
if (minfo->broadcast) {
int optval = 1;
myassert(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) >= 0, "setsockopt()\n");
printf("enable broadcast...\n");
}
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = INADDR_ANY;
client_addr.sin_port = 0;
myassert(bind(sockfd, (struct sockaddr*)&client_addr, sizeof(client_addr)) >= 0, "bind()\n");
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = ip.s_addr;
server_addr.sin_port = htons(minfo->port);
#ifdef _DEBUG
printf("debug: port: %u <-> %d\n", server_addr.sin_port, minfo->port);
printf("debug: %s <-> %u\n", minfo->ip_str, ip.s_addr);
#endif
myassert(sendto(sockfd, minfo->magic_packet, sizeof(minfo->magic_packet), 0, (struct sockaddr*)&server_addr, sizeof(server_addr)) == sizeof(minfo->magic_packet), "sendto()\n");
printf("sent magic packet...\n");
myassert(close(sockfd) >= 0, "close()\n");
return 0;
}
int main(int argc, char **argv)
{
if (argc != 4 && argc != 5) {
fprintf(stderr, "usage: %s <FQDN> <MAC address> <UDP port no> [<0 or 1>]\n0: disable broadcast\n1: enable broadcast\ndefault: 1\n", argv[0]);
exit(EXIT_FAILURE);
}
struct magic_packet_info minfo;
char *node = argv[1];
char *mac = argv[2];
minfo.port = (uint16_t)atoi(argv[3]);
minfo.broadcast = argv[4] ? (bool)atoi(argv[4]) : true;
resolv_name(&minfo, node);
check_mac(&minfo, mac);
set_magic_packet(&minfo);
#ifdef _DEBUG
printf("debug: %s: %s\n", node, minfo.ip_str);
int i;
printf("debug: packet:\n");
for (i=0; i<102; i++)
printf("%02x", minfo.magic_packet[i]);
printf("\n");
#endif
send_magic_packet(&minfo);
return 0;
}