Example how to register a DNS-SD service using the client interface to avahi-daemon. It behaves like a network printer registering both an IPP and a BSD LPR service.
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <avahi-common/timeval.h>
static char *name = NULL;
assert(g == group || group == NULL);
group = g;
switch (state) {
fprintf(stderr, "Service '%s' successfully established.\n", name);
break;
char *n;
name = n;
fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
break;
}
break;
;
}
}
char *n, r[128];
int ret;
assert(c);
if (!group)
goto fail;
}
fprintf(stderr, "Adding service '%s'\n", name);
snprintf(r, sizeof(r), "random=%i", rand());
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) {
goto collision;
fprintf(stderr,
"Failed to add _ipp._tcp service: %s\n",
avahi_strerror(ret));
goto fail;
}
goto collision;
fprintf(stderr,
"Failed to add _printer._tcp service: %s\n",
avahi_strerror(ret));
goto fail;
}
fprintf(stderr,
"Failed to add subtype _magic._sub._printer._tcp: %s\n",
avahi_strerror(ret));
goto fail;
}
fprintf(stderr,
"Failed to commit entry group: %s\n",
avahi_strerror(ret));
goto fail;
}
}
return;
collision:
name = n;
fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
create_services(c);
return;
fail:
}
assert(c);
switch (state) {
create_services(c);
break;
break;
if (group)
break;
;
}
}
static void modify_callback(AVAHI_GCC_UNUSED
AvahiTimeout *e,
void *userdata) {
fprintf(stderr, "Doing some weird modification\n");
if (group)
create_services(client);
}
}
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
int error;
int ret = 1;
struct timeval tv;
fprintf(stderr, "Failed to create simple poll object.\n");
goto fail;
}
if (!client) {
fprintf(stderr,
"Failed to create client: %s\n",
avahi_strerror(error));
goto fail;
}
avahi_elapse_time(&tv, 1000*10, 0),
modify_callback,
client);
ret = 0;
fail:
if (client)
if (simple_poll)
return ret;
}