]> git.meshlink.io Git - catta/commitdiff
* implement AvahiServiceBrowser
authorLennart Poettering <lennart@poettering.net>
Sun, 22 May 2005 15:04:39 +0000 (15:04 +0000)
committerLennart Poettering <lennart@poettering.net>
Sun, 22 May 2005 15:04:39 +0000 (15:04 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@87 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-core/Makefile.am
avahi-core/avahi-test.c
avahi-core/browse-service-type.c
avahi-core/browse-service.c [new file with mode: 0644]
avahi-core/core.h
avahi-core/server.c
avahi-core/server.h

index 39d784401f6878401354ea53bd55d9342bba62eb..6c30809886c6e44174f92278b1a81d87643b79d2 100644 (file)
@@ -61,7 +61,8 @@ libavahi_core_la_SOURCES = \
        resolve-host-name.c \
        resolve-address.c \
        browse-domain.c \
-       browse-service-type.c
+       browse-service-type.c \
+       browse-service.c
 
 prioq_test_SOURCES = \
        prioq-test.c  \
index 56034819e69cb33cbd509b8de5f0c2a7a6bec054..e4ed4abcb7d66601cbc0192d1930dfb9f15513d9 100644 (file)
@@ -128,7 +128,7 @@ static void hnr_callback(AvahiHostNameResolver *r, gint iface, guchar protocol,
     if (a)
         avahi_address_snprint(t, sizeof(t), a);
 
-    g_message("HNR: (%i.%i) %s -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout");
+    g_message("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout");
 }
 
 static void ar_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
@@ -136,17 +136,21 @@ static void ar_callback(AvahiAddressResolver *r, gint iface, guchar protocol, Av
 
     avahi_address_snprint(t, sizeof(t), a);
 
-    g_message("AR: (%i.%i) %s -> %s [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout");
+    g_message("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout");
 }
 
 static void db_callback(AvahiDomainBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
 
-    g_message("DB: (%i.%i) %s [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
+    g_message("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
 }
 
 static void stb_callback(AvahiServiceTypeBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *service_type, const gchar *domain, gpointer userdata) {
 
-    g_message("STB: (%i.%i) %s in %s [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
+    g_message("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
+}
+
+static void sb_callback(AvahiServiceBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *name, const gchar *service_type, const gchar *domain, gpointer userdata) {
+   g_message("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
 }
     
 int main(int argc, char *argv[]) {
@@ -159,6 +163,7 @@ int main(int argc, char *argv[]) {
     AvahiAddress a;
     AvahiDomainBrowser *db;
     AvahiServiceTypeBrowser *stb;
+    AvahiServiceBrowser *sb;
 
     avahi_server_config_init(&config);
 /*     config.host_name = g_strdup("test"); */
@@ -176,6 +181,8 @@ int main(int argc, char *argv[]) {
     db = avahi_domain_browser_new(server, -1, AF_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, db_callback, NULL);
 
     stb = avahi_service_type_browser_new(server, -1, AF_UNSPEC, NULL, stb_callback, NULL);
+
+    sb = avahi_service_browser_new(server, -1, AF_UNSPEC, "_http._tcp", NULL, sb_callback, NULL);
     
     loop = g_main_loop_new(NULL, FALSE);
     
@@ -189,6 +196,7 @@ int main(int argc, char *argv[]) {
     avahi_host_name_resolver_free(hnr);
     avahi_address_resolver_free(ar);
     avahi_service_type_browser_free(stb);
+    avahi_service_browser_free(sb);
 
     if (group)
         avahi_entry_group_free(group);   
index d9816927209d769df74adb0c41e270b743ca8254..dec7eefc7451dee69c28c0e77bf83f82cbeb9e50 100644 (file)
@@ -71,7 +71,7 @@ static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, gucha
     if (!avahi_domain_equal(c, b->domain_name))
         goto fail;
     
-    b->callback(b, interface, protocol, event, n, b->domain_name, b->userdata);
+    b->callback(b, interface, protocol, event, n, c, b->userdata);
     g_free(n);
 
     return;
diff --git a/avahi-core/browse-service.c b/avahi-core/browse-service.c
new file mode 100644 (file)
index 0000000..10b3f33
--- /dev/null
@@ -0,0 +1,123 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi 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 Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+
+#include "browse.h"
+#include "util.h"
+
+struct AvahiServiceBrowser {
+    AvahiServer *server;
+    gchar *domain_name;
+    gchar *service_type;
+    
+    AvahiRecordBrowser *record_browser;
+
+    AvahiServiceBrowserCallback callback;
+    gpointer userdata;
+
+    AVAHI_LLIST_FIELDS(AvahiServiceBrowser, browser);
+};
+
+static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
+    AvahiServiceBrowser *b = userdata;
+    gchar *n, *e, *c, *s;
+    gchar service[128];
+
+    g_assert(rr);
+    g_assert(record);
+    g_assert(b);
+
+    g_assert(record->key->type == AVAHI_DNS_TYPE_PTR);
+
+    c = n = avahi_normalize_name(record->data.ptr.name);
+
+    if (!(avahi_unescape_label((const gchar**) &c, service, sizeof(service))))
+        goto fail;
+
+    for (s = e = c; *c == '_';) {
+        c += strcspn(c, ".");
+
+        if (*c == 0)
+            goto fail;
+
+        g_assert(*c == '.');
+        e = c;
+        c++;
+    }
+
+    *e = 0;
+
+    if (!avahi_domain_equal(c, b->domain_name))
+        goto fail;
+    
+    b->callback(b, interface, protocol, event, service, s, c, b->userdata);
+    g_free(n);
+
+    return;
+
+fail:
+    g_warning("Invalid service '%s'", n);
+    g_free(n);
+}
+
+AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *service_type, const gchar *domain, AvahiServiceBrowserCallback callback, gpointer userdata) {
+    AvahiServiceBrowser *b;
+    AvahiKey *k;
+    gchar *n = NULL;
+    
+    g_assert(server);
+    g_assert(callback);
+    g_assert(service_type);
+
+    b = g_new(AvahiServiceBrowser, 1);
+    b->server = server;
+    b->domain_name = avahi_normalize_name(domain ? domain : "local.");
+    b->service_type = avahi_normalize_name(service_type);
+    b->callback = callback;
+    b->userdata = userdata;
+
+    n = g_strdup_printf("%s%s", b->service_type, b->domain_name);
+    k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
+    g_free(n);
+    
+    b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
+    avahi_key_unref(k);
+
+    AVAHI_LLIST_PREPEND(AvahiServiceBrowser, browser, server->service_browsers, b);
+    
+    return b;
+}
+
+void avahi_service_browser_free(AvahiServiceBrowser *b) {
+    g_assert(b);
+
+    AVAHI_LLIST_REMOVE(AvahiServiceBrowser, browser, b->server->service_browsers, b);
+
+    avahi_record_browser_free(b->record_browser);
+    g_free(b->domain_name);
+    g_free(b->service_type);
+    g_free(b);
+}
index 2c3dafd0ef4cc0d9447b49210bdcfd76af3e7f81..4577584ddd6a1a52f739eda6ce4cc5eda5ceab1b 100644 (file)
@@ -249,10 +249,13 @@ typedef void (*AvahiServiceTypeBrowserCallback)(AvahiServiceTypeBrowser *b, gint
 AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *domain, AvahiServiceTypeBrowserCallback callback, gpointer userdata);
 void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b);
 
+typedef struct AvahiServiceBrowser AvahiServiceBrowser;
+typedef void (*AvahiServiceBrowserCallback)(AvahiServiceBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata);
+AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *service_type, const gchar *domain, AvahiServiceBrowserCallback callback, gpointer userdata);
+void avahi_service_browser_free(AvahiServiceBrowser *b);
 
 /* Not yet implemented */
 
-typedef struct AvahiServiceBrowser AvahiServiceBrowser;
 typedef struct AvahiServiceResolver AvahiServiceResolver;
 
 #endif
index 83bec157a00ddb6a7a46f9a7653170f51c298a17..fd17078399539befbb582698aea7e763ccca1621 100644 (file)
@@ -966,6 +966,7 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah
     AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, s->address_resolvers);
     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, s->domain_browsers);
     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, s->service_type_browsers);
+    AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, s->service_browsers);
 
     /* Get host name */
     s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name();
@@ -1012,6 +1013,8 @@ void avahi_server_free(AvahiServer* s) {
         avahi_domain_browser_free(s->domain_browsers);
     while (s->service_type_browsers)
         avahi_service_type_browser_free(s->service_type_browsers);
+    while (s->service_browsers)
+        avahi_service_browser_free(s->service_browsers);
     while (s->record_browsers)
         avahi_record_browser_destroy(s->record_browsers);
     g_hash_table_destroy(s->record_browser_hashtable);
index 60737cea4d999af3db9adf1ffd3510a5a8da462b..0e6f2e72a19e7cbea52190b785b0d134bdb90799 100644 (file)
@@ -81,6 +81,7 @@ struct AvahiServer {
     AVAHI_LLIST_HEAD(AvahiAddressResolver, address_resolvers);
     AVAHI_LLIST_HEAD(AvahiDomainBrowser, domain_browsers);
     AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers);
+    AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers);
 
     gboolean need_entry_cleanup, need_group_cleanup, need_browser_cleanup;