Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / network / EventPacket.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "system.h"
22
23 #ifdef HAS_EVENT_SERVER
24
25 #include "EventPacket.h"
26 #include "Socket.h"
27 #include "utils/log.h"
28
29 using namespace EVENTPACKET;
30
31 /************************************************************************/
32 /* CEventPacket                                                         */
33 /************************************************************************/
34 bool CEventPacket::Parse(int datasize, const void *data)
35 {
36   unsigned char* buf = (unsigned char *)data;
37   if (datasize < HEADER_SIZE || datasize > PACKET_SIZE)
38     return false;
39
40   // check signature
41   if (memcmp(data, (const void*)HEADER_SIG, HEADER_SIG_LENGTH) != 0)
42     return false;
43
44   buf += HEADER_SIG_LENGTH;
45
46   // extract protocol version
47   m_cMajVer = (*buf++);
48   m_cMinVer = (*buf++);
49
50   if (m_cMajVer != 2 && m_cMinVer != 0)
51     return false;
52
53   // get packet type
54   m_eType = (PacketType)ntohs(*((uint16_t*)buf));
55
56   if (m_eType < (unsigned short)PT_HELO || m_eType >= (unsigned short)PT_LAST)
57     return false;
58
59   // get packet sequence id
60   buf += 2;
61   m_iSeq  = ntohl(*((uint32_t*)buf));
62
63   // get total message length
64   buf += 4;
65   m_iTotalPackets = ntohl(*((uint32_t*)buf));
66
67   // get payload size
68   buf += 4;
69   m_iPayloadSize = ntohs(*((uint16_t*)buf));
70
71   if ((m_iPayloadSize + HEADER_SIZE) != (unsigned int)datasize)
72     return false;
73
74   // get the client's token
75   buf += 2;
76   m_iClientToken = ntohl(*((uint32_t*)buf));
77
78   buf += 4;
79
80   // get payload
81   if (m_iPayloadSize)
82   {
83     // forward past reserved bytes
84     buf += 10;
85
86     if (m_pPayload)
87     {
88       free(m_pPayload);
89       m_pPayload = NULL;
90     }
91
92     m_pPayload = malloc(m_iPayloadSize);
93     if (!m_pPayload)
94     {
95       CLog::Log(LOGERROR, "ES: Out of memory");
96       return false;
97     }
98     memcpy(m_pPayload, buf, (size_t)m_iPayloadSize);
99   }
100   m_bValid = true;
101   return true;
102 }
103
104 #endif // HAS_EVENT_SERVER