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>
35 #define _PATH_PROCNET_ARP "/proc/net/arp"
37 Wol* Wol::instance = NULL;
45 logger = Log::getInstance();
52 Wol* Wol::getInstance()
57 void Wol::setEnabled(bool tEnabled)
59 wolEnabled = tEnabled;
62 /* Display the contents of the ARP cache in the kernel. */
63 int Wol::find_ether(char *name)
70 /* Open the PROCps kernel table. */
71 if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL)
73 perror(_PATH_PROCNET_ARP);
76 /* Bypass header -- read until newline */
77 if (fgets(line, sizeof(line), fp) != (char *) NULL)
79 /* Read the ARP cache entries. */
80 for (; fgets(line, sizeof(line), fp);) {
81 num = sscanf(line, "%s 0x%*x 0x%*x %100s %*100s %*100s\n",
85 /* if the user specified address differs, skip it */
86 if (name[0] && !strcmp(ip, name))
88 logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
97 /* Input an Ethernet address and convert to binary. */
98 int Wol::convertToBinary(char *bufp, unsigned char *addr)
102 unsigned char *ptr = addr;
107 while ((*bufp != '\0') && (i < ETH_ALEN))
113 else if (c >= 'a' && c <= 'f')
115 else if (c >= 'A' && c <= 'F')
120 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
129 else if (c >= 'a' && c <= 'f')
131 else if (c >= 'A' && c <= 'F')
133 else if (c == ':' || c == 0)
138 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
145 *ptr++ = (unsigned char) (val & 0377);
148 /* We might get a semicolon here - not required. */
163 if (!wolEnabled) return -2;
164 if (!bEtherKnown) return -3;
168 struct sockaddr_in sap;
169 unsigned char ethaddr[8];
171 unsigned char buf [128];
174 /* Fetch the hardware address. */
175 if (convertToBinary (ether_addr, ethaddr) < 0)
180 /* setup the packet socket */
181 if ((packet = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
186 /* Set socket options */
187 if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval,
188 sizeof (optval)) < 0)
190 fprintf (stderr, "setsocket failed %s\n",
196 /* Set up broadcast address */
197 sap.sin_family = AF_INET;
198 sap.sin_addr.s_addr = htonl(0xffffffff); /* broadcast address
200 sap.sin_port = htons(60000);
202 /* Build the message to send - 6 x 0xff then 16 x MAC address */
204 for (i = 0; i < 6; i++)
206 for (j = 0; j < 16; j++)
207 for (i = 0; i < ETH_ALEN; i++)
208 *ptr++ = ethaddr [i];
210 /* Send the packet out */
211 if (sendto (packet, buf, 102, 0, (struct sockaddr *)&sap, sizeof (sap)) < 0)
213 fprintf (stderr, " sendto failed, %s\n",
219 logger->log("Wol", Log::NOTICE, "Send wakeonlan to server");
225 void Wol::setWakeUpIP(char* ip_addr)
227 if(find_ether(ip_addr))
230 logger->log("Wol", Log::NOTICE, "Server IP set");