static void
avahi_client_callback (AvahiClient *c, AvahiClientState state, void *user_data)
{
- printf ("XXX: Callback on client, state -> %d, data -> %s\n", state, (char*)user_data);
+ printf ("Callback on client, state -> %d, data -> %s\n", state, (char*)user_data);
}
static void
avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *user_data)
{
- printf ("XXX: Callback on %s, state -> %d, data -> %s\n", avahi_entry_group_get_dbus_path(g), state, (char*)user_data);
+ printf ("Callback on %s, state -> %d, data -> %s\n", avahi_entry_group_get_dbus_path(g), state, (char*)user_data);
}
static void
avahi_domain_browser_callback (AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void *user_data)
{
- printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", avahi_domain_browser_get_dbus_path (b), interface, protocol, event, domain, (char*)user_data);
+ printf ("Callback on %s, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", avahi_domain_browser_get_dbus_path (b), interface, protocol, event, domain, (char*)user_data);
}
static void
if (event == AVAHI_RESOLVER_TIMEOUT)
{
printf ("ServiceResolver timed out\n");
+ avahi_service_resolver_free (r);
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);
+ printf ("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
{
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);
+ printf ("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);
}
static void
avahi_service_type_browser_callback (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void *user_data)
{
- 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);
+ printf ("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)
+avahi_address_resolver_callback (AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiProtocol aprotocol, const AvahiAddress *address, const char *name, const char *userdata)
{
char addr[64];
-
if (event == AVAHI_RESOLVER_TIMEOUT)
{
- printf ("XXX: Callback on HostNameResolver, timed out.\n");
+ printf ("Callback on AddressResolver, timed out.\n");
+ avahi_address_resolver_free (r);
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);
+ avahi_address_snprint (&addr, sizeof (addr), address);
+ printf ("Callback on AddressResolver, interface (%d), protocol (%d), even (%d), aprotocol (%d), address (%s), name (%s), data(%s)\n", interface, protocol, event, aprotocol, addr, name);
}
+static void
+avahi_host_name_resolver_callback (AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const AvahiAddress *a, void *user_data)
+{
+ AvahiClient *client;
+ AvahiAddressResolver *ar;
+ char addr[64];
+
+ if (event == AVAHI_RESOLVER_TIMEOUT)
+ {
+ printf ("Callback on HostNameResolver, timed out.\n");
+ avahi_host_name_resolver_free (r);
+ return;
+ }
+ client = avahi_host_name_resolver_get_client (r);
+ ar = avahi_address_resolver_new_a (client, AVAHI_IF_UNSPEC, a, avahi_address_resolver_callback, "omghai6u");
+ if (ar)
+ {
+ printf ("Succesfully created address resolver object\n");
+ } else {
+ printf ("Failed to create AddressResolver\n");
+ }
+ avahi_address_snprint (&addr, sizeof (addr), a);
+ printf ("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;
- printf ("XXX: freeing domain browser\n");
+ printf ("Freeing domain browser\n");
avahi_service_browser_free (b);
}
static void test_free_entry_group (AvahiTimeout *timeout, void* userdata)
{
AvahiEntryGroup *g = userdata;
- printf ("XXX: freeing entry group\n");
+ printf ("Freeing entry group\n");
avahi_entry_group_free (g);
}
{
AvahiEntryGroup *g = userdata;
- printf ("XXX: resetting entry group\n");
+ printf ("Resetting entry group\n");
avahi_entry_group_reset (g);
avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AF_UNSPEC, "Lathiat's Site", "_http._tcp", "", "", 80, "foo=bar2", NULL);
AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
+ AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
+ AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
if (!(client->bus = avahi_dbus_bus_get(&error)) ||
dbus_error_is_set (&error))
/** A service resolver object */
typedef struct AvahiHostNameResolver AvahiHostNameResolver;
+/** An address resolver object */
+typedef struct AvahiAddressResolver AvahiAddressResolver;
+
/** States of a client object, a superset of AvahiServerState */
typedef enum {
AVAHI_CLIENT_S_INVALID = AVAHI_SERVER_INVALID,
const AvahiAddress *a,
void *userdata);
+/** The function prototype for the callback of an AvahiAddressResolver */
+typedef void (*AvahiAddressResolverCallback) (
+ AvahiAddressResolver *r,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ AvahiResolverEvent event,
+ AvahiProtocol aprotocol,
+ const AvahiAddress *a,
+ const char *name,
+ void *userdata);
+
/** Creates a new client instance */
AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error);
/** Block until the resolving is complete */
int avahi_host_name_resolver_block(AvahiHostNameResolver *r);
+/** Create a new address resolver object from an address string. Set protocol to AF_UNSPEC for protocol detection. */
+AvahiAddressResolver * avahi_address_resolver_new(
+ AvahiClient *client,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ const char *address,
+ AvahiAddressResolverCallback callback,
+ void *userdata);
+
+/** Create a new address resolver object from an AvahiAddress object */
+AvahiAddressResolver* avahi_address_resolver_new_a(
+ AvahiClient *client,
+ AvahiIfIndex interface,
+ const AvahiAddress *a,
+ AvahiAddressResolverCallback callback,
+ void *userdata);
+
+/** Get the parent client of an AvahiAddressResolver object */
+AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *);
+
+/** Free a AvahiAddressResolver resolver object */
+int avahi_address_resolver_free(AvahiAddressResolver *r);
+
+/** Block until the resolving is complete */
+int avahi_address_resolver_block(AvahiAddressResolver *r);
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
AVAHI_C_DECL_END
#endif
AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers);
AVAHI_LLIST_HEAD(AvahiServiceResolver, service_resolvers);
AVAHI_LLIST_HEAD(AvahiHostNameResolver, host_name_resolvers);
+ AVAHI_LLIST_HEAD(AvahiAddressResolver, address_resolvers);
};
struct AvahiEntryGroup {
AVAHI_LLIST_FIELDS(AvahiHostNameResolver, host_name_resolvers);
};
+struct AvahiAddressResolver {
+ char *path;
+ DBusPendingCall *call;
+ AvahiClient *client;
+ AvahiAddressResolverCallback callback;
+ void *userdata;
+ AVAHI_LLIST_FIELDS(AvahiAddressResolver, address_resolvers);
+};
+
int avahi_client_set_errno (AvahiClient *client, int error);
int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error);
if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
int j;
- int32_t interface, protocol, aprotocol;
+ int32_t interface;
+ AvahiProtocol protocol, aprotocol;
char *name, *type, *domain, *host, *address;
uint16_t port;
DBusMessageIter iter, sub;
goto fail;
if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
- int32_t interface, protocol, aprotocol;
+ int32_t interface;
+ AvahiProtocol protocol, aprotocol;
char *name, *address;
AvahiAddress a;
dbus_error_free (&error);
}
+static void
+address_pending_call_callback(DBusPendingCall *pending, void *userdata) {
+ AvahiAddressResolver *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;
+ AvahiProtocol protocol, aprotocol;
+ char *name, *address;
+ AvahiAddress a;
+
+ if (!dbus_message_get_args(
+ message, &error,
+ DBUS_TYPE_INT32, &interface,
+ DBUS_TYPE_INT32, &protocol,
+ DBUS_TYPE_INT32, &aprotocol,
+ DBUS_TYPE_STRING, &address,
+ DBUS_TYPE_STRING, &name,
+ 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, (AvahiProtocol) aprotocol, &a, name, 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, (AvahiProtocol) 0, NULL, NULL, r->userdata);
+ }
+
+fail:
+
+ if (message)
+ dbus_message_unref(message);
+
+ dbus_error_free (&error);
+}
+
+/* AvahiServiceResolver implementation */
AvahiServiceResolver * avahi_service_resolver_new(
AvahiClient *client,
AvahiIfIndex interface,
}
AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
- AvahiClient *client;
-
assert (r);
return r->client;
return AVAHI_OK;
}
+/* AvahiHostNameResolver implementation */
+
AvahiHostNameResolver * avahi_host_name_resolver_new(
AvahiClient *client,
AvahiIfIndex interface,
}
AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
+ 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;
+}
+
+/* AvahiAddressResolver implementation */
+
+AvahiAddressResolver * avahi_address_resolver_new_a(
+ AvahiClient *client,
+ AvahiIfIndex interface,
+ const AvahiAddress *a,
+ AvahiAddressResolverCallback callback,
+ void *userdata) {
+
+ char addr[64];
+
+ assert (a);
+
+ avahi_address_snprint (addr, sizeof (addr), a);
+
+ return avahi_address_resolver_new (client, interface,
+ a->family, addr,
+ callback, userdata);
+}
+
+AvahiAddressResolver * avahi_address_resolver_new(
+ AvahiClient *client,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ const char *address,
+ AvahiAddressResolverCallback callback,
+ void *userdata) {
+
+ DBusError error;
+ AvahiAddressResolver *r;
+ DBusMessage *message;
+ int32_t i_interface;
+ AvahiProtocol i_protocol;
+
+ assert(client);
+
+ 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(AvahiAddressResolver, 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(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
+
+ if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress"))) {
+ avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+
+ i_interface = interface;
+ i_protocol = protocol;
+
+ if (!(dbus_message_append_args(
+ message,
+ DBUS_TYPE_INT32, &i_interface,
+ DBUS_TYPE_INT32, &i_protocol,
+ DBUS_TYPE_STRING, &address,
+ 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, address_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_address_resolver_free(r);
+
+ if (message)
+ dbus_message_unref(message);
+
+ return NULL;
+
+}
+
+AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
assert (r);
return r->client;
}
-int avahi_host_name_resolver_block(AvahiHostNameResolver *r) {
+int avahi_address_resolver_free(AvahiAddressResolver *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(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
+
+ avahi_free(r);
+
+ return AVAHI_OK;
+}
+
+int avahi_address_resolver_block(AvahiAddressResolver *r) {
AvahiClient *client;
assert(r);