summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-dev-kit/include/kodi/Network.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/addons/kodi-dev-kit/include/kodi/Network.h')
-rw-r--r--xbmc/addons/kodi-dev-kit/include/kodi/Network.h282
1 files changed, 282 insertions, 0 deletions
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/Network.h b/xbmc/addons/kodi-dev-kit/include/kodi/Network.h
new file mode 100644
index 0000000..910385f
--- /dev/null
+++ b/xbmc/addons/kodi-dev-kit/include/kodi/Network.h
@@ -0,0 +1,282 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include "AddonBase.h"
12#include "c-api/network.h"
13
14#ifdef __cplusplus
15
16//==============================================================================
17/// @defgroup cpp_kodi_network Interface - kodi::network
18/// @ingroup cpp
19/// @brief **Network functions**\n
20/// The network module offers functions that allow you to control it.
21///
22/// It has the header @ref Network.h "#include <kodi/Network.h>" be included
23/// to enjoy it.
24///
25//------------------------------------------------------------------------------
26
27namespace kodi
28{
29namespace network
30{
31
32//============================================================================
33/// @ingroup cpp_kodi_network
34/// @brief Send WakeOnLan magic packet.
35///
36/// @param[in] mac Network address of the host to wake.
37/// @return True if the magic packet was successfully sent, false otherwise.
38///
39inline bool ATTRIBUTE_HIDDEN WakeOnLan(const std::string& mac)
40{
41 using namespace ::kodi::addon;
42
43 return CAddonBase::m_interface->toKodi->kodi_network->wake_on_lan(
44 CAddonBase::m_interface->toKodi->kodiBase, mac.c_str());
45}
46//----------------------------------------------------------------------------
47
48//============================================================================
49/// @ingroup cpp_kodi_network
50/// @brief To the current own ip address as a string.
51///
52/// @return Own system ip.
53///
54///
55/// ------------------------------------------------------------------------
56///
57/// **Example:**
58/// ~~~~~~~~~~~~~{.cpp}
59/// #include <kodi/Network.h>
60/// ...
61/// std::string ipAddress = kodi::network::GetIPAddress();
62/// fprintf(stderr, "My IP is '%s'\n", ipAddress.c_str());
63/// ...
64/// ~~~~~~~~~~~~~
65///
66inline std::string ATTRIBUTE_HIDDEN GetIPAddress()
67{
68 using namespace ::kodi::addon;
69
70 std::string ip;
71 char* string = CAddonBase::m_interface->toKodi->kodi_network->get_ip_address(
72 CAddonBase::m_interface->toKodi->kodiBase);
73 if (string != nullptr)
74 {
75 ip = string;
76 CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string);
77 }
78 return ip;
79}
80//----------------------------------------------------------------------------
81
82//============================================================================
83/// @ingroup cpp_kodi_network
84/// @brief Return our hostname.
85///
86/// @return String about hostname, empty in case of error
87///
88///
89/// ------------------------------------------------------------------------
90///
91/// **Example:**
92/// ~~~~~~~~~~~~~{.cpp}
93/// #include <kodi/Network.h>
94/// ...
95/// std::string hostname = kodi::network::GetHostname();
96/// fprintf(stderr, "My hostname is '%s'\n", hostname.c_str());
97/// ...
98/// ~~~~~~~~~~~~~
99///
100inline std::string ATTRIBUTE_HIDDEN GetHostname()
101{
102 using namespace ::kodi::addon;
103
104 std::string ip;
105 char* string = CAddonBase::m_interface->toKodi->kodi_network->get_hostname(
106 CAddonBase::m_interface->toKodi->kodiBase);
107 if (string != nullptr)
108 {
109 ip = string;
110 CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string);
111 }
112 return ip;
113}
114//----------------------------------------------------------------------------
115
116//============================================================================
117/// @ingroup cpp_kodi_network
118/// @brief Returns Kodi's HTTP UserAgent string.
119///
120/// @return HTTP user agent
121///
122///
123/// ------------------------------------------------------------------------
124///
125/// **Example:**
126/// ~~~~~~~~~~~~~{.py}
127/// ..
128/// std::string agent = kodi::network::GetUserAgent()
129/// ..
130/// ~~~~~~~~~~~~~
131///
132/// example output:
133/// Kodi/19.0-ALPHA1 (X11; Linux x86_64) Ubuntu/20.04 App_Bitness/64 Version/19.0-ALPHA1-Git:20200522-0076d136d3-dirty
134///
135inline std::string ATTRIBUTE_HIDDEN GetUserAgent()
136{
137 using namespace ::kodi::addon;
138
139 std::string agent;
140 char* string = CAddonBase::m_interface->toKodi->kodi_network->get_user_agent(
141 CAddonBase::m_interface->toKodi->kodiBase);
142 if (string != nullptr)
143 {
144 agent = string;
145 CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string);
146 }
147 return agent;
148}
149//----------------------------------------------------------------------------
150
151//============================================================================
152/// @ingroup cpp_kodi_network
153/// @brief Check given name or ip address corresponds to localhost.
154///
155/// @param[in] hostname Hostname to check
156/// @return Return true if given name or ip address corresponds to localhost
157///
158///
159/// ------------------------------------------------------------------------
160///
161/// **Example:**
162/// ~~~~~~~~~~~~~{.cpp}
163/// #include <kodi/Network.h>
164/// ...
165/// if (kodi::network::IsLocalHost("127.0.0.1"))
166/// fprintf(stderr, "Is localhost\n");
167/// ...
168/// ~~~~~~~~~~~~~
169///
170inline bool ATTRIBUTE_HIDDEN IsLocalHost(const std::string& hostname)
171{
172 using namespace ::kodi::addon;
173
174 return CAddonBase::m_interface->toKodi->kodi_network->is_local_host(
175 CAddonBase::m_interface->toKodi->kodiBase, hostname.c_str());
176}
177//----------------------------------------------------------------------------
178
179//==============================================================================
180/// @ingroup cpp_kodi_network
181/// @brief Checks whether the specified path refers to a local network.
182///
183/// @param[in] hostname Hostname to check
184/// @param[in] offLineCheck Check if in private range, see https://en.wikipedia.org/wiki/Private_network
185/// @return True if host is on a LAN, false otherwise
186///
187inline bool ATTRIBUTE_HIDDEN IsHostOnLAN(const std::string& hostname, bool offLineCheck = false)
188{
189 using namespace kodi::addon;
190
191 return CAddonBase::m_interface->toKodi->kodi_network->is_host_on_lan(
192 CAddonBase::m_interface->toKodi->kodiBase, hostname.c_str(), offLineCheck);
193}
194//------------------------------------------------------------------------------
195
196//============================================================================
197/// @ingroup cpp_kodi_network
198/// @brief URL encodes the given string
199///
200/// This function converts the given input string to a URL encoded string and
201/// returns that as a new allocated string. All input characters that are
202/// not a-z, A-Z, 0-9, '-', '.', '_' or '~' are converted to their "URL escaped"
203/// version (%NN where NN is a two-digit hexadecimal number).
204///
205/// @param[in] url The code of the message to get.
206/// @return Encoded URL string
207///
208///
209/// ------------------------------------------------------------------------
210///
211/// **Example:**
212/// ~~~~~~~~~~~~~{.cpp}
213/// #include <kodi/Network.h>
214/// ...
215/// std::string encodedUrl = kodi::network::URLEncode("François");
216/// fprintf(stderr, "Encoded URL is '%s'\n", encodedUrl.c_str());
217/// ...
218/// ~~~~~~~~~~~~~
219/// For example, the string: François ,would be encoded as: Fran%C3%A7ois
220///
221inline std::string ATTRIBUTE_HIDDEN URLEncode(const std::string& url)
222{
223 using namespace ::kodi::addon;
224
225 std::string retString;
226 char* string = CAddonBase::m_interface->toKodi->kodi_network->url_encode(
227 CAddonBase::m_interface->toKodi->kodiBase, url.c_str());
228 if (string != nullptr)
229 {
230 retString = string;
231 CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string);
232 }
233 return retString;
234}
235//----------------------------------------------------------------------------
236
237//============================================================================
238/// @ingroup cpp_kodi_network
239/// @brief Lookup URL in DNS cache
240///
241/// This test will get DNS record for a domain. The DNS lookup is done directly
242/// against the domain's authoritative name server, so changes to DNS Records
243/// should show up instantly. By default, the DNS lookup tool will return an
244/// IP address if you give it a name (e.g. www.example.com)
245///
246/// @param[in] hostName The code of the message to get.
247/// @param[out] ipAddress Returned address
248/// @return true if successfull
249///
250///
251/// ------------------------------------------------------------------------
252///
253/// **Example:**
254/// ~~~~~~~~~~~~~{.cpp}
255/// #include <kodi/Network.h>
256/// ...
257/// std::string ipAddress;
258/// bool ret = kodi::network::DNSLookup("www.google.com", ipAddress);
259/// fprintf(stderr, "DNSLookup returned for www.google.com the IP '%s', call was %s\n", ipAddress.c_str(), ret ? "ok" : "failed");
260/// ...
261/// ~~~~~~~~~~~~~
262///
263inline bool ATTRIBUTE_HIDDEN DNSLookup(const std::string& hostName, std::string& ipAddress)
264{
265 using namespace ::kodi::addon;
266
267 bool ret = false;
268 char* string = CAddonBase::m_interface->toKodi->kodi_network->dns_lookup(
269 CAddonBase::m_interface->toKodi->kodiBase, hostName.c_str(), &ret);
270 if (string != nullptr)
271 {
272 ipAddress = string;
273 CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string);
274 }
275 return ret;
276}
277//----------------------------------------------------------------------------
278
279} /* namespace network */
280} /* namespace kodi */
281
282#endif /* __cplusplus */