24 #include <gui_utils/service_model.h> 25 #include <netcomm/dns-sd/avahi_thread.h> 26 #include <utils/misc/string_conversions.h> 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <arpa/inet.h> 88 ServiceModel::ServiceModel(
const char* service)
90 m_service_list = Gtk::ListStore::create(m_service_record);
93 m_avahi->watch_service(service,
this);
96 m_own_avahi_thread =
true;
98 m_signal_service_added.connect( sigc::mem_fun(*
this, &ServiceModel::on_service_added) );
99 m_signal_service_removed.connect( sigc::mem_fun(*
this, &ServiceModel::on_service_removed) );
108 m_service_list = Gtk::ListStore::create(m_service_record);
110 m_avahi = avahi_thread;
111 m_own_avahi_thread =
false;
115 ServiceModel::~ServiceModel()
117 if (m_own_avahi_thread)
128 Glib::RefPtr<Gtk::ListStore>&
129 ServiceModel::get_list_store()
131 return m_service_list;
138 ServiceModel::get_column_record()
140 return m_service_record;
144 ServiceModel::all_for_now()
149 ServiceModel::cache_exhausted()
154 ServiceModel::browse_failed(
const char* name,
161 ServiceModel::service_added(
const char* name,
const char* type,
162 const char* domain,
const char* host_name,
const char *interface,
163 const struct sockaddr* addr,
const socklen_t addr_size,
164 uint16_t port, std::list<std::string>& txt,
int flags )
167 if (addr->sa_family == AF_INET) {
168 char ipaddr[INET_ADDRSTRLEN];
169 struct sockaddr_in *saddr = (
struct sockaddr_in *)addr;
170 if (inet_ntop(AF_INET, &(saddr->sin_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
172 s.
addrport = std::string(ipaddr) +
":" + StringConversions::to_string(port);
175 s.
addrport = std::string(
"Failed to convert IPv4: ") + strerror(errno);
177 }
else if (addr->sa_family == AF_INET6) {
178 char ipaddr[INET6_ADDRSTRLEN];
179 struct sockaddr_in6 *saddr = (
struct sockaddr_in6 *)addr;
180 if (inet_ntop(AF_INET6, &(saddr->sin6_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
182 s.
addrport = std::string(
"[") + ipaddr +
"%" +
interface + "]:" + StringConversions::to_string(port);
185 s.
addrport = std::string(
"Failed to convert IPv6: ") + strerror(errno);
189 s.
addrport =
"Unknown address family";
198 memcpy(&s.
sockaddr, addr, addr_size);
200 m_added_services.push_locked(s);
202 m_signal_service_added();
206 ServiceModel::service_removed(
const char* name,
const char* type,
const char* domain)
209 s.
name = string(name);
210 s.
type = string(type);
211 s.
domain = string(domain);
213 m_removed_services.push_locked(s);
215 m_signal_service_removed();
220 ServiceModel::on_service_added()
222 m_added_services.lock();
224 while ( !m_added_services.empty() )
228 Gtk::TreeModel::Row row = *m_service_list->append();
230 row[m_service_record.name] = s.
name;
231 row[m_service_record.type] = s.
type;
232 row[m_service_record.domain] = s.
domain;
233 row[m_service_record.hostname] = s.
hostname;
234 row[m_service_record.interface] = s.
interface;
235 row[m_service_record.ipaddr] = s.
ipaddr;
236 row[m_service_record.port] = s.
port;
237 row[m_service_record.addrport] = s.
addrport;
238 row[m_service_record.sockaddr] = s.
sockaddr;
240 m_added_services.pop();
243 m_added_services.unlock();
248 ServiceModel::on_service_removed()
250 m_removed_services.lock();
252 while ( !m_removed_services.empty() )
257 iter = m_service_list->children().begin();
259 while ( iter != m_service_list->children().end() )
261 Gtk::TreeModel::Row row = *iter;
262 if ( (row[m_service_record.name] == s.
name) &&
263 (row[m_service_record.type] == s.
type) &&
264 (row[m_service_record.domain] == s.
domain) )
266 m_service_list->row_deleted( m_service_list->get_path(iter) );
267 iter = m_service_list->erase(iter);
273 m_removed_services.pop();
276 m_removed_services.unlock();
std::string ipaddr
the IP address of the new service
struct sockaddr_storage sockaddr
sockaddr structure
std::string hostname
the hostname of the new service
Fawkes library namespace.
Detects services and manages information about detected services.
std::string type
the type of the service
std::string interface
name of network interface to reach service
unsigned short port
the port the new service is running on
Data structure to hold information about a recently removed services.
std::string type
the type of the new service
std::string name
the name of the new service
void cancel()
Cancel a thread.
std::string domain
the domain of the service
std::string addrport
address:port
Data structure to hold information about a newly added services.
std::string domain
the domain of the new service
std::string name
the name of the service