--- /dev/null
+/*
+ Copyright 2006 Matthias Haas <vomp@pompase.net>
+
+ This file is part of VOMP.
+
+ VOMP is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ VOMP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with VOMP; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ Based on the work by R. Edwards (bob@cs.anu.edu.au) for the wakeonlan stuff
+ Based ont the work of Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> for the arp stuff
+ Thanks to you guys.
+*/
+
+#include <ctype.h>
+#include <errno.h>
+#include <unistd.h>
+#include <net/ethernet.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include "wol.h"
+
+#define _PATH_PROCNET_ARP "/proc/net/arp"
+
+Wol* Wol::instance = NULL;
+
+Wol::Wol()
+{
+ if (instance) return;
+ instance = this;
+ bEtherKnown = false;
+ wolEnabled = true;
+ logger = Log::getInstance();
+}
+
+Wol::~Wol()
+{
+}
+
+Wol* Wol::getInstance()
+{
+ return instance;
+}
+
+void Wol::setEnabled(bool tEnabled)
+{
+ wolEnabled = tEnabled;
+}
+
+/* Display the contents of the ARP cache in the kernel. */
+int Wol::find_ether(char *name)
+{
+ char ip[100];
+ char line[200];
+ FILE *fp;
+ int num;
+
+ /* Open the PROCps kernel table. */
+ if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL)
+ {
+ perror(_PATH_PROCNET_ARP);
+ return -1;
+ }
+ /* Bypass header -- read until newline */
+ if (fgets(line, sizeof(line), fp) != (char *) NULL)
+ {
+ /* Read the ARP cache entries. */
+ for (; fgets(line, sizeof(line), fp);) {
+ num = sscanf(line, "%s 0x%*x 0x%*x %100s %*100s %*100s\n",
+ ip, ether_addr);
+ if (num < 2)
+ break;
+ /* if the user specified address differs, skip it */
+ if (name[0] && !strcmp(ip, name))
+ {
+ logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
+ return 1;
+ }
+ }
+ }
+ fclose(fp);
+ return 0;
+}
+
+/* Input an Ethernet address and convert to binary. */
+int Wol::convertToBinary(char *bufp, unsigned char *addr)
+{
+ char c, *orig;
+ int i;
+ unsigned char *ptr = addr;
+ unsigned val;
+
+ i = 0;
+ orig = bufp;
+ while ((*bufp != '\0') && (i < ETH_ALEN))
+ {
+ val = 0;
+ c = *bufp++;
+ if (isdigit(c))
+ val = c - '0';
+ else if (c >= 'a' && c <= 'f')
+ val = c - 'a' + 10;
+ else if (c >= 'A' && c <= 'F')
+ val = c - 'A' + 10;
+ else
+ {
+#ifdef DEBUG
+ fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
+#endif
+ errno = EINVAL;
+ return (-1);
+ }
+ val <<= 4;
+ c = *bufp;
+ if (isdigit(c))
+ val |= c - '0';
+ else if (c >= 'a' && c <= 'f')
+ val |= c - 'a' + 10;
+ else if (c >= 'A' && c <= 'F')
+ val |= c - 'A' + 10;
+ else if (c == ':' || c == 0)
+ val >>= 4;
+ else
+ {
+#ifdef DEBUG
+ fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
+#endif
+ errno = EINVAL;
+ return (-1);
+ }
+ if (c != 0)
+ bufp++;
+ *ptr++ = (unsigned char) (val & 0377);
+ i++;
+
+ /* We might get a semicolon here - not required. */
+ if (*bufp == ':')
+ {
+ if (i == ETH_ALEN)
+ {
+ ; /* nothing */
+ }
+ bufp++;
+ }
+ }
+ return (0);
+} /* in_ether */
+
+int Wol::doWakeUp()
+{
+ if (!wolEnabled) return -2;
+ if (!bEtherKnown) return -3;
+
+ int i, j;
+ int packet;
+ struct sockaddr_in sap;
+ unsigned char ethaddr[8];
+ unsigned char *ptr;
+ unsigned char buf [128];
+ int optval = 1;
+
+ /* Fetch the hardware address. */
+ if (convertToBinary (ether_addr, ethaddr) < 0)
+ {
+ return (-1);
+ }
+
+ /* setup the packet socket */
+ if ((packet = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
+ {
+ return (-1);
+ }
+
+ /* Set socket options */
+ if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval,
+ sizeof (optval)) < 0)
+ {
+ fprintf (stderr, "setsocket failed %s\n",
+ strerror (errno));
+ close (packet);
+ return (-1);
+ }
+
+ /* Set up broadcast address */
+ sap.sin_family = AF_INET;
+ sap.sin_addr.s_addr = htonl(0xffffffff); /* broadcast address
+*/
+ sap.sin_port = htons(60000);
+
+ /* Build the message to send - 6 x 0xff then 16 x MAC address */
+ ptr = buf;
+ for (i = 0; i < 6; i++)
+ *ptr++ = 0xff;
+ for (j = 0; j < 16; j++)
+ for (i = 0; i < ETH_ALEN; i++)
+ *ptr++ = ethaddr [i];
+
+ /* Send the packet out */
+ if (sendto (packet, buf, 102, 0, (struct sockaddr *)&sap, sizeof (sap)) < 0)
+ {
+ fprintf (stderr, " sendto failed, %s\n",
+ strerror(errno));
+ close (packet);
+ return (-1);
+ }
+
+ logger->log("Wol", Log::NOTICE, "Send wakeonlan to server");
+
+ close (packet);
+ return (0);
+}
+
+void Wol::setWakeUpIP(char* ip_addr)
+{
+ if(find_ether(ip_addr))
+ {
+ bEtherKnown = true;
+ logger->log("Wol", Log::NOTICE, "Server IP set");
+ }
+}
+
--- /dev/null
+/*
+ Copyright 2006 Matthias Haas <vomp@pompase.net>
+
+ This file is part of VOMP.
+
+ VOMP is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ VOMP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with VOMP; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ Based on the work by R. Edwards (bob@cs.anu.edu.au) for the wakeonlan stuff
+ Based ont the work of Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> for the arp stuff
+ Thanks to you guys.
+
+*/
+
+#ifndef WOL_H
+#define WOL_H
+
+#include <stdio.h>
+#include <string.h>
+#include "log.h"
+
+class Wol
+{
+ public:
+ Wol();
+ ~Wol();
+ static Wol* getInstance();
+ int doWakeUp();
+ void setWakeUpIP(char* ip_addr);
+ void setEnabled(bool enabled);
+
+ private:
+ static Wol* instance;
+ int find_ether(char *name);
+ int convertToBinary(char *bufp, unsigned char *addr);
+ char ether_addr[100];
+ bool bEtherKnown;
+ bool wolEnabled;
+ Log* logger;
+};
+
+#endif