diff options
| author | manuel <manuel@mausz.at> | 2020-07-02 23:09:26 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-07-02 23:09:26 +0200 |
| commit | 5f8335c1e49ce108ef3481863833c98efa00411b (patch) | |
| tree | f02b5c1c9765bb6a14c8eb42bb4f81b9face0b55 /xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h | |
| parent | e317daf081a1048904fdf0b548946fa3ba6593a7 (diff) | |
| download | kodi-pvr-build-master.tar.gz kodi-pvr-build-master.tar.bz2 kodi-pvr-build-master.zip | |
Diffstat (limited to 'xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h')
| -rw-r--r-- | xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h | 386 |
1 files changed, 246 insertions, 140 deletions
diff --git a/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h b/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h index 73390c4..910385f 100644 --- a/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h +++ b/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h | |||
| @@ -9,35 +9,17 @@ | |||
| 9 | #pragma once | 9 | #pragma once |
| 10 | 10 | ||
| 11 | #include "AddonBase.h" | 11 | #include "AddonBase.h" |
| 12 | #include "c-api/network.h" | ||
| 12 | 13 | ||
| 13 | /* | 14 | #ifdef __cplusplus |
| 14 | * For interface between add-on and kodi. | ||
| 15 | * | ||
| 16 | * This structure defines the addresses of functions stored inside Kodi which | ||
| 17 | * are then available for the add-on to call | ||
| 18 | * | ||
| 19 | * All function pointers there are used by the C++ interface functions below. | ||
| 20 | * You find the set of them on xbmc/addons/interfaces/General.cpp | ||
| 21 | * | ||
| 22 | * Note: For add-on development itself this is not needed | ||
| 23 | */ | ||
| 24 | typedef struct AddonToKodiFuncTable_kodi_network | ||
| 25 | { | ||
| 26 | bool (*wake_on_lan)(void* kodiBase, const char *mac); | ||
| 27 | char* (*get_ip_address)(void* kodiBase); | ||
| 28 | char* (*dns_lookup)(void* kodiBase, const char* url, bool* ret); | ||
| 29 | char* (*url_encode)(void* kodiBase, const char* url); | ||
| 30 | } AddonToKodiFuncTable_kodi_network; | ||
| 31 | 15 | ||
| 32 | //============================================================================== | 16 | //============================================================================== |
| 33 | /// | 17 | /// @defgroup cpp_kodi_network Interface - kodi::network |
| 34 | /// \defgroup cpp_kodi_network Interface - kodi::network | 18 | /// @ingroup cpp |
| 35 | /// \ingroup cpp | 19 | /// @brief **Network functions**\n |
| 36 | /// @brief **Network functions** | ||
| 37 | /// | ||
| 38 | /// The network module offers functions that allow you to control it. | 20 | /// The network module offers functions that allow you to control it. |
| 39 | /// | 21 | /// |
| 40 | /// It has the header \ref Network.h "#include <kodi/Network.h>" be included | 22 | /// It has the header @ref Network.h "#include <kodi/Network.h>" be included |
| 41 | /// to enjoy it. | 23 | /// to enjoy it. |
| 42 | /// | 24 | /// |
| 43 | //------------------------------------------------------------------------------ | 25 | //------------------------------------------------------------------------------ |
| @@ -47,130 +29,254 @@ namespace kodi | |||
| 47 | namespace network | 29 | namespace network |
| 48 | { | 30 | { |
| 49 | 31 | ||
| 50 | //============================================================================ | 32 | //============================================================================ |
| 51 | /// | 33 | /// @ingroup cpp_kodi_network |
| 52 | /// \ingroup cpp_kodi_network | 34 | /// @brief Send WakeOnLan magic packet. |
| 53 | /// @brief Send WakeOnLan magic packet. | 35 | /// |
| 54 | /// | 36 | /// @param[in] mac Network address of the host to wake. |
| 55 | /// @param[in] mac Network address of the host to wake. | 37 | /// @return True if the magic packet was successfully sent, false otherwise. |
| 56 | /// @return True if the magic packet was successfully sent, false otherwise. | 38 | /// |
| 57 | /// | 39 | inline bool ATTRIBUTE_HIDDEN WakeOnLan(const std::string& mac) |
| 58 | inline bool 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 | /// | ||
| 66 | inline 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) | ||
| 59 | { | 74 | { |
| 60 | return ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->wake_on_lan(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, mac.c_str()); | 75 | ip = string; |
| 76 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 61 | } | 77 | } |
| 62 | //---------------------------------------------------------------------------- | 78 | return ip; |
| 63 | 79 | } | |
| 64 | //============================================================================ | 80 | //---------------------------------------------------------------------------- |
| 65 | /// | 81 | |
| 66 | /// \ingroup cpp_kodi_network | 82 | //============================================================================ |
| 67 | /// @brief To the current own ip address as a string. | 83 | /// @ingroup cpp_kodi_network |
| 68 | /// | 84 | /// @brief Return our hostname. |
| 69 | /// @return Own system ip. | 85 | /// |
| 70 | /// | 86 | /// @return String about hostname, empty in case of error |
| 71 | /// | 87 | /// |
| 72 | /// ------------------------------------------------------------------------ | 88 | /// |
| 73 | /// | 89 | /// ------------------------------------------------------------------------ |
| 74 | /// **Example:** | 90 | /// |
| 75 | /// ~~~~~~~~~~~~~{.cpp} | 91 | /// **Example:** |
| 76 | /// #include <kodi/Network.h> | 92 | /// ~~~~~~~~~~~~~{.cpp} |
| 77 | /// ... | 93 | /// #include <kodi/Network.h> |
| 78 | /// std::string ipAddress = kodi::network::GetIPAddress(); | 94 | /// ... |
| 79 | /// fprintf(stderr, "My IP is '%s'\n", ipAddress.c_str()); | 95 | /// std::string hostname = kodi::network::GetHostname(); |
| 80 | /// ... | 96 | /// fprintf(stderr, "My hostname is '%s'\n", hostname.c_str()); |
| 81 | /// ~~~~~~~~~~~~~ | 97 | /// ... |
| 82 | /// | 98 | /// ~~~~~~~~~~~~~ |
| 83 | inline std::string GetIPAddress() | 99 | /// |
| 100 | inline 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) | ||
| 84 | { | 108 | { |
| 85 | std::string ip; | 109 | ip = string; |
| 86 | char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->get_ip_address(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase); | 110 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string); |
| 87 | if (string != nullptr) | ||
| 88 | { | ||
| 89 | ip = string; | ||
| 90 | ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 91 | } | ||
| 92 | return ip; | ||
| 93 | } | 111 | } |
| 94 | //---------------------------------------------------------------------------- | 112 | return ip; |
| 95 | 113 | } | |
| 96 | //============================================================================ | 114 | //---------------------------------------------------------------------------- |
| 97 | /// | 115 | |
| 98 | /// \ingroup cpp_kodi_network | 116 | //============================================================================ |
| 99 | /// @brief URL encodes the given string | 117 | /// @ingroup cpp_kodi_network |
| 100 | /// | 118 | /// @brief Returns Kodi's HTTP UserAgent string. |
| 101 | /// This function converts the given input string to a URL encoded string and | 119 | /// |
| 102 | /// returns that as a new allocated string. All input characters that are | 120 | /// @return HTTP user agent |
| 103 | /// not a-z, A-Z, 0-9, '-', '.', '_' or '~' are converted to their "URL escaped" | 121 | /// |
| 104 | /// version (%NN where NN is a two-digit hexadecimal number). | 122 | /// |
| 105 | /// | 123 | /// ------------------------------------------------------------------------ |
| 106 | /// @param[in] url The code of the message to get. | 124 | /// |
| 107 | /// @return Encoded URL string | 125 | /// **Example:** |
| 108 | /// | 126 | /// ~~~~~~~~~~~~~{.py} |
| 109 | /// | 127 | /// .. |
| 110 | /// ------------------------------------------------------------------------ | 128 | /// std::string agent = kodi::network::GetUserAgent() |
| 111 | /// | 129 | /// .. |
| 112 | /// **Example:** | 130 | /// ~~~~~~~~~~~~~ |
| 113 | /// ~~~~~~~~~~~~~{.cpp} | 131 | /// |
| 114 | /// #include <kodi/Network.h> | 132 | /// example output: |
| 115 | /// ... | 133 | /// Kodi/19.0-ALPHA1 (X11; Linux x86_64) Ubuntu/20.04 App_Bitness/64 Version/19.0-ALPHA1-Git:20200522-0076d136d3-dirty |
| 116 | /// std::string encodedUrl = kodi::network::URLEncode("François"); | 134 | /// |
| 117 | /// fprintf(stderr, "Encoded URL is '%s'\n", encodedUrl.c_str()); | 135 | inline std::string ATTRIBUTE_HIDDEN GetUserAgent() |
| 118 | /// ... | 136 | { |
| 119 | /// ~~~~~~~~~~~~~ | 137 | using namespace ::kodi::addon; |
| 120 | /// For example, the string: François ,would be encoded as: Fran%C3%A7ois | 138 | |
| 121 | /// | 139 | std::string agent; |
| 122 | inline std::string URLEncode(const std::string& url) | 140 | char* string = CAddonBase::m_interface->toKodi->kodi_network->get_user_agent( |
| 141 | CAddonBase::m_interface->toKodi->kodiBase); | ||
| 142 | if (string != nullptr) | ||
| 123 | { | 143 | { |
| 124 | std::string retString; | 144 | agent = string; |
| 125 | char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->url_encode(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, url.c_str()); | 145 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string); |
| 126 | if (string != nullptr) | ||
| 127 | { | ||
| 128 | retString = string; | ||
| 129 | ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 130 | } | ||
| 131 | return retString; | ||
| 132 | } | 146 | } |
| 133 | //---------------------------------------------------------------------------- | 147 | return agent; |
| 134 | 148 | } | |
| 135 | //============================================================================ | 149 | //---------------------------------------------------------------------------- |
| 136 | /// | 150 | |
| 137 | /// \ingroup cpp_kodi_network | 151 | //============================================================================ |
| 138 | /// @brief Lookup URL in DNS cache | 152 | /// @ingroup cpp_kodi_network |
| 139 | /// | 153 | /// @brief Check given name or ip address corresponds to localhost. |
| 140 | /// This test will get DNS record for a domain. The DNS lookup is done directly | 154 | /// |
| 141 | /// against the domain's authoritative name server, so changes to DNS Records | 155 | /// @param[in] hostname Hostname to check |
| 142 | /// should show up instantly. By default, the DNS lookup tool will return an | 156 | /// @return Return true if given name or ip address corresponds to localhost |
| 143 | /// IP address if you give it a name (e.g. www.example.com) | 157 | /// |
| 144 | /// | 158 | /// |
| 145 | /// @param[in] hostName The code of the message to get. | 159 | /// ------------------------------------------------------------------------ |
| 146 | /// @param[out] ipAddress Returned address | 160 | /// |
| 147 | /// @return true if successfull | 161 | /// **Example:** |
| 148 | /// | 162 | /// ~~~~~~~~~~~~~{.cpp} |
| 149 | /// | 163 | /// #include <kodi/Network.h> |
| 150 | /// ------------------------------------------------------------------------ | 164 | /// ... |
| 151 | /// | 165 | /// if (kodi::network::IsLocalHost("127.0.0.1")) |
| 152 | /// **Example:** | 166 | /// fprintf(stderr, "Is localhost\n"); |
| 153 | /// ~~~~~~~~~~~~~{.cpp} | 167 | /// ... |
| 154 | /// #include <kodi/Network.h> | 168 | /// ~~~~~~~~~~~~~ |
| 155 | /// ... | 169 | /// |
| 156 | /// std::string ipAddress; | 170 | inline bool ATTRIBUTE_HIDDEN IsLocalHost(const std::string& hostname) |
| 157 | /// bool ret = kodi::network::DNSLookup("www.google.com", ipAddress); | 171 | { |
| 158 | /// fprintf(stderr, "DNSLookup returned for www.google.com the IP '%s', call was %s\n", ipAddress.c_str(), ret ? "ok" : "failed"); | 172 | using namespace ::kodi::addon; |
| 159 | /// ... | 173 | |
| 160 | /// ~~~~~~~~~~~~~ | 174 | return CAddonBase::m_interface->toKodi->kodi_network->is_local_host( |
| 161 | /// | 175 | CAddonBase::m_interface->toKodi->kodiBase, hostname.c_str()); |
| 162 | inline bool DNSLookup(const std::string& hostName, std::string& ipAddress) | 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 | /// | ||
| 187 | inline 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 | /// | ||
| 221 | inline 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 | /// | ||
| 263 | inline 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) | ||
| 163 | { | 271 | { |
| 164 | bool ret = false; | 272 | ipAddress = string; |
| 165 | char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->dns_lookup(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, hostName.c_str(), &ret); | 273 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, string); |
| 166 | if (string != nullptr) | ||
| 167 | { | ||
| 168 | ipAddress = string; | ||
| 169 | ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 170 | } | ||
| 171 | return ret; | ||
| 172 | } | 274 | } |
| 173 | //---------------------------------------------------------------------------- | 275 | return ret; |
| 276 | } | ||
| 277 | //---------------------------------------------------------------------------- | ||
| 174 | 278 | ||
| 175 | } /* namespace network */ | 279 | } /* namespace network */ |
| 176 | } /* namespace kodi */ | 280 | } /* namespace kodi */ |
| 281 | |||
| 282 | #endif /* __cplusplus */ | ||
