summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h
blob: f27b710cabf97e163f5f14d4b96639b1a5051aac (plain)
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
182
183
184
185
186
187
#pragma once
/*
 *      Copyright (C) 2005-2017 Team Kodi
 *      http://kodi.tv
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with KODI; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "AddonBase.h"

/*
 * For interface between add-on and kodi.
 *
 * This structure defines the addresses of functions stored inside Kodi which
 * are then available for the add-on to call
 *
 * All function pointers there are used by the C++ interface functions below.
 * You find the set of them on xbmc/addons/interfaces/General.cpp
 *
 * Note: For add-on development itself this is not needed
 */
typedef struct AddonToKodiFuncTable_kodi_network
{
  bool (*wake_on_lan)(void* kodiBase, const char *mac);
  char* (*get_ip_address)(void* kodiBase);
  char* (*dns_lookup)(void* kodiBase, const char* url, bool* ret);
  char* (*url_encode)(void* kodiBase, const char* url);
} AddonToKodiFuncTable_kodi_network;

//==============================================================================
///
/// \defgroup cpp_kodi_network  Interface - kodi::network
/// \ingroup cpp
/// @brief **Network functions**
///
/// The network module offers functions that allow you to control it.
///
/// It has the header \ref Network.h "#include <kodi/Network.h>" be included
/// to enjoy it.
///
//------------------------------------------------------------------------------

namespace kodi
{
namespace network
{

  //============================================================================
  ///
  /// \ingroup cpp_kodi_network
  /// @brief Send WakeOnLan magic packet.
  ///
  /// @param[in] mac Network address of the host to wake.
  /// @return True if the magic packet was successfully sent, false otherwise.
  ///
  inline bool WakeOnLan(const std::string& mac)
  {
    return ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->wake_on_lan(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, mac.c_str());
  }
  //----------------------------------------------------------------------------

  //============================================================================
  ///
  /// \ingroup cpp_kodi_network
  /// @brief To the current own ip address as a string.
  ///
  /// @return Own system ip.
  ///
  ///
  /// ------------------------------------------------------------------------
  ///
  /// **Example:**
  /// ~~~~~~~~~~~~~{.cpp}
  /// #include <kodi/Network.h>
  /// ...
  /// std::string ipAddress = kodi::network::GetIPAddress();
  /// fprintf(stderr, "My IP is '%s'\n", ipAddress.c_str());
  /// ...
  /// ~~~~~~~~~~~~~
  ///
  inline std::string GetIPAddress()
  {
    std::string ip;
    char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->get_ip_address(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase);
    if (string != nullptr)
    {
      ip = string;
      ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string);
    }
    return ip;
  }
  //----------------------------------------------------------------------------

  //============================================================================
  ///
  /// \ingroup cpp_kodi_network
  /// @brief URL encodes the given string
  ///
  /// This function converts the given input string to a URL encoded string and
  /// returns that as a new allocated string. All input characters that are
  /// not a-z, A-Z, 0-9, '-', '.', '_' or '~' are converted to their "URL escaped"
  /// version (%NN where NN is a two-digit hexadecimal number).
  ///
  /// @param[in] url The code of the message to get.
  /// @return Encoded URL string
  ///
  ///
  /// ------------------------------------------------------------------------
  ///
  /// **Example:**
  /// ~~~~~~~~~~~~~{.cpp}
  /// #include <kodi/Network.h>
  /// ...
  /// std::string encodedUrl = kodi::network::URLEncode("François");
  /// fprintf(stderr, "Encoded URL is '%s'\n", encodedUrl.c_str());
  /// ...
  /// ~~~~~~~~~~~~~
  /// For example, the string: François ,would be encoded as: Fran%C3%A7ois
  ///
  inline std::string URLEncode(const std::string& url)
  {
    std::string retString;
    char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->url_encode(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, url.c_str());
    if (string != nullptr)
    {
      retString = string;
      ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string);
    }
    return retString;
  }
  //----------------------------------------------------------------------------

  //============================================================================
  ///
  /// \ingroup cpp_kodi_network
  /// @brief Lookup URL in DNS cache
  ///
  /// This test will get DNS record for a domain. The DNS lookup is done directly
  /// against the domain's authoritative name server, so changes to DNS Records
  /// should show up instantly. By default, the DNS lookup tool will return an
  /// IP address if you give it a name (e.g. www.example.com)
  ///
  /// @param[in] hostName   The code of the message to get.
  /// @param[out] ipAddress Returned address
  /// @return true if successfull
  ///
  ///
  /// ------------------------------------------------------------------------
  ///
  /// **Example:**
  /// ~~~~~~~~~~~~~{.cpp}
  /// #include <kodi/Network.h>
  /// ...
  /// std::string ipAddress;
  /// bool ret = kodi::network::DNSLookup("www.google.com", ipAddress);
  /// fprintf(stderr, "DNSLookup returned for www.google.com the IP '%s', call was %s\n", ipAddress.c_str(), ret ? "ok" : "failed");
  /// ...
  /// ~~~~~~~~~~~~~
  ///
  inline bool DNSLookup(const std::string& hostName, std::string& ipAddress)
  {
    bool ret = false;
    char* string = ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_network->dns_lookup(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, hostName.c_str(), &ret);
    if (string != nullptr)
    {
      ipAddress = string;
      ::kodi::addon::CAddonBase::m_interface->toKodi->free_string(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, string);
    }
    return ret;
  }
  //----------------------------------------------------------------------------

} /* namespace network */
} /* namespace kodi */