-
Notifications
You must be signed in to change notification settings - Fork 37
/
WakeOnLAN.c
205 lines (183 loc) · 5.92 KB
/
WakeOnLAN.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* WakeOnLAN v0.3
* A simple C program that sends a magic packet
*
*
* MIT License
*
* Copyright (c) 2017 Grammatopoulos Athanasios-Vasileios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifdef __linux
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#endif
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
// Create Magic Packet
void createMagicPacket(unsigned char packet[], unsigned int macAddress[]){
int i;
// Mac Address Variable
unsigned char mac[6];
// 6 x 0xFF on start of packet
for(i = 0; i < 6; i++){
packet[i] = 0xFF;
mac[i] = macAddress[i];
}
// Rest of the packet is MAC address of the pc
for(i = 1; i <= 16; i++){
memcpy(&packet[i * 6], &mac, 6 * sizeof(unsigned char));
}
}
// Main Program
int main(int argc, const char* argv[]){
// Default broadcast address
char broadcastAddress[] = "255.255.255.255";
// Packet buffer
unsigned char packet[102];
// Mac address
unsigned int mac[6];
// Set broadcast
int broadcast = 1;
// Windows socket
#ifdef _WIN32
// Socket data
WSADATA data;
// Socket
SOCKET udpSocket;
#endif
// Socket address
struct sockaddr_in udpClient, udpServer;
// Help variables
int i = 0;
// If no arguments
if(argc < 2){
printf("Usage:\n./wakeonlan <mac address> (<broadcast address>) (<interface>)\n");
exit(EXIT_FAILURE);
}
// Parse Mac Address
i = sscanf(argv[1],"%x:%x:%x:%x:%x:%x", &(mac[0]), &(mac[1]), &(mac[2]), &(mac[3]), &(mac[4]), &(mac[5]));
if(i != 6){
printf("Invalid mac address was given.\n");
exit(EXIT_FAILURE);
}
// Print address
printf("Packet will be sent to %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// Check if a broadcast address was given too
if(argc > 2){
// Parse Broadcast Address
i = sscanf(argv[2],"%d.%d.%d.%d", &i, &i, &i, &i);
if(i == 4){
strncpy(broadcastAddress, argv[2], sizeof(broadcastAddress)-1);
}
}
printf("Broadcast address %s will be used.\n", broadcastAddress);
// Create Magic Packet
createMagicPacket(packet, mac);
// MacOS and Linux
#if defined(__APPLE__) || defined(__linux)
int udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket == -1) {
printf("An error was encountered creating the UDP socket: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}
int setsock_result = setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast);
if (setsock_result == -1) {
printf("Failed to set socket options: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}
// Set parameters
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;
// Bind socket
int bind_result = bind(udpSocket, (struct sockaddr*) &udpClient, sizeof(udpClient));
if (bind_result == -1) {
printf("Failed to bind socket: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(argv[3]) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", argv[3]);
if (setsockopt(udpSocket, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
printf("Failed to bind interface: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
// Set server end point (the broadcast addres)
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(broadcastAddress);
udpServer.sin_port = htons(9);
// Send the packet
int result = sendto(udpSocket, &packet, sizeof(unsigned char) * 102, 0, (struct sockaddr*) &udpServer, sizeof(udpServer));
if (result == -1) {
printf("Failed to send magic packet to socket: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}
#endif
// Windows socket
#ifdef _WIN32
// Setup broadcast socket
WSAStartup(MAKEWORD(2, 2), &data);
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast, sizeof(broadcast)) == -1){
printf("Failed to setup a broadcast socket.\n");
exit(EXIT_FAILURE);
}
// Set parameters
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = htons(0);
// Bind socket
bind(udpSocket, (struct sockaddr*) &udpClient, sizeof(udpClient));
// Set server end point (the broadcast addres)
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(broadcastAddress);
udpServer.sin_port = htons(9);
// Send the packet
sendto(udpSocket, (char*)&packet, sizeof(unsigned char) * 102, 0, (struct sockaddr*) &udpServer, sizeof(udpServer));
#endif
// Done
printf("Wake up packet was sent.\n");
exit(EXIT_SUCCESS);
}