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-core/core.h>
30 #include <avahi-core/publish.h>
31 #include <avahi-common/simple-watch.h>
32 #include <avahi-common/malloc.h>
33 #include <avahi-common/alternative.h>
34 #include <avahi-common/error.h>
36 static AvahiSEntryGroup *group = NULL;
37 static AvahiSimplePoll *simple_poll = NULL;
38 static char *name = NULL;
40 static void create_services(AvahiServer *s);
42 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
46 /* Called whenever the entry group state changes */
50 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 happened. Let's pick a new name */
60 n = avahi_alternative_service_name(name);
64 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
66 /* And recreate the services */
71 case AVAHI_ENTRY_GROUP_FAILURE :
73 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_server_errno(s)));
75 /* Some kind of failure happened while we were registering our services */
76 avahi_simple_poll_quit(simple_poll);
79 case AVAHI_ENTRY_GROUP_UNCOMMITED:
80 case AVAHI_ENTRY_GROUP_REGISTERING:
85 static void create_services(AvahiServer *s) {
90 /* If this is the first time we're called, let's create a new entry group */
92 if (!(group = avahi_s_entry_group_new(s, entry_group_callback, NULL))) {
93 fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_server_errno(s)));
97 fprintf(stderr, "Adding service '%s'\n", name);
99 /* Create some random TXT data */
100 snprintf(r, sizeof(r), "random=%i", rand());
102 /* Add the service for IPP */
103 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) {
104 fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
108 /* Add the same service for BSD LPR */
109 if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
110 fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
114 /* Add an additional (hypothetic) subtype */
115 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)) {
116 fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
120 /* Tell the server to register the service */
121 if ((ret = avahi_s_entry_group_commit(group)) < 0) {
122 fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret));
129 avahi_simple_poll_quit(simple_poll);
132 static void server_callback(AvahiServer *s, AvahiServerState state, AVAHI_GCC_UNUSED void * userdata) {
135 /* Called whenever the server state changes */
139 case AVAHI_SERVER_RUNNING:
140 /* The serve has startup successfully and registered its host
141 * name on the network, so it's time to create our services */
148 case AVAHI_SERVER_COLLISION: {
152 /* A host name collision happened. Let's pick a new name for the server */
153 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
154 fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
155 r = avahi_server_set_host_name(s, n);
159 fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r));
161 avahi_simple_poll_quit(simple_poll);
169 case AVAHI_SERVER_REGISTERING:
171 /* Let's drop our registered services. When the server is back
172 * in AVAHI_SERVER_RUNNING state we will register them
173 * again with the new host name. */
175 avahi_s_entry_group_reset(group);
179 case AVAHI_SERVER_FAILURE:
181 /* Terminate on failure */
183 fprintf(stderr, "Server failure: %s\n", avahi_strerror(avahi_server_errno(s)));
184 avahi_simple_poll_quit(simple_poll);
187 case AVAHI_SERVER_INVALID:
192 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
193 AvahiServerConfig config;
194 AvahiServer *server = NULL;
198 /* Initialize the pseudo-RNG */
201 /* Allocate main loop object */
202 if (!(simple_poll = avahi_simple_poll_new())) {
203 fprintf(stderr, "Failed to create simple poll object.\n");
207 name = avahi_strdup("MegaPrinter");
209 /* Let's set the host name for this server. */
210 avahi_server_config_init(&config);
211 config.host_name = avahi_strdup("gurkiman");
212 config.publish_workstation = 0;
214 /* Allocate a new server */
215 server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error);
217 /* Free the configuration data */
218 avahi_server_config_free(&config);
220 /* Check wether creating the server object succeeded */
222 fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
226 /* Run the main loop */
227 avahi_simple_poll_loop(simple_poll);
236 avahi_server_free(server);
239 avahi_simple_poll_free(simple_poll);