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/>.
23 #include <avahi-client/client.h>
24 #include <avahi-client/publish.h>
25 #include <avahi-common/error.h>
27 struct avahi_state_struct {
28 struct AvahiPoll *poll;
30 AvahiEntryGroup *entry_group;
34 static void avahi_entry_group_callback(AvahiEntryGroup *g,
35 AvahiEntryGroupState status,
38 struct avahi_state_struct *state = talloc_get_type_abort(
39 userdata, struct avahi_state_struct);
43 case AVAHI_ENTRY_GROUP_ESTABLISHED:
44 DEBUG(10, ("avahi_entry_group_callback: "
45 "AVAHI_ENTRY_GROUP_ESTABLISHED\n"));
47 case AVAHI_ENTRY_GROUP_FAILURE:
48 error = avahi_client_errno(state->client);
50 DEBUG(10, ("avahi_entry_group_callback: "
51 "AVAHI_ENTRY_GROUP_FAILURE: %s\n",
52 avahi_strerror(error)));
54 case AVAHI_ENTRY_GROUP_COLLISION:
55 DEBUG(10, ("avahi_entry_group_callback: "
56 "AVAHI_ENTRY_GROUP_COLLISION\n"));
58 case AVAHI_ENTRY_GROUP_UNCOMMITED:
59 DEBUG(10, ("avahi_entry_group_callback: "
60 "AVAHI_ENTRY_GROUP_UNCOMMITED\n"));
62 case AVAHI_ENTRY_GROUP_REGISTERING:
63 DEBUG(10, ("avahi_entry_group_callback: "
64 "AVAHI_ENTRY_GROUP_REGISTERING\n"));
69 static void avahi_client_callback(AvahiClient *c, AvahiClientState status,
72 struct avahi_state_struct *state = talloc_get_type_abort(
73 userdata, struct avahi_state_struct);
77 case AVAHI_CLIENT_S_RUNNING:
78 DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_S_RUNNING\n"));
80 state->entry_group = avahi_entry_group_new(
81 c, avahi_entry_group_callback, state);
82 if (state->entry_group == NULL) {
83 error = avahi_client_errno(c);
84 DEBUG(10, ("avahi_entry_group_new failed: %s\n",
85 avahi_strerror(error)));
88 if (avahi_entry_group_add_service(
89 state->entry_group, AVAHI_IF_UNSPEC,
90 AVAHI_PROTO_UNSPEC, 0, global_myname(),
91 "_smb._tcp", NULL, NULL, state->port, NULL) < 0) {
92 error = avahi_client_errno(c);
93 DEBUG(10, ("avahi_entry_group_add_service failed: "
94 "%s\n", avahi_strerror(error)));
95 avahi_entry_group_free(state->entry_group);
96 state->entry_group = NULL;
99 if (avahi_entry_group_commit(state->entry_group) < 0) {
100 error = avahi_client_errno(c);
101 DEBUG(10, ("avahi_entry_group_commit failed: "
102 "%s\n", avahi_strerror(error)));
103 avahi_entry_group_free(state->entry_group);
104 state->entry_group = NULL;
108 case AVAHI_CLIENT_FAILURE:
109 error = avahi_client_errno(c);
111 DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_FAILURE: %s\n",
112 avahi_strerror(error)));
114 if (error != AVAHI_ERR_DISCONNECTED) {
117 avahi_client_free(c);
118 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
119 avahi_client_callback, state,
121 if (state->client == NULL) {
122 DEBUG(10, ("avahi_client_new failed: %s\n",
123 avahi_strerror(error)));
127 case AVAHI_CLIENT_S_COLLISION:
128 DEBUG(10, ("avahi_client_callback: "
129 "AVAHI_CLIENT_S_COLLISION\n"));
131 case AVAHI_CLIENT_S_REGISTERING:
132 DEBUG(10, ("avahi_client_callback: "
133 "AVAHI_CLIENT_S_REGISTERING\n"));
135 case AVAHI_CLIENT_CONNECTING:
136 DEBUG(10, ("avahi_client_callback: "
137 "AVAHI_CLIENT_CONNECTING\n"));
142 void *avahi_start_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
145 struct avahi_state_struct *state;
148 state = talloc(mem_ctx, struct avahi_state_struct);
153 state->poll = tevent_avahi_poll(state, ev);
154 if (state->poll == NULL) {
157 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
158 avahi_client_callback, state,
160 if (state->client == NULL) {
161 DEBUG(10, ("avahi_client_new failed: %s\n",
162 avahi_strerror(error)));