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
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "Network.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "addons/binary-addons/AddonDll.h"
#include "addons/kodi-dev-kit/include/kodi/Network.h"
#include "network/DNSNameCache.h"
#include "network/Network.h"
#include "utils/SystemInfo.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
namespace ADDON
{
void Interface_Network::Init(AddonGlobalInterface *addonInterface)
{
addonInterface->toKodi->kodi_network = new AddonToKodiFuncTable_kodi_network();
addonInterface->toKodi->kodi_network->wake_on_lan = wake_on_lan;
addonInterface->toKodi->kodi_network->get_ip_address = get_ip_address;
addonInterface->toKodi->kodi_network->get_hostname = get_hostname;
addonInterface->toKodi->kodi_network->get_user_agent = get_user_agent;
addonInterface->toKodi->kodi_network->is_local_host = is_local_host;
addonInterface->toKodi->kodi_network->is_host_on_lan = is_host_on_lan;
addonInterface->toKodi->kodi_network->dns_lookup = dns_lookup;
addonInterface->toKodi->kodi_network->url_encode = url_encode;
}
void Interface_Network::DeInit(AddonGlobalInterface* addonInterface)
{
if (addonInterface->toKodi) /* <-- needed as long as the old addon way is used */
{
delete addonInterface->toKodi->kodi_network;
addonInterface->toKodi->kodi_network = nullptr;
}
}
bool Interface_Network::wake_on_lan(void* kodiBase, const char* mac)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr || mac == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}', mac='{}')", __FUNCTION__,
kodiBase, static_cast<const void*>(mac));
return false;
}
return CServiceBroker::GetNetwork().WakeOnLan(mac);
}
char* Interface_Network::get_ip_address(void* kodiBase)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}')", __FUNCTION__,
kodiBase);
return nullptr;
}
std::string titleIP;
CNetworkInterface* iface = CServiceBroker::GetNetwork().GetFirstConnectedInterface();
if (iface)
titleIP = iface->GetCurrentIPAddress();
else
titleIP = "127.0.0.1";
char* buffer = nullptr;
if (!titleIP.empty())
buffer = strdup(titleIP.c_str());
return buffer;
}
char* Interface_Network::get_hostname(void* kodiBase)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}')", __FUNCTION__,
kodiBase);
return nullptr;
}
std::string hostname;
if (!CServiceBroker::GetNetwork().GetHostName(hostname))
return nullptr;
char* buffer = nullptr;
if (!hostname.empty())
buffer = strdup(hostname.c_str());
return buffer;
}
char* Interface_Network::get_user_agent(void* kodiBase)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}')", __FUNCTION__,
kodiBase);
return nullptr;
}
std::string string = CSysInfo::GetUserAgent();
char* buffer = nullptr;
if (!string.empty())
buffer = strdup(string.c_str());
return buffer;
}
bool Interface_Network::is_local_host(void* kodiBase, const char* hostname)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr || hostname == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}', hostname='{}')",
__FUNCTION__, kodiBase, static_cast<const void*>(hostname));
return false;
}
return CServiceBroker::GetNetwork().IsLocalHost(hostname);
}
bool Interface_Network::is_host_on_lan(void* kodiBase, const char* hostname, bool offLineCheck)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr || hostname == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}', hostname='{}')",
__FUNCTION__, kodiBase, static_cast<const void*>(hostname));
return false;
}
return URIUtils::IsHostOnLAN(hostname, offLineCheck);
}
char* Interface_Network::dns_lookup(void* kodiBase, const char* url, bool* ret)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr || url == nullptr || ret == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}', url='{}', ret='{}')",
__FUNCTION__, kodiBase, static_cast<const void*>(url), static_cast<void*>(ret));
return nullptr;
}
std::string string;
*ret = CDNSNameCache::Lookup(url, string);
char* buffer = nullptr;
if (!string.empty())
buffer = strdup(string.c_str());
return buffer;
}
char* Interface_Network::url_encode(void* kodiBase, const char* url)
{
CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
if (addon == nullptr || url == nullptr)
{
CLog::Log(LOGERROR, "Interface_Network::{} - invalid data (addon='{}', url='{}')", __FUNCTION__,
kodiBase, static_cast<const void*>(url));
return nullptr;
}
std::string string = CURL::Encode(url);
char* buffer = nullptr;
if (!string.empty())
buffer = strdup(string.c_str());
return buffer;
}
} /* namespace ADDON */
|