diff options
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 | 187 |
1 files changed, 187 insertions, 0 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 new file mode 100644 index 0000000..f27b710 --- /dev/null +++ b/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | #pragma once | ||
| 2 | /* | ||
| 3 | * Copyright (C) 2005-2017 Team Kodi | ||
| 4 | * http://kodi.tv | ||
| 5 | * | ||
| 6 | * This Program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2, or (at your option) | ||
| 9 | * any later version. | ||
| 10 | * | ||
| 11 | * This Program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with KODI; see the file COPYING. If not, see | ||
| 18 | * <http://www.gnu.org/licenses/>. | ||
| 19 | * | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "AddonBase.h" | ||
| 23 | |||
| 24 | /* | ||
| 25 | * For interface between add-on and kodi. | ||
| 26 | * | ||
| 27 | * This structure defines the addresses of functions stored inside Kodi which | ||
| 28 | * are then available for the add-on to call | ||
| 29 | * | ||
| 30 | * All function pointers there are used by the C++ interface functions below. | ||
| 31 | * You find the set of them on xbmc/addons/interfaces/General.cpp | ||
| 32 | * | ||
| 33 | * Note: For add-on development itself this is not needed | ||
| 34 | */ | ||
| 35 | typedef struct AddonToKodiFuncTable_kodi_network | ||
| 36 | { | ||
| 37 | bool (*wake_on_lan)(void* kodiBase, const char *mac); | ||
| 38 | char* (*get_ip_address)(void* kodiBase); | ||
| 39 | char* (*dns_lookup)(void* kodiBase, const char* url, bool* ret); | ||
| 40 | char* (*url_encode)(void* kodiBase, const char* url); | ||
| 41 | } AddonToKodiFuncTable_kodi_network; | ||
| 42 | |||
| 43 | //============================================================================== | ||
| 44 | /// | ||
| 45 | /// \defgroup cpp_kodi_network Interface - kodi::network | ||
| 46 | /// \ingroup cpp | ||
| 47 | /// @brief **Network functions** | ||
| 48 | /// | ||
| 49 | /// The network module offers functions that allow you to control it. | ||
| 50 | /// | ||
| 51 | /// It has the header \ref Network.h "#include <kodi/Network.h>" be included | ||
| 52 | /// to enjoy it. | ||
| 53 | /// | ||
| 54 | //------------------------------------------------------------------------------ | ||
| 55 | |||
| 56 | namespace kodi | ||
| 57 | { | ||
| 58 | namespace network | ||
| 59 | { | ||
| 60 | |||
| 61 | //============================================================================ | ||
| 62 | /// | ||
| 63 | /// \ingroup cpp_kodi_network | ||
| 64 | /// @brief Send WakeOnLan magic packet. | ||
| 65 | /// | ||
| 66 | /// @param[in] mac Network address of the host to wake. | ||
| 67 | /// @return True if the magic packet was successfully sent, false otherwise. | ||
| 68 | /// | ||
| 69 | inline bool WakeOnLan(const std::string& mac) | ||
| 70 | { | ||
| 71 | return ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->wake_on_lan(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, mac.c_str()); | ||
| 72 | } | ||
| 73 | //---------------------------------------------------------------------------- | ||
| 74 | |||
| 75 | //============================================================================ | ||
| 76 | /// | ||
| 77 | /// \ingroup cpp_kodi_network | ||
| 78 | /// @brief To the current own ip address as a string. | ||
| 79 | /// | ||
| 80 | /// @return Own system ip. | ||
| 81 | /// | ||
| 82 | /// | ||
| 83 | /// ------------------------------------------------------------------------ | ||
| 84 | /// | ||
| 85 | /// **Example:** | ||
| 86 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 87 | /// #include <kodi/Network.h> | ||
| 88 | /// ... | ||
| 89 | /// std::string ipAddress = kodi::network::GetIPAddress(); | ||
| 90 | /// fprintf(stderr, "My IP is '%s'\n", ipAddress.c_str()); | ||
| 91 | /// ... | ||
| 92 | /// ~~~~~~~~~~~~~ | ||
| 93 | /// | ||
| 94 | inline std::string GetIPAddress() | ||
| 95 | { | ||
| 96 | std::string ip; | ||
| 97 | char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->get_ip_address(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase); | ||
| 98 | if (string != nullptr) | ||
| 99 | { | ||
| 100 | ip = string; | ||
| 101 | ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 102 | } | ||
| 103 | return ip; | ||
| 104 | } | ||
| 105 | //---------------------------------------------------------------------------- | ||
| 106 | |||
| 107 | //============================================================================ | ||
| 108 | /// | ||
| 109 | /// \ingroup cpp_kodi_network | ||
| 110 | /// @brief URL encodes the given string | ||
| 111 | /// | ||
| 112 | /// This function converts the given input string to a URL encoded string and | ||
| 113 | /// returns that as a new allocated string. All input characters that are | ||
| 114 | /// not a-z, A-Z, 0-9, '-', '.', '_' or '~' are converted to their "URL escaped" | ||
| 115 | /// version (%NN where NN is a two-digit hexadecimal number). | ||
| 116 | /// | ||
| 117 | /// @param[in] url The code of the message to get. | ||
| 118 | /// @return Encoded URL string | ||
| 119 | /// | ||
| 120 | /// | ||
| 121 | /// ------------------------------------------------------------------------ | ||
| 122 | /// | ||
| 123 | /// **Example:** | ||
| 124 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 125 | /// #include <kodi/Network.h> | ||
| 126 | /// ... | ||
| 127 | /// std::string encodedUrl = kodi::network::URLEncode("François"); | ||
| 128 | /// fprintf(stderr, "Encoded URL is '%s'\n", encodedUrl.c_str()); | ||
| 129 | /// ... | ||
| 130 | /// ~~~~~~~~~~~~~ | ||
| 131 | /// For example, the string: François ,would be encoded as: Fran%C3%A7ois | ||
| 132 | /// | ||
| 133 | inline std::string URLEncode(const std::string& url) | ||
| 134 | { | ||
| 135 | std::string retString; | ||
| 136 | char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->url_encode(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, url.c_str()); | ||
| 137 | if (string != nullptr) | ||
| 138 | { | ||
| 139 | retString = string; | ||
| 140 | ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 141 | } | ||
| 142 | return retString; | ||
| 143 | } | ||
| 144 | //---------------------------------------------------------------------------- | ||
| 145 | |||
| 146 | //============================================================================ | ||
| 147 | /// | ||
| 148 | /// \ingroup cpp_kodi_network | ||
| 149 | /// @brief Lookup URL in DNS cache | ||
| 150 | /// | ||
| 151 | /// This test will get DNS record for a domain. The DNS lookup is done directly | ||
| 152 | /// against the domain's authoritative name server, so changes to DNS Records | ||
| 153 | /// should show up instantly. By default, the DNS lookup tool will return an | ||
| 154 | /// IP address if you give it a name (e.g. www.example.com) | ||
| 155 | /// | ||
| 156 | /// @param[in] hostName The code of the message to get. | ||
| 157 | /// @param[out] ipAddress Returned address | ||
| 158 | /// @return true if successfull | ||
| 159 | /// | ||
| 160 | /// | ||
| 161 | /// ------------------------------------------------------------------------ | ||
| 162 | /// | ||
| 163 | /// **Example:** | ||
| 164 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 165 | /// #include <kodi/Network.h> | ||
| 166 | /// ... | ||
| 167 | /// std::string ipAddress; | ||
| 168 | /// bool ret = kodi::network::DNSLookup("www.google.com", ipAddress); | ||
| 169 | /// fprintf(stderr, "DNSLookup returned for www.google.com the IP '%s', call was %s\n", ipAddress.c_str(), ret ? "ok" : "failed"); | ||
| 170 | /// ... | ||
| 171 | /// ~~~~~~~~~~~~~ | ||
| 172 | /// | ||
| 173 | inline bool DNSLookup(const std::string& hostName, std::string& ipAddress) | ||
| 174 | { | ||
| 175 | bool ret = false; | ||
| 176 | char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->dns_lookup(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, hostName.c_str(), &ret); | ||
| 177 | if (string != nullptr) | ||
| 178 | { | ||
| 179 | ipAddress = string; | ||
| 180 | ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string); | ||
| 181 | } | ||
| 182 | return ret; | ||
| 183 | } | ||
| 184 | //---------------------------------------------------------------------------- | ||
| 185 | |||
| 186 | } /* namespace network */ | ||
| 187 | } /* namespace kodi */ | ||
