1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
|
//
// Copyright (C) 2010-2011 Stonyx
// http://www.stonyx.com
//
// This library is free software. You can redistribute it and/or modify it
// under the terms of the GNU General Public License Version 2 (or at your
// option any later version) as published by The Free Software Foundation.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// If you did not received a copy of the GNU General Public License along
// with this library see http://www.gnu.org/copyleft/gpl.html or write to
// The Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include "SlingboxLib.h"
#if defined _WIN32 || defined _WIN64
#include <ws2tcpip.h>
typedef int socklen_t;
#else
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
#endif
// ********************
// Public Functions
// ********************
// Default constructor.
CSlingbox::CSlingbox()
{
// initialize all members
init();
}
// Alternative constructor used to set the address (IP or hostname) and port of the
// Slingbox we want to connect to. If no port is specified default port 5001 is used.
CSlingbox::CSlingbox(const char * szAddress, unsigned int uiPort /* = 5001 */)
{
// initialize all members
init();
// Set address and port variables to passed values
SetAddress(szAddress, uiPort);
}
// Destructor.
CSlingbox::~CSlingbox()
{
// Close all connections if they are still open
if (m_socStream != INVALID_SOCKET)
CloseSocket(m_socStream);
if (m_socCommunication != INVALID_SOCKET)
CloseSocket(m_socCommunication);
}
// Function used to initialize class members
void CSlingbox::init()
{
// Set all variables to default/invalid values
m_socCommunication = INVALID_SOCKET;
m_socStream = INVALID_SOCKET;
memset(m_szAddress, 0, sizeof(m_szAddress));
m_uiPort = 0;
m_usCode = 0;
m_usSequence = 0;
m_iChannel = -1;
m_iInput = -1;
memset(&m_receivedMessages, 0, sizeof(m_receivedMessages));
}
// Function used to find a Slingbox. Address and port of found Slingbox can be
// retrieved with the GetAddress function afterwards.
bool CSlingbox::FindSlingbox(unsigned int uiTimeout /* = 10 */)
{
// Open a UDP connection
SOCKET socSocket = OpenSocket(NULL, 0, true);
if (socSocket != INVALID_SOCKET)
{
// Prepare and send data
uint32_t uiData[8];
memset(uiData, 0, sizeof(uiData));
uiData[0] = 0x00000101;
uiData[1] = 0x00000002;
if (Broadcast(socSocket, 5004, uiData, sizeof(uiData), uiTimeout) <= 0)
return false;
// Reset address and port variables
memset(m_szAddress, 0, sizeof(m_szAddress));
m_uiPort = 0;
// Give the Slingbox time to respond
Wait(250);
// Look for correct return message and properly set variables
if (!ReceiveMessage(socSocket, true, uiTimeout) ||
!m_receivedMessages.bFindMessage ||
strlen(m_szAddress) == 0 || m_uiPort == 0)
{
CloseSocket(socSocket);
return false;
}
// Close socket
CloseSocket(socSocket);
}
else
{
return false;
}
return true;
}
// Function used to retrieve the address and port of a Slingbox found with the
// FindSlingbox function.
void CSlingbox::GetAddress(char * szAddress, unsigned int uiAddressLength,
unsigned int * uiPort)
{
// Copy requested data into passed pointers
memset(szAddress, 0, uiAddressLength);
strncpy(szAddress, m_szAddress, uiAddressLength - 1);
*uiPort = m_uiPort;
}
// Function used to set the address (IP or hostname) and port of the Slingbox we
// want to connect to. If no port is specified default port 5001 is used.
void CSlingbox::SetAddress(const char * szAddress, unsigned int uiPort /* = 5001 */)
{
// Set address and port variables to passed values
strncpy(m_szAddress, szAddress, sizeof(m_szAddress) - 1);
m_uiPort = uiPort;
}
// Function used to login into the Slingbox.
bool CSlingbox::Connect(bool bLoginAsAdmin, const char * szPassword)
{
// Check if a communication connection is already open
if (m_socCommunication != INVALID_SOCKET)
return false;
#if defined _WIN32 || defined _WIN64
// Enable use of the Winsock DLL
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
return false;
#endif
// Open the communication connection
m_socCommunication = OpenSocket(m_szAddress, m_uiPort);
if (m_socCommunication == INVALID_SOCKET)
{
#if defined _WIN32 || defined _WIN64
// Finish using the Winsock DLL
WSACleanup();
#endif
return false;
}
// Prepare and send string
char * szString = "GET /stream.asf HTTP/1.1\r\nAccept: */*\r\n"
"Pragma: Sling-Connection-Type=Control, Session-Id=0\r\n\r\n";
if (Send(m_socCommunication, (void *)szString, strlen(szString)) <= 0)
{
// Close the communication connection
CloseSocket(m_socCommunication);
m_socCommunication = INVALID_SOCKET;
#if defined _WIN32 || defined _WIN64
// Finish using the Winsock DLL
WSACleanup();
#endif
return false;
}
// Invalidate variables
m_usCode = 0;
m_usSequence = 0;
m_iChannel = -1;
m_iInput = -1;
// Prepare and send the connect message
ConnectMessage message(bLoginAsAdmin, szPassword);
if (!SendReceiveMessage(m_socCommunication, &message) ||
!m_receivedMessages.bConnectMessage)
{
// Close the communication connection
CloseSocket(m_socCommunication);
m_socCommunication = INVALID_SOCKET;
#if defined _WIN32 || defined _WIN64
// Finish using the Winsock DLL
WSACleanup();
#endif
return false;
}
// Check if we got a valid code
if (m_usCode == 0)
{
// Close the communication connection
CloseSocket(m_socCommunication);
m_socCommunication = INVALID_SOCKET;
#if defined _WIN32 || defined _WIN64
// Finish using the Winsock DLL
WSACleanup();
#endif
return false;
}
return true;
}
// Function used to send the initialization message and initialize the stream.
bool CSlingbox::InitializeStream()
{
// Prepare and send start message
InitializationMessage message;
return SendReceiveMessage(m_socCommunication, &message) &&
m_receivedMessages.bInitializationMessage;
}
// Function used to set the stream settings.
bool CSlingbox::StreamSettings(Resolution eResolution /* = RESOLUTION320X240 */,
uint32_t uiVideoBitrate /* = 704 */, uint32_t uiFrameRate /* = 30 */,
uint32_t uiVideoSmoothing /* = 50 */, uint32_t uiAudioBitrate /* = 64 */,
uint32_t uiIFrameInterval /* = 10 */)
{
// Check if a resolution was specified that requires encryption
if (eResolution == RESOLUTION320X480 ||
eResolution == RESOLUTION640X240 ||
eResolution == RESOLUTION640X480)
{
// Enable encryption to enable requested resolution
EncryptionMessage message;
if (!SendReceiveMessage(m_socCommunication, &message) ||
!m_receivedMessages.bEncryptionMessage)
return false;
}
// Create and send video message with all the stream details
SettingsMessage message(eResolution, uiVideoBitrate, uiFrameRate, uiVideoSmoothing,
uiAudioBitrate, uiIFrameInterval);
if (!SendReceiveMessage(m_socCommunication, &message) ||
!m_receivedMessages.bSettingsMessage)
return false;
// Give the Slingbox time to change settings
Wait(500);
return true;
}
// Function used to start the stream.
bool CSlingbox::StartStream()
{
// Check if a stream connection is already open
if (m_socStream != INVALID_SOCKET)
return false;
// Open a new connection
m_socStream = OpenSocket(m_szAddress, m_uiPort);
if (m_socStream != INVALID_SOCKET)
{
// Prepare and send string
char szString[128] = "GET /stream.asf HTTP/1.1\r\nAccept: */*\r\n"
"Pragma: Sling-Connection-Type=Stream, Session-Id=";
sprintf(&szString[strlen(szString)], "%u", m_usCode);
strcpy(&szString[strlen(szString)], "\r\n\r\n");
if (Send(m_socStream, (void *)szString, strlen(szString)) <= 0)
{
// Close the stream connection
CloseSocket(m_socStream);
m_socStream = INVALID_SOCKET;
return false;
}
// Invalidate channel and input variables
m_iChannel = -1;
m_iInput = -1;
// Give the Slingbox time to respond
Wait(250);
// Check for correct return message
if (!ReceiveMessage(m_socCommunication) ||
!(m_receivedMessages.bChannelStatusMessage ||
m_receivedMessages.bInputStatusMessage))
{
// Close the stream connection
CloseSocket(m_socStream);
m_socStream = INVALID_SOCKET;
return false;
}
}
else
{
return false;
}
return true;
}
// Function used to read the stream. Returns number of bytes actually received
// or -1 if an error occured.
int CSlingbox::ReadStream(void * pBuffer, unsigned int uiSize)
{
return Receive(m_socStream, pBuffer, uiSize);
}
// Function used to stop the stream.
bool CSlingbox::StopStream()
{
// Close the stream connection
bool bSuccess = CloseSocket(m_socStream);
m_socStream = INVALID_SOCKET;
// Invalidate channel and input variables
m_iChannel = -1;
m_iInput = -1;
return bSuccess;
}
// Function used to disconnect from the Slingbox
bool CSlingbox::Disconnect()
{
// Prepare variables
bool bSuccess = true;
// Prepare and send the disconnect message
DisconnectMessage message;
if (!SendReceiveMessage(m_socCommunication, &message) ||
!m_receivedMessages.bDisconnectMessage)
bSuccess = false;
// Close the stream connection if it's still active
if (m_socStream != INVALID_SOCKET && !StopStream())
bSuccess = false;
// Close the communication connection
if (!CloseSocket(m_socCommunication))
bSuccess = false;
m_socCommunication = INVALID_SOCKET;
#if defined _WIN32 || defined _WIN64
// Finish using the Winsock DLL
if (WSACleanup() != 0)
bSuccess = false;
#endif
// Invalidate variables
m_usCode = 0;
m_usSequence = 0;
m_iChannel = -1;
m_iInput = -1;
return bSuccess;
}
// Function used to check if a connection to the Slingbox is active
bool CSlingbox::IsConnected()
{
// Prepare and send status message
StatusMessage message;
return SendReceiveMessage(m_socCommunication, &message) &&
m_receivedMessages.bStatusMessage;
}
// Function used to change the channel up.
bool CSlingbox::ChannelUp()
{
// Prepare and send channel message
ChannelMessage message;
message.Up();
if (!SendMessage(m_socCommunication, &message))
return false;
// Invalidate channel variable
m_iChannel = -1;
// Give the Slingbox time to change things
Wait(1000);
// Check for return message
return ReceiveMessage(m_socCommunication) &&
m_receivedMessages.bChannelMessage;
}
// Function used to change the channel down.
bool CSlingbox::ChannelDown()
{
// Prepare and send channel message
ChannelMessage message;
message.Down();
if (!SendMessage(m_socCommunication, &message))
return false;
// Invalidate channel variable
m_iChannel = -1;
// Give the Slingbox time to change things
Wait(1000);
// Check for return message
return ReceiveMessage(m_socCommunication) &&
m_receivedMessages.bChannelMessage;
}
// Function used to set the channel.
bool CSlingbox::SetChannel(unsigned int uiChannel)
{
// Prepare and send channel message
ChannelMessage message(uiChannel);
if (!SendMessage(m_socCommunication, &message))
return false;
// Invalidate channel variable
m_iChannel = -1;
// Give the Slingbox time to change things
Wait(1000);
// Check for return message
return ReceiveMessage(m_socCommunication) &&
m_receivedMessages.bChannelMessage;
}
// Function used to set the input.
bool CSlingbox::SetInput(unsigned int uiInput)
{
// Prepare and send input message
InputMessage message(uiInput);
if (!SendMessage(m_socCommunication, &message))
return false;
// Invalidate input variable
m_iInput = -1;
// Give the Slingbox time to change things
Wait(1000);
// Check for return message
return ReceiveMessage(m_socCommunication) &&
m_receivedMessages.bInputMessage;
}
// Function used to get the current channel. Returns -1 if valid data is not
// available.
int CSlingbox::GetChannel()
{
return m_iChannel;
}
// Function used to get the current input. Returns -1 if valid data is not
// available.
int CSlingbox::GetInput()
{
return m_iInput;
}
// Function used to send IR commands.
bool CSlingbox::SendIRCommand(uint8_t ucCode)
{
// Prepare and send IR message
IRMessage message(ucCode);
if (!SendMessage(m_socCommunication, &message))
return false;
// Give the Slingbox time to send command
Wait(1000);
// Check for return message
return ReceiveMessage(m_socCommunication) &&
m_receivedMessages.bIRMessage;
}
// ********************
// Protected Functions
// ********************
// Function used to send a message and receive messages.
bool CSlingbox::SendReceiveMessage(SOCKET socSocket, MessageHeader * pHeader,
bool bEncrypt /* = true */, unsigned int uiTimeout /* = 10 */)
{
// Send message
if (!SendMessage(socSocket, pHeader, bEncrypt, uiTimeout))
return false;
// Give the Slingbox time to respond
Wait(250);
// Receive messages
if (!ReceiveMessage(socSocket, false, uiTimeout))
return false;
return true;
}
// Function used to send a messages to the Slingbox.
bool CSlingbox::SendMessage(SOCKET socSocket, MessageHeader * pHeader,
bool bEncode /* = true */, unsigned int uiTimeout /* = 10 */)
{
// Set message code and sequence numbers
pHeader->m_usCode = m_usCode;
pHeader->m_usSequence = m_usSequence;
m_usSequence++;
// Check if we need to encode the data from the message
if (bEncode)
{
void* pPointer = ((uint8_t *)pHeader) + sizeof(MessageHeader);
Encode(pPointer, pHeader->m_usSize);
pHeader->m_usEncoded = 0x2000;
}
// Send the message
if (Send(socSocket, pHeader, sizeof(MessageHeader) +
pHeader->m_usSize, uiTimeout) <= 0)
return false;
return true;
}
// Function used to receive messages from the Slingbox and process them.
bool CSlingbox::ReceiveMessage(SOCKET socSocket, bool bUDPMessage /* = false */,
unsigned int uiTimeout /* = 10 */)
{
// Prepare variables
bool bMessageReceived = false;
int iReceived;
int iProcessed = 0;
uint8_t ucBuffer[1024];
sockaddr sSocketAddress;
memset(&sSocketAddress, 0, sizeof(sSocketAddress));
// Reset received messages struct
memset(&m_receivedMessages, 0, sizeof(m_receivedMessages));
// Get the data
if (bUDPMessage)
{
iReceived = ReceiveFrom(socSocket, ucBuffer, sizeof(ucBuffer), uiTimeout,
&sSocketAddress);
}
else
{
iReceived = Receive(socSocket, ucBuffer, sizeof(ucBuffer), uiTimeout);
}
// Check for errors
if (iReceived == SOCKET_ERROR)
return false;
// Loop until we've processed all received data
while (iProcessed < iReceived)
{
// Prepare pointer
MessageHeader * pHeader = (MessageHeader *)(ucBuffer + iProcessed);
// Make sure it's a message and we got enough data to work with
if (pHeader->m_usHeader == 0x0101 && (iReceived - iProcessed) >=
(int)(sizeof(MessageHeader) + pHeader->m_usSize))
{
// Signal that we've recevied a message
bMessageReceived = true;
// Decode the message data
if (pHeader->m_usEncoded == 0x2000)
{
Decode((uint8_t *)pHeader + sizeof(MessageHeader), pHeader->m_usSize);
pHeader->m_usEncoded = 0x0000;
}
// Find message
if (pHeader->m_usMessageID == 0x0002 && pHeader->m_usSize == 0x005C)
{
m_receivedMessages.bFindMessage = true;
// Remove the port information from the structure received from ReceiveFrom
if (sSocketAddress.sa_family == AF_INET)
((sockaddr_in *)&sSocketAddress)->sin_port = 0;
else if (sSocketAddress.sa_family == AF_INET6)
((sockaddr_in6 *)&sSocketAddress)->sin6_port = 0;
// Convert the address received from ReceiveFrom to a string
#if defined _WIN32 || defined _WIN64
unsigned long ulSizeOfAddress = sizeof(m_szAddress);
WSAAddressToString(&sSocketAddress, sizeof(sSocketAddress), NULL,
m_szAddress, &ulSizeOfAddress);
#else
socklen_t iSizeOfAddress = sizeof(m_szAddress);
if (sSocketAddress.sa_family == AF_INET)
inet_ntop(sSocketAddress.sa_family,
&((sockaddr_in *)&sSocketAddress)->sin_addr, m_szAddress, iSizeOfAddress);
else if (sSocketAddress.sa_family == AF_INET6)
inet_ntop(sSocketAddress.sa_family,
&((sockaddr_in6 *)&sSocketAddress)->sin6_addr, m_szAddress, iSizeOfAddress);
#endif
// Get the port information from the received data
m_uiPort = ucBuffer[120] + (ucBuffer[121] << 8);
}
// Connect message
else if (pHeader->m_usMessageID == 0x0067 && pHeader->m_usSize == 0x0008)
{
m_receivedMessages.bConnectMessage = true;
m_usCode = pHeader->m_usCode;
}
// Initialization message
else if (pHeader->m_usMessageID == 0x007E)
{
m_receivedMessages.bInitializationMessage = true;
}
// Encryption message
else if (pHeader->m_usMessageID == 0x00A6)
{
m_receivedMessages.bEncryptionMessage = true;
}
// Settings message
else if (pHeader->m_usMessageID == 0x00B5)
{
m_receivedMessages.bSettingsMessage = true;
}
// Disconnect message
else if (pHeader->m_usMessageID == 0x0068)
{
m_receivedMessages.bDisconnectMessage = true;
}
// Status message
else if (pHeader->m_usMessageID == 0x0065 && pHeader->m_usSize == 0x0000)
{
m_receivedMessages.bStatusMessage = true;
}
// Channel message
else if (pHeader->m_usMessageID == 0x0089)
{
m_receivedMessages.bChannelMessage = true;
}
// Channel status massage
else if (pHeader->m_usMessageID == 0x0065 && pHeader->m_usSize == 0x0078)
{
m_receivedMessages.bChannelStatusMessage = true;
m_iChannel = *(uint16_t *)((uint8_t *)pHeader + sizeof(MessageHeader));
}
// Input message
else if (pHeader->m_usMessageID == 0x0085)
{
m_receivedMessages.bInputMessage = true;
}
// Input status message
else if (pHeader->m_usMessageID == 0x0065 && pHeader->m_usSize == 0x0008)
{
m_receivedMessages.bInputStatusMessage = true;
m_iInput = *((uint8_t *)pHeader + sizeof(MessageHeader));
}
// IR message
else if (pHeader->m_usMessageID == 0x0087)
{
m_receivedMessages.bIRMessage = true;
}
// Increase our counter
iProcessed += sizeof(MessageHeader) + pHeader->m_usSize;
}
else
{
return bMessageReceived;
}
}
return bMessageReceived;
}
// Function used to encode data being send to the Slingbox.
void CSlingbox::Encode(void * pData, unsigned int iSize)
{
for (unsigned int i = 0; i < iSize; i += 8)
{
// Prepare variables
const uint32_t ulKey[] = {0xBCDEAAAA, 0x87FBBBBA, 0x7CCCCFFA, 0xDDDDAABC};
uint32_t ulVar1 = ((uint32_t *)&((uint8_t *)pData)[i])[0];
uint32_t ulVar2 = ((uint32_t *)&((uint8_t *)pData)[i])[1];
uint32_t ulVar3 = 0;
// Encode
for (unsigned int j = 0; j < 32; j++)
{
ulVar3 -= 0x61C88647;
ulVar1 += ((ulVar2 >> 5) + ulKey[1]) ^ ((ulVar2 << 4) + ulKey[0]) ^ (ulVar3 +
ulVar2);
ulVar2 += ((ulVar1 >> 5) + ulKey[3]) ^ ((ulVar1 << 4) + ulKey[2]) ^ (ulVar3 +
ulVar1);
}
// Finish up
((uint32_t *)&((uint8_t *)pData)[i])[0] = ulVar1;
((uint32_t *)&((uint8_t *)pData)[i])[1] = ulVar2;
}
}
// Function used to decode data being received from the Slingbox.
void CSlingbox::Decode(void * pData, unsigned int iSize)
{
for (unsigned int i = 0 ; i < iSize; i += 8 )
{
// Prepare variables
const uint32_t ulKey[] = {0xBCDEAAAA, 0x87FBBBBA, 0x7CCCCFFA, 0xDDDDAABC};
uint32_t ulVar1 = ((uint32_t *)&((uint8_t *)pData)[i])[0];
uint32_t ulVar2 = ((uint32_t *)&((uint8_t *)pData)[i])[1];
uint32_t ulVar3 = 0xC6EF3720;
// Decode
for (unsigned int j = 0; j < 32; j++)
{
ulVar2 -= ((ulVar1 >> 5) + ulKey[3]) ^ ((ulVar1 << 4) + ulKey[2]) ^ (ulVar3 +
ulVar1);
ulVar1 -= ((ulVar2 >> 5) + ulKey[1]) ^ ((ulVar2 << 4) + ulKey[0]) ^ (ulVar3 +
ulVar2);
ulVar3 += 0x61C88647;
}
// Finish up
((uint32_t *)&((uint8_t *)pData)[i])[0] = ulVar1;
((uint32_t *)&((uint8_t *)pData)[i])[1] = ulVar2;
}
}
// Function used to open a new socket.
SOCKET CSlingbox::OpenSocket(const char * szAddress, unsigned int uiPort,
bool bUDP /* = false */)
{
// Prepare needed variables
SOCKET socSocket = INVALID_SOCKET;
struct addrinfo * pAddressInfo;
struct addrinfo addressInfoHints;
struct addrinfo * pPointer;
memset(&addressInfoHints, 0, sizeof(addressInfoHints));
addressInfoHints.ai_family = bUDP ? AF_INET : AF_UNSPEC;
addressInfoHints.ai_socktype = bUDP ? SOCK_DGRAM : SOCK_STREAM;
addressInfoHints.ai_protocol = bUDP ? IPPROTO_UDP : IPPROTO_TCP;
addressInfoHints.ai_flags = bUDP ? AI_PASSIVE : 0;
char szPort[32];
sprintf(szPort, "%u", uiPort);
// Get address info
if (getaddrinfo(szAddress, szPort, &addressInfoHints, &pAddressInfo) != 0)
return INVALID_SOCKET;
// Loop thru all the received address info
for (pPointer = pAddressInfo; pPointer != NULL; pPointer = pPointer->ai_next)
{
// Open a new socket
socSocket = socket(pPointer->ai_family, pPointer->ai_socktype,
pPointer->ai_protocol);
if (socSocket != INVALID_SOCKET)
{
// Bind if we're using UDP
if (bUDP)
{
if (bind(socSocket, pAddressInfo->ai_addr, pAddressInfo->ai_addrlen) == 0)
{
break;
}
else
{
CloseSocket(socSocket);
socSocket = INVALID_SOCKET;
}
}
// Open a new connection if we're using TCP
else
{
if (connect(socSocket, pAddressInfo->ai_addr, pAddressInfo->ai_addrlen) == 0)
{
break;
}
else
{
CloseSocket(socSocket);
socSocket = INVALID_SOCKET;
}
}
}
}
// Free variables
freeaddrinfo(pAddressInfo);
return socSocket;
}
// Function used to broadcast data to the local subnet. Returns the number of bytes
// actually sent or -1 if an error occured.
int CSlingbox::Broadcast(SOCKET socSocket, unsigned int uiPort, void * pBuffer,
unsigned int uiSize, unsigned int uiTimeout /* = 10 */)
{
// Enable broadcasting
#if defined _WIN32 || defined _WIN64
char vBroadcast = '1';
#else
int vBroadcast = 1;
#endif
if (setsockopt(socSocket, SOL_SOCKET, SO_BROADCAST, &vBroadcast,
sizeof(vBroadcast)) != 0)
return SOCKET_ERROR;
// Prepare variable
struct sockaddr sSocketAddress;
memset(&sSocketAddress, 0, sizeof(sSocketAddress));
((sockaddr_in *)&sSocketAddress)->sin_family = AF_INET;
#if defined _WIN32 || defined _WIN64
((sockaddr_in *)&sSocketAddress)->sin_addr.S_un.S_addr = htonl(0xFFFFFFFF);
#else
((sockaddr_in *)&sSocketAddress)->sin_addr.s_addr = htonl(0xFFFFFFFF);
#endif
((sockaddr_in *)&sSocketAddress)->sin_port = htons(uiPort);
// Send the data
return SendTo(socSocket, pBuffer, uiSize, uiTimeout, &sSocketAddress);
}
// Function used to send data. Returns the number of bytes actually sent
// or -1 if an error occured.
int CSlingbox::Send(SOCKET socSocket, void * pBuffer, unsigned int uiSize,
unsigned int uiTimeout /* = 10 */)
{
// Prepare variables
int iFDS = socSocket + 1;
fd_set sendFDS;
FD_ZERO(&sendFDS);
FD_SET(socSocket, &sendFDS);
timeval timeOut;
timeOut.tv_sec = uiTimeout;
timeOut.tv_usec = 0;
int iResult;
unsigned int uiBytesSent = 0;
unsigned int uiBytesLeft = uiSize;
// Loop until all data is sent
while (uiBytesSent < uiSize)
{
// Send the data once the socket is ready
if (select(iFDS, NULL, &sendFDS, NULL, &timeOut) > 0)
iResult = send(socSocket, (const char *)pBuffer + uiBytesSent, uiBytesLeft, 0);
else
return uiBytesSent;
// Check for any errors
if (iResult == SOCKET_ERROR)
return SOCKET_ERROR;
// Adjust variables
uiBytesSent += iResult;
uiBytesLeft -= iResult;
}
return uiBytesSent;
}
// Function used to send data to a specific address. Returns the number of bytes
// actually sent or -1 if an error occured.
int CSlingbox::SendTo(SOCKET socSocket, void * pBuffer, unsigned int uiSize,
unsigned int uiTimeout, struct sockaddr * pSocketAddress)
{
// Prepare variables
int iFDS = socSocket + 1;
fd_set sendFDS;
FD_ZERO(&sendFDS);
FD_SET(socSocket, &sendFDS);
timeval timeOut;
timeOut.tv_sec = uiTimeout;
timeOut.tv_usec = 0;
int iResult;
unsigned int uiBytesSent = 0;
unsigned int uiBytesLeft = uiSize;
// Loop until all data is sent
while (uiBytesSent < uiSize)
{
// Send the data once the socket is ready
if (select(iFDS, NULL, &sendFDS, NULL, &timeOut) > 0)
iResult = sendto(socSocket, (const char *)pBuffer + uiBytesSent, uiBytesLeft, 0,
pSocketAddress, sizeof(*pSocketAddress));
else
return uiBytesSent;
// Check for any errors
if (iResult == SOCKET_ERROR)
return SOCKET_ERROR;
// Adjust variables
uiBytesSent += iResult;
uiBytesLeft -= iResult;
}
return uiBytesSent;
}
// Function used to receive data. Returns the number of bytes actually received
// or -1 if an error occured.
int CSlingbox::Receive(SOCKET socSocket, void * pBuffer, unsigned int uiSize,
unsigned int uiTimeout /* = 10 */)
{
// Prepare needed variables
int iFDS = socSocket + 1;
fd_set readFDS;
FD_ZERO(&readFDS);
FD_SET(socSocket, &readFDS);
timeval timeValue;
timeValue.tv_sec = uiTimeout;
timeValue.tv_usec = 0;
// Do the actual receiving of data after waiting for socket to be readable
if (select(iFDS, &readFDS, NULL, NULL, &timeValue) > 0)
return recv(socSocket, (char *)pBuffer, uiSize, 0);
else
return 0;
}
// Function used to receive data and tell us from where. Returns the number of bytes
// actually received or -1 if an error occured. If successful, populates the
// pSocketAddress variable with details of where the data was received from.
int CSlingbox::ReceiveFrom(SOCKET socSocket, void * pBuffer, unsigned int uiSize,
unsigned int uiTimeout, struct sockaddr * pSocketAddress)
{
// Prepare needed variables
int iFDS = socSocket + 1;
fd_set readFDS;
FD_ZERO(&readFDS);
FD_SET(socSocket, &readFDS);
timeval timeValue;
timeValue.tv_sec = uiTimeout;
timeValue.tv_usec = 0;
socklen_t iSizeOfAddress = sizeof(*pSocketAddress);
// Do the actual receiving of data after waiting for socket to be readable
if (select(iFDS, &readFDS, NULL, NULL, &timeValue) > 0)
return recvfrom(socSocket, (char *)pBuffer, uiSize, 0, pSocketAddress,
&iSizeOfAddress);
else
return 0;
}
// Function used to close a socket connection.
bool CSlingbox::CloseSocket(SOCKET socSocket)
{
#if defined _WIN32 || defined _WIN64
if (closesocket(socSocket) != 0)
return false;
#else
if (close(socSocket) != 0)
return false;
#endif
return true;
}
// Function used to wait on the Slingbox for various reasons
void CSlingbox::Wait(unsigned int uiMilliseconds)
{
#if defined _WIN32 || defined _WIN64
Sleep(uiMilliseconds);
#else
struct timespec time;
time.tv_sec = uiMilliseconds / 1000;
time.tv_nsec = (uiMilliseconds % 1000) * 1000000;
while (nanosleep(&time, &time) == -1 && errno == EINTR &&
(time.tv_sec > 0 || time.tv_nsec > 0));
#endif
}
|