@@ -199,7 +199,7 @@ void DisplayPowerOnThread(SESSIONPARAMETERS * CallingSessionParameters, bool * C
199199 handshake.replace (ckf, ck.length (), key);
200200 }
201201
202- // try waking up the display ten times
202+ // try waking up the display ten times, but not longer than timeout user preference
203203 while (time (0 ) - origtim < (Timeout+1 ))
204204 {
205205 time_t looptim = time (0 );
@@ -291,7 +291,11 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
291291 return ;
292292
293293 vector<string> MACs = CallingSessionParameters->MAC ;
294+ string IP = CallingSessionParameters->IP ;
294295 string device = CallingSessionParameters->DeviceId ;
296+ string subnet = CallingSessionParameters->Subnet ;
297+ int WOLtype = CallingSessionParameters->WOLtype ;
298+
295299 SOCKET WOLsocket = INVALID_SOCKET;
296300 string logmsg;
297301
@@ -301,10 +305,68 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
301305 struct sockaddr_in LANDestination {};
302306 LANDestination.sin_family = AF_INET;
303307 LANDestination.sin_port = htons (9 );
304- LANDestination.sin_addr .s_addr = 0xFFFFFFFF ;
308+
309+ stringstream wolstr;
310+
311+ if (WOLtype == WOL_SUBNETBROADCAST && subnet != " " )
312+ {
313+ vector<string> vIP = stringsplit (IP, " ." );
314+ vector<string> vSubnet = stringsplit (subnet, " ." );
315+ stringstream broadcastaddress;
316+
317+ if (vIP.size () == 4 && vSubnet.size () == 4 )
318+ {
319+ for (int i = 0 ; i < 4 ; i++)
320+ {
321+ int a = atoi (vIP[i].c_str ());
322+ int b = atoi (vSubnet[i].c_str ());
323+ int c = 256 + (a | (~b));
324+
325+ broadcastaddress << c;
326+ if (i < 3 )
327+ broadcastaddress << " ." ;
328+ }
329+
330+ stringstream ss;
331+
332+ wolstr << " using broadcast address: " << broadcastaddress.str ();
333+
334+ LANDestination.sin_addr .s_addr = inet_addr (broadcastaddress.str ().c_str ());
335+
336+ }
337+ else
338+ {
339+ stringstream ss;
340+ ss << device;
341+ ss << " , ERROR! WOLthread malformed subnet/IP" ;
342+ Log (ss.str ());
343+ return ;
344+ }
345+
346+
347+ LANDestination.sin_addr .s_addr = 0xFFFFFFFF ;
348+ }
349+ else if (WOLtype == WOL_IPSEND)
350+ {
351+ LANDestination.sin_addr .s_addr = inet_addr (IP.c_str ());
352+ wolstr << " using IP address: " << IP;
353+
354+ }
355+ else
356+ {
357+ LANDestination.sin_addr .s_addr = 0xFFFFFFFF ;
358+ wolstr << " using network broadcast: 255.255.255.255" ;
359+ }
360+
361+ /* test test test
362+ sockaddr_in host_interface;
363+ host_interface.sin_family = AF_INET;
364+ host_interface.sin_port = htons(0);
365+ host_interface.sin_addr.s_addr = inet_addr("192.168.1.100");
366+ */
305367 time_t origtim = time (0 );
306368
307- WOLsocket= socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
369+ WOLsocket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
308370
309371 if (WOLsocket == INVALID_SOCKET)
310372 {
@@ -318,6 +380,20 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
318380 }
319381 else
320382 {
383+ /* test test test
384+ int iRes = ::bind(WOLsocket, (SOCKADDR*)&host_interface, sizeof(host_interface));
385+ if ( iRes == SOCKET_ERROR)
386+ {
387+ closesocket(WOLsocket);
388+ int dw = WSAGetLastError();
389+ stringstream ss;
390+ ss << device;
391+ ss << ", ERROR! WOLthread WS bind(): ";
392+ ss << dw;
393+ Log(ss.str());
394+ return;
395+ }
396+ */
321397 const bool optval = TRUE ;
322398 if (setsockopt (WOLsocket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof (optval)) == SOCKET_ERROR)
323399 {
@@ -336,6 +412,8 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
336412 logmsg = device;
337413 logmsg += " , repeating WOL broadcast started to MAC: " ;
338414 logmsg += MAC;
415+ if (wolstr.str () != " " )
416+ logmsg += wolstr.str ();
339417 Log (logmsg);
340418
341419 // remove filling from MAC
@@ -473,7 +551,7 @@ void DisplayPowerOffThread(SESSIONPARAMETERS* CallingSessionParameters, bool* Ca
473551 host += ' :' + std::to_string (ep.port ());
474552 if (time (0 ) - origtim > 10 ) // this thread should not run too long
475553 {
476- Log (" DisplayPowerOffThread() forced exit. " );
554+ Log (" DisplayPowerOffThread() - forced exit" );
477555 goto threadoffend;
478556 }
479557// Log("DEBUG INFO: DisplayPowerOffThread() setting options...");
@@ -488,7 +566,7 @@ void DisplayPowerOffThread(SESSIONPARAMETERS* CallingSessionParameters, bool* Ca
488566 }));
489567 if (time (0 ) - origtim > 10 ) // this thread should not run too long
490568 {
491- Log (" DisplayPowerOffThread() forced exit. " );
569+ Log (" DisplayPowerOffThread() - forced exit" );
492570 goto threadoffend;
493571 }
494572
0 commit comments