|
a/libupnpp/upnpplib.hxx |
|
b/libupnpp/upnpplib.hxx |
1 |
/* Copyright (C) 2013 J.F.Dockes
|
1 |
/* Copyright (C) 2013 J.F.Dockes
|
2 |
* This program is free software; you can redistribute it and/or modify
|
2 |
* This program is free software; you can redistribute it and/or modify
|
3 |
* it under the terms of the GNU General Public License as published by
|
3 |
* it under the terms of the GNU General Public License as published by
|
4 |
* the Free Software Foundation; either version 2 of the License, or
|
4 |
* the Free Software Foundation; either version 2 of the License, or
|
5 |
* (at your option) any later version.
|
5 |
* (at your option) any later version.
|
6 |
*
|
6 |
*
|
7 |
* This program is distributed in the hope that it will be useful,
|
7 |
* This program is distributed in the hope that it will be useful,
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10 |
* GNU General Public License for more details.
|
10 |
* GNU General Public License for more details.
|
11 |
*
|
11 |
*
|
12 |
* You should have received a copy of the GNU General Public License
|
12 |
* You should have received a copy of the GNU General Public License
|
13 |
* along with this program; if not, write to the
|
13 |
* along with this program; if not, write to the
|
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
#ifndef _LIBUPNP_H_X_INCLUDED_
|
17 |
#ifndef _LIBUPNP_H_X_INCLUDED_
|
18 |
#define _LIBUPNP_H_X_INCLUDED_
|
18 |
#define _LIBUPNP_H_X_INCLUDED_
|
19 |
|
19 |
|
20 |
#include <string>
|
20 |
#include <string>
|
|
... |
|
... |
26 |
#include "ptmutex.hxx"
|
26 |
#include "ptmutex.hxx"
|
27 |
|
27 |
|
28 |
/** Our link to libupnp. Initialize and keep the handle around */
|
28 |
/** Our link to libupnp. Initialize and keep the handle around */
|
29 |
class LibUPnP {
|
29 |
class LibUPnP {
|
30 |
public:
|
30 |
public:
|
31 |
~LibUPnP();
|
31 |
~LibUPnP();
|
32 |
|
32 |
|
33 |
/** Retrieve the singleton LibUPnP object
|
33 |
/** Retrieve the singleton LibUPnP object
|
34 |
*
|
34 |
*
|
35 |
* This initializes libupnp, possibly setting an address and port, possibly
|
35 |
* This initializes libupnp, possibly setting an address and port, possibly
|
36 |
* registering a client if serveronly is false.
|
36 |
* registering a client if serveronly is false.
|
37 |
*
|
37 |
*
|
38 |
* @param serveronly no client init
|
38 |
* @param serveronly no client init
|
39 |
* @param hwaddr returns the hardware address for the specified network
|
39 |
* @param hwaddr returns the hardware address for the specified network
|
40 |
* interface, or the first one is ifname is empty. If the IP address is
|
40 |
* interface, or the first one is ifname is empty. If the IP address is
|
41 |
* specified instead of the interface name, the hardware address
|
41 |
* specified instead of the interface name, the hardware address
|
42 |
* returned is not necessarily the one matching the IP.
|
42 |
* returned is not necessarily the one matching the IP.
|
43 |
* @param ifname if not empty, network interface to use. Translated to
|
43 |
* @param ifname if not empty, network interface to use. Translated to
|
44 |
* IP address for the UpnpInit() call.
|
44 |
* IP address for the UpnpInit() call.
|
45 |
* @param ip if not empty, IP address to use. Only used if ifname is empty.
|
45 |
* @param ip if not empty, IP address to use. Only used if ifname is empty.
|
46 |
* @param port port parameter to UpnpInit() (0 for default).
|
46 |
* @param port port parameter to UpnpInit() (0 for default).
|
47 |
* @return 0 for failure.
|
47 |
* @return 0 for failure.
|
48 |
*/
|
48 |
*/
|
49 |
static LibUPnP* getLibUPnP(bool serveronly = false, std::string* hwaddr = 0,
|
49 |
static LibUPnP* getLibUPnP(bool serveronly = false, std::string* hwaddr = 0,
|
50 |
const std::string ifname = string(),
|
50 |
const std::string ifname = string(),
|
51 |
const std::string ip = string(),
|
51 |
const std::string ip = string(),
|
52 |
unsigned short port = 0);
|
52 |
unsigned short port = 0);
|
53 |
|
53 |
|
54 |
/** Set libupnp log file name and activate logging.
|
54 |
/** Set libupnp log file name and activate logging.
|
55 |
*
|
55 |
*
|
56 |
* @param fn file name to use. Use empty string to turn logging off
|
56 |
* @param fn file name to use. Use empty string to turn logging off
|
57 |
*/
|
57 |
*/
|
58 |
enum LogLevel {LogLevelNone, LogLevelError, LogLevelDebug};
|
58 |
enum LogLevel {LogLevelNone, LogLevelError, LogLevelDebug};
|
59 |
bool setLogFileName(const std::string& fn, LogLevel level = LogLevelError);
|
59 |
bool setLogFileName(const std::string& fn, LogLevel level = LogLevelError);
|
60 |
bool setLogLevel(LogLevel level);
|
60 |
bool setLogLevel(LogLevel level);
|
61 |
|
61 |
|
62 |
/** Set max library buffer size for reading content from servers. */
|
62 |
/** Set max library buffer size for reading content from servers. */
|
63 |
void setMaxContentLength(int bytes);
|
63 |
void setMaxContentLength(int bytes);
|
64 |
|
64 |
|
65 |
/** Check state after initialization */
|
65 |
/** Check state after initialization */
|
66 |
bool ok() const
|
66 |
bool ok() const
|
67 |
{
|
67 |
{
|
68 |
return m_ok;
|
68 |
return m_ok;
|
69 |
}
|
69 |
}
|
70 |
|
70 |
|
71 |
/** Retrieve init error if state not ok */
|
71 |
/** Retrieve init error if state not ok */
|
72 |
int getInitError() const
|
72 |
int getInitError() const
|
73 |
{
|
73 |
{
|
74 |
return m_init_error;
|
74 |
return m_init_error;
|
75 |
}
|
75 |
}
|
76 |
|
76 |
|
77 |
/** Build a unique persistent UUID for a root device. This uses a hash
|
77 |
/** Build a unique persistent UUID for a root device. This uses a hash
|
78 |
of the input name (e.g.: friendlyName), and the host Ethernet address */
|
78 |
of the input name (e.g.: friendlyName), and the host Ethernet address */
|
79 |
static std::string makeDevUUID(const std::string& name, const std::string& hw);
|
79 |
static std::string makeDevUUID(const std::string& name,
|
|
|
80 |
const std::string& hw);
|
80 |
|
81 |
|
81 |
/** Translate libupnp integer error code (UPNP_E_XXX) to string */
|
82 |
/** Translate libupnp integer error code (UPNP_E_XXX) to string */
|
82 |
static std::string errAsString(const std::string& who, int code);
|
83 |
static std::string errAsString(const std::string& who, int code);
|
83 |
|
84 |
|
84 |
/////////////////////////////////////////////////////////////////////////////
|
85 |
/////////////////////////////////////////////////////////////////////////////
|
85 |
/* The methods which follow are normally for use by the
|
86 |
/* The methods which follow are normally for use by the
|
86 |
* intermediate layers in libupnpp, such as the base device class
|
87 |
* intermediate layers in libupnpp, such as the base device class
|
87 |
* or the server directory, end-user code should not need them in
|
88 |
* or the server directory, end-user code should not need them in
|
88 |
* general.
|
89 |
* general.
|
89 |
*/
|
90 |
*/
|
90 |
|
91 |
|
91 |
/** Specify function to be called on given UPnP event. This will happen
|
92 |
/** Specify function to be called on given UPnP event. This will happen
|
92 |
* in the libupnp thread context.
|
93 |
* in the libupnp thread context.
|
93 |
*/
|
94 |
*/
|
94 |
void registerHandler(Upnp_EventType et, Upnp_FunPtr handler, void *cookie);
|
95 |
void registerHandler(Upnp_EventType et, Upnp_FunPtr handler, void *cookie);
|
95 |
|
96 |
|
96 |
/** Translate libupnp event type as string */
|
97 |
/** Translate libupnp event type as string */
|
97 |
static std::string evTypeAsString(Upnp_EventType);
|
98 |
static std::string evTypeAsString(Upnp_EventType);
|
98 |
|
99 |
|
99 |
int setupWebServer(const std::string& description);
|
100 |
int setupWebServer(const std::string& description);
|
100 |
|
101 |
|
101 |
UpnpClient_Handle getclh()
|
102 |
UpnpClient_Handle getclh()
|
102 |
{
|
103 |
{
|
103 |
return m_clh;
|
104 |
return m_clh;
|
104 |
}
|
105 |
}
|
105 |
UpnpDevice_Handle getdvh()
|
106 |
UpnpDevice_Handle getdvh()
|
106 |
{
|
107 |
{
|
107 |
return m_dvh;
|
108 |
return m_dvh;
|
108 |
}
|
109 |
}
|
109 |
private:
|
110 |
private:
|
110 |
|
111 |
|
111 |
// A Handler object records the data from registerHandler.
|
112 |
// A Handler object records the data from registerHandler.
|
112 |
class Handler {
|
113 |
class Handler {
|
113 |
public:
|
114 |
public:
|
114 |
Handler()
|
115 |
Handler()
|
115 |
: handler(0), cookie(0) {}
|
116 |
: handler(0), cookie(0) {}
|
116 |
Handler(Upnp_FunPtr h, void *c)
|
117 |
Handler(Upnp_FunPtr h, void *c)
|
117 |
: handler(h), cookie(c) {}
|
118 |
: handler(h), cookie(c) {}
|
118 |
Upnp_FunPtr handler;
|
119 |
Upnp_FunPtr handler;
|
119 |
void *cookie;
|
120 |
void *cookie;
|
120 |
};
|
121 |
};
|
121 |
|
122 |
|
122 |
|
123 |
|
123 |
LibUPnP(bool serveronly, std::string *hwaddr,
|
124 |
LibUPnP(bool serveronly, std::string *hwaddr,
|
124 |
const std::string ifname, const std::string ip,
|
125 |
const std::string ifname, const std::string ip,
|
125 |
unsigned short port);
|
126 |
unsigned short port);
|
126 |
|
127 |
|
127 |
LibUPnP(const LibUPnP &);
|
128 |
LibUPnP(const LibUPnP &);
|
128 |
LibUPnP& operator=(const LibUPnP &);
|
129 |
LibUPnP& operator=(const LibUPnP &);
|
129 |
|
130 |
|
130 |
static int o_callback(Upnp_EventType, void *, void *);
|
131 |
static int o_callback(Upnp_EventType, void *, void *);
|
131 |
|
132 |
|
132 |
bool m_ok;
|
133 |
bool m_ok;
|
133 |
int m_init_error;
|
134 |
int m_init_error;
|
134 |
UpnpClient_Handle m_clh;
|
135 |
UpnpClient_Handle m_clh;
|
|
|
136 |
// !TBD: there should be one handle per device if we ever want to have
|
|
|
137 |
// several devices in the same process.
|
135 |
UpnpDevice_Handle m_dvh;
|
138 |
UpnpDevice_Handle m_dvh;
|
136 |
PTMutexInit m_mutex;
|
139 |
PTMutexInit m_mutex;
|
137 |
std::map<Upnp_EventType, Handler> m_handlers;
|
140 |
std::map<Upnp_EventType, Handler> m_handlers;
|
138 |
};
|
141 |
};
|
139 |
|
142 |
|
140 |
#endif /* _LIBUPNP.H_X_INCLUDED_ */
|
143 |
#endif /* _LIBUPNP.H_X_INCLUDED_ */
|
141 |
/* Local Variables: */
|
|
|
142 |
/* mode: c++ */
|
|
|
143 |
/* c-basic-offset: 4 */
|
|
|
144 |
/* tab-width: 4 */
|
|
|
145 |
/* indent-tabs-mode: t */
|
|
|
146 |
/* End: */
|
|
|