2 Copyright 2006 Matthias Haas <vomp@pompase.net>
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 Based on the work by R. Edwards (bob@cs.anu.edu.au) for the wakeonlan stuff
21 Based ont the work of Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> for the arp stuff
28 #include <net/ethernet.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
33 #define _PATH_PROCNET_ARP "/proc/net/arp"
35 Wol* Wol::instance = NULL;
43 logger = Log::getInstance();
50 Wol* Wol::getInstance()
55 void Wol::setEnabled(bool tEnabled)
57 wolEnabled = tEnabled;
60 /* Display the contents of the ARP cache in the kernel. */
61 int Wol::find_ether(char *name)
68 /* Open the PROCps kernel table. */
69 if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL)
71 perror(_PATH_PROCNET_ARP);
74 /* Bypass header -- read until newline */
75 if (fgets(line, sizeof(line), fp) != (char *) NULL)
77 /* Read the ARP cache entries. */
78 for (; fgets(line, sizeof(line), fp);) {
79 num = sscanf(line, "%s 0x%*x 0x%*x %100s %*100s %*100s\n",
83 /* if the user specified address differs, skip it */
84 if (name[0] && !strcmp(ip, name))
86 logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
95 /* Input an Ethernet address and convert to binary. */
96 int Wol::convertToBinary(char *bufp, unsigned char *addr)
100 unsigned char *ptr = addr;
105 while ((*bufp != '\0') && (i < ETH_ALEN))
111 else if (c >= 'a' && c <= 'f')
113 else if (c >= 'A' && c <= 'F')
118 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
127 else if (c >= 'a' && c <= 'f')
129 else if (c >= 'A' && c <= 'F')
131 else if (c == ':' || c == 0)
136 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
143 *ptr++ = (unsigned char) (val & 0377);
146 /* We might get a semicolon here - not required. */
161 if (!wolEnabled) return -2;
162 if (!bEtherKnown) return -3;
166 struct sockaddr_in sap;
167 unsigned char ethaddr[8];
169 unsigned char buf [128];
172 /* Fetch the hardware address. */
173 if (convertToBinary (ether_addr, ethaddr) < 0)
178 /* setup the packet socket */
179 if ((packet = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
184 /* Set socket options */
185 if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval,
186 sizeof (optval)) < 0)
188 fprintf (stderr, "setsocket failed %s\n",
194 /* Set up broadcast address */
195 sap.sin_family = AF_INET;
196 sap.sin_addr.s_addr = htonl(0xffffffff); /* broadcast address
198 sap.sin_port = htons(60000);
200 /* Build the message to send - 6 x 0xff then 16 x MAC address */
202 for (i = 0; i < 6; i++)
204 for (j = 0; j < 16; j++)
205 for (i = 0; i < ETH_ALEN; i++)
206 *ptr++ = ethaddr [i];
208 /* Send the packet out */
209 if (sendto (packet, buf, 102, 0, (struct sockaddr *)&sap, sizeof (sap)) < 0)
211 fprintf (stderr, " sendto failed, %s\n",
217 logger->log("Wol", Log::NOTICE, "Send wakeonlan to server");
223 void Wol::setWakeUpIP(char* ip_addr)
225 if(find_ether(ip_addr))
228 logger->log("Wol", Log::NOTICE, "Server IP set");