implemented wake on lan

This commit is contained in:
Diego Waxemberg
2015-01-02 19:15:47 -05:00
parent a52b20ee52
commit ee61dc9a59
4 changed files with 133 additions and 6 deletions

View File

@@ -0,0 +1,16 @@
//
// WakeOnLanManager.h
// Limelight
//
// Created by Diego Waxemberg on 1/2/15.
// Copyright (c) 2015 Limelight Stream. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Host.h"
@interface WakeOnLanManager : NSObject
+ (void) wakeHost:(Host*)host;
@end

View File

@@ -0,0 +1,82 @@
//
// WakeOnLanManager.m
// Limelight
//
// Created by Diego Waxemberg on 1/2/15.
// Copyright (c) 2015 Limelight Stream. All rights reserved.
//
#import "WakeOnLanManager.h"
#import "Utils.h"
#import <CoreFoundation/CoreFoundation.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
@implementation WakeOnLanManager
static const int numPorts = 5;
static const int ports[numPorts] = {7, 9, 47998, 47999, 48000};
+ (void) wakeHost:(Host*)host {
NSData* wolPayload = [WakeOnLanManager createPayload:host];
CFDataRef dataPayload = CFDataCreate(kCFAllocatorDefault, [wolPayload bytes], [wolPayload length]);
CFSocketRef wolSocket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, NULL, NULL);
if (!wolSocket) {
NSLog(@"Failed to create WOL socket");
return;
}
NSLog(@"WOL socket created");
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_len = sizeof(addr);
for (int i = 0; i < 2; i++) {
// try all ip addresses
if (i == 0) {
inet_aton([host.localAddress UTF8String], &addr.sin_addr);
} else {
inet_aton([host.externalAddress UTF8String], &addr.sin_addr);
}
CFDataRef dataAddress = CFDataCreate(kCFAllocatorDefault, (unsigned char*)&addr, sizeof(addr));
// try all ports
for (int j = 0; j < numPorts; j++) {
addr.sin_port = htons(ports[j]);
NSLog(@"Sending WOL packet");
CFSocketSendData(wolSocket, dataAddress, dataPayload, 0);
}
CFRelease(dataAddress);
}
CFRelease(dataPayload);
}
+ (NSData*) createPayload:(Host*)host {
NSMutableData* payload = [[NSMutableData alloc] initWithCapacity:102];
// 6 bytes of FF
UInt8 header = 0xFF;
for (int i = 0; i < 6; i++) {
[payload appendBytes:&header length:1];
}
// 16 repitiions of MAC address
NSData* macAddress = [self macStringToBytes:host.mac];
for (int j = 0; j < 16; j++) {
[payload appendData:macAddress];
}
return payload;
}
+ (NSData*) macStringToBytes:(NSString*)mac {
NSString* macString = [mac stringByReplacingOccurrencesOfString:@":" withString:@""];
NSLog(@"MAC: %@", macString);
return [Utils hexToBytes:macString];
}
@end

View File

@@ -18,6 +18,7 @@
#import "SettingsViewController.h"
#import "DataManager.h"
#import "Settings.h"
#import "WakeOnLanManager.h"
@implementation MainFrameViewController {
NSOperationQueue* _opQueue;
@@ -115,12 +116,30 @@ static StreamConfiguration* streamConfig;
- (void)hostLongClicked:(Host *)host {
NSLog(@"Long clicked host: %@", host.name);
UIAlertController* longClickAlert = [UIAlertController alertControllerWithTitle:host.name message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
[longClickAlert addAction:[UIAlertAction actionWithTitle:@"Unpair" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
HttpManager* hMan = [[HttpManager alloc] initWithHost:host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert];
[hMan executeRequestSynchronously:[hMan newUnpairRequest]];
});
}]];
if (host.online) {
[longClickAlert addAction:[UIAlertAction actionWithTitle:@"Unpair" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
HttpManager* hMan = [[HttpManager alloc] initWithHost:host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert];
[hMan executeRequestSynchronously:[hMan newUnpairRequest]];
});
}]];
} else {
[longClickAlert addAction:[UIAlertAction actionWithTitle:@"Wake" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
UIAlertController* wolAlert = [UIAlertController alertControllerWithTitle:@"Wake On Lan" message:@"" preferredStyle:UIAlertControllerStyleAlert];
[wolAlert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
if (host.pairState != PairStatePaired) {
wolAlert.message = @"Cannot wake host because you are not paired";
} else if (host.mac == nil || [host.mac isEqualToString:@"00:00:00:00:00:00"]) {
wolAlert.message = @"Host MAC unknown, unable to send WOL Packet";
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[WakeOnLanManager wakeHost:host];
});
wolAlert.message = @"Sent WOL Packet";
}
[self presentViewController:wolAlert animated:YES completion:nil];
}]];
}
[longClickAlert addAction:[UIAlertAction actionWithTitle:@"Remove Host" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action) {
@synchronized(hostList) {
[hostList removeObject:host];