1/***
2  This file is part of avahi.
3
4  avahi is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) any later version.
8
9  avahi is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12  Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public
15  License along with avahi; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  USA.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <string.h>
25
26#include <avahi-common/domain.h>
27#include <avahi-common/malloc.h>
28#include <avahi-common/error.h>
29
30#include "browse.h"
31#include "log.h"
32
33struct AvahiSServiceTypeBrowser {
34    AvahiServer *server;
35    char *domain_name;
36
37    AvahiSRecordBrowser *record_browser;
38
39    AvahiSServiceTypeBrowserCallback callback;
40    void* userdata;
41
42    AVAHI_LLIST_FIELDS(AvahiSServiceTypeBrowser, browser);
43};
44
45static void record_browser_callback(
46    AvahiSRecordBrowser*rr,
47    AvahiIfIndex interface,
48    AvahiProtocol protocol,
49    AvahiBrowserEvent event,
50    AvahiRecord *record,
51    AvahiLookupResultFlags flags,
52    void* userdata) {
53
54    AvahiSServiceTypeBrowser *b = userdata;
55
56    assert(rr);
57    assert(b);
58
59    /* Filter flags */
60    flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
61
62    if (record) {
63        char type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX];
64
65        assert(record->key->type == AVAHI_DNS_TYPE_PTR);
66
67        if (avahi_service_name_split(record->data.ptr.name, NULL, 0, type, sizeof(type), domain, sizeof(domain)) < 0) {
68            avahi_log_warn("Invalid service type '%s'", record->key->name);
69            return;
70        }
71
72        b->callback(b, interface, protocol, event, type, domain, flags, b->userdata);
73    } else
74        b->callback(b, interface, protocol, event, NULL, b->domain_name, flags, b->userdata);
75}
76
77AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(
78    AvahiServer *server,
79    AvahiIfIndex interface,
80    AvahiProtocol protocol,
81    const char *domain,
82    AvahiLookupFlags flags,
83    AvahiSServiceTypeBrowserCallback callback,
84    void* userdata) {
85
86    AvahiSServiceTypeBrowser *b;
87    AvahiKey *k = NULL;
88    char n[AVAHI_DOMAIN_NAME_MAX];
89    int r;
90
91    assert(server);
92    assert(callback);
93
94    AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
95    AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
96    AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
97    AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
98
99    if (!domain)
100        domain = server->domain_name;
101
102    if ((r = avahi_service_name_join(n, sizeof(n), NULL, "_services._dns-sd._udp", domain)) < 0) {
103        avahi_server_set_errno(server, r);
104        return NULL;
105    }
106
107    if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) {
108        avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
109        return NULL;
110    }
111
112    b->server = server;
113    b->callback = callback;
114    b->userdata = userdata;
115    b->record_browser = NULL;
116
117    AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b);
118
119    if (!(b->domain_name = avahi_normalize_name_strdup(domain))) {
120        avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
121        goto fail;
122    }
123
124    if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
125        avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
126        goto fail;
127    }
128
129    if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
130        goto fail;
131
132    avahi_key_unref(k);
133
134    return b;
135
136fail:
137    if (k)
138        avahi_key_unref(k);
139
140    avahi_s_service_type_browser_free(b);
141
142    return NULL;
143}
144
145void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) {
146    assert(b);
147
148    AVAHI_LLIST_REMOVE(AvahiSServiceTypeBrowser, browser, b->server->service_type_browsers, b);
149
150    if (b->record_browser)
151        avahi_s_record_browser_free(b->record_browser);
152
153    avahi_free(b->domain_name);
154    avahi_free(b);
155}
156
157
158