ctdb-vacuum: rename ctdb_vacuum_db_full --> ctdb_vacuum_traverse_db
[Samba.git] / source3 / smbd / avahi_register.c
blob368168d41dc4a8c07f192786f03cec472caf9504
1 /*
2 * Unix SMB/CIFS implementation.
3 * Register _smb._tcp with avahi
5 * Copyright (C) Volker Lendecke 2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
24 #include <avahi-client/client.h>
25 #include <avahi-client/publish.h>
26 #include <avahi-common/error.h>
28 struct avahi_state_struct {
29 struct AvahiPoll *poll;
30 AvahiClient *client;
31 AvahiEntryGroup *entry_group;
32 uint16_t port;
35 static void avahi_entry_group_callback(AvahiEntryGroup *g,
36 AvahiEntryGroupState status,
37 void *userdata)
39 struct avahi_state_struct *state = talloc_get_type_abort(
40 userdata, struct avahi_state_struct);
41 int error;
43 switch (status) {
44 case AVAHI_ENTRY_GROUP_ESTABLISHED:
45 DEBUG(10, ("avahi_entry_group_callback: "
46 "AVAHI_ENTRY_GROUP_ESTABLISHED\n"));
47 break;
48 case AVAHI_ENTRY_GROUP_FAILURE:
49 error = avahi_client_errno(state->client);
51 DEBUG(10, ("avahi_entry_group_callback: "
52 "AVAHI_ENTRY_GROUP_FAILURE: %s\n",
53 avahi_strerror(error)));
54 break;
55 case AVAHI_ENTRY_GROUP_COLLISION:
56 DEBUG(10, ("avahi_entry_group_callback: "
57 "AVAHI_ENTRY_GROUP_COLLISION\n"));
58 break;
59 case AVAHI_ENTRY_GROUP_UNCOMMITED:
60 DEBUG(10, ("avahi_entry_group_callback: "
61 "AVAHI_ENTRY_GROUP_UNCOMMITED\n"));
62 break;
63 case AVAHI_ENTRY_GROUP_REGISTERING:
64 DEBUG(10, ("avahi_entry_group_callback: "
65 "AVAHI_ENTRY_GROUP_REGISTERING\n"));
66 break;
70 static void avahi_client_callback(AvahiClient *c, AvahiClientState status,
71 void *userdata)
73 struct avahi_state_struct *state = talloc_get_type_abort(
74 userdata, struct avahi_state_struct);
75 int error;
77 switch (status) {
78 case AVAHI_CLIENT_S_RUNNING:
79 DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_S_RUNNING\n"));
81 state->entry_group = avahi_entry_group_new(
82 c, avahi_entry_group_callback, state);
83 if (state->entry_group == NULL) {
84 error = avahi_client_errno(c);
85 DEBUG(10, ("avahi_entry_group_new failed: %s\n",
86 avahi_strerror(error)));
87 break;
89 if (avahi_entry_group_add_service(
90 state->entry_group, AVAHI_IF_UNSPEC,
91 AVAHI_PROTO_UNSPEC, 0, lp_netbios_name(),
92 "_smb._tcp", NULL, NULL, state->port, NULL) < 0) {
93 error = avahi_client_errno(c);
94 DEBUG(10, ("avahi_entry_group_add_service failed: "
95 "%s\n", avahi_strerror(error)));
96 avahi_entry_group_free(state->entry_group);
97 state->entry_group = NULL;
98 break;
100 if (avahi_entry_group_commit(state->entry_group) < 0) {
101 error = avahi_client_errno(c);
102 DEBUG(10, ("avahi_entry_group_commit failed: "
103 "%s\n", avahi_strerror(error)));
104 avahi_entry_group_free(state->entry_group);
105 state->entry_group = NULL;
106 break;
108 break;
109 case AVAHI_CLIENT_FAILURE:
110 error = avahi_client_errno(c);
112 DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_FAILURE: %s\n",
113 avahi_strerror(error)));
115 if (error != AVAHI_ERR_DISCONNECTED) {
116 break;
118 avahi_client_free(c);
119 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
120 avahi_client_callback, state,
121 &error);
122 if (state->client == NULL) {
123 DEBUG(10, ("avahi_client_new failed: %s\n",
124 avahi_strerror(error)));
125 break;
127 break;
128 case AVAHI_CLIENT_S_COLLISION:
129 DEBUG(10, ("avahi_client_callback: "
130 "AVAHI_CLIENT_S_COLLISION\n"));
131 break;
132 case AVAHI_CLIENT_S_REGISTERING:
133 DEBUG(10, ("avahi_client_callback: "
134 "AVAHI_CLIENT_S_REGISTERING\n"));
135 break;
136 case AVAHI_CLIENT_CONNECTING:
137 DEBUG(10, ("avahi_client_callback: "
138 "AVAHI_CLIENT_CONNECTING\n"));
139 break;
143 void *avahi_start_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
144 uint16_t port)
146 struct avahi_state_struct *state;
147 int error;
149 state = talloc(mem_ctx, struct avahi_state_struct);
150 if (state == NULL) {
151 return state;
153 state->port = port;
154 state->poll = tevent_avahi_poll(state, ev);
155 if (state->poll == NULL) {
156 goto fail;
158 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
159 avahi_client_callback, state,
160 &error);
161 if (state->client == NULL) {
162 DEBUG(10, ("avahi_client_new failed: %s\n",
163 avahi_strerror(error)));
164 goto fail;
166 return state;
168 fail:
169 TALLOC_FREE(state);
170 return NULL;