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
30 #include <avahi-core/core.h>
32 static AvahiEntryGroup *group = NULL;
33 static GMainLoop *main_loop = NULL;
34 static gchar *name = NULL;
36 static void create_services(AvahiServer *s);
38 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
42 /* Called whenever the entry group state changes */
44 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
45 /* The entry group has been established successfully */
46 g_message("Service '%s' successfully established.", name);
48 else if (state == AVAHI_ENTRY_GROUP_COLLISION) {
51 /* A service name collision happened. Let's pick a new name */
52 n = avahi_alternative_service_name(name);
56 g_message("Service name collision, renaming service to '%s'", name);
58 /* And recreate the services */
63 static void create_services(AvahiServer *s) {
68 /* If this is the first time we're called, let's create a new entry group */
70 if (!(group = avahi_entry_group_new(s, entry_group_callback, NULL))) {
71 g_message("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(s)));
76 g_message("Adding service '%s'", name);
78 /* Create some random TXT data */
79 snprintf(r, sizeof(r), "random=%i", rand());
81 /* Add the service for IPP */
82 if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
83 g_message("Failed to add _ipp._tcp service: %s", avahi_strerror(ret));
87 /* Add the same service for BSD LPR */
88 if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
89 g_message("Failed to add _printer._tcp service: %s", avahi_strerror(ret));
93 /* Tell the server to register the service */
94 if ((ret = avahi_entry_group_commit(group)) < 0) {
95 g_message("Failed to commit entry_group: %s", avahi_strerror(ret));
102 g_main_loop_quit(main_loop);
106 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
109 /* Called whenever the server state changes */
111 if (state == AVAHI_SERVER_RUNNING)
112 /* The serve has startup successfully and registered its host
113 * name on the network, so it's time to create our services */
116 else if (state == AVAHI_SERVER_COLLISION) {
120 /* A host name collision happened. Let's pick a new name for the server */
121 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
122 g_message("Host name collision, retrying with '%s'", n);
123 r = avahi_server_set_host_name(s, n);
127 g_message("Failed to set new host name: %s", avahi_strerror(r));
129 g_main_loop_quit(main_loop);
133 /* Let's drop our registered services. When the server is back
134 * in AVAHI_SERVER_RUNNING state we will register them
135 * again with the new host name. */
137 avahi_entry_group_reset(group);
141 int main(int argc, char*argv[]) {
142 AvahiServerConfig config;
143 AvahiServer *server = NULL;
149 name = g_strdup("MegaPrinter");
151 /* Let's set the host name for this server. */
152 avahi_server_config_init(&config);
153 config.host_name = g_strdup("gurkiman");
154 config.publish_workstation = FALSE;
156 /* Allocate a new server */
157 server = avahi_server_new(NULL, &config, server_callback, NULL, &error);
159 /* Free the configuration data */
160 avahi_server_config_free(&config);
162 /* Check wether creating the server object succeeded */
164 g_message("Failed to create server: %s", avahi_strerror(error));
168 /* Run the main loop */
169 main_loop = g_main_loop_new(NULL, FALSE);
170 g_main_loop_run(main_loop);
178 avahi_entry_group_free(group);
181 avahi_server_free(server);
184 g_main_loop_unref(main_loop);