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