]> git.meshlink.io Git - catta/blob - avahi-core/browse-service.c
* rename avahi_service_name_snprint() to avahi_service_name_join()
[catta] / avahi-core / browse-service.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 <string.h>
27
28 #include <avahi-common/domain.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/error.h>
31
32 #include "browse.h"
33 #include "log.h"
34
35 struct AvahiSServiceBrowser {
36     AvahiServer *server;
37     char *domain_name;
38     char *service_type;
39     
40     AvahiSRecordBrowser *record_browser;
41
42     AvahiSServiceBrowserCallback callback;
43     void* userdata;
44
45     AVAHI_LLIST_FIELDS(AvahiSServiceBrowser, browser);
46 };
47
48 static void record_browser_callback(
49     AvahiSRecordBrowser*rr,
50     AvahiIfIndex interface,
51     AvahiProtocol protocol,
52     AvahiBrowserEvent event,
53     AvahiRecord *record,
54     AvahiLookupResultFlags flags,
55     void* userdata) {
56     
57     AvahiSServiceBrowser *b = userdata;
58
59     assert(rr);
60     assert(b);
61
62     if (record) {
63         char service[AVAHI_LABEL_MAX], 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, service, sizeof(service), type, sizeof(type), domain, sizeof(domain)) < 0) {
68             avahi_log_warn("Failed to split '%s'", record->key->name);
69             return;
70         }
71
72         b->callback(b, interface, protocol, event, service, type, domain, flags, b->userdata);
73         
74     } else
75         b->callback(b, interface, protocol, event, NULL, b->service_type, b->domain_name, flags, b->userdata);
76
77 }
78
79 AvahiSServiceBrowser *avahi_s_service_browser_new(
80     AvahiServer *server,
81     AvahiIfIndex interface,
82     AvahiProtocol protocol,
83     const char *service_type,
84     const char *domain,
85     AvahiLookupFlags flags,
86     AvahiSServiceBrowserCallback callback,
87     void* userdata) {
88
89     AvahiSServiceBrowser *b;
90     AvahiKey *k = NULL;
91     char n[AVAHI_DOMAIN_NAME_MAX];
92     int r;
93     
94     assert(server);
95     assert(callback);
96     assert(service_type);
97
98     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
99     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
100     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
101     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
102     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_is_valid_service_type(service_type), AVAHI_ERR_INVALID_SERVICE_TYPE);
103
104     if (!domain)
105         domain = server->domain_name;
106
107     if ((r = avahi_service_name_join(n, sizeof(n), NULL, service_type, domain)) < 0) {
108         avahi_server_set_errno(server, r);
109         return NULL;
110     }
111     
112     if (!(b = avahi_new(AvahiSServiceBrowser, 1))) {
113         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
114         return NULL;
115     }
116     
117     b->server = server;
118     b->domain_name = b->service_type = NULL;
119     b->callback = callback;
120     b->userdata = userdata;
121     b->record_browser = NULL;
122     
123     AVAHI_LLIST_PREPEND(AvahiSServiceBrowser, browser, server->service_browsers, b);
124
125     if (!(b->domain_name = avahi_normalize_name_strdup(domain)) ||
126         !(b->service_type = avahi_normalize_name_strdup(service_type))) {
127         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
128         goto fail;
129     }
130     
131     if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
132         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
133         goto fail;
134     }
135     
136     if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
137         goto fail;
138
139     avahi_key_unref(k);
140
141     return b;
142
143 fail:
144
145     if (k)
146         avahi_key_unref(k);
147     
148     avahi_s_service_browser_free(b);
149     return NULL;
150 }
151
152 void avahi_s_service_browser_free(AvahiSServiceBrowser *b) {
153     assert(b);
154
155     AVAHI_LLIST_REMOVE(AvahiSServiceBrowser, browser, b->server->service_browsers, b);
156
157     if (b->record_browser)
158         avahi_s_record_browser_free(b->record_browser);
159     
160     avahi_free(b->domain_name);
161     avahi_free(b->service_type);
162     avahi_free(b);
163 }