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/>.
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;
31 AvahiEntryGroup *entry_group;
35 static void avahi_entry_group_callback(AvahiEntryGroup *g,
36 AvahiEntryGroupState status,
39 struct avahi_state_struct *state = talloc_get_type_abort(
40 userdata, struct avahi_state_struct);
44 case AVAHI_ENTRY_GROUP_ESTABLISHED:
45 DEBUG(10, ("avahi_entry_group_callback: "
46 "AVAHI_ENTRY_GROUP_ESTABLISHED\n"));
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)));
55 case AVAHI_ENTRY_GROUP_COLLISION:
56 DEBUG(10, ("avahi_entry_group_callback: "
57 "AVAHI_ENTRY_GROUP_COLLISION\n"));
59 case AVAHI_ENTRY_GROUP_UNCOMMITED:
60 DEBUG(10, ("avahi_entry_group_callback: "
61 "AVAHI_ENTRY_GROUP_UNCOMMITED\n"));
63 case AVAHI_ENTRY_GROUP_REGISTERING:
64 DEBUG(10, ("avahi_entry_group_callback: "
65 "AVAHI_ENTRY_GROUP_REGISTERING\n"));
70 static void avahi_client_callback(AvahiClient *c, AvahiClientState status,
73 struct avahi_state_struct *state = talloc_get_type_abort(
74 userdata, struct avahi_state_struct);
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)));
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;
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;
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) {
118 avahi_client_free(c);
119 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
120 avahi_client_callback, state,
122 if (state->client == NULL) {
123 DEBUG(10, ("avahi_client_new failed: %s\n",
124 avahi_strerror(error)));
128 case AVAHI_CLIENT_S_COLLISION:
129 DEBUG(10, ("avahi_client_callback: "
130 "AVAHI_CLIENT_S_COLLISION\n"));
132 case AVAHI_CLIENT_S_REGISTERING:
133 DEBUG(10, ("avahi_client_callback: "
134 "AVAHI_CLIENT_S_REGISTERING\n"));
136 case AVAHI_CLIENT_CONNECTING:
137 DEBUG(10, ("avahi_client_callback: "
138 "AVAHI_CLIENT_CONNECTING\n"));
143 void *avahi_start_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
146 struct avahi_state_struct *state;
149 state = talloc(mem_ctx, struct avahi_state_struct);
154 state->poll = tevent_avahi_poll(state, ev);
155 if (state->poll == NULL) {
158 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
159 avahi_client_callback, state,
161 if (state->client == NULL) {
162 DEBUG(10, ("avahi_client_new failed: %s\n",
163 avahi_strerror(error)));