summaryrefslogtreecommitdiff
path: root/source3/smbd/avahi_register.c
blob: 1903b0ef9642bfc39b6b45526d5290aa0d363f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Unix SMB/CIFS implementation.
 * Register _smb._tcp with avahi
 *
 * Copyright (C) Volker Lendecke 2009
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"

#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/error.h>

struct avahi_state_struct {
	struct AvahiPoll *poll;
	AvahiClient *client;
	AvahiEntryGroup *entry_group;
	uint16_t port;
};

static void avahi_entry_group_callback(AvahiEntryGroup *g,
				       AvahiEntryGroupState status,
				       void *userdata)
{
	struct avahi_state_struct *state = talloc_get_type_abort(
		userdata, struct avahi_state_struct);
	int error;

	switch (status) {
	case AVAHI_ENTRY_GROUP_ESTABLISHED:
		DEBUG(10, ("avahi_entry_group_callback: "
			   "AVAHI_ENTRY_GROUP_ESTABLISHED\n"));
		break;
	case AVAHI_ENTRY_GROUP_FAILURE:
		error = avahi_client_errno(state->client);

		DEBUG(10, ("avahi_entry_group_callback: "
			   "AVAHI_ENTRY_GROUP_FAILURE: %s\n",
			   avahi_strerror(error)));
		break;
	case AVAHI_ENTRY_GROUP_COLLISION:
		DEBUG(10, ("avahi_entry_group_callback: "
			   "AVAHI_ENTRY_GROUP_COLLISION\n"));
		break;
	case AVAHI_ENTRY_GROUP_UNCOMMITED:
		DEBUG(10, ("avahi_entry_group_callback: "
                           "AVAHI_ENTRY_GROUP_UNCOMMITED\n"));
		break;
	case AVAHI_ENTRY_GROUP_REGISTERING:
		DEBUG(10, ("avahi_entry_group_callback: "
                           "AVAHI_ENTRY_GROUP_REGISTERING\n"));
		break;
	}
}

static void avahi_client_callback(AvahiClient *c, AvahiClientState status,
				  void *userdata)
{
	struct avahi_state_struct *state = talloc_get_type_abort(
		userdata, struct avahi_state_struct);
	int error;

	switch (status) {
	case AVAHI_CLIENT_S_RUNNING:
		DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_S_RUNNING\n"));

		state->entry_group = avahi_entry_group_new(
			c, avahi_entry_group_callback, state);
		if (state->entry_group == NULL) {
			error = avahi_client_errno(c);
			DEBUG(10, ("avahi_entry_group_new failed: %s\n",
				   avahi_strerror(error)));
			break;
		}
		if (avahi_entry_group_add_service(
			    state->entry_group, AVAHI_IF_UNSPEC,
			    AVAHI_PROTO_UNSPEC, 0, global_myname(),
			    "_smb._tcp", NULL, NULL, state->port, NULL) < 0) {
			error = avahi_client_errno(c);
			DEBUG(10, ("avahi_entry_group_add_service failed: "
				   "%s\n", avahi_strerror(error)));
			avahi_entry_group_free(state->entry_group);
			state->entry_group = NULL;
			break;
		}
		if (avahi_entry_group_commit(state->entry_group) < 0) {
			error = avahi_client_errno(c);
			DEBUG(10, ("avahi_entry_group_commit failed: "
				   "%s\n", avahi_strerror(error)));
			avahi_entry_group_free(state->entry_group);
			state->entry_group = NULL;
			break;
		}
		break;
	case AVAHI_CLIENT_FAILURE:
		error = avahi_client_errno(c);

		DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_FAILURE: %s\n",
			   avahi_strerror(error)));

		if (error != AVAHI_ERR_DISCONNECTED) {
			break;
		}
		avahi_client_free(c);
		state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
						 avahi_client_callback, state,
						 &error);
		if (state->client == NULL) {
			DEBUG(10, ("avahi_client_new failed: %s\n",
				   avahi_strerror(error)));
			break;
		}
		break;
	case AVAHI_CLIENT_S_COLLISION:
		DEBUG(10, ("avahi_client_callback: "
			   "AVAHI_CLIENT_S_COLLISION\n"));
		break;
	case AVAHI_CLIENT_S_REGISTERING:
		DEBUG(10, ("avahi_client_callback: "
			   "AVAHI_CLIENT_S_REGISTERING\n"));
		break;
	case AVAHI_CLIENT_CONNECTING:
		DEBUG(10, ("avahi_client_callback: "
			   "AVAHI_CLIENT_CONNECTING\n"));
		break;
	}
}

void *avahi_start_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
			   uint16_t port)
{
	struct avahi_state_struct *state;
	int error;

	state = talloc(mem_ctx, struct avahi_state_struct);
	if (state == NULL) {
		return state;
	}
	state->port = port;
	state->poll = tevent_avahi_poll(state, ev);
	if (state->poll == NULL) {
		goto fail;
	}
	state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
					 avahi_client_callback, state,
					 &error);
	if (state->client == NULL) {
		DEBUG(10, ("avahi_client_new failed: %s\n",
			   avahi_strerror(error)));
		goto fail;
	}
	return state;

 fail:
	TALLOC_FREE(state);
	return NULL;
}