2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <avahi-client/client.h>
30 #include <avahi-client/publish.h>
32 #include <avahi-common/alternative.h>
33 #include <avahi-common/simple-watch.h>
34 #include <avahi-common/malloc.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/timeval.h>
38 static AvahiEntryGroup *group = NULL;
39 static AvahiSimplePoll *simple_poll = NULL;
40 static char *name = NULL;
42 static void create_services(AvahiClient *c);
44 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
45 assert(g == group || group == NULL);
48 /* Called whenever the entry group state changes */
51 case AVAHI_ENTRY_GROUP_ESTABLISHED :
52 /* The entry group has been established successfully */
53 fprintf(stderr, "Service '%s' successfully established.\n", name);
56 case AVAHI_ENTRY_GROUP_COLLISION : {
59 /* A service name collision with a remote service
60 * happened. Let's pick a new name */
61 n = avahi_alternative_service_name(name);
65 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
67 /* And recreate the services */
68 create_services(avahi_entry_group_get_client(g));
72 case AVAHI_ENTRY_GROUP_FAILURE :
74 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
76 /* Some kind of failure happened while we were registering our services */
77 avahi_simple_poll_quit(simple_poll);
80 case AVAHI_ENTRY_GROUP_UNCOMMITED:
81 case AVAHI_ENTRY_GROUP_REGISTERING:
86 static void create_services(AvahiClient *c) {
91 /* If this is the first time we're called, let's create a new
92 * entry group if necessary */
95 if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
96 fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
100 /* If the group is empty (either because it was just created, or
101 * because it was reset previously, add our entries. */
103 if (avahi_entry_group_is_empty(group)) {
104 fprintf(stderr, "Adding service '%s'\n", name);
106 /* Create some random TXT data */
107 snprintf(r, sizeof(r), "random=%i", rand());
109 /* We will now add two services and one subtype to the entry
110 * group. The two services have the same name, but differ in
111 * the service type (IPP vs. BSD LPR). Only services with the
112 * same name should be put in the same entry group. */
114 /* Add the service for IPP */
115 if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
117 if (ret == AVAHI_ERR_COLLISION)
120 fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
124 /* Add the same service for BSD LPR */
125 if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
127 if (ret == AVAHI_ERR_COLLISION)
130 fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
134 /* Add an additional (hypothetic) subtype */
135 if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) {
136 fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
140 /* Tell the server to register the service */
141 if ((ret = avahi_entry_group_commit(group)) < 0) {
142 fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
151 /* A service name collision with a local service happened. Let's
153 n = avahi_alternative_service_name(name);
157 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
159 avahi_entry_group_reset(group);
165 avahi_simple_poll_quit(simple_poll);
168 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
171 /* Called whenever the client or server state changes */
174 case AVAHI_CLIENT_S_RUNNING:
176 /* The server has startup successfully and registered its host
177 * name on the network, so it's time to create our services */
181 case AVAHI_CLIENT_FAILURE:
183 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
184 avahi_simple_poll_quit(simple_poll);
188 case AVAHI_CLIENT_S_COLLISION:
190 /* Let's drop our registered services. When the server is back
191 * in AVAHI_SERVER_RUNNING state we will register them
192 * again with the new host name. */
194 case AVAHI_CLIENT_S_REGISTERING:
196 /* The server records are now being established. This
197 * might be caused by a host name change. We need to wait
198 * for our own records to register until the host name is
199 * properly esatblished. */
202 avahi_entry_group_reset(group);
206 case AVAHI_CLIENT_CONNECTING:
211 static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) {
212 AvahiClient *client = userdata;
214 fprintf(stderr, "Doing some weird modification\n");
217 name = avahi_strdup("Modified MegaPrinter");
219 /* If the server is currently running, we need to remove our
220 * service and create it anew */
221 if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
223 /* Remove the old services */
225 avahi_entry_group_reset(group);
227 /* And create them again with the new name */
228 create_services(client);
232 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
233 AvahiClient *client = NULL;
238 /* Allocate main loop object */
239 if (!(simple_poll = avahi_simple_poll_new())) {
240 fprintf(stderr, "Failed to create simple poll object.\n");
244 name = avahi_strdup("MegaPrinter");
246 /* Allocate a new client */
247 client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);
249 /* Check wether creating the client object succeeded */
251 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
255 /* After 10s do some weird modification to the service */
256 avahi_simple_poll_get(simple_poll)->timeout_new(
257 avahi_simple_poll_get(simple_poll),
258 avahi_elapse_time(&tv, 1000*10, 0),
262 /* Run the main loop */
263 avahi_simple_poll_loop(simple_poll);
272 avahi_client_free(client);
275 avahi_simple_poll_free(simple_poll);