26 #include <avahi-client/client.h> 27 #include <avahi-client/publish.h> 29 #include <avahi-common/alternative.h> 30 #include <avahi-common/simple-watch.h> 31 #include <avahi-common/malloc.h> 32 #include <avahi-common/error.h> 33 #include <avahi-common/timeval.h> 35 static AvahiEntryGroup *
group = NULL;
41 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED
void *userdata) {
48 case AVAHI_ENTRY_GROUP_ESTABLISHED :
49 std::cout <<
"Entry group established" << std::endl;
51 fprintf(stderr,
"Service '%s' successfully established.\n",
name);
54 case AVAHI_ENTRY_GROUP_COLLISION : {
56 std::cout <<
"Entry group collision" << std::endl;
60 n = avahi_alternative_service_name(
name);
64 fprintf(stderr,
"Service name collision, renaming service to '%s'\n",
name);
71 case AVAHI_ENTRY_GROUP_FAILURE :
73 std::cout <<
"Entry group failure" << std::endl;
74 fprintf(stderr,
"Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
80 case AVAHI_ENTRY_GROUP_UNCOMMITED:
81 std::cout <<
"Entry group uncommitted" << std::endl;
83 case AVAHI_ENTRY_GROUP_REGISTERING:
84 std::cout <<
"Entry group registering" << std::endl;
99 fprintf(stderr,
"avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
106 if (avahi_entry_group_is_empty(
group)) {
107 fprintf(stderr,
"Adding service '%s'\n",
name);
110 snprintf(r,
sizeof(r),
"random=%i", rand());
118 if ((ret = avahi_entry_group_add_service(
group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0),
name,
"_ipp._tcp", NULL, NULL, 651,
"test=blah", r, NULL)) < 0) {
120 if (ret == AVAHI_ERR_COLLISION)
123 fprintf(stderr,
"Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
128 if ((ret = avahi_entry_group_add_service(
group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0),
name,
"_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
130 if (ret == AVAHI_ERR_COLLISION)
133 fprintf(stderr,
"Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
138 if ((ret = avahi_entry_group_add_service_subtype(
group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0),
name,
"_printer._tcp", NULL,
"_magic._sub._printer._tcp") < 0)) {
139 fprintf(stderr,
"Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
144 if ((ret = avahi_entry_group_commit(
group)) < 0) {
145 fprintf(stderr,
"Failed to commit entry group: %s\n", avahi_strerror(ret));
156 n = avahi_alternative_service_name(
name);
160 fprintf(stderr,
"Service name collision, renaming service to '%s'\n",
name);
162 avahi_entry_group_reset(
group);
171 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED
void * userdata) {
177 case AVAHI_CLIENT_S_RUNNING:
184 case AVAHI_CLIENT_FAILURE:
186 fprintf(stderr,
"Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
191 case AVAHI_CLIENT_S_COLLISION:
197 case AVAHI_CLIENT_S_REGISTERING:
204 avahi_entry_group_reset(
group);
208 case AVAHI_CLIENT_CONNECTING:
214 AvahiClient *client =
static_cast<AvahiClient*
>(userdata);
216 fprintf(stderr,
"Doing some weird modification\n");
219 name = avahi_strdup(
"Modified MegaPrinter");
223 if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
227 avahi_entry_group_reset(
group);
238 int main(AVAHI_GCC_UNUSED
int argc, AVAHI_GCC_UNUSED
char **argv) {
240 AvahiClient *client = NULL;
247 fprintf(stderr,
"Failed to create simple poll object.\n");
251 name = avahi_strdup(
"MegaPrinter");
254 client = avahi_client_new(avahi_simple_poll_get(
simple_poll), static_cast<AvahiClientFlags>(0),
client_callback, NULL, &error);
258 fprintf(stderr,
"Failed to create client: %s\n", avahi_strerror(error));
265 avahi_elapse_time(&tv, 1000*10, 0),
279 avahi_client_free(client);
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void *userdata)
static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata)
static void create_services(AvahiClient *c)
static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata)
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char **argv)
static AvahiSimplePoll * simple_poll
static AvahiEntryGroup * group