Logo Search packages:      
Sourcecode: avahi version File versions

AvahiServiceBrowser* avahi_service_browser_new ( AvahiClient client,
AvahiIfIndex  interface,
AvahiProtocol  protocol,
const char *  type,
const char *  domain,
AvahiLookupFlags  flags,
AvahiServiceBrowserCallback  callback,
void *  userdata 
)

Browse for services of a type on the network. In most cases you probably want to pass AVAHI_IF_UNSPEC and AVAHI_PROTO_UNSPED in interface, resp. protocol to browse on all local networks. The specified callback will be called whenever a new service appears or is removed from the network. Please note that events may be collapsed to minimize traffic (i.e. a REMOVED followed by a NEW for the same service data is dropped because redundant). If you want to subscribe to service data changes, you should use avahi_service_resolver_new() and keep it open, in which case you will be notified via AVAHI_RESOLVE_FOUND everytime the service data changes.

Parameters:
interface  In most cases pass AVAHI_IF_UNSPEC here
protocol  In most cases pass AVAHI_PROTO_UNSPEC here
type  A service type such as "_http._tcp"
domain  A domain to browse in. In most cases you want to pass NULL here for the default domain (usually ".local")

Definition at line 566 of file browser.c.

References AVAHI_ERR_BAD_STATE, AVAHI_ERR_DBUS_ERROR, AVAHI_ERR_NO_MEMORY, and AVAHI_LLIST_PREPEND.

                    {
    
    AvahiServiceBrowser *b = NULL;
    DBusMessage *message = NULL, *reply = NULL;
    DBusError error;
    char *path;
    int32_t i_protocol, i_interface;
    uint32_t u_flags;

    assert(client);
    assert(type);
    assert(callback);

    dbus_error_init(&error);

    if (!avahi_client_is_connected(client)) {
        avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
        goto fail;
    }

    if (!domain)
        domain = "";

    if (!(b = avahi_new(AvahiServiceBrowser, 1))) {
        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }
    
    b->client = client;
    b->callback = callback;
    b->userdata = userdata;
    b->path = NULL;
    b->type = b->domain = NULL;
    b->interface = interface;
    b->protocol = protocol;

    AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, b);

    if (!(b->type = avahi_strdup(type))) {
        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }
    
    if (domain && domain[0])
        if (!(b->domain = avahi_strdup(domain))) {
            avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
            goto fail;
        }
    
    if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew"))) {
        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }

    i_interface = (int32_t) interface;
    i_protocol = (int32_t) protocol;
    u_flags = (uint32_t) flags;

    if (!dbus_message_append_args(
            message,
            DBUS_TYPE_INT32, &i_interface,
            DBUS_TYPE_INT32, &i_protocol,
            DBUS_TYPE_STRING, &type,
            DBUS_TYPE_STRING, &domain,
            DBUS_TYPE_UINT32, &u_flags,
            DBUS_TYPE_INVALID)) {
        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }

    if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
        dbus_error_is_set(&error)) {
        avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
        goto fail;
    }

    if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
        dbus_error_is_set(&error) ||
        !path) {
        avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
        goto fail;
    }

    if (!(b->path = avahi_strdup(path))) {

        /* FIXME: We don't remove the object on the server side */

        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }

    dbus_message_unref(message);
    dbus_message_unref(reply);
    
    return b;

fail:
    if (dbus_error_is_set(&error)) {
        avahi_client_set_dbus_error(client, &error);
        dbus_error_free(&error);
    }

    if (b)
        avahi_service_browser_free(b);
    
    if (message)
        dbus_message_unref(message);

    if (reply)
        dbus_message_unref(reply);

    return NULL;
}


Generated by  Doxygen 1.6.0   Back to index