Revision: 13942
http://xbmc.svn.sourceforge.net/xbmc/?rev=13942&view=rev
Author: wiso
Date: 2008-07-05 08:15:45 -0700 (Sat, 05 Jul 2008)
Log Message:
-----------
[win32]: added basic network functions. It's still not finalized and you can't change network parameters yet. Once finalized we may remove all the ifdefs. Hopefully this won't break other things ;)
Modified Paths:
--------------
branches/linuxport/XBMC/xbmc/Application.cpp
branches/linuxport/XBMC/xbmc/Application.h
branches/linuxport/XBMC/xbmc/GUIWindowSettingsCategory.cpp
branches/linuxport/XBMC/xbmc/UPnP.cpp
branches/linuxport/XBMC/xbmc/Util.cpp
branches/linuxport/XBMC/xbmc/cores/DllLoader/exports/emu_socket.cpp
branches/linuxport/XBMC/xbmc/utils/GUIInfoManager.cpp
branches/linuxport/XBMC/xbmc/utils/Network.h
Added Paths:
-----------
branches/linuxport/XBMC/xbmc/win32/NetworkWin32.cpp
branches/linuxport/XBMC/xbmc/win32/NetworkWin32.h
Modified: branches/linuxport/XBMC/xbmc/Application.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/Application.cpp 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/Application.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -1468,7 +1468,7 @@
}
#endif
CSectionLoader::Load("LIBHTTP");
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
if (m_network.GetFirstConnectedInterface())
{
m_pWebServer = new CWebServer();
@@ -5727,11 +5727,16 @@
return m_applicationMessenger;
}
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK)
CNetworkLinux& CApplication::getNetwork()
{
return m_network;
}
+#elif defined(HAS_WIN32_NETWORK)
+CNetworkWin32& CApplication::getNetwork()
+{
+ return m_network;
+}
#else
CNetwork& CApplication::getNetwork()
{
Modified: branches/linuxport/XBMC/xbmc/Application.h
===================================================================
--- branches/linuxport/XBMC/xbmc/Application.h 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/Application.h 2008-07-05 15:15:45 UTC (rev 13942)
@@ -168,8 +168,10 @@
void CheckMusicPlaylist();
CApplicationMessenger& getApplicationMessenger();
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK)
CNetworkLinux& getNetwork();
+#elif defined(HAS_WIN32_NETWORK)
+ CNetworkWin32& getNetwork();
#else
CNetwork& getNetwork();
#endif
@@ -299,8 +301,10 @@
CProfile* InitDirectoriesWin32();
CApplicationMessenger m_applicationMessenger;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK)
CNetworkLinux m_network;
+#elif defined(HAS_WIN32_NETWORK)
+ CNetworkWin32 m_network;
#else
CNetwork m_network;
#endif
Modified: branches/linuxport/XBMC/xbmc/GUIWindowSettingsCategory.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/GUIWindowSettingsCategory.cpp 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/GUIWindowSettingsCategory.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -1086,12 +1086,14 @@
CGUIControl *pControl = (CGUIControl *)GetControl(pSettingControl->GetID());
if (pControl) pControl->SetEnabled(enabled);
}
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
else if (strSetting.Equals("network.assignment"))
{
CGUISpinControlEx* pControl1 = (CGUISpinControlEx *)GetControl(GetSetting("network.assignment")->GetID());
- if (pControl1)
+#ifdef HAS_LINUX_NETWORK
+ if (pControl1)
pControl1->SetEnabled(geteuid() == 0);
+#endif
}
else if (strSetting.Equals("network.essid") || strSetting.Equals("network.enc") || strSetting.Equals("network.key"))
{
@@ -1101,7 +1103,11 @@
CNetworkInterface* iface = g_application.getNetwork().GetInterfaceByName(ifaceName);
bool bIsWireless = iface->IsWireless();
+#ifdef HAS_LINUX_NETWORK
bool enabled = bIsWireless && (geteuid() == 0);
+#else
+ bool enabled = bIsWireless;
+#endif
CGUISpinControlEx* pControl1 = (CGUISpinControlEx *)GetControl(GetSetting("network.assignment")->GetID());
if (pControl1)
enabled &= (pControl1->GetValue() != NETWORK_DISABLED);
@@ -3455,7 +3461,7 @@
CGUISpinControlEx *pControl = (CGUISpinControlEx *)GetControl(GetSetting(pSetting->GetSetting())->GetID());
pControl->Clear();
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
// query list of interfaces
vector<CStdString> vecInterfaces;
std::vector<CNetworkInterface*>& ifaces = g_application.getNetwork().GetInterfaceList();
Modified: branches/linuxport/XBMC/xbmc/UPnP.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/UPnP.cpp 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/UPnP.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -1334,7 +1334,7 @@
{
if (!m_ServerHolder->m_Device.IsNull()) return;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
NPT_String ip;
if (g_application.getNetwork().GetFirstConnectedInterface())
{
@@ -1413,7 +1413,7 @@
CUtil::AddFileToFolder(g_settings.GetUserDataFolder(), "upnpserver.xml", filename);
g_settings.LoadUPnPXml(filename);
-#ifdef _LINUX
+#if defined(_LINUX) || defined(HAS_WIN32_NETWORK)
NPT_String ip;
if (g_application.getNetwork().GetFirstConnectedInterface())
{
Modified: branches/linuxport/XBMC/xbmc/Util.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/Util.cpp 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/Util.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -870,7 +870,7 @@
else
{
// check if we are on the local subnet
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
unsigned long subnet = ntohl(inet_addr(g_application.getNetwork().GetFirstConnectedInterface()->GetCurrentNetmask()));
unsigned long local = ntohl(inet_addr(g_application.getNetwork().GetFirstConnectedInterface()->GetCurrentIPAddress()));
#else
Modified: branches/linuxport/XBMC/xbmc/cores/DllLoader/exports/emu_socket.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/cores/DllLoader/exports/emu_socket.cpp 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/cores/DllLoader/exports/emu_socket.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -23,9 +23,14 @@
#include "../DllLoaderContainer.h"
#include "DNSNameCache.h"
-#include "xbox\Network.h"
#include "emu_dummy.h"
#include "emu_socket.h"
+#ifdef _WIN32PC
+#include "Application.h"
+#include "utils\Network.h"
+#else
+#include "xbox\Network.h"
+#endif
using namespace std;
@@ -248,9 +253,10 @@
address->sin_addr.S_un.S_un_b.s_b2,
address->sin_addr.S_un.S_un_b.s_b3,
address->sin_addr.S_un.S_un_b.s_b4);
+
+ g_application.getNetwork().GetFirstConnectedInterface();
-
- if( address->sin_addr.S_un.S_addr == inet_addr(g_network.m_networkinfo.ip)
+ if( address->sin_addr.S_un.S_addr == inet_addr(g_application.getNetwork().GetFirstConnectedInterface()->GetCurrentIPAddress().c_str())
|| address->sin_addr.S_un.S_addr == inet_addr("127.0.0.1") )
{
// local xbox, correct for xbox stack
@@ -478,8 +484,11 @@
// unspecifed address will always be on local xbox ip
// some dll's assume this will return a proper address
// even if windows standard doesn't gurantee it
- if( g_network.IsAvailable() )
- addr->sin_addr.S_un.S_addr = inet_addr(g_network.m_networkinfo.ip);
+ if( g_application.getNetwork().IsAvailable() )
+ {
+ g_application.getNetwork().GetFirstConnectedInterface();
+ addr->sin_addr.S_un.S_addr = inet_addr(g_application.getNetwork().GetFirstConnectedInterface()->GetCurrentIPAddress().c_str());
+ }
}
}
return res;
Modified: branches/linuxport/XBMC/xbmc/utils/GUIInfoManager.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/utils/GUIInfoManager.cpp 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/utils/GUIInfoManager.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -1224,7 +1224,7 @@
case NETWORK_IP_ADDRESS:
{
CStdString ip;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
if (iface)
ip.Format("%s: %s", g_localizeStrings.Get(150).c_str(), iface->GetCurrentIPAddress().c_str());
@@ -1237,7 +1237,7 @@
case NETWORK_SUBNET_ADDRESS:
{
CStdString subnet;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
if (iface)
subnet.Format("%s: %s", g_localizeStrings.Get(13159), iface->GetCurrentNetmask().c_str());
@@ -1250,7 +1250,7 @@
case NETWORK_GATEWAY_ADDRESS:
{
CStdString gateway;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
if (iface)
gateway.Format("%s: %s", g_localizeStrings.Get(13160), iface->GetCurrentDefaultGateway().c_str());
@@ -1263,7 +1263,7 @@
case NETWORK_DNS1_ADDRESS:
{
CStdString dns;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
std::vector<CStdString> nss = g_application.getNetwork().GetNameServers();
if (nss.size() >= 1)
dns.Format("%s: %s", g_localizeStrings.Get(13161), nss[0].c_str());
@@ -1276,7 +1276,7 @@
case NETWORK_DNS2_ADDRESS:
{
CStdString dns;
-#ifdef HAS_LINUX_NETWORK
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
std::vector<CStdString> nss = g_application.getNetwork().GetNameServers();
if (nss.size() >= 2)
dns.Format("%s: %s", g_localizeStrings.Get(20307), nss[1].c_str());
@@ -1296,7 +1296,7 @@
{
CStdString linkStatus = g_localizeStrings.Get(151);
linkStatus += " ";
-#if defined(HAS_LINUX_NETWORK)
+#if defined(HAS_LINUX_NETWORK) || defined(HAS_WIN32_NETWORK)
CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
if (iface && iface->IsConnected())
linkStatus += g_localizeStrings.Get(15207);
Modified: branches/linuxport/XBMC/xbmc/utils/Network.h
===================================================================
--- branches/linuxport/XBMC/xbmc/utils/Network.h 2008-07-05 13:59:28 UTC (rev 13941)
+++ branches/linuxport/XBMC/xbmc/utils/Network.h 2008-07-05 15:15:45 UTC (rev 13942)
@@ -74,8 +74,8 @@
virtual void SetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode) = 0;
};
-#ifdef HAS_LINUX_NETWORK
+
class CNetwork
{
public:
@@ -110,9 +110,9 @@
static int ParseHex(char *str, unsigned char *addr);
};
-
+#ifdef HAS_LINUX_NETWORK
#include "linux/NetworkLinux.h"
#else
-#include "xbox/Network.h"
+#include "win32/NetworkWin32.h"
#endif
#endif
Added: branches/linuxport/XBMC/xbmc/win32/NetworkWin32.cpp
===================================================================
--- branches/linuxport/XBMC/xbmc/win32/NetworkWin32.cpp (rev 0)
+++ branches/linuxport/XBMC/xbmc/win32/NetworkWin32.cpp 2008-07-05 15:15:45 UTC (rev 13942)
@@ -0,0 +1,641 @@
+/*
+ * Copyright (C) 2005-2008 Team XBMC
+ * http://www.xbmc.org
+ *
+ * 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 XBMC; see the file COPYING. If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include "stdafx.h"
+#include <errno.h>
+#include "PlatformDefs.h"
+#include "NetworkWin32.h"
+#include "Util.h"
+#include "log.h"
+
+using namespace std;
+
+CNetworkInterfaceWin32::CNetworkInterfaceWin32(CNetworkWin32* network, IP_ADAPTER_INFO adapter)
+
+{
+ m_network = network;
+ m_adapter = adapter;
+ m_adaptername = adapter.Description;
+}
+
+CNetworkInterfaceWin32::~CNetworkInterfaceWin32(void)
+{
+}
+
+CStdString& CNetworkInterfaceWin32::GetName(void)
+{
+ return m_adaptername;
+}
+
+bool CNetworkInterfaceWin32::IsWireless()
+{
+ return (m_adapter.Type == IF_TYPE_IEEE80211);
+}
+
+bool CNetworkInterfaceWin32::IsEnabled()
+{
+#ifdef _LINUX
+ struct ifreq ifr;
+ strcpy(ifr.ifr_name, m_interfaceName.c_str());
+ if (ioctl(m_network->GetSocket(), SIOCGIFFLAGS, &ifr) < 0)
+ return false;
+
+ return ((ifr.ifr_flags & IFF_UP) == IFF_UP);
+#else
+ return true;
+#endif
+}
+
+bool CNetworkInterfaceWin32::IsConnected()
+{
+ CStdString strIP = m_adapter.IpAddressList.IpAddress.String;
+ return (strIP != "0.0.0.0");
+}
+
+CStdString CNetworkInterfaceWin32::GetMacAddress()
+{
+ CStdString result = "";
+ result = CStdString((char*)m_adapter.Address);
+ return result;
+}
+
+CStdString CNetworkInterfaceWin32::GetCurrentIPAddress(void)
+{
+ return m_adapter.IpAddressList.IpAddress.String;
+}
+
+CStdString CNetworkInterfaceWin32::GetCurrentNetmask(void)
+{
+ return m_adapter.IpAddressList.IpMask.String;
+}
+
+CStdString CNetworkInterfaceWin32::GetCurrentWirelessEssId(void)
+{
+ CStdString result = "";
+
+#ifdef _LINUX
+ char essid[IW_ESSID_MAX_SIZE + 1];
+ memset(&essid, 0, sizeof(essid));
+
+ struct iwreq wrq;
+ strcpy(wrq.ifr_name, m_interfaceName.c_str());
+ wrq.u.essid.pointer = (caddr_t) essid;
+ wrq.u.essid.length = IW_ESSID_MAX_SIZE;
+ wrq.u.essid.flags = 0;
+ if (ioctl(m_network->GetSocket(), SIOCGIWESSID, &wrq) >= 0)
+ {
+ result = essid;
+ }
+#endif
+
+ return result;
+}
+
+CStdString CNetworkInterfaceWin32::GetCurrentDefaultGateway(void)
+{
+ return m_adapter.GatewayList.IpAddress.String;
+}
+
+CNetworkWin32::CNetworkWin32(void)
+{
+ queryInterfaceList();
+}
+
+CNetworkWin32::~CNetworkWin32(void)
+{
+ vector<CNetworkInterface*>::iterator it = m_interfaces.begin();
+ while(it != m_interfaces.end())
+ {
+ CNetworkInterface* nInt = *it;
+ delete nInt;
+ it = m_interfaces.erase(it);
+ }
+}
+
+std::vector<CNetworkInterface*>& CNetworkWin32::GetInterfaceList(void)
+{
+ return m_interfaces;
+}
+
+void CNetworkWin32::queryInterfaceList()
+{
+ m_interfaces.clear();
+
+ PIP_ADAPTER_INFO adapterInfo;
+ PIP_ADAPTER_INFO adapter = NULL;
+
+ ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
+
+ adapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO));
+ if (adapterInfo == NULL)
+ return;
+
+ if (GetAdaptersInfo(adapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
+ {
+ free(adapterInfo);
+ adapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen);
+ if (adapterInfo == NULL)
+ {
+ OutputDebugString("Error allocating memory needed to call GetAdaptersinfo\n");
+ return;
+ }
+ }
+
+ if ((GetAdaptersInfo(adapterInfo, &ulOutBufLen)) == NO_ERROR)
+ {
+ adapter = adapterInfo;
+ while (adapter)
+ {
+ m_interfaces.push_back(new CNetworkInterfaceWin32(this, *adapter));
+
+ adapter = adapter->Next;
+ }
+ }
+
+ free(adapterInfo);
+}
+
+std::vector<CStdString> CNetworkWin32::GetNameServers(void)
+{
+ std::vector<CStdString> result;
+
+ FIXED_INFO *pFixedInfo;
+ ULONG ulOutBufLen;
+ IP_ADDR_STRING *pIPAddr;
+
+ pFixedInfo = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
+ if (pFixedInfo == NULL)
+ {
+ OutputDebugString("Error allocating memory needed to call GetNetworkParams\n");
+ return result;
+ }
+ ulOutBufLen = sizeof (FIXED_INFO);
+ if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
+ {
+ free(pFixedInfo);
+ pFixedInfo = (FIXED_INFO *) malloc(ulOutBufLen);
+ if (pFixedInfo == NULL)
+ {
+ OutputDebugString("Error allocating memory needed to call GetNetworkParams\n");
+ return result;
+ }
+ }
+
+ if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == NO_ERROR)
+ {
+ result.push_back(pFixedInfo->DnsServerList.IpAddress.String);
+ pIPAddr = pFixedInfo->DnsServerList.Next;
+ while(pIPAddr)
+ {
+ result.push_back(pIPAddr->IpAddress.String);
+ pIPAddr = pIPAddr->Next;
+ }
+
+ }
+ free(pFixedInfo);
+
+ return result;
+}
+
+void CNetworkWin32::SetNameServers(std::vector<CStdString> nameServers)
+{
+ FILE* fp = fopen("/etc/resolv.conf", "w");
+ if (fp != NULL)
+ {
+ for (unsigned int i = 0; i < nameServers.size(); i++)
+ {
+ fprintf(fp, "nameserver %s\n", nameServers[i].c_str());
+ }
+ fclose(fp);
+ }
+ else
+ {
+ // TODO:
+ }
+}
+
+std::vector<NetworkAccessPoint> CNetworkInterfaceWin32::GetAccessPoints(void)
+{
+ std::vector<NetworkAccessPoint> result;
+
+ if (!IsWireless())
+ return result;
+
+#ifdef _LINUX
+ // Query the wireless extentsions version number. It will help us when we
+ // parse the resulting events
+ struct iwreq iwr;
+ char rangebuffer[sizeof(iw_range) * 2]; /* Large enough */
+ struct iw_range* range = (struct iw_range*) rangebuffer;
+
+ memset(rangebuffer, 0, sizeof(rangebuffer));
+ iwr.u.data.pointer = (caddr_t) rangebuffer;
+ iwr.u.data.length = sizeof(rangebuffer);
+ iwr.u.data.flags = 0;
+ strncpy(iwr.ifr_name, GetName().c_str(), IFNAMSIZ);
+ if (ioctl(m_network->GetSocket(), SIOCGIWRANGE, &iwr) < 0)
+ {
+ CLog::Log(LOGWARNING, "%-8.16s Driver has no Wireless Extension version information.",
+ GetName().c_str());
+ return result;
+ }
+
+ // Scan for wireless access points
+ memset(&iwr, 0, sizeof(iwr));
+ strncpy(iwr.ifr_name, GetName().c_str(), IFNAMSIZ);
+ if (ioctl(m_network->GetSocket(), SIOCSIWSCAN, &iwr) < 0)
+ {
+ CLog::Log(LOGWARNING, "Cannot initiate wireless scan: ioctl[SIOCSIWSCAN]: %s", strerror(errno));
+ return result;
+ }
+
+ // Get the results of the scanning. Three scenarios:
+ // 1. There's not enough room in the result buffer (E2BIG)
+ // 2. The scanning is not complete (EAGAIN) and we need to try again. We cap this with 15 seconds.
+ // 3. Were'e good.
+ int duration = 0; // ms
+ unsigned char* res_buf = NULL;
+ int res_buf_len = IW_SCAN_MAX_DATA;
+ while (duration < 15000)
+ {
+ if (!res_buf)
+ res_buf = (unsigned char*) malloc(res_buf_len);
+
+ if (res_buf == NULL)
+ {
+ CLog::Log(LOGWARNING, "Cannot alloc memory for wireless scanning");
+ return result;
+ }
+
+ strncpy(iwr.ifr_name, GetName().c_str(), IFNAMSIZ);
+ iwr.u.data.pointer = res_buf;
+ iwr.u.data.length = res_buf_len;
+ iwr.u.data.flags = 0;
+ int x = ioctl(m_network->GetSocket(), SIOCGIWSCAN, &iwr);
+ if (x == 0)
+ break;
+
+ if (errno == E2BIG && res_buf_len < 100000)
+ {
+ free(res_buf);
+ res_buf = NULL;
+ res_buf_len *= 2;
+ CLog::Log(LOGDEBUG, "Scan results did not fit - trying larger buffer (%lu bytes)",
+ (unsigned long) res_buf_len);
+ }
+ else if (errno == EAGAIN)
+ {
+ usleep(250000); // sleep for 250ms
+ duration += 250;
+ }
+ else
+ {
+ CLog::Log(LOGWARNING, "Cannot get wireless scan results: ioctl[SIOCGIWSCAN]: %s", strerror(errno));
+ free(res_buf);
+ return result;
+ }
+ }
+
+ size_t len = iwr.u.data.length;
+ char* pos = (char *) res_buf;
+ char* end = (char *) res_buf + len;
+ char* custom;
+ struct iw_event iwe_buf, *iwe = &iwe_buf;
+
+ CStdString essId;
+ int quality = 0;
+ EncMode encryption = ENC_NONE;
+ bool first = true;
+
+ while (pos + IW_EV_LCP_LEN <= end)
+ {
+ /* Event data may be unaligned, so make a local, aligned copy
+ * before processing. */
+ memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
+ if (iwe->len <= IW_EV_LCP_LEN)
+ break;
+
+ custom = pos + IW_EV_POINT_LEN;
+ if (range->we_version_compiled > 18 &&
+ (iwe->cmd == SIOCGIWESSID ||
+ iwe->cmd == SIOCGIWENCODE ||
+ iwe->cmd == IWEVGENIE ||
+ iwe->cmd == IWEVCUSTOM))
+ {
+ /* Wireless extentsions v19 removed the pointer from struct iw_point */
+ char *dpos = (char *) &iwe_buf.u.data.length;
+ int dlen = dpos - (char *) &iwe_buf;
+ memcpy(dpos, pos + IW_EV_LCP_LEN, sizeof(struct iw_event) - dlen);
+ }
+ else
+ {
+ memcpy(&iwe_buf, pos, sizeof(struct iw_event));
+ custom += IW_EV_POINT_OFF;
+ }
+
+ switch (iwe->cmd)
+ {
+ case SIOCGIWAP:
+ if (first)
+ first = false;
+ else
+ result.push_back(NetworkAccessPoint(essId, quality, encryption));
+ encryption = ENC_NONE;
+ break;
+
+ case SIOCGIWESSID:
+ {
+ char essid[IW_ESSID_MAX_SIZE+1];
+ memset(essid, '\0', sizeof(essid));
+ if ((custom) && (iwe->u.essid.length))
+ {
+ memcpy(essid, custom, iwe->u.essid.length);
+ essId = essid;
+ }
+ break;
+ }
+
+ case IWEVQUAL:
+ quality = iwe->u.qual.qual;
+ break;
+
+ case SIOCGIWENCODE:
+ if (!(iwe->u.data.flags & IW_ENCODE_DISABLED) && encryption == ENC_NONE)
+ encryption = ENC_WEP;
+ break;
+
+ case IWEVGENIE:
+ {
+ int offset = 0;
+ while (offset <= iwe_buf.u.data.length)
+ {
+ switch ((unsigned char)custom[offset])
+ {
+ case 0xdd: /* WPA1 */
+ if (encryption != ENC_WPA2)
+ encryption = ENC_WPA;
+ break;
+ case 0x30: /* WPA2 */
+ encryption = ENC_WPA2;
+ }
+
+ offset += custom[offset+1] + 2;
+ }
+ }
+ }
+
+ pos += iwe->len;
+ }
+
+ if (!first)
+ result.push_back(NetworkAccessPoint(essId, quality, encryption));
+
+ free(res_buf);
+ res_buf = NULL;
+#endif
+
+ return result;
+}
+
+void CNetworkInterfaceWin32::GetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode)
+{
+ ipAddress = "0.0.0.0";
+ networkMask = "0.0.0.0";
+ defaultGateway = "0.0.0.0";
+ essId = "";
+ key = "";
+ encryptionMode = ENC_NONE;
+ assignment = NETWORK_DISABLED;
+
+#ifdef _LINUX
+ FILE* fp = fopen("/etc/network/interfaces", "r");
+ if (!fp)
+ {
+ // TODO
+ return;
+ }
+
+ char* line = NULL;
+ size_t linel = 0;
+ CStdString s;
+ bool foundInterface = false;
+
+ while (getdelim(&line, &linel, '\n', fp) > 0)
+ {
+ vector<CStdString> tokens;
+
+ s = line;
+ s.TrimLeft(" \t").TrimRight(" \n");
+
+ // skip comments
+ if (s.length() == 0 || s.GetAt(0) == '#')
+ continue;
+
+ // look for "iface <interface name> inet"
+ CUtil::Tokenize(s, tokens, " ");
+ if (!foundInterface &&
+ tokens.size() >=3 &&
+ tokens[0].Equals("iface") &&
+ tokens[1].Equals(GetName()) &&
+ tokens[2].Equals("inet"))
+ {
+ if (tokens[3].Equals("dhcp"))
+ {
+ assignment = NETWORK_DHCP;
+ foundInterface = true;
+ }
+ if (tokens[3].Equals("static"))
+ {
+ assignment = NETWORK_STATIC;
+ foundInterface = true;
+ }
+ }
+
+ if (foundInterface && tokens.size() == 2)
+ {
+ if (tokens[0].Equals("address")) ipAddress = tokens[1];
+ else if (tokens[0].Equals("netmask")) networkMask = tokens[1];
+ else if (tokens[0].Equals("gateway")) defaultGateway = tokens[1];
+ else if (tokens[0].Equals("wireless-essid")) essId = tokens[1];
+ else if (tokens[0].Equals("wireless-key"))
+ {
+ key = tokens[1];
+ if (key.length() > 2 && key[0] == 's' && key[1] == ':')
+ key.erase(0, 2);
+ encryptionMode = ENC_WEP;
+ }
+ else if (tokens[0].Equals("wpa-ssid")) essId = tokens[1];
+ else if (tokens[0].Equals("wpa-proto") && tokens[1].Equals("WPA")) encryptionMode = ENC_WPA;
+ else if (tokens[0].Equals("wpa-proto") && tokens[1].Equals("WPA2")) encryptionMode = ENC_WPA2;
+ else if (tokens[0].Equals("wpa-psk")) key = tokens[1];
+ else if (tokens[0].Equals("auto") || tokens[0].Equals("iface") || tokens[0].Equals("mapping")) break;
+ }
+ }
+
+ if (line)
+ free(line);
+
+ // Fallback in case wpa-proto is not set
+ if (key != "" && encryptionMode == ENC_NONE)
+ encryptionMode = ENC_WPA;
+
+ fclose(fp);
+#endif
+}
+
+void CNetworkInterfaceWin32::SetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode)
+{
+#ifdef _LINUX
+ FILE* fr = fopen("/etc/network/interfaces", "r");
+ if (!fr)
+ {
+ // TODO
+ return;
+ }
+
+ FILE* fw = fopen("/tmp/interfaces.temp", "w");
+ if (!fw)
+ {
+ // TODO
+ return;
+ }
+
+ char* line = NULL;
+ size_t linel = 0;
+ CStdString s;
+ bool foundInterface = false;
+ bool dataWritten = false;
+
+ while (getdelim(&line, &linel, '\n', fr) > 0)
+ {
+ vector<CStdString> tokens;
+
+ s = line;
+ s.TrimLeft(" \t").TrimRight(" \n");
+
+ // skip comments
+ if (!foundInterface && (s.length() == 0 || s.GetAt(0) == '#'))
+ {
+ fprintf(fw, "%s", line);
+ continue;
+ }
+
+ // look for "iface <interface name> inet"
+ CUtil::Tokenize(s, tokens, " ");
+ if (tokens.size() == 2 &&
+ tokens[0].Equals("auto") &&
+ tokens[1].Equals(GetName()))
+ {
+ continue;
+ }
+ else if (!foundInterface &&
+ tokens.size() == 4 &&
+ tokens[0].Equals("iface") &&
+ tokens[1].Equals(GetName()) &&
+ tokens[2].Equals("inet"))
+ {
+ foundInterface = true;
+ WriteSettings(fw, assignment, ipAddress, networkMask, defaultGateway, essId, key, encryptionMode);
+ dataWritten = true;
+ }
+ else if (foundInterface &&
+ tokens.size() == 4 &&
+ tokens[0].Equals("iface"))
+ {
+ foundInterface = false;
+ fprintf(fw, "%s", line);
+ }
+ else if (!foundInterface)
+ {
+ fprintf(fw, "%s", line);
+ }
+ }
+
+ if (line)
+ free(line);
+
+ if (!dataWritten && assignment != NETWORK_DISABLED)
+ {
+ fprintf(fw, "\n");
+ WriteSettings(fw, assignment, ipAddress, networkMask, defaultGateway, essId, key, encryptionMode);
+ }
+
+ fclose(fr);
+ fclose(fw);
+
+ // Rename the file
+ if (rename("/tmp/interfaces.temp", "/etc/network/interfaces") < 0)
+ {
+ // TODO
+ return;
+ }
+
+ CLog::Log(LOGINFO, "Stopping interface %s", GetName().c_str());
+ std::string cmd = "/sbin/ifdown " + GetName();
+ system(cmd.c_str());
+
+ if (assignment != NETWORK_DISABLED)
+ {
+ CLog::Log(LOGINFO, "Starting interface %s", GetName().c_str());
+ cmd = "/sbin/ifup " + GetName();
+ system(cmd.c_str());
+ }
+#endif
+}
+
+void CNetworkInterfaceWin32::WriteSettings(FILE* fw, NetworkAssignment assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode)
+{
+#ifdef _LINUX
+ if (assignment == NETWORK_DHCP)
+ {
+ fprintf(fw, "iface %s inet dhcp\n", GetName().c_str());
+ }
+ else if (assignment == NETWORK_STATIC)
+ {
+ fprintf(fw, "iface %s inet static\n", GetName().c_str());
+ fprintf(fw, " address %s\n", ipAddress.c_str());
+ fprintf(fw, " netmask %s\n", networkMask.c_str());
+ fprintf(fw, " gateway %s\n", defaultGateway.c_str());
+ }
+
+ if (assignment != NETWORK_DISABLED && IsWireless())
+ {
+ if (encryptionMode == ENC_NONE)
+ {
+ fprintf(fw, " wireless-essid %s\n", essId.c_str());
+ }
+ else if (encryptionMode == ENC_WEP)
+ {
+ fprintf(fw, " wireless-essid %s\n", essId.c_str());
+ fprintf(fw, " wireless-key s:%s\n", key.c_str());
+ }
+ else if (encryptionMode == ENC_WPA || encryptionMode == ENC_WPA2)
+ {
+ fprintf(fw, " wpa-ssid %s\n", essId.c_str());
+ fprintf(fw, " wpa-psk %s\n", key.c_str());
+ fprintf(fw, " wpa-proto %s\n", encryptionMode == ENC_WPA ? "WPA" : "WPA2");
+ }
+ }
+
+ if (assignment != NETWORK_DISABLED)
+ fprintf(fw, "auto %s\n\n", GetName().c_str());
+#endif
+}
\ No newline at end of file
Added: branches/linuxport/XBMC/xbmc/win32/NetworkWin32.h
===================================================================
--- branches/linuxport/XBMC/xbmc/win32/NetworkWin32.h (rev 0)
+++ branches/linuxport/XBMC/xbmc/win32/NetworkWin32.h 2008-07-05 15:15:45 UTC (rev 13942)
@@ -0,0 +1,86 @@
+#ifndef NETWORK_LINUX_H_
+#define NETWORK_LINUX_H_
+
+/*
+ * Copyright (C) 2005-2008 Team XBMC
+ * http://www.xbmc.org
+ *
+ * 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 XBMC; see the file COPYING. If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include <vector>
+#include "StdString.h"
+#include "utils/Network.h"
+#include "Iphlpapi.h"
+
+class CNetworkWin32;
+
+class CNetworkInterfaceWin32 : public CNetworkInterface
+{
+public:
+ CNetworkInterfaceWin32(CNetworkWin32* network, IP_ADAPTER_INFO adapter);
+ ~CNetworkInterfaceWin32(void);
+
+ virtual CStdString& GetName(void);
+
+ virtual bool IsEnabled(void);
+ virtual bool IsConnected(void);
+ virtual bool IsWireless(void);
+
+ virtual CStdString GetMacAddress(void);
+
+ virtual CStdString GetCurrentIPAddress();
+ virtual CStdString GetCurrentNetmask();
+ virtual CStdString GetCurrentDefaultGateway(void);
+ virtual CStdString GetCurrentWirelessEssId(void);
+
+ virtual void GetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode);
+ virtual void SetSettings(NetworkAssignment& assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode);
+
+ // Returns the list of access points in the area
+ virtual std::vector<NetworkAccessPoint> GetAccessPoints(void);
+
+private:
+ void WriteSettings(FILE* fw, NetworkAssignment assignment, CStdString& ipAddress, CStdString& networkMask, CStdString& defaultGateway, CStdString& essId, CStdString& key, EncMode& encryptionMode);
+ IP_ADAPTER_INFO m_adapter;
+ CNetworkWin32* m_network;
+ CStdString m_adaptername;
+};
+
+class CNetworkWin32 : public CNetwork
+{
+public:
+ CNetworkWin32(void);
+ virtual ~CNetworkWin32(void);
+
+ // Return the list of interfaces
+ virtual std::vector<CNetworkInterface*>& GetInterfaceList(void);
+
+ // Get/set the nameserver(s)
+ virtual std::vector<CStdString> GetNameServers(void);
+ virtual void SetNameServers(std::vector<CStdString> nameServers);
+
+ friend class CNetworkInterfaceWin32;
+
+private:
+ int GetSocket() { return m_sock; }
+ void queryInterfaceList();
+ std::vector<CNetworkInterface*> m_interfaces;
+ int m_sock;
+};
+
+#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|