/
DAAPFile.cpp
258 lines (203 loc) · 6.33 KB
/
DAAPFile.cpp
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
/*
* DAAP Support for XBMC
* Copyright (c) 2004 Forza (Chris Barnett)
* Portions Copyright (c) by the authors of libOpenDAAP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "DAAPFile.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include <sys/stat.h>
#include "lib/libXDAAP/private.h"
using namespace XFILE;
#define HEDAER_VERSION "Client-DAAP-Version"
#define HEADER_REQUESTID "Client-DAAP-Request-ID"
#define HEADER_VALIDATE "Client-DAAP-Validation"
#define HEADER_ACCESS_INDEX "Client-DAAP-Access-Index"
#define DAAP_USERAGENT "iTunes/4.6 (Windows; N)"
#define DAAP_PORT 3689
extern "C"
{
/* prototype of function in LIBXDAAP to generate the has needed for requests */
void GenerateHash(short version_major,
const unsigned char *url, unsigned char hashSelect,
unsigned char *outhash,
int request_id);
}
CDaapClient g_DaapClient;
CDaapClient::CDaapClient()
{
m_pClient = NULL;
m_Status = DAAP_STATUS_error;
m_pArtistsHead = NULL;
m_iDatabase = 0;
}
CDaapClient::~CDaapClient()
{
}
void CDaapClient::Release()
{
m_mapHosts.clear();
if( m_pClient )
{
try
{
while( DAAP_Client_Release(m_pClient) != 0 ) {}
}
catch(...)
{
CLog::Log(LOGINFO, "CDaapClient::Disconnect - Unexpected exception");
}
m_pClient = NULL;
}
}
DAAP_SClientHost* CDaapClient::GetHost(const CStdString &strHost)
{
try
{
ITHOST it;
it = m_mapHosts.find(strHost);
if( it != m_mapHosts.end() )
return it->second;
if( !m_pClient )
m_pClient = DAAP_Client_Create((DAAP_fnClientStatus)StatusCallback, (void*)this);
DAAP_SClientHost* pHost = DAAP_Client_AddHost(m_pClient, (char *)strHost.c_str(), (char *)"A", (char *)"A");
if( !pHost )
throw("Unable to add host");
if( DAAP_ClientHost_Connect(pHost) != 0 )
throw("Unable to connect");
m_mapHosts[strHost] = pHost;
return pHost;
}
catch(char* err)
{
CLog::Log(LOGERROR, "CDaapClient::GetHost(%s) - %s", strHost.c_str(), err );
return NULL;
}
catch(...)
{
CLog::Log(LOGERROR, "CDaapClient::GetHost(%s) - Unknown Exception", strHost.c_str());
return NULL;
}
}
void CDaapClient::StatusCallback(DAAP_SClient *pClient, DAAP_Status status, int value, void* pContext)
{
((CDaapClient*)pContext)->m_Status = status;
switch(status)
{
case DAAP_STATUS_connecting:
CLog::Log(LOGINFO, "CDaapClient::Callback - Connecting");
break;
case DAAP_STATUS_downloading:
CLog::Log(LOGINFO, "CDaapClient::Callback - Downlading");
break;
case DAAP_STATUS_idle:
CLog::Log(LOGINFO, "CDaapClient::Callback - Idle");
break;
default:
CLog::Log(LOGINFO, "CDaapClient::Callback - Status %d", status);
break;
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDAAPFile::CDAAPFile()
{
m_thisHost = NULL;
m_thisClient = NULL;
m_bOpened = false;
}
CDAAPFile::~CDAAPFile()
{
Close();
}
//*********************************************************************************************
bool CDAAPFile::Open(const CURL& url)
{
CSingleLock lock(g_DaapClient);
if (m_bOpened) Close();
m_url = url;
CLog::Log(LOGDEBUG, "CDAAPFile::Open(%s)", url.GetFileName().c_str());
CStdString host = url.GetHostName();
if (url.HasPort())
host.Format("%s:%i",url.GetHostName(),url.GetPort());
m_thisHost = g_DaapClient.GetHost(host);
if (!m_thisHost)
return false;
/* get us a new request id */
int requestid = ++m_thisHost->request_id;
m_hashurl = "/" + m_url.GetFileName();
m_hashurl += m_url.GetOptions();
char hash[33] = {0};
GenerateHash(m_thisHost->version_major, (unsigned char*)(m_hashurl.c_str()), 2, (unsigned char*)hash, requestid);
m_curl.SetUserAgent(DAAP_USERAGENT);
//m_curl.SetRequestHeader(HEADER_VERSION, "3.0");
m_curl.SetRequestHeader(HEADER_REQUESTID, requestid);
m_curl.SetRequestHeader(HEADER_VALIDATE, CStdString(hash));
m_curl.SetRequestHeader(HEADER_ACCESS_INDEX, 2);
m_url.SetProtocol("http");
if(!m_url.HasPort())
m_url.SetPort(DAAP_PORT);
m_bOpened = true;
return m_curl.Open(m_url);
}
//*********************************************************************************************
unsigned int CDAAPFile::Read(void *lpBuf, int64_t uiBufSize)
{
return m_curl.Read(lpBuf, uiBufSize);
}
//*********************************************************************************************
void CDAAPFile::Close()
{
m_curl.Close();
m_bOpened = false;
}
//*********************************************************************************************
int64_t CDAAPFile::Seek(int64_t iFilePosition, int iWhence)
{
CSingleLock lock(g_DaapClient);
int requestid = ++m_thisHost->request_id;
char hash[33] = {0};
GenerateHash(m_thisHost->version_major, (unsigned char*)(m_hashurl.c_str()), 2, (unsigned char*)hash, requestid);
m_curl.SetRequestHeader(HEADER_REQUESTID, requestid);
m_curl.SetRequestHeader(HEADER_VALIDATE, CStdString(hash));
return m_curl.Seek(iFilePosition, iWhence);
}
//*********************************************************************************************
int64_t CDAAPFile::GetLength()
{
return m_curl.GetLength();
}
//*********************************************************************************************
int64_t CDAAPFile::GetPosition()
{
return m_curl.GetPosition();
}
bool CDAAPFile::Exists(const CURL& url)
{
return false;
}
int CDAAPFile::Stat(const CURL& url, struct __stat64* buffer)
{
return -1;
}
int CDAAPFile::IoControl(EIoControl request, void* param)
{
if(request == IOCTRL_SEEK_POSSIBLE)
return 1;
return -1;
}