{"id":915,"date":"2013-06-23T19:57:19","date_gmt":"2013-06-23T19:57:19","guid":{"rendered":"https:\/\/zogspat.tk\/blog\/?p=915"},"modified":"2013-08-17T11:12:01","modified_gmt":"2013-08-17T11:12:01","slug":"wake-on-lan-for-ios-using-gcdasyncudpsocket","status":"publish","type":"post","link":"https:\/\/the-plot.com\/blog\/?p=915","title":{"rendered":"Wake on Lan for iOS Using GCDAsyncUdpSocket"},"content":{"rendered":"<p>I&#8217;ve spent the last couple of weeks working on a new app which runs in the background, and uses cell tower &#8216;significant change&#8217; functionality to do geofencing. Having got it to work, I&#8217;ve now turned my attention to one of the ultimate goals, which is to start sending WoL packets when I enter a CLRegion I&#8217;ve defined around the house. [This is actually going to be quite tricky, by virtue of the fact that the regions need to be pretty big, given the sort of accuracy that\u00a0startMonitoringSignificantLocationChanges provides, but I&#8217;ll come back to that another time].<\/p>\n<p>There is a fair amount of example code for WoL, but I couldn&#8217;t find anything particularly easy to adapt. What I&#8217;ve ended up doing is grafting <a href=\"http:\/\/shadesfgray.wordpress.com\/2010\/12\/17\/wake-on-lan-how-to-tutorial\/\">this C example <\/a>onto GCDAsyncUdpSocket. It works fine, but there is one annoying side effect, which I haven&#8217;t been able to work out yet.<\/p>\n<p>First the code:<\/p>\n<pre>\/\/http:\/\/shadesfgray.wordpress.com\/2010\/12\/17\/wake-on-lan-how-to-tutorial\/\r\n    unsigned char tosend[102];\r\n    unsigned char mac[6];\r\n    for(int i = 0; i &lt; 6; i++)\r\n    {\r\n        tosend[i] = 0xff;\r\n    }\r\n    \/\/ substitute MAC address: E.g.,\r\n    mac[0] = 0x00;\r\n    mac[1] = 0x2a;\r\n    mac[2] = 0x00;\r\n    mac[3] = 0xfe;\r\n    mac[4] = 0xff;\r\n    mac[5] = 0x33;\r\n\r\nfor(int i = 1; i <= 16; i++)\r\n    {\r\n        memcpy(&#038;tosend[i * 6], &#038;mac, 6 * sizeof(unsigned char));\r\n    }\r\n    \r\n\r\n    NSData *payLoad = [NSData dataWithBytes:tosend length:102];\r\n    GCDAsyncUdpSocket *mysocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];\r\n    [mysocket enableBroadcast:YES error:nil]; \r\n    [mysocket sendData:payLoad toHost:@\"192.168.1.255\" port:9 withTimeout:-1 tag:0];\r\n\r\n<\/pre>\n<p>So, this works for me insofar as I'm happy enough with broadcasting to the network, with the final quad of the IP set to 255. What I haven't been able to work out is how to send the packet to a single machine. I've written about WoL functionality <a href=\"https:\/\/the-plot.com\/blog\/?p=485\">before<\/a> - unfortunately the project I mentioned in that post is no longer making the source code available. However, I still have the executable and resorted to WireShark to see if I could fiddle around with the code above to make it play ball:<\/p>\n<div id=\"attachment_919\" style=\"width: 160px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/the-plot.com\/blog\/wp-content\/uploads\/2013\/06\/Screen-Shot-2013-06-23-at-20.44.11.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-919\" class=\"size-thumbnail wp-image-919\" title=\"WireShark\" src=\"https:\/\/the-plot.com\/blog\/wp-content\/uploads\/2013\/06\/Screen-Shot-2013-06-23-at-20.44.11-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><p id=\"caption-attachment-919\" class=\"wp-caption-text\">WireShark<\/p><\/div>\n<p>So the .69 example above is from my iPhone, and the .73 is from my MBA, using wol from the command line. To cut a long story short, the full IP is identified as being sufficiently well formed for the WireShark filter to call it WoL, but it won't wake the machine. Final point: all 255s will also wake the machine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve spent the last couple of weeks working on a new app which runs in the background, and uses cell tower &#8216;significant change&#8217; functionality to do geofencing. Having got it to work, I&#8217;ve now turned my attention to one of &hellip; <a href=\"https:\/\/the-plot.com\/blog\/?p=915\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-915","post","type-post","status-publish","format-standard","hentry","category-ios-development"],"_links":{"self":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=915"}],"version-history":[{"count":10,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/915\/revisions"}],"predecessor-version":[{"id":937,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/915\/revisions\/937"}],"wp:attachment":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}