]> git.meshlink.io Git - catta/blob - avahi-core/browse-domain.c
Rename some server side objects/symbols so that they do not conflict with the same...
[catta] / avahi-core / browse-domain.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-common/domain.h>
27 #include <avahi-common/malloc.h>
28
29 #include "browse.h"
30
31 struct AvahiSDomainBrowser {
32     AvahiServer *server;
33     char *domain_name;
34     
35     AvahiSRecordBrowser *record_browser;
36
37     AvahiSDomainBrowserCallback callback;
38     void* userdata;
39
40     AVAHI_LLIST_FIELDS(AvahiSDomainBrowser, browser);
41 };
42
43 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
44     AvahiSDomainBrowser *b = userdata;
45     char *n;
46
47     assert(rr);
48     assert(record);
49     assert(b);
50
51     assert(record->key->type == AVAHI_DNS_TYPE_PTR);
52
53     n = avahi_normalize_name(record->data.ptr.name);
54     b->callback(b, interface, protocol, event, n, b->userdata);
55     avahi_free(n);
56 }
57
58 AvahiSDomainBrowser *avahi_s_domain_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType type, AvahiSDomainBrowserCallback callback, void* userdata) {
59     AvahiSDomainBrowser *b;
60     AvahiKey *k;
61     char *n = NULL;
62     
63     assert(server);
64     assert(callback);
65     assert(type >= AVAHI_DOMAIN_BROWSER_BROWSE && type <= AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY);
66
67     if (domain && !avahi_is_valid_domain_name(domain)) {
68         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
69         return NULL;
70     }
71
72     if (!(b = avahi_new(AvahiSDomainBrowser, 1))) {
73         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
74         return NULL;
75     }
76     
77     b->server = server;
78     b->domain_name = avahi_normalize_name(domain ? domain : "local");
79     b->callback = callback;
80     b->userdata = userdata;
81
82     AVAHI_LLIST_PREPEND(AvahiSDomainBrowser, browser, server->domain_browsers, b);
83
84     switch (type) {
85         case AVAHI_DOMAIN_BROWSER_BROWSE:
86             n = avahi_strdup_printf("b._dns-sd._udp.%s", b->domain_name);
87             break;
88         case AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT:
89             n = avahi_strdup_printf("db._dns-sd._udp.%s", b->domain_name);
90             break;
91         case AVAHI_DOMAIN_BROWSER_REGISTER:
92             n = avahi_strdup_printf("r._dns-sd._udp.%s", b->domain_name);
93             break;
94         case AVAHI_DOMAIN_BROWSER_REGISTER_DEFAULT:
95             n = avahi_strdup_printf("dr._dns-sd._udp.%s", b->domain_name);
96             break;
97         case AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY:
98             n = avahi_strdup_printf("lb._dns-sd._udp.%s", b->domain_name);
99             break;
100
101         case AVAHI_DOMAIN_BROWSER_MAX:
102             assert(0);
103             break;
104     }
105
106     assert(n);
107
108     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
109     avahi_free(n);
110     
111     b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
112     avahi_key_unref(k);
113
114     if (!b->record_browser) {
115         avahi_s_domain_browser_free(b);
116         return NULL;
117     }
118     
119     return b;
120 }
121
122 void avahi_s_domain_browser_free(AvahiSDomainBrowser *b) {
123     assert(b);
124
125     AVAHI_LLIST_REMOVE(AvahiSDomainBrowser, browser, b->server->domain_browsers, b);
126
127     if (b->record_browser)
128         avahi_s_record_browser_free(b->record_browser);
129     
130     avahi_free(b->domain_name);
131     avahi_free(b);
132 }