├── LICENSE ├── README.md └── WakeOnLAN.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Grammatopoulos Athanasios-Vasileios 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![latest version](https://img.shields.io/badge/latest%20version-v0.3-green.svg?style=flat-square)](https://github.com/GramThanos/WakeOnLAN/releases/latest) 2 | ![Windows build](https://img.shields.io/badge/window%20build-pass-blue.svg?style=flat-square) 3 | ![Linux build](https://img.shields.io/badge/linux%20build-pass-blue.svg?style=flat-square) 4 | ![Mac build](https://img.shields.io/badge/mac%20build-not--available-red.svg?style=flat-square) 5 | 6 | # WakeOnLAN 7 | A simple C program that sends a magic packet 8 | 9 | You can use this program to wake up a PC over the network. It sends the so called magic packet to the network card of the target PC, instructing it to open the PC. [[Download]](https://github.com/GramThanos/WakeOnLAN/releases) 10 | 11 | ___ 12 | 13 | ### Usage 14 | ```bash 15 | ./WakeOnLAN [ ] 16 | ``` 17 | The first parameter is the mac address of the target (usually your's network card's mac address). 18 | The second parameter is optional and defines the broadcast address to send the packet. 19 | 20 | Example 21 | ```bash 22 | ./WakeOnLAN 00:11:22:33:44:55 192.168.1.255 23 | ``` 24 | 25 | ___ 26 | 27 | ### Pre-compiled Binaries 28 | 29 | Platform | Links 30 | ------------ | ------------- 31 | Windows | [x86](https://github.com/GramThanos/WakeOnLAN/releases/download/v0.3/WakeOnLAN_v0.3_windows_x86.zip) 32 | Linux | [x64](https://github.com/GramThanos/WakeOnLAN/releases/download/v0.3/WakeOnLAN_v0.3_linux_x64.zip) 33 | Raspberry Pi | [arm](https://github.com/GramThanos/WakeOnLAN/releases/download/v0.3/WakeOnLAN_v0.3_raspberrypi.zip) 34 | 35 | ___ 36 | 37 | ### Compile from source 38 | For Linux you can compile the source using GCC 39 | ```bash 40 | gcc WakeOnLAN.c -o WakeOnLAN 41 | ``` 42 | 43 | For Windows you can compile the source using MinGW 44 | ```bash 45 | gcc WakeOnLAN.c -o WakeOnLAN.exe -lwsock32 46 | ``` 47 | 48 | ___ 49 | 50 | ### Feedback 51 | 52 | For any problem you may [open an issue](https://github.com/GramThanos/WakeOnLAN/issues) 53 | 54 | Also, you can [send me a mail](mailto:gramthanos@gmail.com) 55 | 56 | ___ 57 | 58 | ### License 59 | 60 | This project is under [The MIT license](https://opensource.org/licenses/MIT). 61 | I do although appreciate attribute. 62 | 63 | Copyright (c) 2019 Grammatopoulos Athanasios-Vasileios 64 | 65 | ___ 66 | 67 | [![GramThanos](https://avatars2.githubusercontent.com/u/14858959?s=42&v=4)](https://github.com/GramThanos) 68 | [![DinoDevs](https://avatars1.githubusercontent.com/u/17518066?s=42&v=4)](https://github.com/DinoDevs) 69 | 70 | -------------------------------------------------------------------------------- /WakeOnLAN.c: -------------------------------------------------------------------------------- 1 | /* 2 | * WakeOnLAN v0.3 3 | * A simple C program that sends a magic packet 4 | * 5 | * 6 | * MIT License 7 | * 8 | * Copyright (c) 2017 Grammatopoulos Athanasios-Vasileios 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy 11 | * of this software and associated documentation files (the "Software"), to deal 12 | * in the Software without restriction, including without limitation the rights 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | * copies of the Software, and to permit persons to whom the Software is 15 | * furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in all 18 | * copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | * SOFTWARE. 27 | * 28 | */ 29 | 30 | 31 | #ifdef __linux 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #endif 38 | #ifdef _WIN32 39 | #include 40 | #include 41 | #include 42 | #pragma comment(lib, "Ws2_32.lib") 43 | #endif 44 | 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | 51 | // Create Magic Packet 52 | void createMagicPacket(unsigned char packet[], unsigned int macAddress[]){ 53 | int i; 54 | // Mac Address Variable 55 | unsigned char mac[6]; 56 | 57 | // 6 x 0xFF on start of packet 58 | for(i = 0; i < 6; i++){ 59 | packet[i] = 0xFF; 60 | mac[i] = macAddress[i]; 61 | } 62 | // Rest of the packet is MAC address of the pc 63 | for(i = 1; i <= 16; i++){ 64 | memcpy(&packet[i * 6], &mac, 6 * sizeof(unsigned char)); 65 | } 66 | } 67 | 68 | // Main Program 69 | int main(int argc, const char* argv[]){ 70 | // Default broadcast address 71 | char broadcastAddress[16] = "192.168.1.255"; 72 | // Packet buffer 73 | unsigned char packet[102]; 74 | // Mac address 75 | unsigned int mac[6]; 76 | // Set broadcast 77 | int broadcast = 1; 78 | 79 | // Linux socket 80 | #ifdef __linux 81 | // Socket 82 | int udpSocket; 83 | #endif 84 | 85 | // Windows socket 86 | #ifdef _WIN32 87 | // Socket data 88 | WSADATA data; 89 | // Socket 90 | SOCKET udpSocket; 91 | #endif 92 | 93 | // Socket address 94 | struct sockaddr_in udpClient, udpServer; 95 | 96 | // Help variables 97 | int i = 0; 98 | 99 | // If no arguments 100 | if(argc < 2){ 101 | printf("Usage:\n./wakeonlan ()\n"); 102 | exit(EXIT_FAILURE); 103 | } 104 | 105 | // Parse Mac Address 106 | i = sscanf(argv[1],"%x:%x:%x:%x:%x:%x", &(mac[0]), &(mac[1]), &(mac[2]), &(mac[3]), &(mac[4]), &(mac[5])); 107 | if(i != 6){ 108 | printf("Invalid mac address was given.\n"); 109 | exit(EXIT_FAILURE); 110 | } 111 | 112 | // Print address 113 | printf("Packet will be sent to %02hhx:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 114 | 115 | // Check if a broadcast address was given too 116 | if(argc > 2){ 117 | // Parse Broadcast Address 118 | i = sscanf(argv[2],"%d.%d.%d.%d", &i, &i, &i, &i); 119 | if(i == 4){ 120 | strcpy(broadcastAddress, argv[2]); 121 | } 122 | } 123 | printf("Broadcast address %s will be used.\n", broadcastAddress); 124 | 125 | // Create Magic Packet 126 | createMagicPacket(packet, mac); 127 | 128 | // Linux socket 129 | #ifdef __linux 130 | // Setup broadcast socket 131 | udpSocket = socket(AF_INET, SOCK_DGRAM, 0); 132 | if(setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) == -1){ 133 | printf("Failed to setup a broadcast socket.\n"); 134 | exit(EXIT_FAILURE); 135 | } 136 | // Set parameters 137 | udpClient.sin_family = AF_INET; 138 | udpClient.sin_addr.s_addr = INADDR_ANY; 139 | udpClient.sin_port = 0; 140 | // Bind socket 141 | bind(udpSocket, (struct sockaddr*) &udpClient, sizeof(udpClient)); 142 | 143 | // Set server end point (the broadcast addres) 144 | udpServer.sin_family = AF_INET; 145 | udpServer.sin_addr.s_addr = inet_addr(broadcastAddress); 146 | udpServer.sin_port = htons(9); 147 | 148 | // Send the packet 149 | sendto(udpSocket, &packet, sizeof(unsigned char) * 102, 0, (struct sockaddr*) &udpServer, sizeof(udpServer)); 150 | #endif 151 | 152 | // Windows socket 153 | #ifdef _WIN32 154 | // Setup broadcast socket 155 | WSAStartup(MAKEWORD(2, 2), &data); 156 | udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 157 | if(setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast, sizeof(broadcast)) == -1){ 158 | printf("Failed to setup a broadcast socket.\n"); 159 | exit(EXIT_FAILURE); 160 | } 161 | // Set parameters 162 | udpClient.sin_family = AF_INET; 163 | udpClient.sin_addr.s_addr = INADDR_ANY; 164 | udpClient.sin_port = htons(0); 165 | // Bind socket 166 | bind(udpSocket, (struct sockaddr*) &udpClient, sizeof(udpClient)); 167 | 168 | // Set server end point (the broadcast addres) 169 | udpServer.sin_family = AF_INET; 170 | udpServer.sin_addr.s_addr = inet_addr(broadcastAddress); 171 | udpServer.sin_port = htons(9); 172 | 173 | // Send the packet 174 | sendto(udpSocket, (char*)&packet, sizeof(unsigned char) * 102, 0, (struct sockaddr*) &udpServer, sizeof(udpServer)); 175 | #endif 176 | 177 | // Done 178 | printf("Wake up packet was sent.\n"); 179 | exit(EXIT_SUCCESS); 180 | } 181 | --------------------------------------------------------------------------------