official_publish_example.cpp
Go to the documentation of this file.
1 
12 /*****************************************************************************
13 ** Includes
14 *****************************************************************************/
15 
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19 
20 #include <iostream>
21 #include <time.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 
26 #include <avahi-client/client.h>
27 #include <avahi-client/publish.h>
28 
29 #include <avahi-common/alternative.h>
30 #include <avahi-common/simple-watch.h>
31 #include <avahi-common/malloc.h>
32 #include <avahi-common/error.h>
33 #include <avahi-common/timeval.h>
34 
35 static AvahiEntryGroup *group = NULL;
36 static AvahiSimplePoll *simple_poll = NULL;
37 static char *name = NULL;
38 
39 static void create_services(AvahiClient *c);
40 
41 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
42  assert(g == group || group == NULL);
43  group = g;
44 
45  /* Called whenever the entry group state changes */
46 
47  switch (state) {
48  case AVAHI_ENTRY_GROUP_ESTABLISHED :
49  std::cout << "Entry group established" << std::endl;
50  /* The entry group has been established successfully */
51  fprintf(stderr, "Service '%s' successfully established.\n", name);
52  break;
53 
54  case AVAHI_ENTRY_GROUP_COLLISION : {
55  char *n;
56  std::cout << "Entry group collision" << std::endl;
57 
58  /* A service name collision with a remote service
59  * happened. Let's pick a new name */
60  n = avahi_alternative_service_name(name);
61  avahi_free(name);
62  name = n;
63 
64  fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
65 
66  /* And recreate the services */
67  create_services(avahi_entry_group_get_client(g));
68  break;
69  }
70 
71  case AVAHI_ENTRY_GROUP_FAILURE :
72 
73  std::cout << "Entry group failure" << std::endl;
74  fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
75 
76  /* Some kind of failure happened while we were registering our services */
77  avahi_simple_poll_quit(simple_poll);
78  break;
79 
80  case AVAHI_ENTRY_GROUP_UNCOMMITED:
81  std::cout << "Entry group uncommitted" << std::endl;
82  break;
83  case AVAHI_ENTRY_GROUP_REGISTERING:
84  std::cout << "Entry group registering" << std::endl;
85  break;
86  }
87 }
88 
89 static void create_services(AvahiClient *c) {
90  char *n, r[128];
91  int ret;
92  assert(c);
93 
94  /* If this is the first time we're called, let's create a new
95  * entry group if necessary */
96 
97  if (!group)
98  if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
99  fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
100  goto fail;
101  }
102 
103  /* If the group is empty (either because it was just created, or
104  * because it was reset previously, add our entries. */
105 
106  if (avahi_entry_group_is_empty(group)) {
107  fprintf(stderr, "Adding service '%s'\n", name);
108 
109  /* Create some random TXT data */
110  snprintf(r, sizeof(r), "random=%i", rand());
111 
112  /* We will now add two services and one subtype to the entry
113  * group. The two services have the same name, but differ in
114  * the service type (IPP vs. BSD LPR). Only services with the
115  * same name should be put in the same entry group. */
116 
117  /* Add the service for IPP */
118  if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0), name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
119 
120  if (ret == AVAHI_ERR_COLLISION)
121  goto collision;
122 
123  fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
124  goto fail;
125  }
126 
127  /* Add the same service for BSD LPR */
128  if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0), name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
129 
130  if (ret == AVAHI_ERR_COLLISION)
131  goto collision;
132 
133  fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
134  goto fail;
135  }
136 
137  /* Add an additional (hypothetic) subtype */
138  if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0), name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) {
139  fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
140  goto fail;
141  }
142 
143  /* Tell the server to register the service */
144  if ((ret = avahi_entry_group_commit(group)) < 0) {
145  fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
146  goto fail;
147  }
148  }
149 
150  return;
151 
152 collision:
153 
154  /* A service name collision with a local service happened. Let's
155  * pick a new name */
156  n = avahi_alternative_service_name(name);
157  avahi_free(name);
158  name = n;
159 
160  fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
161 
162  avahi_entry_group_reset(group);
163 
164  create_services(c);
165  return;
166 
167 fail:
168  avahi_simple_poll_quit(simple_poll);
169 }
170 
171 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
172  assert(c);
173 
174  /* Called whenever the client or server state changes */
175 
176  switch (state) {
177  case AVAHI_CLIENT_S_RUNNING:
178 
179  /* The server has startup successfully and registered its host
180  * name on the network, so it's time to create our services */
181  create_services(c);
182  break;
183 
184  case AVAHI_CLIENT_FAILURE:
185 
186  fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
187  avahi_simple_poll_quit(simple_poll);
188 
189  break;
190 
191  case AVAHI_CLIENT_S_COLLISION:
192 
193  /* Let's drop our registered services. When the server is back
194  * in AVAHI_SERVER_RUNNING state we will register them
195  * again with the new host name. */
196 
197  case AVAHI_CLIENT_S_REGISTERING:
198  /* The server records are now being established. This
199  * might be caused by a host name change. We need to wait
200  * for our own records to register until the host name is
201  * properly esatblished. */
202 
203  if (group)
204  avahi_entry_group_reset(group);
205 
206  break;
207 
208  case AVAHI_CLIENT_CONNECTING:
209  ;
210  }
211 }
212 
213 static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) {
214  AvahiClient *client = static_cast<AvahiClient*>(userdata);
215 
216  fprintf(stderr, "Doing some weird modification\n");
217 
218  avahi_free(name);
219  name = avahi_strdup("Modified MegaPrinter");
220 
221  /* If the server is currently running, we need to remove our
222  * service and create it anew */
223  if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
224 
225  /* Remove the old services */
226  if (group)
227  avahi_entry_group_reset(group);
228 
229  /* And create them again with the new name */
230  create_services(client);
231  }
232 }
233 
234 /*****************************************************************************
235 ** Main
236 *****************************************************************************/
237 
238 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char **argv) {
239 
240  AvahiClient *client = NULL;
241  int error;
242  int ret = 1;
243  struct timeval tv;
244 
245  /* Allocate main loop object */
246  if (!(simple_poll = avahi_simple_poll_new())) {
247  fprintf(stderr, "Failed to create simple poll object.\n");
248  goto fail;
249  }
250 
251  name = avahi_strdup("MegaPrinter");
252 
253  /* Allocate a new client */
254  client = avahi_client_new(avahi_simple_poll_get(simple_poll), static_cast<AvahiClientFlags>(0), client_callback, NULL, &error);
255 
256  /* Check wether creating the client object succeeded */
257  if (!client) {
258  fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
259  goto fail;
260  }
261 
262  /* After 10s do some weird modification to the service */
263  avahi_simple_poll_get(simple_poll)->timeout_new(
264  avahi_simple_poll_get(simple_poll),
265  avahi_elapse_time(&tv, 1000*10, 0),
267  client);
268 
269  /* Run the main loop */
270  avahi_simple_poll_loop(simple_poll);
271 
272  ret = 0;
273 
274 fail:
275 
276  /* Cleanup things */
277 
278  if (client)
279  avahi_client_free(client);
280 
281  if (simple_poll)
282  avahi_simple_poll_free(simple_poll);
283 
284  avahi_free(name);
285 
286  return ret;
287 }
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void *userdata)
static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata)
static void create_services(AvahiClient *c)
static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata)
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char **argv)
static AvahiSimplePoll * simple_poll
static char * name
static AvahiEntryGroup * group


zeroconf_avahi_demos
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 15:49:05