summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/Network.h
blob: 73390c4915b360c70c046a9307d9e87542530c41 (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
/*
 *  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.
 */

#pragma once

#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 */