]> git.vomp.tv Git - vompclient.git/blob - wol.cc
WOL support
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #include <unistd.h>
28 #include <net/ethernet.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include "wol.h"
32
33 #define _PATH_PROCNET_ARP "/proc/net/arp"
34
35 Wol* Wol::instance = NULL;
36
37 Wol::Wol()
38 {
39   if (instance) return;
40   instance = this;
41   bEtherKnown = false;
42   wolEnabled = true;
43   logger = Log::getInstance();
44 }
45
46 Wol::~Wol()
47 {
48 }
49
50 Wol* Wol::getInstance()
51 {
52   return instance;
53 }
54
55 void Wol::setEnabled(bool tEnabled)
56 {
57   wolEnabled = tEnabled;
58 }
59
60 /* Display the contents of the ARP cache in the kernel. */
61 int Wol::find_ether(char *name)
62 {
63   char ip[100];
64   char line[200];
65   FILE *fp;
66   int num;
67
68   /* Open the PROCps kernel table. */
69   if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL)
70   {
71     perror(_PATH_PROCNET_ARP);
72     return -1;
73   }
74   /* Bypass header -- read until newline */
75   if (fgets(line, sizeof(line), fp) != (char *) NULL)
76   {
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",
80         ip, ether_addr);
81       if (num < 2)
82         break;
83       /* if the user specified address differs, skip it */
84       if (name[0] && !strcmp(ip, name))
85       {
86         logger->log("Wol", Log::NOTICE, "Found etheraddr %s", ether_addr);
87         return 1;
88       }
89     }
90   }
91   fclose(fp);
92   return 0;
93 }
94
95 /* Input an Ethernet address and convert to binary. */
96 int Wol::convertToBinary(char *bufp, unsigned char *addr)
97 {
98     char c, *orig;
99     int i;
100         unsigned char *ptr = addr;
101     unsigned val;
102
103     i = 0;
104     orig = bufp;
105     while ((*bufp != '\0') && (i < ETH_ALEN))
106     {
107         val = 0;
108         c = *bufp++;
109         if (isdigit(c))
110             val = c - '0';
111         else if (c >= 'a' && c <= 'f')
112             val = c - 'a' + 10;
113         else if (c >= 'A' && c <= 'F')
114             val = c - 'A' + 10;
115         else
116         {
117 #ifdef DEBUG
118             fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
119 #endif
120             errno = EINVAL;
121             return (-1);
122         }
123         val <<= 4;
124         c = *bufp;
125         if (isdigit(c))
126             val |= c - '0';
127         else if (c >= 'a' && c <= 'f')
128             val |= c - 'a' + 10;
129         else if (c >= 'A' && c <= 'F')
130             val |= c - 'A' + 10;
131         else if (c == ':' || c == 0)
132             val >>= 4;
133         else
134         {
135 #ifdef DEBUG
136             fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
137 #endif
138             errno = EINVAL;
139             return (-1);
140         }
141         if (c != 0)
142             bufp++;
143         *ptr++ = (unsigned char) (val & 0377);
144         i++;
145
146         /* We might get a semicolon here - not required. */
147         if (*bufp == ':')
148         {
149             if (i == ETH_ALEN)
150             {
151                     ;           /* nothing */
152             }
153             bufp++;
154         }
155     }
156     return (0);
157 } /* in_ether */
158
159 int Wol::doWakeUp()
160 {
161   if (!wolEnabled) return -2;
162   if (!bEtherKnown) return -3;
163
164   int i, j;
165   int packet;
166   struct sockaddr_in sap;
167   unsigned char ethaddr[8];
168   unsigned char *ptr;
169   unsigned char buf [128];
170   int optval = 1;
171
172   /* Fetch the hardware address. */
173   if (convertToBinary (ether_addr, ethaddr) < 0)
174   {
175     return (-1);
176   }
177
178   /* setup the packet socket */
179   if ((packet = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
180   {
181     return (-1);
182   }
183
184   /* Set socket options */
185   if (setsockopt (packet, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval,
186      sizeof (optval)) < 0)
187   {
188     fprintf (stderr, "setsocket failed %s\n",
189     strerror (errno));
190     close (packet);
191     return (-1);
192   }
193
194   /* Set up broadcast address */
195   sap.sin_family = AF_INET;
196   sap.sin_addr.s_addr = htonl(0xffffffff);        /* broadcast address
197 */
198   sap.sin_port = htons(60000);
199
200   /* Build the message to send - 6 x 0xff then 16 x MAC address */
201   ptr = buf;
202   for (i = 0; i < 6; i++)
203     *ptr++ = 0xff;
204   for (j = 0; j < 16; j++)
205     for (i = 0; i < ETH_ALEN; i++)
206       *ptr++ = ethaddr [i];
207
208   /* Send the packet out */
209   if (sendto (packet, buf, 102, 0, (struct sockaddr *)&sap, sizeof (sap)) < 0)
210   {
211     fprintf (stderr, " sendto failed, %s\n",
212     strerror(errno));
213     close (packet);
214     return (-1);
215   }
216
217   logger->log("Wol", Log::NOTICE, "Send wakeonlan to server");
218
219   close (packet);
220   return (0);
221 }
222
223 void Wol::setWakeUpIP(char* ip_addr)
224 {
225   if(find_ether(ip_addr))
226   {
227     bEtherKnown = true;
228     logger->log("Wol", Log::NOTICE, "Server IP set");
229   }
230 }
231