summaryrefslogtreecommitdiff
path: root/server/server.cpp
blob: 28895ac50f27b4b57821d0837c8b17596eb14e02 (plain)
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
/*
 * server.cpp
 *
 *  Created on: 31.07.2012
 *      Author: savop
 */

#include <vdr/tools.h>
#include <string>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <unistd.h>
#include "../include/server.h"
#include "../include/service.h"
#include "../include/webserver.h"
#include "../include/tools.h"
#include "../include/media/mediaManager.h"
#include "../upnp.h"

using namespace upnp;
using namespace std;

cMediaServer* cMediaServer::GetInstance(){
	static cMediaServer server;
	return &server;
}

cMediaServer::cMediaServer()
: mServerDescription("VDR UPnP/DLNA MS", "Denis Loh", "http://upnp.vdr-developer.org",
                     tr(DESCRIPTION), "VDR UPnP-DLNA MS", VERSION,
                     "http://projects.vdr-developer.org/projects/plg-upnp/files", VERSION,
                     "deviceDescription.xml")
, mDeviceHandle(0)
, mAnnounceMaxAge(1800)
, mMaxContentLength(KB(20))
, mWebserver(NULL)
, mMediaManager(NULL)
{
  char tmp[255];

  if (gethostname(tmp, sizeof(tmp)) == 0) {
    string name = tmp;
    boost::to_upper(name);
    mServerDescription.friendlyName = name + " - " + mServerDescription.friendlyName;
  }

  mServerIcons.push_back(ServerIcon(image::DLNA_ICON_PNG_SM_24A, "images/upnpIconSm.png"));
  mServerIcons.push_back(ServerIcon(image::DLNA_ICON_PNG_LRG_24A, "images/upnpIconLrg.png"));
  mServerIcons.push_back(ServerIcon(image::DLNA_ICON_JPEG_SM_24, "images/upnpIconSm.jpeg"));
  mServerIcons.push_back(ServerIcon(image::DLNA_ICON_JPEG_LRG_24, "images/upnpIconLrg.jpeg"));
}

cMediaServer::~cMediaServer(){
  UpnpFinish();

  if(mWebserver){
    mWebserver->Stop();

    delete mWebserver;
    mWebserver = NULL;
  }

  if(mMediaManager){
    delete mMediaManager;
    mMediaManager = NULL;
  }

  try {
    mConnection.execute("VACUUM");
  } catch (const std::exception& e) {
    esyslog("UPnP\tFailed to vacuum database: '%s'", e.what());
  }
}

bool cMediaServer::Start(){

  // If the plugin is not enabled, do not start it.
  if(!mCurrentConfiguration.enabled) return true;

  isyslog("UPnP\tStarting UPnP media server");

  int ret;

  // Disable internal webserver, we don't need it.
  UpnpEnableWebserver(false);

  if(!mWebserver->Start()){
    esyslog("UPnP\tFailed to start the web server");
    return false;
  }

  isyslog("UPnP\tRegistering UPnP media server");

  string description = GetDeviceDescriptionUrl();

  ret = UpnpRegisterRootDevice2(UPNPREG_URL_DESC,
                                description.c_str(),
                                description.size(),
                                0,
                                &cMediaServer::ActionCallback,
                                this,
                                &mDeviceHandle);

  if(ret != UPNP_E_SUCCESS){
    esyslog("UPnP\tFailed to register the device. Error code: %s Help: %s",
        GetErrorMessage(ret).c_str(), GetErrorHelp(ret).c_str());
    return false;
  }

  ret = UpnpUnRegisterRootDevice(mDeviceHandle);
  if (ret != UPNP_E_SUCCESS) {
    esyslog("UPnP\tUnregistering old devices failed");
    return false;
  }

  ret = UpnpRegisterRootDevice2(UPNPREG_URL_DESC,
                                description.c_str(),
                                description.size(),
                                0,
                                &cMediaServer::ActionCallback,
                                this,
                                &mDeviceHandle);

  if(ret != UPNP_E_SUCCESS){
    esyslog("UPnP\tFailed to register the device. Error code: %s Help: %s",
        GetErrorMessage(ret).c_str(), GetErrorHelp(ret).c_str());
    return false;
  }

  isyslog("UPnP\tInitialising services...");
  serviceMap&  services = cMediaServer::GetServices();
  for(serviceMap::iterator it = services.begin(); it != services.end(); ++it){
    isyslog("UPnP\t...%s", (*it).second->GetServiceDescription().serviceType.c_str());
    (*it).second->Init(this, mDeviceHandle);
  }

  //send first advertisments
  isyslog("UPnP\tSend first advertisements to publish start in network");
  ret = UpnpSendAdvertisement(mDeviceHandle, GetAnnounceMaxAge());
  if (ret != UPNP_E_SUCCESS) {
    esyslog("UPnP\tError while sending first advertisments. Error code: %s Help: %s",
        GetErrorMessage(ret).c_str(), GetErrorHelp(ret).c_str());
    return false;
  }

  return true;
}

bool cMediaServer::Stop(){

  int ret = 0;

  isyslog("UPnP\tStopping services...");
  serviceMap&  services = cMediaServer::GetServices();
  for(serviceMap::iterator it = services.begin(); it != services.end(); ++it){
    isyslog("UPnP\t...%s", (*it).second->GetServiceDescription().serviceType.c_str());
    (*it).second->Stop();
  }

  isyslog("UPnP\tStopping UPnP media server");
  UpnpUnRegisterRootDevice(mDeviceHandle);
  if (ret != UPNP_E_SUCCESS) {
    esyslog("UPnP\tError while unregistering device. Error code: %s Help: %s",
        GetErrorMessage(ret).c_str(), GetErrorHelp(ret).c_str());
    return false;
  }

  isyslog("UPnP\tStopping web server...");
  if(mWebserver){
    mWebserver->Stop();
  }

  return true;
}

void cMediaServer::Housekeeping(){
  if(mMediaManager){
    mMediaManager->Housekeeping();
  }
}

bool cMediaServer::Initialize(){
  string address;
  uint16_t port = 0;

  mConfigDirectory = cPlugin::ConfigDirectory(PLUGIN_NAME_I18N);

  if(mCurrentConfiguration.expertSettings){
    address = mCurrentConfiguration.bindToAddress
              ? mCurrentConfiguration.address
              : tools::GetAddressByInterface(mCurrentConfiguration.interface);

    if(address.empty() || address.compare("0.0.0.0") == 0){
      address = tools::GetAddressByInterface(tools::GetNetworkInterfaceByIndex(0, true));
    }

    port = mCurrentConfiguration.port;
  } else {
    address = tools::GetAddressByInterface(tools::GetNetworkInterfaceByIndex(0, true));
    port = 0;
  }

  int ret = 0;

  LOG(1, "Initializing UPnP media server on %s:%d", address.empty()?"0":address.c_str(), port);

  ret = UpnpInit(address.empty()?"127.0.0.1":address.c_str(), mCurrentConfiguration.port);

  if(ret != UPNP_E_SUCCESS && ret != UPNP_E_INIT){
    esyslog("UPnP\tFailed to initialise UPnP media server. Error code: %s Help: %s",
        GetErrorMessage(ret).c_str(), GetErrorHelp(ret).c_str());
    return false;
  }

  isyslog("UPnP\tInitialized UPnP media server on %s:%d", UpnpGetServerIpAddress(), UpnpGetServerPort());

  mWebserver = new cWebserver(GetServerIPAddress());
  mMediaManager = new cMediaManager();
  stringstream ss;

  if(mCurrentConfiguration.expertSettings){

    if(mCurrentConfiguration.maxContentLength)
      SetMaxContentLength(mCurrentConfiguration.maxContentLength);

    if(mCurrentConfiguration.announceMaxAge)
      SetAnnounceMaxAge(mCurrentConfiguration.announceMaxAge);

    if(!mCurrentConfiguration.webServerRoot.empty())
      mWebserver->SetWebserverRootDir(mCurrentConfiguration.webServerRoot);

    if(!mCurrentConfiguration.useLive){
      if(!mCurrentConfiguration.presentationURL.empty())
        mWebserver->SetPresentationUrl(mCurrentConfiguration.presentationURL);
    } else {
      uint16_t port = mCurrentConfiguration.livePort ? mCurrentConfiguration.livePort : 8008;

      ss << "http://" << GetServerIPAddress() << ":" << port << "/";

      mWebserver->SetPresentationUrl(ss.str());
    }

    if(mCurrentConfiguration.webServerPort)
      mWebserver->SetListenerPort(mCurrentConfiguration.webServerPort);

    if(mCurrentConfiguration.maxRequestTime)
      mWebserver->SetMaxRequestTime(mCurrentConfiguration.maxRequestTime);

  }

  ss.str(string());
  ss << "sqlite:" << mCurrentConfiguration.databaseDir << "/metadata.db";
  try {
    mConnection = tntdb::connect(ss.str());
  } catch (const std::exception& e) {
    esyslog("UPnP\tException occurred while connecting to database '%s': %s", ss.str().c_str(), e.what());
    return false;
  }

  ret = UpnpSetMaxContentLength(GetMaxContentLength());

  if(ret != UPNP_E_SUCCESS){
    esyslog("UPnP\tFailed to set max. content length of SOAP messages. Error code: %s Help: %s",
        GetErrorMessage(ret).c_str(), GetErrorHelp(ret).c_str());
    return false;
  }

  isyslog("UPnP\tInitialising webserver");
  if(!mWebserver->Initialise()){
    esyslog("UPnP\tFailed to initialise the web server.");
    return false;
  }

  isyslog("UPnP\tInitialising media manager");
  if(!mMediaManager->Initialise()){
    esyslog("UPnP\tFailed to initialise the media manager.");
    return false;
  }

  return true;
}

void cMediaServer::SetAnnounceMaxAge(int announceMaxAge){
  mAnnounceMaxAge = (announceMaxAge != 0) ? announceMaxAge : 1800;
}

void cMediaServer::SetMaxContentLength(size_t maxContentLength){
  mMaxContentLength = (maxContentLength != 0) ? maxContentLength : KB(20);
}

void cMediaServer::SetConfiguration(upnp::cConfig newConfig){
  mCurrentConfiguration = newConfig;
}

upnp::cConfig cMediaServer::GetConfiguration() const {
  return mCurrentConfiguration;
}

const char* cMediaServer::GetServerIPAddress() const {
  return UpnpGetServerIpAddress();
}

uint16_t cMediaServer::GetServerPort() const {
  return UpnpGetServerPort();
}

string cMediaServer::GetDeviceDescriptionUrl() const {
  return mWebserver->GetServiceUrl() + mServerDescription.descriptionFile;
}

cMediaServer::serviceMap& cMediaServer::GetServices(){
  static serviceMap services;
  return services;
}

void cMediaServer::RegisterService(cUPnPService* service){
  if(service != NULL){
    LOG(1, "Registered service: %s", service->GetServiceDescription().serviceType.c_str());
    GetServices()[service->GetServiceDescription().serviceID] = service;
  }
}

int cMediaServer::ActionCallback(Upnp_EventType eventtype, void *event, void *cookie){
  Upnp_Subscription_Request* eventRequest = NULL;
  Upnp_Action_Request*       actionRequest = NULL;

  cMediaServer* mediaServer = (cMediaServer*)cookie;

  //check committed event variable
  if (event == NULL) {
      esyslog("UPnP\tUPnP Callback - NULL request");
      return UPNP_E_BAD_REQUEST;
  }

  cUPnPService* service;

  switch(eventtype){
  case UPNP_CONTROL_ACTION_REQUEST:
    actionRequest = (Upnp_Action_Request*) event;

    LOG(3, "Action request: %s", actionRequest->ActionName);

    if(!mediaServer->CheckDeviceUUID(actionRequest->DevUDN)){
      esyslog("UPnP\tUPnP Callback - action request not for this device");
      return UPNP_E_BAD_REQUEST;
    }

    service = cMediaServer::GetServices()[actionRequest->ServiceID];

    if(service == NULL){
      esyslog("UPnP\tCallback - unsupported service called for control");
      return UPNP_E_BAD_REQUEST;
    }

    return service->Execute(actionRequest);

  case UPNP_EVENT_SUBSCRIPTION_REQUEST:
    eventRequest = (Upnp_Subscription_Request*) event;

    LOG(3, "Subscription request from: %s", eventRequest->ServiceId);

    if(!mediaServer->CheckDeviceUUID(eventRequest->UDN)){
      esyslog("UPnP\tUPnP Callback - event request not for this device");
      return UPNP_E_BAD_REQUEST;
    }

    service = cMediaServer::GetServices()[eventRequest->ServiceId];

    if(service == NULL){
      esyslog("UPnP\tCallback - unsupported service called for eventing");
      return UPNP_E_BAD_REQUEST;
    }

    return service->Subscribe(eventRequest);

  default:
    esyslog("UPnP\tUPnP Action Callback - Unsupported Event");
    return UPNP_E_BAD_REQUEST;
  }

}

bool cMediaServer::CheckDeviceUUID(string deviceUUID) const {
  return deviceUUID.find(mCurrentConfiguration.deviceUUID) != string::npos;
}

string cMediaServer::GetErrorHelp(int error) const {
  switch (error){
  case UPNP_E_INVALID_DESC:
    return "Invalid device description. Most likely, the web server has an issue start listening on a specific interface or port.";
  default:
    return string();
  }
}

string cMediaServer::GetErrorMessage(int error) const {
  switch (error){
  case UPNP_E_SUCCESS:
    return "Success";
  case UPNP_E_INVALID_HANDLE:
    return "Invalid UPnP handle.";
  case UPNP_E_INVALID_PARAM:
    return "Invalid parameter.";
  case UPNP_E_OUTOF_HANDLE:
    return "Out of UPnP handles.";
  case UPNP_E_OUTOF_CONTEXT:
    return "Out of context";
  case UPNP_E_OUTOF_MEMORY:
    return "Out of memory";
  case UPNP_E_INIT:
    return "Initialization error";
  case UPNP_E_BUFFER_TOO_SMALL:
    return "Buffer too small";
  case UPNP_E_INVALID_DESC:
    return "Invalid device description";
  case UPNP_E_INVALID_URL:
    return "Invalid URL";
  case UPNP_E_INVALID_SID:
    return "Invalid service ID";
  case UPNP_E_INVALID_SERVICE:
    return "Invalid service";
  case UPNP_E_INVALID_DEVICE:
    return "Invalid device";
  case UPNP_E_BAD_RESPONSE:
    return "Bad response";
  case UPNP_E_BAD_REQUEST:
    return "Bad request";
  case UPNP_E_INVALID_ACTION:
    return "Invalid action";
  case UPNP_E_FINISH:
    return "Library finished already";
  case UPNP_E_INIT_FAILED:
    return "Initialization failed";
  case UPNP_E_URL_TOO_BIG:
    return "URL too big";
  case UPNP_E_BAD_HTTPMSG:
    return "Bad HTTP message";
  case UPNP_E_ALREADY_REGISTERED:
    return "Already registered";
  case UPNP_E_NETWORK_ERROR:
    return "Network error";
  case UPNP_E_SOCKET_WRITE:
    return "Socket write error";
  case UPNP_E_SOCKET_READ:
    return "Socket read error";
  case UPNP_E_SOCKET_BIND:
    return "Socket bind error";
  case UPNP_E_SOCKET_CONNECT:
    return "Socket connect error";
  case UPNP_E_OUTOF_SOCKET:
    return "Out of sockets";
  case UPNP_E_LISTEN:
    return "Socket listen error";
  case UPNP_E_TIMEDOUT:
    return "Socket timeout";
  case UPNP_E_SOCKET_ERROR:
    return "General socket error";
  case UPNP_E_FILE_WRITE_ERROR:
    return "File write error";
  case UPNP_E_CANCELED:
    return "Canceled";
  case UPNP_E_EVENT_PROTOCOL:
    return "Event protocol";
  case UPNP_E_SUBSCRIBE_UNACCEPTED:
    return "Subscription rejected";
  case UPNP_E_UNSUBSCRIBE_UNACCEPTED:
    return "Unsubscription rejected";
  case UPNP_E_NOTIFY_UNACCEPTED:
    return "Notification rejected";
  case UPNP_E_INVALID_ARGUMENT:
    return "Invalid argument";
  case UPNP_E_FILE_NOT_FOUND:
    return "File not found";
  case UPNP_E_FILE_READ_ERROR:
    return "File read error";
  case UPNP_E_EXT_NOT_XML:
    return "Not an \".xml\" extension";
  case UPNP_E_NO_WEB_SERVER:
    return "No web server";
  case UPNP_E_OUTOF_BOUNDS:
    return "Out of bounds";
  case UPNP_E_NOT_FOUND:
    return "Not found";
  case UPNP_E_INTERNAL_ERROR:
    return "Internal error";
  default:
    return "Unknown error code. Please see the rest of the logs.";
  }
}

cMediaServer::Description::Description(
    string fn, string m, string murl,
    string mod, string mon, string mono,
    string mourl, string sno, string desc)
: friendlyName(fn)
, manufacturer(m)
, manufacturerURL(murl)
, modelDescription(mod)
, modelName(mon)
, modelNumber(mono)
, modelURL(mourl)
, serialNumber(sno)
, descriptionFile(desc)
{
}

cMediaServer::ServerIcon::ServerIcon(image::cIcon profile, string filename)
: profile(profile)
, filename(filename)
{
}