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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
29 #if !defined(__ANDROID__) && !defined(WIN32)
30 #include <net/ethernet.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
49 #define _PATH_PROCNET_ARP "/proc/net/arp"
51 Wol* Wol::instance = NULL;
59 logger = Log::getInstance();
69 Wol* Wol::getInstance()
74 void Wol::setEnabled(bool tEnabled)
76 wolEnabled = tEnabled;
79 /* Display the contents of the ARP cache in the kernel. */
80 int Wol::find_ether(const char *name)
87 /* Open the PROCps kernel table. */
88 if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL)
90 perror(_PATH_PROCNET_ARP);
93 /* Bypass header -- read until newline */
94 if (fgets(line, sizeof(line), fp) != (char *) NULL)
96 /* Read the ARP cache entries. */
97 for (; fgets(line, sizeof(line), fp);) {
98 num = sscanf(line, "%s 0x%*x 0x%*x %100s %*100s %*100s\n",
102 /* if the user specified address differs, skip it */
103 if (name[0] && !strcmp(ip, name))
105 logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
115 int Wol::find_ether(const char *name)
117 sockaddr_in sock_address;
118 int sockname_len=sizeof(sock_address);
119 WSAStringToAddress(const_cast<LPSTR>(name),AF_INET,NULL,(sockaddr*)&sock_address,&sockname_len);
121 DWORD ip=sock_address.sin_addr.s_addr;;
123 PMIB_IPNETTABLE table;
125 GetIpNetTable(NULL,&size,TRUE);
126 table=(PMIB_IPNETTABLE)new char[size];
127 if (table==NULL) return -1;
128 if (GetIpNetTable(table,&size,TRUE)!=NOERROR)
130 logger->log("Wol", Log::NOTICE, "Error getting IPtable");
134 for (int x=0;x<table->dwNumEntries;x++)
136 if (table->table[x].dwAddr==ip) {
137 char *ethp=ether_addr;
138 for (int i=0;i<table->table[x].dwPhysAddrLen;i++) {
139 ethp+=sprintf(ethp,"%X:",table->table[x].bPhysAddr[i]);
142 logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
155 /* Input an Ethernet address and convert to binary. */
156 int Wol::convertToBinary(char *bufp, unsigned char *addr)
163 unsigned char *ptr = addr;
167 while ((*bufp != '\0') && (i < ETH_ALEN))
173 else if (c >= 'a' && c <= 'f')
175 else if (c >= 'A' && c <= 'F')
180 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
189 else if (c >= 'a' && c <= 'f')
191 else if (c >= 'A' && c <= 'F')
193 else if (c == ':' || c == 0)
198 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
205 *ptr++ = static_cast<unsigned char>(val & 0377);
208 /* We might get a semicolon here - not required. */
223 if (!wolEnabled) return -2;
224 if (!bEtherKnown) return -3;
228 struct sockaddr_in sap;
229 unsigned char ethaddr[8];
231 unsigned char buf [128];
234 /* Fetch the hardware address. */
235 if (convertToBinary (ether_addr, ethaddr) < 0)
240 /* setup the packet socket */
241 if ((packet = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
246 /* Set socket options */
248 if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, static_cast<void*>(&optval),
249 sizeof (optval)) < 0)
251 if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (char*)&optval,
252 sizeof (optval)) < 0)
255 fprintf (stderr, "setsocket failed %s\n",
261 /* Set up broadcast address */
262 sap.sin_family = AF_INET;
263 sap.sin_addr.s_addr = htonl(0xffffffff); /* broadcast address
265 sap.sin_port = htons(60000);
267 /* Build the message to send - 6 x 0xff then 16 x MAC address */
269 for (i = 0; i < 6; i++)
271 for (j = 0; j < 16; j++)
272 for (i = 0; i < ETH_ALEN; i++)
273 *ptr++ = ethaddr [i];
275 /* Send the packet out */
277 if (sendto(packet, reinterpret_cast<const char*>(buf), 102, 0, reinterpret_cast<struct sockaddr *>(&sap), sizeof (sap)) < 0)
279 if (sendto(packet, static_cast<void*>(buf), 102, 0, reinterpret_cast<struct sockaddr *>(&sap), sizeof(sap)) < 0)
282 fprintf (stderr, " sendto failed, %s\n",
288 logger->log("Wol", Log::NOTICE, "Send wakeonlan to server");
294 void Wol::setWakeUpIP(const char* ip_addr)
296 if(find_ether(ip_addr))
299 logger->log("Wol", Log::NOTICE, "Server IP set");