Ooooh, my sweet Wake On Lan message!
So take action and wake up all your machines using this Objective-C method!
/** Sends a Wake On Lan (WOL) messagge to the specified target.
MAC address must be in the 12:34:56:78:9A:BC format.
@param mac The MAC address to wake up.
@param addr The subnet mask to use.
@return Returns true on success, false otherwise.
@see wakeOnLanByMacAddress:
*/
+ (BOOL)wakeOnLanByMacAddress:(char *)mac usingSubnet:(char *)addr {
int BUFMAX = 1024;
int MACLEN = 6;
int sd;
int optval;
char unsigned buf[BUFMAX];
int len;
struct sockaddr_in sin;
unsigned char macAddr[MACLEN];
unsigned char *p;
int i, j;
bzero((char *)&sin,sizeof(sin)); // Clear sin struct.
sin.sin_family = AF_INET;
host2addr(addr,&sin.sin_addr,(short*)&sin.sin_family); // Host.
sin.sin_port = htons(9); // Port.
if ((sd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
LogError(@"Can't get socket");
return NO;
}
optval = 1;
if (setsockopt(sd,SOL_SOCKET,SO_BROADCAST,&optval,sizeof(optval)) < 0) {
LogError(@"Can't set sockopt (%d)", errno);
return NO;
}
p = (unsigned char*)mac;
j = hex2((char*)p);
if (j < 0) {
MACerror:
LogError(@"Illegal MAC address: %s",mac);
return NO;
}
macAddr[0] = j;
p += 2;
for (i=1; i < MACLEN; i++) {
if (*p++ != ':') goto MACerror;
j = hex2((char*)p);
if (j < 0) goto MACerror;
macAddr[i] = j;
p += 2;
}
p = buf;
for (i=0; i < 6; i++) { // 6 bytes of FFhex.
*p++ = 0xFF;
}
for (i=0; i < 16; i++) { // MAC addresses repeated 16 times.
for (j=0; j < MACLEN; j++) {
*p++ = macAddr[j];
}
}
len = p - buf;
if (sendto(sd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin)) != len) {
LogError(@"Sendto failed (%d)", errno);
return NO;
}
return YES;
}
Download the ready-for-use source file (.m) of WOL: Wake On Lan (within custom subnet mask)
Thanks for this informative article. I had some troubles with Win 10. For example, after I upgraded it from win 8, my Wake-on-LAN does not work. I tried many things and
ReplyDeletefinally I solved the problem.
Here you can find how to configure Wake on LAN Windows 10