Skip to content

Commit

Permalink
[linux/osx/ios] - retrieve the txt-records list from zeroconf announc…
Browse files Browse the repository at this point in the history
…ments for later usage (share detection)
  • Loading branch information
Memphiz committed Jun 18, 2011
1 parent eedceeb commit ec74a99
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
11 changes: 11 additions & 0 deletions xbmc/network/ZeroconfBrowser.cpp
Expand Up @@ -206,6 +206,17 @@ void CZeroconfBrowser::ZeroconfService::SetPort(int f_port)
m_port = f_port; m_port = f_port;
} }


void CZeroconfBrowser::ZeroconfService::SetTxtRecords(const tTxtRecordMap& txt_records)
{
m_txtrecords_map = txt_records;

CLog::Log(LOGDEBUG,"CZeroconfBrowser: dump txt-records");
for(tTxtRecordMap::const_iterator it = m_txtrecords_map.begin(); it != m_txtrecords_map.end(); ++it)
{
CLog::Log(LOGDEBUG,"CZeroconfBrowser: key: %s value: %s",it->first.c_str(), it->second.c_str());
}
}

CStdString CZeroconfBrowser::ZeroconfService::toPath(const ZeroconfService& fcr_service) CStdString CZeroconfBrowser::ZeroconfService::toPath(const ZeroconfService& fcr_service)
{ {
return CStdString(fcr_service.m_type + "@" + fcr_service.m_domain + "@" + fcr_service.m_name); return CStdString(fcr_service.m_type + "@" + fcr_service.m_domain + "@" + fcr_service.m_name);
Expand Down
9 changes: 9 additions & 0 deletions xbmc/network/ZeroconfBrowser.h
Expand Up @@ -24,6 +24,7 @@
#include <string> #include <string>
#include <set> #include <set>
#include <vector> #include <vector>
#include <map>
#include "URL.h" #include "URL.h"


//forwards //forwards
Expand All @@ -36,6 +37,8 @@ class CZeroconfBrowser
class ZeroconfService class ZeroconfService
{ {
public: public:
typedef std::map<std::string, std::string> tTxtRecordMap;

ZeroconfService(); ZeroconfService();
ZeroconfService(const CStdString& fcr_name, const CStdString& fcr_type, const CStdString& fcr_domain); ZeroconfService(const CStdString& fcr_name, const CStdString& fcr_type, const CStdString& fcr_domain);


Expand Down Expand Up @@ -64,6 +67,9 @@ class CZeroconfBrowser


void SetPort(int f_port); void SetPort(int f_port);
int GetPort() const {return m_port;} int GetPort() const {return m_port;}

void SetTxtRecords(const tTxtRecordMap& txt_records);
const tTxtRecordMap& GetTxtRecords() const { return m_txtrecords_map;}
///@} ///@}
private: private:
//3 entries below identify a service //3 entries below identify a service
Expand All @@ -74,6 +80,9 @@ class CZeroconfBrowser
//2 entries below store 1 ip:port pair for this service //2 entries below store 1 ip:port pair for this service
CStdString m_ip; CStdString m_ip;
int m_port; int m_port;

//1 entry below stores the txt-record as a key value map for this service
tTxtRecordMap m_txtrecords_map;
}; };


// starts browsing // starts browsing
Expand Down
29 changes: 29 additions & 0 deletions xbmc/network/linux/ZeroconfBrowserAvahi.cpp
Expand Up @@ -314,6 +314,33 @@ void CZeroconfBrowserAvahi::browseCallback (
} }
} }


CZeroconfBrowser::ZeroconfService::tTxtRecordMap GetTxtRecords(AvahiStringList *txt)
{
AvahiStringList *i = NULL;
CZeroconfBrowser::ZeroconfService::tTxtRecordMap recordMap;

for( i = txt; i; i = i->next )
{
char *key, *value;

if( avahi_string_list_get_pair( i, &key, &value, NULL ) < 0 )
continue;

recordMap.insert(
std::make_pair(
CStdString(key),
CStdString(value)
)
);

if( key )
avahi_free(key);
if( value )
avahi_free(value);
}
return recordMap;
}

void CZeroconfBrowserAvahi::resolveCallback( void CZeroconfBrowserAvahi::resolveCallback(
AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event,
const char *name, const char *type, const char *domain, const char *host_name, const char *name, const char *type, const char *domain, const char *host_name,
Expand All @@ -335,6 +362,8 @@ void CZeroconfBrowserAvahi::resolveCallback(
avahi_address_snprint ( a, sizeof ( a ), address ); avahi_address_snprint ( a, sizeof ( a ), address );
p_instance->m_resolving_service.SetIP(a); p_instance->m_resolving_service.SetIP(a);
p_instance->m_resolving_service.SetPort(port); p_instance->m_resolving_service.SetPort(port);
//get txt-record list
p_instance->m_resolving_service.SetTxtRecords(GetTxtRecords(txt));
break; break;
} }
} }
Expand Down
43 changes: 43 additions & 0 deletions xbmc/network/osx/ZeroconfBrowserOSX.cpp
Expand Up @@ -53,6 +53,47 @@ namespace
return myString; return myString;
} }


//helper for getting a the txt-records list
//returns true on success, false if nothing found or error
CZeroconfBrowser::ZeroconfService::tTxtRecordMap GetTxtRecords(CFNetServiceRef serviceRef)
{
CFIndex idx = 0;
CZeroconfBrowser::ZeroconfService::tTxtRecordMap recordMap;
CFDataRef data = NULL;

data=CFNetServiceGetTXTData(serviceRef);
if( data != NULL )
{
CFDictionaryRef dict = NULL;
dict = CFNetServiceCreateDictionaryWithTXTData(kCFAllocatorDefault, data);

if( dict != NULL )
{
CFIndex numValues = 0;
numValues = CFDictionaryGetCount(dict);
if( numValues > 0)
{
CFStringRef keys[numValues];
CFDataRef values[numValues];

CFDictionaryGetKeysAndValues(dict, (const void **)&keys, (const void **)&values);

for(idx = 0; idx < numValues; idx++)
{
recordMap.insert(
std::make_pair(
CFStringToCStdString(keys[idx]),
CStdString((const char *)CFDataGetBytePtr(values[idx]))
)
);
}
}
CFRelease(dict);
}
}
return recordMap;
}

//helper to get (first) IP and port from a resolved service //helper to get (first) IP and port from a resolved service
//returns true on success, false on if none was found //returns true on success, false on if none was found
bool CopyFirstIPv4Address(CFNetServiceRef serviceRef, CStdString& fr_address, int& fr_port) bool CopyFirstIPv4Address(CFNetServiceRef serviceRef, CStdString& fr_address, int& fr_port)
Expand Down Expand Up @@ -313,6 +354,8 @@ bool CZeroconfBrowserOSX::doResolveService(CZeroconfBrowser::ZeroconfService& fr
ret = CopyFirstIPv4Address(service, ip, port); ret = CopyFirstIPv4Address(service, ip, port);
fr_service.SetIP(ip); fr_service.SetIP(ip);
fr_service.SetPort(port); fr_service.SetPort(port);
//get txt-record list
fr_service.SetTxtRecords(GetTxtRecords(service));
} }
CFRelease(type); CFRelease(type);
CFRelease(name); CFRelease(name);
Expand Down

0 comments on commit ec74a99

Please sign in to comment.