4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <avahi-client/client.h>
32 #include <avahi-client/publish.h>
34 #include <avahi-common/alternative.h>
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/error.h>
38 #include <avahi-common/timeval.h>
40 static AvahiEntryGroup *group = NULL;
41 static AvahiSimplePoll *simple_poll = NULL;
42 static char *name = NULL;
44 static void create_services(AvahiClient *c);
46 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
47 assert(g == group || group == NULL);
50 /* Called whenever the entry group state changes */
53 case AVAHI_ENTRY_GROUP_ESTABLISHED :
54 /* The entry group has been established successfully */
55 fprintf(stderr, "Service '%s' successfully established.\n", name);
58 case AVAHI_ENTRY_GROUP_COLLISION : {
61 /* A service name collision with a remote service
62 * happened. Let's pick a new name */
63 n = avahi_alternative_service_name(name);
67 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
69 /* And recreate the services */
70 create_services(avahi_entry_group_get_client(g));
74 case AVAHI_ENTRY_GROUP_FAILURE :
76 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
78 /* Some kind of failure happened while we were registering our services */
79 avahi_simple_poll_quit(simple_poll);
82 case AVAHI_ENTRY_GROUP_UNCOMMITED:
83 case AVAHI_ENTRY_GROUP_REGISTERING:
88 static void create_services(AvahiClient *c) {
93 /* If this is the first time we're called, let's create a new
94 * entry group if necessary */
97 if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
98 fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
102 /* If the group is empty (either because it was just created, or
103 * because it was reset previously, add our entries. */
105 if (avahi_entry_group_is_empty(group)) {
106 fprintf(stderr, "Adding service '%s'\n", name);
108 /* Create some random TXT data */
109 snprintf(r, sizeof(r), "random=%i", rand());
111 /* Add the service for IPP */
112 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) {
114 if (ret == AVAHI_ERR_COLLISION) {
117 /* A service name collision with a local service
118 * happened. Let's pick a new name */
119 n = avahi_alternative_service_name(name);
123 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
129 fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
133 /* Add the same service for BSD LPR */
134 if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
135 fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
139 /* Add an additional (hypothetic) subtype */
140 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)) {
141 fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
145 /* Tell the server to register the service */
146 if ((ret = avahi_entry_group_commit(group)) < 0) {
147 fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
155 avahi_simple_poll_quit(simple_poll);
158 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
161 /* Called whenever the client or server state changes */
164 case AVAHI_CLIENT_S_RUNNING:
166 /* The server has startup successfully and registered its host
167 * name on the network, so it's time to create our services */
171 case AVAHI_CLIENT_FAILURE:
173 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
174 avahi_simple_poll_quit(simple_poll);
178 case AVAHI_CLIENT_S_COLLISION:
180 /* Let's drop our registered services. When the server is back
181 * in AVAHI_SERVER_RUNNING state we will register them
182 * again with the new host name. */
184 case AVAHI_CLIENT_S_REGISTERING:
186 /* The server records are now being established. This
187 * might be caused by a host name change. We need to wait
188 * for our own records to register until the host name is
189 * properly esatblished. */
192 avahi_entry_group_reset(group);
196 case AVAHI_CLIENT_CONNECTING:
201 static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) {
202 AvahiClient *client = userdata;
204 fprintf(stderr, "Doing some weird modification\n");
207 name = avahi_strdup("Modified MegaPrinter");
209 /* If the server is currently running, we need to remove our
210 * service and create it anew */
211 if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
213 /* Remove the old services */
215 avahi_entry_group_reset(group);
217 /* And create them again with the new name */
218 create_services(client);
222 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
223 AvahiClient *client = NULL;
228 /* Allocate main loop object */
229 if (!(simple_poll = avahi_simple_poll_new())) {
230 fprintf(stderr, "Failed to create simple poll object.\n");
234 name = avahi_strdup("MegaPrinter");
236 /* Allocate a new client */
237 client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);
239 /* Check wether creating the client object succeeded */
241 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
245 /* After 10s do some weird modification to the service */
246 avahi_simple_poll_get(simple_poll)->timeout_new(
247 avahi_simple_poll_get(simple_poll),
248 avahi_elapse_time(&tv, 1000*10, 0),
252 /* Run the main loop */
253 avahi_simple_poll_loop(simple_poll);
262 avahi_client_free(client);
265 avahi_simple_poll_free(simple_poll);