Switch to unified view

a/libupnpp/upnpplib.cxx b/libupnpp/upnpplib.cxx
...
...
41
#include "md5.hxx"
41
#include "md5.hxx"
42
#include "log.hxx"
42
#include "log.hxx"
43
43
44
static LibUPnP *theLib;
44
static LibUPnP *theLib;
45
45
46
LibUPnP *LibUPnP::getLibUPnP(bool server)
46
LibUPnP *LibUPnP::getLibUPnP(bool serveronly, string* hwaddr,
47
                           const string ifname, const string ip,
48
                           unsigned short port)
47
{
49
{
48
    if (theLib == 0)
50
    if (theLib == 0)
49
      theLib = new LibUPnP(server);
51
      theLib = new LibUPnP(serveronly, hwaddr, ifname, ip, port);
50
    if (theLib && !theLib->ok()) {
52
    if (theLib && !theLib->ok()) {
51
        delete theLib;
53
        delete theLib;
52
        theLib = 0;
54
        theLib = 0;
53
        return 0;
55
        return 0;
54
    }
56
    }
55
    return theLib;
57
    return theLib;
56
}
58
}
57
59
58
60
59
LibUPnP::LibUPnP(bool server)
61
LibUPnP::LibUPnP(bool serveronly, string* hwaddr,
62
               const string ifname, const string inip, unsigned short port)
60
    : m_ok(false)
63
    : m_ok(false)
61
{
64
{
62
  m_init_error = UpnpInit(0, 0);
65
  LOGDEB("LibUPnP: serveronly " << serveronly << " &hwaddr " << hwaddr <<
66
         " ifname [" << ifname << "] inip [" << inip << "] port " << port 
67
         << endl);
68
69
  // If our caller wants to retrieve an ethernet address (typically
70
  // for uuid purposes), or has specified an interface we have to
71
  // look at the network config.
72
  const int ipalen(100);
73
  char ip_address[ipalen];
74
  ip_address[0] = 0;
75
  if (hwaddr || !ifname.empty()) {
76
      char mac[20];
77
      if (getsyshwaddr(ifname.c_str(), ip_address, ipalen, mac, 13) < 0) {
78
          LOGERR("LibUPnP::LibUPnP: failed retrieving addr" << endl);
79
          return;
80
      }
81
      if (hwaddr)
82
          *hwaddr = string(mac);
83
  }
84
85
  // If the interface name was not specified, we possibly use the
86
  // supplied IP address.
87
  if (ifname.empty())
88
      strncpy(ip_address, inip.c_str(), ipalen);
89
90
  m_init_error = UpnpInit(ip_address[0] ? ip_address : 0, port);
91
63
    if (m_init_error != UPNP_E_SUCCESS) {
92
    if (m_init_error != UPNP_E_SUCCESS) {
64
        LOGERR(errAsString("UpnpInit", m_init_error) << endl);
93
        LOGERR(errAsString("UpnpInit", m_init_error) << endl);
65
        return;
94
        return;
66
    }
95
    }
67
    setMaxContentLength(2000*1024);
96
    setMaxContentLength(2000*1024);
68
97
69
  const char *ip_address = UpnpGetServerIpAddress();
98
  LOGDEB("Using IP " << UpnpGetServerIpAddress() << " port " << 
70
  int port = UpnpGetServerPort();
99
         UpnpGetServerPort() << endl);
71
  LOGDEB("Using IP " << ip_address << " port " << port << endl);
72
100
73
#if defined(HAVE_UPNPSETLOGLEVEL)
101
#if defined(HAVE_UPNPSETLOGLEVEL)
74
    UpnpCloseLog();
102
    UpnpCloseLog();
75
#endif
103
#endif
76
104
105
  // Client initialization is simple, just do it. Defer device
106
  // initialization because it's more complicated.
77
    if (server) {
107
    if (serveronly) {
78
        m_ok = true;
108
        m_ok = true;
79
    } else {
109
    } else {
80
      // Client initialization is simple, just do it. Defer device init
81
      // completion because it's more complicated.
82
        m_init_error = UpnpRegisterClient(o_callback, (void *)this, &m_clh);
110
        m_init_error = UpnpRegisterClient(o_callback, (void *)this, &m_clh);
83
        
111
        
84
        if (m_init_error == UPNP_E_SUCCESS) {
112
        if (m_init_error == UPNP_E_SUCCESS) {
85
            m_ok = true;
113
            m_ok = true;
86
        } else {
114
        } else {
87
            LOGERR(errAsString("UpnpRegisterClient", m_init_error) << endl);
115
            LOGERR(errAsString("UpnpRegisterClient", m_init_error) << endl);
88
        }
116
        }
89
    }
117
    }
90
118
91
    // Servers sometimes make error (e.g.: minidlna returns bad
119
    // Servers sometimes make errors (e.g.: minidlna returns bad utf-8).
92
  // utf-8). 
93
    ixmlRelaxParser(1);
120
    ixmlRelaxParser(1);
94
}
121
}
95
122
96
int LibUPnP::setupWebServer(const string& description)
123
int LibUPnP::setupWebServer(const string& description)
97
{
124
{
...
...
195
        PLOGINF("%s\n", errAsString("UpnpFinish", error).c_str());
222
        PLOGINF("%s\n", errAsString("UpnpFinish", error).c_str());
196
    }
223
    }
197
    PLOGDEB("LibUPnP: done\n");
224
    PLOGDEB("LibUPnP: done\n");
198
}
225
}
199
226
200
string LibUPnP::makeDevUUID(const string& name)
227
string LibUPnP::makeDevUUID(const std::string& name, const std::string& hw)
201
{
228
{
202
    string digest;
229
    string digest;
203
    MD5String(name, digest);
230
    MD5String(name, digest);
204
    // digest has 16 bytes of binary data. UUID is like:
231
    // digest has 16 bytes of binary data. UUID is like:
205
    // f81d4fae-7dec-11d0-a765-00a0c91e6bf6
232
    // f81d4fae-7dec-11d0-a765-00a0c91e6bf6
206
    // Where the last 12 chars are provided by the hw addr
233
    // Where the last 12 chars are provided by the hw addr
207
234
208
  char hw[20];
209
  if (getsyshwaddr(hw, 13) < 0) {
210
      LOGERR("LibUPnP::makeDevUUID: failed retrieving hw addr" << endl);
211
      strcpy(hw, "2e87682c5ce8");
212
  }
213
    char uuid[100];
235
    char uuid[100];
214
    sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%s",
236
    sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%s",
215
            digest[0]&0xff, digest[1]&0xff, digest[2]&0xff, digest[3]&0xff,
237
            digest[0]&0xff, digest[1]&0xff, digest[2]&0xff, digest[3]&0xff,
216
            digest[4]&0xff, digest[5]&0xff,  digest[6]&0xff, digest[7]&0xff,
238
            digest[4]&0xff, digest[5]&0xff,  digest[6]&0xff, digest[7]&0xff,
217
            digest[8]&0xff, digest[9]&0xff, hw);
239
            digest[8]&0xff, digest[9]&0xff, hw.c_str());
218
    return uuid;
240
    return uuid;
219
}
241
}
220
242
221
243
222
string LibUPnP::evTypeAsString(Upnp_EventType et)
244
string LibUPnP::evTypeAsString(Upnp_EventType et)