24 #include <arpa/inet.h> 25 #include <gui_utils/service_model.h> 26 #include <netcomm/dns-sd/avahi_thread.h> 27 #include <sys/socket.h> 28 #include <sys/types.h> 29 #include <utils/misc/string_conversions.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) {
127 Glib::RefPtr<Gtk::ListStore> &
128 ServiceModel::get_list_store()
130 return m_service_list;
137 ServiceModel::get_column_record()
139 return m_service_record;
143 ServiceModel::all_for_now()
148 ServiceModel::cache_exhausted()
153 ServiceModel::browse_failed(
const char *name,
const char *type,
const char *domain)
158 ServiceModel::service_added(
const char * name,
161 const char * host_name,
162 const char * interface,
163 const struct sockaddr * addr,
164 const socklen_t addr_size,
166 std::list<std::string> &txt,
170 if (addr->sa_family == AF_INET) {
171 char ipaddr[INET_ADDRSTRLEN];
172 struct sockaddr_in *saddr = (
struct sockaddr_in *)addr;
173 if (inet_ntop(AF_INET, &(saddr->sin_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
175 s.
addrport = std::string(ipaddr) +
":" + StringConversions::to_string(port);
178 s.
addrport = std::string(
"Failed to convert IPv4: ") + strerror(errno);
180 }
else if (addr->sa_family == AF_INET6) {
181 char ipaddr[INET6_ADDRSTRLEN];
182 struct sockaddr_in6 *saddr = (
struct sockaddr_in6 *)addr;
183 if (inet_ntop(AF_INET6, &(saddr->sin6_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
186 std::string(
"[") + ipaddr +
"%" +
interface + "]:" + StringConversions::to_string(port);
189 s.
addrport = std::string(
"Failed to convert IPv6: ") + strerror(errno);
193 s.
addrport =
"Unknown address family";
202 memcpy(&s.
sockaddr, addr, addr_size);
204 m_added_services.push_locked(s);
206 m_signal_service_added();
210 ServiceModel::service_removed(
const char *name,
const char *type,
const char *domain)
213 s.
name = string(name);
214 s.
type = string(type);
215 s.
domain = string(domain);
217 m_removed_services.push_locked(s);
219 m_signal_service_removed();
224 ServiceModel::on_service_added()
226 m_added_services.lock();
228 while (!m_added_services.empty()) {
231 Gtk::TreeModel::Row row = *m_service_list->append();
233 row[m_service_record.name] = s.
name;
234 row[m_service_record.type] = s.
type;
235 row[m_service_record.domain] = s.
domain;
236 row[m_service_record.hostname] = s.
hostname;
237 row[m_service_record.interface] = s.
interface;
238 row[m_service_record.ipaddr] = s.
ipaddr;
239 row[m_service_record.port] = s.
port;
240 row[m_service_record.addrport] = s.
addrport;
241 row[m_service_record.sockaddr] = s.
sockaddr;
243 m_added_services.pop();
246 m_added_services.unlock();
251 ServiceModel::on_service_removed()
253 m_removed_services.lock();
255 while (!m_removed_services.empty()) {
259 iter = m_service_list->children().begin();
261 while (iter != m_service_list->children().end()) {
262 Gtk::TreeModel::Row row = *iter;
263 if ((row[m_service_record.name] == s.
name) && (row[m_service_record.type] == s.
type)
264 && (row[m_service_record.domain] == s.
domain)) {
265 m_service_list->row_deleted(m_service_list->get_path(iter));
266 iter = m_service_list->erase(iter);
272 m_removed_services.pop();
275 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