Skip to content

Instantly share code, notes, and snippets.

@nobodyzxc
Created August 19, 2016 14:47
Show Gist options
  • Save nobodyzxc/833292d5b26e53cf71c02932105c858c to your computer and use it in GitHub Desktop.
Save nobodyzxc/833292d5b26e53cf71c02932105c858c to your computer and use it in GitHub Desktop.
Wake On Wan
//code reference https://shadesfgray.wordpress.com/2010/12/17/wake-on-lan-how-to-tutorial/
//thx to Shades of Gray's blogger !
//I made some changes and make wol to wow by setting my router.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
/* select one to define
#define IP "www.yourdns.ddns.net"//wan
#define IP "211.74.180.72"//wan
#define IP "192.168.1.10"//lan
*/
unsigned char package[102];
unsigned char mac[6];
int main(void){
int i;
//make package
for(i = 0 ; i < 6 ; i++)
package[i] = 0xff;
//your device's mac
mac[0] = 0x38;
mac[1] = 0x60;
mac[2] = 0x77;
mac[3] = 0xc7;
mac[4] = 0x53;
mac[5] = 0x4e;
//package end
for(i = 1 ; i <= 16 ; i++)
memcpy(&package[i*6] , &mac , 6 * sizeof(unsigned char));
int udpSocket;
struct sockaddr_in udpClient, udpServer;
int broadcast = 1;
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
//broadcast setting
if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) == -1) {
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;
bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
//server setting
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(IP);
udpServer.sin_port = htons(9);
//send package
sendto(udpSocket, &package, sizeof(unsigned char) * 102, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment