kodi
Network.h
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 <string>
12 #include <vector>
13 
14 #include "settings/lib/ISettingCallback.h"
15 
16 #include "PlatformDefs.h"
17 
19 {
20 public:
21  virtual ~CNetworkInterface() = default;
22 
23  virtual bool IsEnabled(void) const = 0;
24  virtual bool IsConnected(void) const = 0;
25 
26  virtual std::string GetMacAddress(void) const = 0;
27  virtual void GetMacAddressRaw(char rawMac[6]) const = 0;
28 
29  virtual bool GetHostMacAddress(unsigned long host, std::string& mac) const = 0;
30 
31  virtual std::string GetCurrentIPAddress() const = 0;
32  virtual std::string GetCurrentNetmask() const = 0;
33  virtual std::string GetCurrentDefaultGateway(void) const = 0;
34 };
35 
36 class CSettings;
37 class CNetworkServices;
38 struct sockaddr;
39 
41 {
42 public:
43  enum EMESSAGE
44  {
45  SERVICES_UP,
46  SERVICES_DOWN
47  };
48 
49  static std::unique_ptr<CNetworkBase> GetNetwork();
50 
51  CNetworkBase();
52  virtual ~CNetworkBase();
53 
54  // Get network services
55  CNetworkServices& GetServices() { return *m_services; }
56 
57  // Return our hostname
58  virtual bool GetHostName(std::string& hostname);
59 
60  // Return the list of interfaces
61  virtual std::vector<CNetworkInterface*>& GetInterfaceList(void) = 0;
62 
63  // Return the first interface which is active
64  virtual CNetworkInterface* GetFirstConnectedInterface(void);
65 
66  // Return true if there is a interface for the same network as address
67  bool HasInterfaceForIP(unsigned long address);
68 
69  // Return true if there's at least one defined network interface
70  bool IsAvailable(void);
71 
72  // Return true if there's at least one interface which is connected
73  bool IsConnected(void);
74 
75  // Return true if the magic packet was send
76  bool WakeOnLan(const char* mac);
77 
78  // Return true if host replies to ping
79  bool PingHost(unsigned long host,
80  unsigned short port,
81  unsigned int timeout_ms = 2000,
82  bool readability_check = false);
83  virtual bool PingHost(unsigned long host, unsigned int timeout_ms = 2000) = 0;
84 
85  // Get/set the nameserver(s)
86  virtual std::vector<std::string> GetNameServers(void) = 0;
87 
88  // callback from application controlled thread to handle any setup
89  void NetworkMessage(EMESSAGE message, int param);
90 
91  static int ParseHex(char* str, unsigned char* addr);
92 
93  // Return true if given name or ip address corresponds to localhost
94  bool IsLocalHost(const std::string& hostname);
95 
96  // Waits for the first network interface to become available
97  void WaitForNet();
98 
108  static std::string GetIpStr(const sockaddr* sa);
109 
115  static std::string GetMaskByPrefixLength(uint8_t prefixLength);
116 
117  std::unique_ptr<CNetworkServices> m_services;
118 };
119 
120 //creates, binds and listens tcp sockets on the desired port. Set bindLocal to
121 //true to bind to localhost only.
122 std::vector<SOCKET> CreateTCPServerSocket(const int port, const bool bindLocal, const int backlog, const char *callerName);
123 
Wrapper around CSettingsManager responsible for properly setting up the settings manager and register...
Definition: Settings.h:27
Definition: Network.h:18
Definition: Network.h:40
Definition: NetworkServices.h:29