summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrent Lloyd <lathiat@bur.st>2005-08-26 18:25:39 +0000
committerTrent Lloyd <lathiat@bur.st>2005-08-26 18:25:39 +0000
commit45bf63f4c05690420fd2d701a6399550160aba02 (patch)
treebffef8a78a3c8d8063d98f8a2d9fa22c81e19379
parentffa44c461dca3b61904b3b2cfe12c9e25b637ec4 (diff)
* Implement the HostNameResolver in the avahi-client C api
* Add HostNameResolver and ServiceResolver to client-test * Add get_client functions to all avahi-client browsers/resolvers git-svn-id: file:///home/lennart/svn/public/avahi/trunk@449 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
-rw-r--r--avahi-client/browser.c20
-rw-r--r--avahi-client/client-test.c41
-rw-r--r--avahi-client/client.h44
-rw-r--r--avahi-client/internal.h10
-rw-r--r--avahi-client/resolver.c190
5 files changed, 302 insertions, 3 deletions
diff --git a/avahi-client/browser.c b/avahi-client/browser.c
index 9a7438e..854ed1f 100644
--- a/avahi-client/browser.c
+++ b/avahi-client/browser.c
@@ -192,6 +192,12 @@ fail:
return NULL;
}
+AvahiClient* avahi_domain_browser_get_client (AvahiDomainBrowser *b)
+{
+ assert(b);
+ return b->client;
+}
+
int avahi_domain_browser_free (AvahiDomainBrowser *b) {
AvahiClient *client;
int r = AVAHI_OK;
@@ -361,6 +367,12 @@ fail:
return NULL;
}
+AvahiClient* avahi_service_type_browser_get_client (AvahiServiceTypeBrowser *b)
+{
+ assert(b);
+ return b->client;
+}
+
int avahi_service_type_browser_free (AvahiServiceTypeBrowser *b) {
AvahiClient *client;
int r = AVAHI_OK;
@@ -533,7 +545,13 @@ fail:
return NULL;
}
-
+
+AvahiClient* avahi_service_browser_get_client (AvahiServiceBrowser *b)
+{
+ assert(b);
+ return b->client;
+}
+
int avahi_service_browser_free (AvahiServiceBrowser *b) {
AvahiClient *client;
int r = AVAHI_OK;
diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c
index 5f7f030..4142172 100644
--- a/avahi-client/client-test.c
+++ b/avahi-client/client-test.c
@@ -53,8 +53,28 @@ avahi_domain_browser_callback (AvahiDomainBrowser *b, AvahiIfIndex interface, Av
}
static void
+avahi_service_resolver_callback (AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *a, uint16_t port, AvahiStringList *txt, void *user_data)
+{
+ char addr[64];
+ char *txtr;
+ if (event == AVAHI_RESOLVER_TIMEOUT)
+ {
+ printf ("ServiceResolver timed out\n");
+ return;
+ }
+ avahi_address_snprint (addr, sizeof (addr), a);
+ txtr = avahi_string_list_to_string (txt);
+ printf ("XXX: Callback on ServiceResolver, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), host_name (%s), address (%s), port (%d), txtdata (%s), data(%s)\n", interface, protocol, event, name, type, domain, host_name, addr, port, txtr, (char*)user_data);
+}
+
+static void
avahi_service_browser_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void *user_data)
{
+ AvahiServiceResolver *sr;
+ if (b && name)
+ {
+ sr = avahi_service_resolver_new (avahi_service_browser_get_client (b), interface, protocol, name, type, domain, AF_UNSPEC, avahi_service_resolver_callback, "xxXXxx");
+ }
printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), data (%s)\n", avahi_service_browser_get_dbus_path (b), interface, protocol, event, name, type, domain, (char*)user_data);
}
@@ -64,6 +84,20 @@ avahi_service_type_browser_callback (AvahiServiceTypeBrowser *b, AvahiIfIndex in
printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), type (%s), domain (%s), data (%s)\n", avahi_service_type_browser_get_dbus_path (b), interface, protocol, event, type, domain, (char*)user_data);
}
+static void
+avahi_host_name_resolver_callback (AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const AvahiAddress *a, void *user_data)
+{
+ char addr[64];
+
+ if (event == AVAHI_RESOLVER_TIMEOUT)
+ {
+ printf ("XXX: Callback on HostNameResolver, timed out.\n");
+ return;
+ }
+ avahi_address_snprint (addr, sizeof (addr), a);
+ printf ("XXX: Callback on HostNameResolver, interface (%d), protocol (%d), event (%d), name (%s), address (%s), data (%s)\n", interface, protocol, event, name, addr, (char*)user_data);
+}
+
static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata)
{
AvahiServiceBrowser *b = userdata;
@@ -101,6 +135,7 @@ int main (int argc, char *argv[]) {
AvahiDomainBrowser *domain;
AvahiServiceBrowser *sb;
AvahiServiceTypeBrowser *st;
+ AvahiHostNameResolver *hnr;
const char *ret;
int error;
struct timeval tv;
@@ -157,6 +192,12 @@ int main (int argc, char *argv[]) {
else
printf ("Sucessfully created service browser, path %s\n", avahi_service_browser_get_dbus_path (sb));
+ hnr = avahi_host_name_resolver_new (avahi, AVAHI_IF_UNSPEC, AF_UNSPEC, "hotbox.local", AF_UNSPEC, avahi_host_name_resolver_callback, "omghai4u");
+ if (hnr == NULL)
+ printf ("Failed to create hostname resolver object\n");
+ else
+ printf ("Successfully created hostname resolver object\n");
+
avahi_elapse_time(&tv, 8000, 0);
poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
diff --git a/avahi-client/client.h b/avahi-client/client.h
index 20054ad..4db2aef 100644
--- a/avahi-client/client.h
+++ b/avahi-client/client.h
@@ -66,6 +66,9 @@ typedef struct AvahiServiceTypeBrowser AvahiServiceTypeBrowser;
/** A service resolver object */
typedef struct AvahiServiceResolver AvahiServiceResolver;
+/** A service resolver object */
+typedef struct AvahiHostNameResolver AvahiHostNameResolver;
+
/** States of a client object, a superset of AvahiServerState */
typedef enum {
AVAHI_CLIENT_S_INVALID = AVAHI_SERVER_INVALID,
@@ -105,6 +108,16 @@ typedef void (*AvahiServiceResolverCallback) (
AvahiStringList *txt,
void *userdata);
+/** The function prototype for the callback of an AvahiHostNameResolver */
+typedef void (*AvahiHostNameResolverCallback) (
+ AvahiHostNameResolver *r,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ AvahiResolverEvent event,
+ const char *name,
+ const AvahiAddress *a,
+ void *userdata);
+
/** Creates a new client instance */
AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error);
@@ -198,6 +211,9 @@ AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client,
AvahiDomainBrowserCallback callback,
void *userdata);
+/** Get the parent client of an AvahiDomainBrowser object */
+AvahiClient* avahi_domain_browser_get_client (AvahiDomainBrowser *);
+
/** Get the D-Bus path of an AvahiDomainBrowser object, for debugging purposes only. */
const char* avahi_domain_browser_get_dbus_path (AvahiDomainBrowser *);
@@ -213,6 +229,9 @@ AvahiServiceTypeBrowser* avahi_service_type_browser_new (
AvahiServiceTypeBrowserCallback callback,
void *userdata);
+/** Get the parent client of an AvahiServiceTypeBrowser object */
+AvahiClient* avahi_service_type_browser_get_client (AvahiServiceTypeBrowser *);
+
/** Get the D-Bus path of an AvahiServiceTypeBrowser object, for debugging purposes only. */
const char* avahi_service_type_browser_get_dbus_path(AvahiServiceTypeBrowser *);
@@ -232,6 +251,9 @@ AvahiServiceBrowser* avahi_service_browser_new (
/** Get the D-Bus path of an AvahiServiceBrowser object, for debugging purposes only. */
const char* avahi_service_browser_get_dbus_path (AvahiServiceBrowser *);
+/** Get the parent client of an AvahiServiceBrowser object */
+AvahiClient* avahi_service_browser_get_client (AvahiServiceBrowser *);
+
/* Cleans up and frees an AvahiServiceBrowser object */
int avahi_service_browser_free (AvahiServiceBrowser *);
@@ -247,12 +269,34 @@ AvahiServiceResolver * avahi_service_resolver_new(
AvahiServiceResolverCallback callback,
void *userdata);
+/** Get the parent client of an AvahiServiceResolver object */
+AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *);
+
/** Free a service resolver object */
int avahi_service_resolver_free(AvahiServiceResolver *r);
/** Block until the resolving is complete */
int avahi_service_resolver_block(AvahiServiceResolver *r);
+/** Create a new hostname resolver object */
+AvahiHostNameResolver * avahi_host_name_resolver_new(
+ AvahiClient *client,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ const char *name,
+ AvahiProtocol aprotocol,
+ AvahiHostNameResolverCallback callback,
+ void *userdata);
+
+/** Get the parent client of an AvahiHostNameResolver object */
+AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *);
+
+/** Free a hostname resolver object */
+int avahi_host_name_resolver_free(AvahiHostNameResolver *r);
+
+/** Block until the resolving is complete */
+int avahi_host_name_resolver_block(AvahiHostNameResolver *r);
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
AVAHI_C_DECL_END
#endif
diff --git a/avahi-client/internal.h b/avahi-client/internal.h
index 1788122..4fc963f 100644
--- a/avahi-client/internal.h
+++ b/avahi-client/internal.h
@@ -42,6 +42,7 @@ struct AvahiClient {
AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers);
AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers);
AVAHI_LLIST_HEAD(AvahiServiceResolver, service_resolvers);
+ AVAHI_LLIST_HEAD(AvahiHostNameResolver, host_name_resolvers);
};
struct AvahiEntryGroup {
@@ -85,6 +86,15 @@ struct AvahiServiceResolver {
AVAHI_LLIST_FIELDS(AvahiServiceResolver, service_resolvers);
};
+struct AvahiHostNameResolver {
+ char *path;
+ DBusPendingCall *call;
+ AvahiClient *client;
+ AvahiHostNameResolverCallback callback;
+ void *userdata;
+ AVAHI_LLIST_FIELDS(AvahiHostNameResolver, host_name_resolvers);
+};
+
int avahi_client_set_errno (AvahiClient *client, int error);
int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error);
diff --git a/avahi-client/resolver.c b/avahi-client/resolver.c
index f23f9d7..2e44351 100644
--- a/avahi-client/resolver.c
+++ b/avahi-client/resolver.c
@@ -38,7 +38,8 @@
#include "client.h"
#include "internal.h"
-static void pending_call_callback(DBusPendingCall *pending, void *userdata) {
+static void
+service_pending_call_callback(DBusPendingCall *pending, void *userdata) {
AvahiServiceResolver *r = userdata;
DBusMessage *message = NULL;
AvahiStringList *strlst = NULL;
@@ -145,6 +146,63 @@ fail:
dbus_error_free (&error);
}
+static void
+hostname_pending_call_callback(DBusPendingCall *pending, void *userdata) {
+ AvahiHostNameResolver *r = userdata;
+ DBusMessage *message = NULL;
+ DBusError error;
+
+ assert(pending);
+ assert(r);
+
+ dbus_error_init(&error);
+
+ if (!(message = dbus_pending_call_steal_reply(pending)))
+ goto fail;
+
+ if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
+ int32_t interface, protocol, aprotocol;
+ char *name, *address;
+ AvahiAddress a;
+
+ if (!dbus_message_get_args(
+ message, &error,
+ DBUS_TYPE_INT32, &interface,
+ DBUS_TYPE_INT32, &protocol,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INT32, &aprotocol,
+ DBUS_TYPE_STRING, &address,
+ DBUS_TYPE_INVALID) ||
+ dbus_error_is_set (&error)) {
+ fprintf(stderr, "Failed to parse resolver event.\n");
+ goto fail;
+ }
+
+ assert(address);
+ if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
+ fprintf(stderr, "Failed to parse address\n");
+ goto fail;
+ }
+
+ r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, r->userdata);
+
+ } else {
+
+ assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
+
+ avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
+
+ r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, r->userdata);
+ }
+
+fail:
+
+ if (message)
+ dbus_message_unref(message);
+
+ dbus_error_free (&error);
+}
+
AvahiServiceResolver * avahi_service_resolver_new(
AvahiClient *client,
AvahiIfIndex interface,
@@ -210,7 +268,7 @@ AvahiServiceResolver * avahi_service_resolver_new(
}
if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
- !dbus_pending_call_set_notify(r->call, pending_call_callback, r, NULL)) {
+ !dbus_pending_call_set_notify(r->call, service_pending_call_callback, r, NULL)) {
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
@@ -236,6 +294,14 @@ fail:
}
+AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
+ AvahiClient *client;
+
+ assert (r);
+
+ return r->client;
+}
+
int avahi_service_resolver_free(AvahiServiceResolver *r) {
AvahiClient *client;
@@ -266,3 +332,123 @@ int avahi_service_resolver_block(AvahiServiceResolver *r) {
return AVAHI_OK;
}
+AvahiHostNameResolver * avahi_host_name_resolver_new(
+ AvahiClient *client,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ const char *name,
+ AvahiProtocol aprotocol,
+ AvahiHostNameResolverCallback callback,
+ void *userdata) {
+
+ DBusError error;
+ AvahiHostNameResolver *r;
+ DBusMessage *message;
+ int32_t i_interface, i_protocol, i_aprotocol;
+
+ assert(client);
+ assert(name);
+
+ dbus_error_init (&error);
+
+ if (client->state == AVAHI_CLIENT_DISCONNECTED) {
+ avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
+ goto fail;
+ }
+
+ if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
+ avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+
+ r->client = client;
+ r->callback = callback;
+ r->userdata = userdata;
+ r->call = NULL;
+
+ AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
+
+ if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName"))) {
+ avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+
+ i_interface = interface;
+ i_protocol = protocol;
+ i_aprotocol = aprotocol;
+
+ if (!(dbus_message_append_args(
+ message,
+ DBUS_TYPE_INT32, &i_interface,
+ DBUS_TYPE_INT32, &i_protocol,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INT32, &i_aprotocol,
+ DBUS_TYPE_INVALID))) {
+ avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+
+ if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
+ !dbus_pending_call_set_notify(r->call, hostname_pending_call_callback, r, NULL)) {
+ avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+
+ dbus_message_unref(message);
+
+ return r;
+
+fail:
+
+ if (dbus_error_is_set(&error)) {
+ avahi_client_set_dbus_error(client, &error);
+ dbus_error_free(&error);
+ }
+
+ if (r)
+ avahi_host_name_resolver_free(r);
+
+ if (message)
+ dbus_message_unref(message);
+
+ return NULL;
+
+}
+
+int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
+ AvahiClient *client;
+
+ assert(r);
+ client = r->client;
+
+ if (r->call) {
+ dbus_pending_call_cancel(r->call);
+ dbus_pending_call_unref(r->call);
+ }
+
+ AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
+
+ avahi_free(r);
+
+ return AVAHI_OK;
+}
+
+AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
+ AvahiClient *client;
+
+ assert (r);
+
+ return r->client;
+}
+
+int avahi_host_name_resolver_block(AvahiHostNameResolver *r) {
+ AvahiClient *client;
+
+ assert(r);
+ client = r->client;
+
+ if (r->call)
+ dbus_pending_call_block(r->call);
+
+ return AVAHI_OK;
+}