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 <stdlib.h> 27#include <assert.h> 28 29#include <avahi-common/malloc.h> 30#include <avahi-common/simple-watch.h> 31#include <avahi-common/alternative.h> 32#include <avahi-common/timeval.h> 33 34#include <avahi-core/core.h> 35#include <avahi-core/log.h> 36#include <avahi-core/publish.h> 37#include <avahi-core/lookup.h> 38 39#define DOMAIN NULL 40#define SERVICE_TYPE "_http._tcp" 41 42static AvahiSServiceBrowser *service_browser1 = NULL, *service_browser2 = NULL; 43static const AvahiPoll * poll_api = NULL; 44static AvahiServer *server = NULL; 45static AvahiSimplePoll *simple_poll; 46 47static const char *browser_event_to_string(AvahiBrowserEvent event) { 48 switch (event) { 49 case AVAHI_BROWSER_NEW : return "NEW"; 50 case AVAHI_BROWSER_REMOVE : return "REMOVE"; 51 case AVAHI_BROWSER_CACHE_EXHAUSTED : return "CACHE_EXHAUSTED"; 52 case AVAHI_BROWSER_ALL_FOR_NOW : return "ALL_FOR_NOW"; 53 case AVAHI_BROWSER_FAILURE : return "FAILURE"; 54 } 55 56 abort(); 57} 58 59static void sb_callback( 60 AvahiSServiceBrowser *b, 61 AvahiIfIndex iface, 62 AvahiProtocol protocol, 63 AvahiBrowserEvent event, 64 const char *name, 65 const char *service_type, 66 const char *domain, 67 AvahiLookupResultFlags flags, 68 AVAHI_GCC_UNUSED void* userdata) { 69 avahi_log_debug("SB%i: (%i.%s) <%s> as <%s> in <%s> [%s] cached=%i", b == service_browser1 ? 1 : 2, iface, avahi_proto_to_string(protocol), name, service_type, domain, browser_event_to_string(event), !!(flags & AVAHI_LOOKUP_RESULT_CACHED)); 70} 71 72static void create_second_service_browser(AvahiTimeout *timeout, AVAHI_GCC_UNUSED void* userdata) { 73 74 service_browser2 = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, SERVICE_TYPE, DOMAIN, 0, sb_callback, NULL); 75 assert(service_browser2); 76 77 poll_api->timeout_free(timeout); 78} 79 80static void quit(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) { 81 avahi_simple_poll_quit(simple_poll); 82} 83 84int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { 85 struct timeval tv; 86 AvahiServerConfig config; 87 88 simple_poll = avahi_simple_poll_new(); 89 assert(simple_poll); 90 91 poll_api = avahi_simple_poll_get(simple_poll); 92 assert(poll_api); 93 94 avahi_server_config_init(&config); 95 config.publish_hinfo = 0; 96 config.publish_addresses = 0; 97 config.publish_workstation = 0; 98 config.publish_domain = 0; 99 100 avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]); 101 config.n_wide_area_servers = 1; 102 config.enable_wide_area = 1; 103 104 server = avahi_server_new(poll_api, &config, NULL, NULL, NULL); 105 assert(server); 106 avahi_server_config_free(&config); 107 108 service_browser1 = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, SERVICE_TYPE, DOMAIN, 0, sb_callback, NULL); 109 assert(service_browser1); 110 111 poll_api->timeout_new(poll_api, avahi_elapse_time(&tv, 10000, 0), create_second_service_browser, NULL); 112 113 poll_api->timeout_new(poll_api, avahi_elapse_time(&tv, 60000, 0), quit, NULL); 114 115 116 for (;;) 117 if (avahi_simple_poll_iterate(simple_poll, -1) != 0) 118 break; 119 120 avahi_server_free(server); 121 avahi_simple_poll_free(simple_poll); 122 123 return 0; 124} 125