necouchman commented on a change in pull request #267: GUACAMOLE-513: Implement 
Wake-on-LAN support in libguac
URL: https://github.com/apache/guacamole-server/pull/267#discussion_r394593897
 
 

 ##########
 File path: src/libguac/wol.c
 ##########
 @@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+
+#include "guacamole/wol.h"
+
+/**
+ * Generate the magic Wake-on-LAN (WoL) packet for the specified MAC address
+ * and place it in the character array.
+ * 
+ * @param packet
+ *     The character array that will contain the generated packet.
+ * 
+ * @param mac_address
+ *     The unsigned int representation of the MAC address to place in the 
packet.
+ */
+static void __guac_wol_create_magic_packet(unsigned char packet[],
+        unsigned int mac_address[]) {
+    
+    int i;
+    unsigned char mac[6];
+    
+    /* Concurrently fill the first part of the packet with 0xFF, and copy the
+     MAC address from the int array to the char array. */
+    for (i = 0; i < 6; i++) {
+        packet[i] = 0xFF;
+        mac[i] = mac_address[i];
+    }
+    
+    /* Copy the MAC address contents into the char array that is storing
+     the rest of the packet. */
+    for (i = 1; i <= 16; i++) {
+        memcpy(&packet[i * 6], &mac, 6 * sizeof(unsigned char));
+    }
+    
+}
+
+/**
+ * Send the magic Wake-on-LAN (WoL) packet to the specified broadcast address,
+ * returning the number of bytes sent, or zero if any error occurred and 
nothing
+ * was sent.
+ * 
+ * @param broadcast_addr
+ *     The broadcast address to which to send the magic WoL packet.
+ * 
+ * @param packet
+ *     The magic WoL packet to send.
+ * 
+ * @return 
+ *     The number of bytes sent, or zero if nothing could be sent.
+ */
+static ssize_t __guac_wol_send_packet(const char* broadcast_addr,
+        unsigned char packet[]) {
+    
+    int wol_bcast = 1;
+    struct sockaddr_in wol_dest;
+    int wol_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    
+    /* Attempt to set broadcast; exit with error if this fails. */
+    if (setsockopt(wol_socket, SOL_SOCKET, SO_BROADCAST, &wol_bcast, 
sizeof(wol_bcast)) < 0) {
+        return 0;
+    }
+    
+    /* Set server endpoint to the broadcast address. */
+    wol_dest.sin_family = AF_INET;
+    wol_dest.sin_port = htons(9);
+    wol_dest.sin_addr.s_addr = inet_addr(broadcast_addr);
 
 Review comment:
   Okay, this may take me a little longer to figure out how to do - the 
`inet_addr()` function appears to be a little outdated and only supports IPv4 
(according to the Linux manpage, anyway), and several of the other ones appear 
to be ones that are not recommended for us, so I've got to figure out a good 
way to detect IPv4 vs. IPv6 and parse out into the arguments that the `socket` 
stuff expects.
   
   Also, for what it's worth, IPv6 doesn't support broadcast, only link-local 
multicast, so I feel like we're going to end up with a couple of different 
functions for this - one for IPv4 and one for IPv6.  Definitely a little more 
work to do, here...

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to