]> git.meshlink.io Git - catta/blob - examples/core-publish-service.c
from now on we enforce a strict whitespace regime
[catta] / examples / core-publish-service.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <time.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30
31 #include <avahi-core/core.h>
32 #include <avahi-core/publish.h>
33 #include <avahi-common/simple-watch.h>
34 #include <avahi-common/malloc.h>
35 #include <avahi-common/alternative.h>
36 #include <avahi-common/error.h>
37
38 static AvahiSEntryGroup *group = NULL;
39 static AvahiSimplePoll *simple_poll = NULL;
40 static char *name = NULL;
41
42 static void create_services(AvahiServer *s);
43
44 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
45     assert(s);
46     assert(g == group);
47
48     /* Called whenever the entry group state changes */
49
50     switch (state) {
51
52         case AVAHI_ENTRY_GROUP_ESTABLISHED:
53
54             /* The entry group has been established successfully */
55             fprintf(stderr, "Service '%s' successfully established.\n", name);
56             break;
57
58         case AVAHI_ENTRY_GROUP_COLLISION: {
59             char *n;
60
61             /* A service name collision happened. Let's pick a new name */
62             n = avahi_alternative_service_name(name);
63             avahi_free(name);
64             name = n;
65
66             fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
67
68             /* And recreate the services */
69             create_services(s);
70             break;
71         }
72
73         case AVAHI_ENTRY_GROUP_FAILURE :
74
75             fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_server_errno(s)));
76
77             /* Some kind of failure happened while we were registering our services */
78             avahi_simple_poll_quit(simple_poll);
79             break;
80
81         case AVAHI_ENTRY_GROUP_UNCOMMITED:
82         case AVAHI_ENTRY_GROUP_REGISTERING:
83             ;
84     }
85 }
86
87 static void create_services(AvahiServer *s) {
88     char r[128];
89     int ret;
90     assert(s);
91
92     /* If this is the first time we're called, let's create a new entry group */
93     if (!group)
94         if (!(group = avahi_s_entry_group_new(s, entry_group_callback, NULL))) {
95             fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_server_errno(s)));
96             goto fail;
97         }
98
99     fprintf(stderr, "Adding service '%s'\n", name);
100
101     /* Create some random TXT data */
102     snprintf(r, sizeof(r), "random=%i", rand());
103
104     /* Add the service for IPP */
105     if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
106         fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
107         goto fail;
108     }
109
110     /* Add the same service for BSD LPR */
111     if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
112         fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
113         goto fail;
114     }
115
116     /* Add an additional (hypothetic) subtype */
117     if ((ret = avahi_server_add_service_subtype(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) {
118         fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
119         goto fail;
120     }
121
122     /* Tell the server to register the service */
123     if ((ret = avahi_s_entry_group_commit(group)) < 0) {
124         fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret));
125         goto fail;
126     }
127
128     return;
129
130 fail:
131     avahi_simple_poll_quit(simple_poll);
132 }
133
134 static void server_callback(AvahiServer *s, AvahiServerState state, AVAHI_GCC_UNUSED void * userdata) {
135     assert(s);
136
137     /* Called whenever the server state changes */
138
139     switch (state) {
140
141         case AVAHI_SERVER_RUNNING:
142             /* The serve has startup successfully and registered its host
143              * name on the network, so it's time to create our services */
144
145             if (!group)
146                 create_services(s);
147
148             break;
149
150         case AVAHI_SERVER_COLLISION: {
151             char *n;
152             int r;
153
154             /* A host name collision happened. Let's pick a new name for the server */
155             n = avahi_alternative_host_name(avahi_server_get_host_name(s));
156             fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
157             r = avahi_server_set_host_name(s, n);
158             avahi_free(n);
159
160             if (r < 0) {
161                 fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r));
162
163                 avahi_simple_poll_quit(simple_poll);
164                 return;
165             }
166
167         }
168
169             /* Fall through */
170
171         case AVAHI_SERVER_REGISTERING:
172
173             /* Let's drop our registered services. When the server is back
174              * in AVAHI_SERVER_RUNNING state we will register them
175              * again with the new host name. */
176             if (group)
177                 avahi_s_entry_group_reset(group);
178
179             break;
180
181         case AVAHI_SERVER_FAILURE:
182
183             /* Terminate on failure */
184
185             fprintf(stderr, "Server failure: %s\n", avahi_strerror(avahi_server_errno(s)));
186             avahi_simple_poll_quit(simple_poll);
187             break;
188
189         case AVAHI_SERVER_INVALID:
190             ;
191     }
192 }
193
194 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
195     AvahiServerConfig config;
196     AvahiServer *server = NULL;
197     int error;
198     int ret = 1;
199
200     /* Initialize the pseudo-RNG */
201     srand(time(NULL));
202
203     /* Allocate main loop object */
204     if (!(simple_poll = avahi_simple_poll_new())) {
205         fprintf(stderr, "Failed to create simple poll object.\n");
206         goto fail;
207     }
208
209     name = avahi_strdup("MegaPrinter");
210
211     /* Let's set the host name for this server. */
212     avahi_server_config_init(&config);
213     config.host_name = avahi_strdup("gurkiman");
214     config.publish_workstation = 0;
215
216     /* Allocate a new server */
217     server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error);
218
219     /* Free the configuration data */
220     avahi_server_config_free(&config);
221
222     /* Check wether creating the server object succeeded */
223     if (!server) {
224         fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
225         goto fail;
226     }
227
228     /* Run the main loop */
229     avahi_simple_poll_loop(simple_poll);
230
231     ret = 0;
232
233 fail:
234
235     /* Cleanup things */
236
237     if (server)
238         avahi_server_free(server);
239
240     if (simple_poll)
241         avahi_simple_poll_free(simple_poll);
242
243     avahi_free(name);
244
245     return ret;
246 }