3 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
26 #include "settings/lib/ISettingCallback.h"
27 #include "utils/StdString.h"
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 };
32 class NetworkAccessPoint
35 NetworkAccessPoint(const CStdString &essId, const CStdString &macAddress, int signalStrength, EncMode encryption, int channel = 0)
38 m_macAddress = macAddress;
39 m_dBm = signalStrength;
40 m_encryptionMode = encryption;
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; }
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
54 int getQuality() const;
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.
62 static int FreqToChannel(float frequency);
66 CStdString m_macAddress;
68 EncMode m_encryptionMode;
72 class CNetworkInterface
75 virtual ~CNetworkInterface() {};
77 virtual CStdString& GetName(void) = 0;
79 virtual bool IsEnabled(void) = 0;
80 virtual bool IsConnected(void) = 0;
81 virtual bool IsWireless(void) = 0;
83 virtual CStdString GetMacAddress(void) = 0;
84 virtual void GetMacAddressRaw(char rawMac[6]) = 0;
86 virtual bool GetHostMacAddress(unsigned long host, CStdString& mac) = 0;
88 virtual CStdString GetCurrentIPAddress() = 0;
89 virtual CStdString GetCurrentNetmask() = 0;
90 virtual CStdString GetCurrentDefaultGateway(void) = 0;
91 virtual CStdString GetCurrentWirelessEssId(void) = 0;
93 // Returns the list of access points in the area
94 virtual std::vector<NetworkAccessPoint> GetAccessPoints(void) = 0;
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;
112 // Return our hostname
113 virtual CStdString GetHostName(void);
115 // Return the list of interfaces
116 virtual std::vector<CNetworkInterface*>& GetInterfaceList(void) = 0;
117 CNetworkInterface* GetInterfaceByName(CStdString& name);
119 // Return the first interface which is active
120 virtual CNetworkInterface* GetFirstConnectedInterface(void);
122 // Return true if there is a interface for the same network as address
123 bool HasInterfaceForIP(unsigned long address);
125 // Return true if there's at least one defined network interface
126 bool IsAvailable(bool wait = false);
128 // Return true if there's at least one interface which is connected
129 bool IsConnected(void);
131 // Return true if the magic packet was send
132 bool WakeOnLan(const char *mac);
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;
138 // Get/set the nameserver(s)
139 virtual std::vector<CStdString> GetNameServers(void) = 0;
140 virtual void SetNameServers(std::vector<CStdString> nameServers) = 0;
142 // callback from application controlled thread to handle any setup
143 void NetworkMessage(EMESSAGE message, int param);
145 void StartServices();
146 void StopServices(bool bWait);
148 static int ParseHex(char *str, unsigned char *addr);
151 #ifdef HAS_LINUX_NETWORK
152 #include "linux/NetworkLinux.h"
154 #include "windows/NetworkWin32.h"
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);