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-client/client.h> 32#include <avahi-client/publish.h> 33 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> 39 40static AvahiEntryGroup *group = NULL; 41static AvahiSimplePoll *simple_poll = NULL; 42static char *name = NULL; 43 44static void create_services(AvahiClient *c); 45 46static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { 47 assert(g == group || group == NULL); 48 group = g; 49 50 /* Called whenever the entry group state changes */ 51 52 switch (state) { 53 case AVAHI_ENTRY_GROUP_ESTABLISHED : 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 with a remote service 62 * happened. Let's pick a new name */ 63 n = avahi_alternative_service_name(name); 64 avahi_free(name); 65 name = n; 66 67 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); 68 69 /* And recreate the services */ 70 create_services(avahi_entry_group_get_client(g)); 71 break; 72 } 73 74 case AVAHI_ENTRY_GROUP_FAILURE : 75 76 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g)))); 77 78 /* Some kind of failure happened while we were registering our services */ 79 avahi_simple_poll_quit(simple_poll); 80 break; 81 82 case AVAHI_ENTRY_GROUP_UNCOMMITED: 83 case AVAHI_ENTRY_GROUP_REGISTERING: 84 ; 85 } 86} 87 88static void create_services(AvahiClient *c) { 89 char *n, r[128]; 90 int ret; 91 assert(c); 92 93 /* If this is the first time we're called, let's create a new 94 * entry group if necessary */ 95 96 if (!group) 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))); 99 goto fail; 100 } 101 102 /* If the group is empty (either because it was just created, or 103 * because it was reset previously, add our entries. */ 104 105 if (avahi_entry_group_is_empty(group)) { 106 fprintf(stderr, "Adding service '%s'\n", name); 107 108 /* Create some random TXT data */ 109 snprintf(r, sizeof(r), "random=%i", rand()); 110 111 /* We will now add two services and one subtype to the entry 112 * group. The two services have the same name, but differ in 113 * the service type (IPP vs. BSD LPR). Only services with the 114 * same name should be put in the same entry group. */ 115 116 /* Add the service for IPP */ 117 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) { 118 119 if (ret == AVAHI_ERR_COLLISION) 120 goto collision; 121 122 fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret)); 123 goto fail; 124 } 125 126 /* Add the same service for BSD LPR */ 127 if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) { 128 129 if (ret == AVAHI_ERR_COLLISION) 130 goto collision; 131 132 fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret)); 133 goto fail; 134 } 135 136 /* Add an additional (hypothetic) subtype */ 137 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)) { 138 fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret)); 139 goto fail; 140 } 141 142 /* Tell the server to register the service */ 143 if ((ret = avahi_entry_group_commit(group)) < 0) { 144 fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret)); 145 goto fail; 146 } 147 } 148 149 return; 150 151collision: 152 153 /* A service name collision with a local service happened. Let's 154 * pick a new name */ 155 n = avahi_alternative_service_name(name); 156 avahi_free(name); 157 name = n; 158 159 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); 160 161 avahi_entry_group_reset(group); 162 163 create_services(c); 164 return; 165 166fail: 167 avahi_simple_poll_quit(simple_poll); 168} 169 170static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { 171 assert(c); 172 173 /* Called whenever the client or server state changes */ 174 175 switch (state) { 176 case AVAHI_CLIENT_S_RUNNING: 177 178 /* The server has startup successfully and registered its host 179 * name on the network, so it's time to create our services */ 180 create_services(c); 181 break; 182 183 case AVAHI_CLIENT_FAILURE: 184 185 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c))); 186 avahi_simple_poll_quit(simple_poll); 187 188 break; 189 190 case AVAHI_CLIENT_S_COLLISION: 191 192 /* Let's drop our registered services. When the server is back 193 * in AVAHI_SERVER_RUNNING state we will register them 194 * again with the new host name. */ 195 196 case AVAHI_CLIENT_S_REGISTERING: 197 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 213static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) { 214 AvahiClient *client = 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 234int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { 235 AvahiClient *client = NULL; 236 int error; 237 int ret = 1; 238 struct timeval tv; 239 240 /* Allocate main loop object */ 241 if (!(simple_poll = avahi_simple_poll_new())) { 242 fprintf(stderr, "Failed to create simple poll object.\n"); 243 goto fail; 244 } 245 246 name = avahi_strdup("MegaPrinter"); 247 248 /* Allocate a new client */ 249 client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); 250 251 /* Check wether creating the client object succeeded */ 252 if (!client) { 253 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); 254 goto fail; 255 } 256 257 /* After 10s do some weird modification to the service */ 258 avahi_simple_poll_get(simple_poll)->timeout_new( 259 avahi_simple_poll_get(simple_poll), 260 avahi_elapse_time(&tv, 1000*10, 0), 261 modify_callback, 262 client); 263 264 /* Run the main loop */ 265 avahi_simple_poll_loop(simple_poll); 266 267 ret = 0; 268 269fail: 270 271 /* Cleanup things */ 272 273 if (client) 274 avahi_client_free(client); 275 276 if (simple_poll) 277 avahi_simple_poll_free(simple_poll); 278 279 avahi_free(name); 280 281 return ret; 282} 283