Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / network / Network.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
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 XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <vector>
23
24 #include "system.h"
25
26 #include "settings/lib/ISettingCallback.h"
27 #include "utils/StdString.h"
28
29 enum EncMode { ENC_NONE = 0, ENC_WEP = 1, ENC_WPA = 2, ENC_WPA2 = 3 };
30 enum NetworkAssignment { NETWORK_DASH = 0, NETWORK_DHCP = 1, NETWORK_STATIC = 2, NETWORK_DISABLED = 3 };
31
32 class NetworkAccessPoint
33 {
34 public:
35    NetworkAccessPoint(const CStdString &essId, const CStdString &macAddress, int signalStrength, EncMode encryption, int channel = 0)
36    {
37       m_essId          = essId;
38       m_macAddress     = macAddress;
39       m_dBm            = signalStrength;
40       m_encryptionMode = encryption;
41       m_channel        = channel;
42    }
43
44    const CStdString &getEssId() const { return m_essId; }
45    const CStdString &getMacAddress() const { return m_macAddress; }
46    int getSignalStrength() const { return m_dBm; }
47    EncMode getEncryptionMode() const { return m_encryptionMode; }
48    int getChannel() const { return m_channel; }
49
50    /*!
51     \brief  Returns the quality, normalized as a percentage, of the network access point
52     \return The quality as an integer between 0 and 100
53     */
54    int getQuality() const;
55
56    /*!
57     \brief  Translates a 802.11a+g frequency into the corresponding channel
58     \param  frequency  The frequency of the channel in units of Hz
59     \return The channel as an integer between 1 and 14 (802.11b+g) or
60             between 36 and 165 (802.11a), or 0 if unknown.
61     */
62    static int FreqToChannel(float frequency);
63
64 private:
65    CStdString  m_essId;
66    CStdString  m_macAddress;
67    int         m_dBm;
68    EncMode     m_encryptionMode;
69    int         m_channel;
70 };
71
72 class CNetworkInterface
73 {
74 public:
75    virtual ~CNetworkInterface() {};
76
77    virtual CStdString& GetName(void) = 0;
78
79    virtual bool IsEnabled(void) = 0;
80    virtual bool IsConnected(void) = 0;
81    virtual bool IsWireless(void) = 0;
82
83    virtual CStdString GetMacAddress(void) = 0;
84    virtual void GetMacAddressRaw(char rawMac[6]) = 0;
85
86    virtual bool GetHostMacAddress(unsigned long host, CStdString& mac) = 0;
87
88    virtual CStdString GetCurrentIPAddress() = 0;
89    virtual CStdString GetCurrentNetmask() = 0;
90    virtual CStdString GetCurrentDefaultGateway(void) = 0;
91    virtual CStdString GetCurrentWirelessEssId(void) = 0;
92
93    // Returns the list of access points in the area
94    virtual std::vector<NetworkAccessPoint> GetAccessPoints(void) = 0;
95
96    virtual void GetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode) = 0;
97    virtual void SetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode) = 0;
98 };
99
100 class CNetwork
101 {
102 public:
103   enum EMESSAGE
104   {
105     SERVICES_UP,
106     SERVICES_DOWN
107   };
108
109    CNetwork();
110    virtual ~CNetwork();
111
112    // Return our hostname
113    virtual CStdString GetHostName(void);
114
115    // Return the list of interfaces
116    virtual std::vector<CNetworkInterface*>& GetInterfaceList(void) = 0;
117    CNetworkInterface* GetInterfaceByName(CStdString& name);
118
119    // Return the first interface which is active
120    virtual CNetworkInterface* GetFirstConnectedInterface(void);
121
122    // Return true if there is a interface for the same network as address
123    bool HasInterfaceForIP(unsigned long address);
124
125    // Return true if there's at least one defined network interface
126    bool IsAvailable(bool wait = false);
127
128    // Return true if there's at least one interface which is connected
129    bool IsConnected(void);
130
131    // Return true if the magic packet was send
132    bool WakeOnLan(const char *mac);
133
134    // Return true if host replies to ping
135    bool PingHost(unsigned long host, unsigned short port, unsigned int timeout_ms = 2000, bool readability_check = false);
136    virtual bool PingHost(unsigned long host, unsigned int timeout_ms = 2000) = 0;
137
138    // Get/set the nameserver(s)
139    virtual std::vector<CStdString> GetNameServers(void) = 0;
140    virtual void SetNameServers(std::vector<CStdString> nameServers) = 0;
141
142    // callback from application controlled thread to handle any setup
143    void NetworkMessage(EMESSAGE message, int param);
144
145    void StartServices();
146    void StopServices(bool bWait);
147
148    static int ParseHex(char *str, unsigned char *addr);
149 };
150
151 #ifdef HAS_LINUX_NETWORK
152 #include "linux/NetworkLinux.h"
153 #else
154 #include "windows/NetworkWin32.h"
155 #endif
156
157 //creates, binds and listens a tcp socket on the desired port. Set bindLocal to
158 //true to bind to localhost only. The socket will listen over ipv6 if possible
159 //and fall back to ipv4 if ipv6 is not available on the platform.
160 int CreateTCPServerSocket(const int port, const bool bindLocal, const int backlog, const char *callerName);
161