- /* Add an additional (hypothetic) subtype */
- 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)) {
- fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
- goto fail;
- }
-
- /* Tell the server to register the service */
- if ((ret = avahi_entry_group_commit(group)) < 0) {
- fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret));
- goto fail;
+ if (avahi_entry_group_is_empty(group)) {
+ fprintf(stderr, "Adding service '%s'\n", name);
+
+ /* Create some random TXT data */
+ snprintf(r, sizeof(r), "random=%i", rand());
+
+ /* Add the service for IPP */
+ 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) {
+
+ if (ret == AVAHI_ERR_COLLISION) {
+ char *n;
+
+ /* A service name collision with a local service
+ * happened. Let's pick a new name */
+ n = avahi_alternative_service_name(name);
+ avahi_free(name);
+ name = n;
+
+ fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
+
+ create_services(c);
+ return;
+ }
+
+ fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
+ goto fail;
+ }
+
+ /* Add the same service for BSD LPR */
+ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
+ fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
+ goto fail;
+ }
+
+ /* Add an additional (hypothetic) subtype */
+ 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)) {
+ fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
+ goto fail;
+ }
+
+ /* Tell the server to register the service */
+ if ((ret = avahi_entry_group_commit(group)) < 0) {
+ fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
+ goto fail;
+ }