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