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