]> git.vomp.tv Git - vompclient.git/blob - wol.cc
Removal of Message::REDRAW and BoxStack handler for it
[vompclient.git] / wol.cc
1 /*
2     Copyright 2006 Matthias Haas <vomp@pompase.net>
3
4     This file is part of VOMP.
5
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.
10
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.
15
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.
19
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
22     Thanks to you guys.
23 */
24
25 #include <ctype.h>
26 #include <errno.h>
27 #ifndef WIN32
28 #include <unistd.h>
29 #include <net/ethernet.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #else
33 #define ETH_ALEN                6
34
35 #include <winsock2.h>
36 #include <errno.h>
37 #include <Ws2tcpip.h>
38 #include <Iphlpapi.h>
39 #endif
40 #include "wol.h"
41 #include "log.h"
42
43 #define _PATH_PROCNET_ARP "/proc/net/arp"
44
45 Wol* Wol::instance = NULL;
46
47 Wol::Wol()
48 {
49   if (instance) return;
50   instance = this;
51   bEtherKnown = false;
52   wolEnabled = true;
53   logger = Log::getInstance();
54 #ifdef WIN32
55 //  goo;
56 #endif
57 }
58
59 Wol::~Wol()
60 {
61 }
62
63 Wol* Wol::getInstance()
64 {
65   return instance;
66 }
67
68 void Wol::setEnabled(bool tEnabled)
69 {
70   wolEnabled = tEnabled;
71 }
72 #ifndef WIN32
73 /* Display the contents of the ARP cache in the kernel. */
74 int Wol::find_ether(char *name)
75 {
76   char ip[100];
77   char line[200];
78   FILE *fp;
79   int num;
80
81   /* Open the PROCps kernel table. */
82   if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL)
83   {
84     perror(_PATH_PROCNET_ARP);
85     return -1;
86   }
87   /* Bypass header -- read until newline */
88   if (fgets(line, sizeof(line), fp) != (char *) NULL)
89   {
90     /* Read the ARP cache entries. */
91     for (; fgets(line, sizeof(line), fp);) {
92       num = sscanf(line, "%s 0x%*x 0x%*x %100s %*100s %*100s\n",
93         ip, ether_addr);
94       if (num < 2)
95         break;
96       /* if the user specified address differs, skip it */
97       if (name[0] && !strcmp(ip, name))
98       {
99         logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
100         return 1;
101       }
102     }
103   }
104   fclose(fp);
105   return 0;
106 }
107 #else
108 int Wol::find_ether(char *name)
109 {
110   sockaddr_in sock_address;
111   int sockname_len=sizeof(sock_address);
112   WSAStringToAddress(name,AF_INET,NULL,(sockaddr*)&sock_address,&sockname_len);
113   
114   DWORD ip=sock_address.sin_addr.s_addr;;
115   ULONG size=0;
116   PMIB_IPNETTABLE table;
117
118   GetIpNetTable(NULL,&size,TRUE);
119   table=(PMIB_IPNETTABLE)new char[size];
120   if (table==NULL) return -1;
121   if (GetIpNetTable(table,&size,TRUE)!=NOERROR)
122   {
123       logger->log("Wol", Log::NOTICE, "Error getting IPtable");
124       delete [] table;
125       return -1;
126   }
127   for (int x=0;x<table->dwNumEntries;x++)
128   {
129       if (table->table[x].dwAddr==ip) {
130           char *ethp=ether_addr;
131           for (int i=0;i<table->table[x].dwPhysAddrLen;i++) {
132               ethp+=sprintf(ethp,"%X:",table->table[x].bPhysAddr[i]);
133           }
134           delete [] table;
135           logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
136           return 1;
137       }
138       
139   }
140
141   delete [] table;
142  return 0;
143
144   
145 }
146 #endif
147
148 /* Input an Ethernet address and convert to binary. */
149 int Wol::convertToBinary(char *bufp, unsigned char *addr)
150 {
151     char c, *orig;
152     int i;
153         unsigned char *ptr = addr;
154     unsigned val;
155
156     i = 0;
157     orig = bufp;
158     while ((*bufp != '\0') && (i < ETH_ALEN))
159     {
160         val = 0;
161         c = *bufp++;
162         if (isdigit(c))
163             val = c - '0';
164         else if (c >= 'a' && c <= 'f')
165             val = c - 'a' + 10;
166         else if (c >= 'A' && c <= 'F')
167             val = c - 'A' + 10;
168         else
169         {
170 #ifdef DEBUG
171             fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
172 #endif
173             errno = EINVAL;
174             return (-1);
175         }
176         val <<= 4;
177         c = *bufp;
178         if (isdigit(c))
179             val |= c - '0';
180         else if (c >= 'a' && c <= 'f')
181             val |= c - 'a' + 10;
182         else if (c >= 'A' && c <= 'F')
183             val |= c - 'A' + 10;
184         else if (c == ':' || c == 0)
185             val >>= 4;
186         else
187         {
188 #ifdef DEBUG
189             fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
190 #endif
191             errno = EINVAL;
192             return (-1);
193         }
194         if (c != 0)
195             bufp++;
196         *ptr++ = (unsigned char) (val & 0377);
197         i++;
198
199         /* We might get a semicolon here - not required. */
200         if (*bufp == ':')
201         {
202             if (i == ETH_ALEN)
203             {
204                     ;           /* nothing */
205             }
206             bufp++;
207         }
208     }
209     return (0);
210 } /* in_ether */
211
212 int Wol::doWakeUp()
213 {
214   if (!wolEnabled) return -2;
215   if (!bEtherKnown) return -3;
216
217   int i, j;
218   int packet;
219   struct sockaddr_in sap;
220   unsigned char ethaddr[8];
221   unsigned char *ptr;
222   unsigned char buf [128];
223   int optval = 1;
224
225   /* Fetch the hardware address. */
226   if (convertToBinary (ether_addr, ethaddr) < 0)
227   {
228     return (-1);
229   }
230
231   /* setup the packet socket */
232   if ((packet = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
233   {
234     return (-1);
235   }
236
237   /* Set socket options */
238 #ifndef WIN32
239   if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval,
240      sizeof (optval)) < 0)
241 #else
242   if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (char*)&optval,
243      sizeof (optval)) < 0)
244 #endif
245   {
246     fprintf (stderr, "setsocket failed %s\n",
247     strerror (errno));
248     CLOSESOCKET(packet);
249     return (-1);
250   }
251
252   /* Set up broadcast address */
253   sap.sin_family = AF_INET;
254   sap.sin_addr.s_addr = htonl(0xffffffff);        /* broadcast address
255 */
256   sap.sin_port = htons(60000);
257
258   /* Build the message to send - 6 x 0xff then 16 x MAC address */
259   ptr = buf;
260   for (i = 0; i < 6; i++)
261     *ptr++ = 0xff;
262   for (j = 0; j < 16; j++)
263     for (i = 0; i < ETH_ALEN; i++)
264       *ptr++ = ethaddr [i];
265
266   /* Send the packet out */
267   if (sendto (packet,(char*) buf, 102, 0, (struct sockaddr *)&sap, sizeof (sap)) < 0)
268   {
269     fprintf (stderr, " sendto failed, %s\n",
270     strerror(errno));
271     CLOSESOCKET(packet);
272     return (-1);
273   }
274
275   logger->log("Wol", Log::NOTICE, "Send wakeonlan to server");
276
277   CLOSESOCKET(packet);
278   return (0);
279 }
280
281 void Wol::setWakeUpIP(char* ip_addr)
282 {
283   if(find_ether(ip_addr))
284   {
285     bEtherKnown = true;
286     logger->log("Wol", Log::NOTICE, "Server IP set");
287   }
288 }
289