summaryrefslogtreecommitdiff
path: root/libre/cups-libre/cups-avahi-2-backend.patch
blob: 9fa367729aaf7b9e7de031072573d441d1d03fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
diff -up cups-1.5.3/backend/dnssd.c.avahi-2-backend cups-1.5.3/backend/dnssd.c
--- cups-1.5.3/backend/dnssd.c.avahi-2-backend	2012-05-15 16:53:18.164774446 +0200
+++ cups-1.5.3/backend/dnssd.c	2012-05-15 17:09:07.684155704 +0200
@@ -15,14 +15,21 @@
  *
  * Contents:
  *
+ *   next_txt_record()       - Get next TXT record from a cups_txt_records_t.
+ *   parse_txt_record_pair() - Read key/value pair in cups_txt_records_t.
  *   main()                  - Browse for printers.
  *   browse_callback()       - Browse devices.
  *   browse_local_callback() - Browse local devices.
  *   compare_devices()       - Compare two devices.
  *   exec_backend()          - Execute the backend that corresponds to the
  *                             resolved service name.
+ *   device_type()           - Get DNS-SD type enumeration from string.
  *   get_device()            - Create or update a device.
  *   query_callback()        - Process query data.
+ *   avahi_client_callback() - Avahi client callback function.
+ *   avahi_query_callback()  - Avahi query callback function.
+ *   avahi_browse_callback() - Avahi browse callback function.
+ *   find_device()           - Find a device from its name and domain.
  *   sigterm_handler()       - Handle termination signals...
  *   unquote()               - Unquote a name string.
  */
@@ -33,7 +40,18 @@
 
 #include "backend-private.h"
 #include <cups/array.h>
-#include <dns_sd.h>
+#ifdef HAVE_DNSSD
+#  include <dns_sd.h>
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+#  include <avahi-client/client.h>
+#  include <avahi-client/lookup.h>
+#  include <avahi-common/simple-watch.h>
+#  include <avahi-common/domain.h>
+#  include <avahi-common/error.h>
+#  include <avahi-common/malloc.h>
+#define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
+#endif /* HAVE_AVAHI */
 
 
 /*
@@ -53,7 +71,12 @@ typedef enum
 
 typedef struct
 {
+#ifdef HAVE_DNSSD
   DNSServiceRef	ref;			/* Service reference for resolve */
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  int		resolved;		/* Did we resolve the device? */
+#endif /* HAVE_AVAHI */
   char		*name,			/* Service name */
 		*domain,		/* Domain name */
 		*fullName,		/* Full name */
@@ -65,6 +88,20 @@ typedef struct
 		sent;			/* Did we list the device? */
 } cups_device_t;
 
+typedef struct
+{
+  char key[256];
+  char value[256];
+
+#ifdef HAVE_DNSSD
+  const uint8_t *data;
+  const uint8_t *datanext;
+  const uint8_t *dataend;
+#else /* HAVE_AVAHI */
+  AvahiStringList *txt;
+#endif /* HAVE_DNSSD */
+} cups_txt_records_t;
+
 
 /*
  * Local globals...
@@ -78,6 +115,7 @@ static int		job_canceled = 0;
  * Local functions...
  */
 
+#ifdef HAVE_DNSSD
 static void		browse_callback(DNSServiceRef sdRef,
 			                DNSServiceFlags flags,
 				        uint32_t interfaceIndex,
@@ -95,13 +133,6 @@ static void		browse_local_callback(DNSSe
 					      const char *replyDomain,
 					      void *context)
 					      __attribute__((nonnull(1,5,6,7,8)));
-static int		compare_devices(cups_device_t *a, cups_device_t *b);
-static void		exec_backend(char **argv);
-static cups_device_t	*get_device(cups_array_t *devices,
-			            const char *serviceName,
-			            const char *regtype,
-				    const char *replyDomain)
-				    __attribute__((nonnull(1,2,3,4)));
 static void		query_callback(DNSServiceRef sdRef,
 			               DNSServiceFlags flags,
 				       uint32_t interfaceIndex,
@@ -111,10 +142,119 @@ static void		query_callback(DNSServiceRe
 				       const void *rdata, uint32_t ttl,
 				       void *context)
 				       __attribute__((nonnull(1,5,9,11)));
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+static void		avahi_client_callback (AvahiClient *client,
+					       AvahiClientState state,
+					       void *context);
+static void		avahi_browse_callback (AvahiServiceBrowser *browser,
+					       AvahiIfIndex interface,
+					       AvahiProtocol protocol,
+					       AvahiBrowserEvent event,
+					       const char *serviceName,
+					       const char *regtype,
+					       const char *replyDomain,
+					       AvahiLookupResultFlags flags,
+					       void *context);
+#endif /* HAVE_AVAHI */
+static cups_device_t *	find_device (cups_array_t *devices,
+				     cups_txt_records_t *txt,
+				     cups_device_t *dkey);
+static int		compare_devices(cups_device_t *a, cups_device_t *b);
+static void		exec_backend(char **argv);
+static cups_device_t	*get_device(cups_array_t *devices,
+			            const char *serviceName,
+			            const char *regtype,
+				    const char *replyDomain)
+				    __attribute__((nonnull(1,2,3,4)));
 static void		sigterm_handler(int sig);
 static void		unquote(char *dst, const char *src, size_t dstsize)
 			    __attribute__((nonnull(1,2)));
 
+#ifdef HAVE_AVAHI
+static AvahiSimplePoll *simple_poll = NULL;
+static int avahi_got_callback;
+#endif /* HAVE_AVAHI */
+
+
+/*
+ * 'next_txt_record()' - Get next TXT record from a cups_txt_records_t.
+ */
+
+static cups_txt_records_t *
+next_txt_record (cups_txt_records_t *txt)
+{
+#ifdef HAVE_DNSSD
+  txt->data = txt->datanext;
+#else /* HAVE_AVAHI */
+  txt->txt = avahi_string_list_get_next (txt->txt);
+  if (txt->txt == NULL)
+    return NULL;
+#endif /* HAVE_DNSSD */
+
+  return txt;
+}
+
+
+/*
+ * 'parse_txt_record_pair()' - Read key/value pair in cups_txt_records_t.
+ */
+
+static int
+parse_txt_record_pair (cups_txt_records_t *txt)
+{
+#ifdef HAVE_DNSSD
+  uint8_t	datalen;
+  uint8_t	*data = txt->data;
+  char		*ptr;
+
+ /*
+  * Read a key/value pair starting with an 8-bit length.  Since the
+  * length is 8 bits and the size of the key/value buffers is 256, we
+  * don't need to check for overflow...
+  */
+
+  datalen = *data++;
+  if (!datalen || (data + datalen) > txt->dataend)
+    return NULL;
+  txt->datanext = data + datalen;
+
+  for (ptr = txt->key; data < txt->datanext && *data != '='; data ++)
+    *ptr++ = *data;
+  *ptr = '\0';
+
+  if (data < txt->datanext && *data == '=')
+  {
+    data++;
+
+    if (data < datanext)
+      memcpy (txt->value, data, txt->datanext - data);
+    value[txt->datanext - data] = '\0';
+  }
+  else
+    return 1;
+#else /* HAVE_AVAHI */
+  char *key, *value;
+  size_t len;
+  avahi_string_list_get_pair (txt->txt, &key, &value, &len);
+  if (len > sizeof (txt->value) - 1)
+    len = sizeof (txt->value) - 1;
+
+  memcpy (txt->value, value, len);
+  txt->value[len] = '\0';
+  len = strlen (key);
+  if (len > sizeof (txt->key) - 1)
+    len = sizeof (txt->key) - 1;
+
+  memcpy (txt->key, key, len);
+  txt->key[len] = '\0';
+  avahi_free (key);
+  avahi_free (value);
+#endif /* HAVE_AVAHI */
+
+  return 0;
+}
+
 
 /*
  * 'main()' - Browse for printers.
@@ -125,6 +265,13 @@ main(int  argc,				/* I - Number of comm
      char *argv[])			/* I - Command-line arguments */
 {
   const char	*name;			/* Backend name */
+  cups_array_t	*devices;		/* Device array */
+  cups_device_t	*device;		/* Current device */
+  char		uriName[1024];		/* Unquoted fullName for URI */
+#ifdef HAVE_DNSSD
+  int		fd;			/* Main file descriptor */
+  fd_set	input;			/* Input set for select() */
+  struct timeval timeout;		/* Timeout for select() */
   DNSServiceRef	main_ref,		/* Main service reference */
 		fax_ipp_ref,		/* IPP fax service reference */
 		ipp_ref,		/* IPP service reference */
@@ -138,12 +285,11 @@ main(int  argc,				/* I - Number of comm
 		pdl_datastream_ref,	/* AppSocket service reference */
 		printer_ref,		/* LPD service reference */
 		riousbprint_ref;	/* Remote IO service reference */
-  int		fd;			/* Main file descriptor */
-  fd_set	input;			/* Input set for select() */
-  struct timeval timeout;		/* Timeout for select() */
-  cups_array_t	*devices;		/* Device array */
-  cups_device_t	*device;		/* Current device */
-  char		uriName[1024];		/* Unquoted fullName for URI */
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  AvahiClient	*client;
+  int		error;
+#endif /* HAVE_AVAHI */
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
   struct sigaction action;		/* Actions for POSIX signals */
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
@@ -203,6 +349,49 @@ main(int  argc,				/* I - Number of comm
   * Browse for different kinds of printers...
   */
 
+#ifdef HAVE_AVAHI
+  if ((simple_poll = avahi_simple_poll_new ()) == NULL)
+  {
+    perror ("ERROR: Unable to create avahi simple poll object");
+    return (1);
+  }
+
+  client = avahi_client_new (avahi_simple_poll_get (simple_poll),
+			     0, avahi_client_callback, NULL, &error);
+  if (!client)
+  {
+    perror ("DEBUG: Unable to create avahi client");
+    return (0);
+  }
+
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
+			     AVAHI_PROTO_UNSPEC,
+			     "_fax-ipp._tcp", NULL, 0,
+			     avahi_browse_callback, devices);
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
+			     AVAHI_PROTO_UNSPEC,
+			     "_ipp._tcp", NULL, 0,
+			     avahi_browse_callback, devices);
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
+			     AVAHI_PROTO_UNSPEC,
+			     "_ipp-tls._tcp", NULL, 0,
+			     avahi_browse_callback, devices);
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
+			     AVAHI_PROTO_UNSPEC,
+			     "_pdl-datastream._tcp",
+			     NULL, 0,
+			     avahi_browse_callback,
+			     devices);
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
+			     AVAHI_PROTO_UNSPEC,
+			     "_printer._tcp", NULL, 0,
+			     avahi_browse_callback, devices);
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
+			     AVAHI_PROTO_UNSPEC,
+			     "_riousbprint._tcp", NULL, 0,
+			     avahi_browse_callback, devices);
+#endif /* HAVE_AVAHI */
+#ifdef HAVE_DNSSD
   if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
   {
     perror("ERROR: Unable to create service connection");
@@ -263,6 +452,7 @@ main(int  argc,				/* I - Number of comm
   riousbprint_ref = main_ref;
   DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
                    "_riousbprint._tcp", NULL, browse_callback, devices);
+#endif /* HAVE_DNSSD */
 
  /*
   * Loop until we are killed...
@@ -270,6 +460,9 @@ main(int  argc,				/* I - Number of comm
 
   while (!job_canceled)
   {
+    int announce = 0;
+
+#ifdef HAVE_DNSSD
     FD_ZERO(&input);
     FD_SET(fd, &input);
 
@@ -289,11 +482,35 @@ main(int  argc,				/* I - Number of comm
     }
     else
     {
+      announce = 1;
+    }
+#else /* HAVE_AVAHI */
+    int r;
+    avahi_got_callback = 0;
+    r = avahi_simple_poll_iterate (simple_poll, 1);
+    if (r != 0 && r != EINTR)
+    {
+     /*
+      * We've been told to exit the loop.  Perhaps the connection to
+      * avahi failed.
+      */
+
+      break;
+    }
+
+    if (avahi_got_callback)
+      announce = 1;
+#endif /* HAVE_DNSSD */
+
+    if (announce)
+    {
      /*
       * Announce any devices we've found...
       */
 
+#ifdef HAVE_DNSSD
       DNSServiceErrorType status;	/* DNS query status */
+#endif /* HAVE_DNSSD */
       cups_device_t *best;		/* Best matching device */
       char	device_uri[1024];	/* Device URI */
       int	count;			/* Number of queries */
@@ -307,6 +524,7 @@ main(int  argc,				/* I - Number of comm
         if (device->sent)
 	  sent ++;
 
+#ifdef HAVE_DNSSD
         if (device->ref)
 	  count ++;
 
@@ -338,14 +556,23 @@ main(int  argc,				/* I - Number of comm
 	      count ++;
           }
 	}
-	else if (!device->sent)
+	else
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+	if (!device->resolved)
+	  continue;
+        else
+#endif /* HAVE_AVAHI */
+	if (!device->sent)
 	{
+#ifdef HAVE_DNSSD
 	 /*
 	  * Got the TXT records, now report the device...
 	  */
 
 	  DNSServiceRefDeallocate(device->ref);
 	  device->ref = 0;
+#endif /* HAVE_DNSSD */
 
           if (!best)
 	    best = device;
@@ -406,6 +633,7 @@ main(int  argc,				/* I - Number of comm
 }
 
 
+#ifdef HAVE_DNSSD
 /*
  * 'browse_callback()' - Browse devices.
  */
@@ -494,6 +722,7 @@ browse_local_callback(
 	  device->fullName);
   device->sent = 1;
 }
+#endif /* HAVE_DNSSD */
 
 
 /*
@@ -574,6 +803,41 @@ exec_backend(char **argv)		/* I - Comman
 
 
 /*
+ * 'device_type()' - Get DNS-SD type enumeration from string.
+ */
+
+static int
+device_type (const char *regtype)
+{
+#ifdef HAVE_AVAHI
+  if (!strcmp(regtype, "_ipp._tcp"))
+    return (CUPS_DEVICE_IPP);
+  else if (!strcmp(regtype, "_ipps._tcp") ||
+	   !strcmp(regtype, "_ipp-tls._tcp"))
+    return (CUPS_DEVICE_IPPS);
+  else if (!strcmp(regtype, "_fax-ipp._tcp"))
+    return (CUPS_DEVICE_FAX_IPP);
+  else if (!strcmp(regtype, "_printer._tcp"))
+    return (CUPS_DEVICE_PDL_DATASTREAM);
+#else
+  if (!strcmp(regtype, "_ipp._tcp."))
+    return (CUPS_DEVICE_IPP);
+  else if (!strcmp(regtype, "_ipps._tcp.") ||
+	   !strcmp(regtype, "_ipp-tls._tcp."))
+    return (CUPS_DEVICE_IPPS);
+  else if (!strcmp(regtype, "_fax-ipp._tcp."))
+    return (CUPS_DEVICE_FAX_IPP);
+  else if (!strcmp(regtype, "_printer._tcp."))
+    return (CUPS_DEVICE_PRINTER);
+  else if (!strcmp(regtype, "_pdl-datastream._tcp."))
+    return (CUPS_DEVICE_PDL_DATASTREAM);
+#endif /* HAVE_AVAHI */
+
+  return (CUPS_DEVICE_RIOUSBPRINT);
+}
+
+
+/*
  * 'get_device()' - Create or update a device.
  */
 
@@ -594,20 +858,7 @@ get_device(cups_array_t *devices,	/* I -
   */
 
   key.name = (char *)serviceName;
-
-  if (!strcmp(regtype, "_ipp._tcp."))
-    key.type = CUPS_DEVICE_IPP;
-  else if (!strcmp(regtype, "_ipps._tcp.") ||
-	   !strcmp(regtype, "_ipp-tls._tcp."))
-    key.type = CUPS_DEVICE_IPPS;
-  else if (!strcmp(regtype, "_fax-ipp._tcp."))
-    key.type = CUPS_DEVICE_FAX_IPP;
-  else if (!strcmp(regtype, "_printer._tcp."))
-    key.type = CUPS_DEVICE_PRINTER;
-  else if (!strcmp(regtype, "_pdl-datastream._tcp."))
-    key.type = CUPS_DEVICE_PDL_DATASTREAM;
-  else
-    key.type = CUPS_DEVICE_RIOUSBPRINT;
+  key.type = device_type (regtype);
 
   for (device = cupsArrayFind(devices, &key);
        device;
@@ -627,8 +878,14 @@ get_device(cups_array_t *devices,	/* I -
         free(device->domain);
 	device->domain = strdup(replyDomain);
 
+#ifdef HAVE_DNSSD
 	DNSServiceConstructFullName(fullName, device->name, regtype,
 	                            replyDomain);
+#else /* HAVE_AVAHI */
+	avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
+				 serviceName, regtype, replyDomain);
+#endif /* HAVE_DNSSD */
+
 	free(device->fullName);
 	device->fullName = strdup(fullName);
       }
@@ -648,6 +905,9 @@ get_device(cups_array_t *devices,	/* I -
   device->domain   = strdup(replyDomain);
   device->type     = key.type;
   device->priority = 50;
+#ifdef HAVE_AVAHI
+  device->resolved = 0;
+#endif /* HAVE_AVAHI */
 
   cupsArrayAdd(devices, device);
 
@@ -655,13 +915,20 @@ get_device(cups_array_t *devices,	/* I -
   * Set the "full name" of this service, which is used for queries...
   */
 
+#ifdef HAVE_DNSSD
   DNSServiceConstructFullName(fullName, serviceName, regtype, replyDomain);
+#else /* HAVE_AVAHI */
+  avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
+			   serviceName, regtype, replyDomain);
+#endif /* HAVE_DNSSD */
+
   device->fullName = strdup(fullName);
 
   return (device);
 }
 
 
+#ifdef HAVE_DNSSD
 /*
  * 'query_callback()' - Process query data.
  */
@@ -685,7 +952,7 @@ query_callback(
 		*ptr;			/* Pointer into string */
   cups_device_t	dkey,			/* Search key */
 		*device;		/* Device */
-
+  cups_txt_records_t txt;
 
   fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
                   "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
@@ -719,94 +986,233 @@ query_callback(
   if ((ptr = strstr(name, "._")) != NULL)
     *ptr = '\0';
 
-  if (strstr(fullName, "_ipp._tcp."))
-    dkey.type = CUPS_DEVICE_IPP;
-  else if (strstr(fullName, "_ipps._tcp.") ||
-           strstr(fullName, "_ipp-tls._tcp."))
-    dkey.type = CUPS_DEVICE_IPPS;
-  else if (strstr(fullName, "_fax-ipp._tcp."))
-    dkey.type = CUPS_DEVICE_FAX_IPP;
-  else if (strstr(fullName, "_printer._tcp."))
-    dkey.type = CUPS_DEVICE_PRINTER;
-  else if (strstr(fullName, "_pdl-datastream._tcp."))
-    dkey.type = CUPS_DEVICE_PDL_DATASTREAM;
+  dkey.type = device_type (fullName);
+
+  txt.data = rdata;
+  txt.dataend = rdata + rdlen;
+  device = find_device ((cups_array_t *) context, &txt, &dkey);
+  if (!device)
+    fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
+}
+#endif /* HAVE_DNSSD */
+
+
+#ifdef HAVE_AVAHI
+/*
+ * 'avahi_client_callback()' - Avahi client callback function.
+ */
+
+static void
+avahi_client_callback(AvahiClient *client,
+		      AvahiClientState state,
+		      void *context)
+{
+ /*
+  * If the connection drops, quit.
+  */
+
+  if (state == AVAHI_CLIENT_FAILURE)
+  {
+    fprintf (stderr, "ERROR: Avahi connection failed\n");
+    avahi_simple_poll_quit (simple_poll);
+  }
+}
+
+
+/*
+ * 'avahi_query_callback()' - Avahi query callback function.
+ */
+
+static void
+avahi_query_callback(AvahiServiceResolver *resolver,
+		     AvahiIfIndex interface,
+		     AvahiProtocol protocol,
+		     AvahiResolverEvent event,
+		     const char *name,
+		     const char *type,
+		     const char *domain,
+		     const char *host_name,
+		     const AvahiAddress *address,
+		     uint16_t port,
+		     AvahiStringList *txt,
+		     AvahiLookupResultFlags flags,
+		     void *context)
+{
+  AvahiClient		*client;
+  cups_device_t		key,
+			*device;
+  char			uqname[1024],
+			*ptr;
+  cups_txt_records_t	txtr;
+
+  client = avahi_service_resolver_get_client (resolver);
+  if (event != AVAHI_RESOLVER_FOUND)
+  {
+    if (event == AVAHI_RESOLVER_FAILURE)
+    {
+      fprintf (stderr, "ERROR: %s\n",
+	       avahi_strerror (avahi_client_errno (client)));
+    }
+
+    avahi_service_resolver_free (resolver);
+    return;
+  }
+
+ /*
+  * Set search key for device.
+  */
+
+  key.name = uqname;
+  unquote (uqname, name, sizeof (uqname));
+  if ((ptr = strstr(name, "._")) != NULL)
+    *ptr = '\0';
+
+  key.domain = (char *) domain;
+  key.type = device_type (type);
+
+ /*
+  * Find the device and the the TXT information.
+  */
+
+  txtr.txt = txt;
+  device = find_device ((cups_array_t *) context, &txtr, &key);
+  if (device)
+  {
+   /*
+    * Let the main loop know to announce the device.
+    */
+
+    device->resolved = 1;
+    avahi_got_callback = 1;
+  }
   else
-    dkey.type = CUPS_DEVICE_RIOUSBPRINT;
+    fprintf (stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", name);
 
-  for (device = cupsArrayFind(devices, &dkey);
+  avahi_service_resolver_free (resolver);
+}
+
+
+/*
+ * 'avahi_browse_callback()' - Avahi browse callback function.
+ */
+
+static void
+avahi_browse_callback(AvahiServiceBrowser *browser,
+		      AvahiIfIndex interface,
+		      AvahiProtocol protocol,
+		      AvahiBrowserEvent event,
+		      const char *name,
+		      const char *type,
+		      const char *domain,
+		      AvahiLookupResultFlags flags,
+		      void *context)
+{
+  AvahiClient *client = avahi_service_browser_get_client (browser);
+
+  switch (event)
+  {
+  case AVAHI_BROWSER_FAILURE:
+    fprintf (stderr, "ERROR: %s\n",
+	     avahi_strerror (avahi_client_errno (client)));
+    avahi_simple_poll_quit (simple_poll);
+    return;
+
+  case AVAHI_BROWSER_NEW:
+   /*
+    * This object is new on the network.
+    */
+
+    if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
+    {
+     /*
+      * This comes from the local machine so ignore it.
+      */
+
+      fprintf (stderr, "DEBUG: ignoring local service %s\n", name);
+    }
+    else
+    {
+     /*
+      * Create a device entry for it if it doesn't yet exist.
+      */
+
+      get_device ((cups_array_t *)context, name, type, domain);
+
+     /*
+      * Now look for a TXT entry.
+      */
+
+      if (avahi_service_resolver_new (client, interface, protocol,
+				      name, type, domain,
+				      AVAHI_PROTO_UNSPEC, 0,
+				      avahi_query_callback, context) == NULL)
+      {
+	fprintf (stderr, "ERROR: failed to resolve service %s: %s\n",
+		 name, avahi_strerror (avahi_client_errno (client)));
+      }
+    }
+
+    break;
+
+  case AVAHI_BROWSER_REMOVE:
+  case AVAHI_BROWSER_ALL_FOR_NOW:
+  case AVAHI_BROWSER_CACHE_EXHAUSTED:
+    break;
+  }
+}
+#endif /* HAVE_AVAHI */
+
+
+/*
+ * 'find_device()' - Find a device from its name and domain.
+ */
+
+static cups_device_t *
+find_device (cups_array_t *devices,
+	     cups_txt_records_t *txt,
+	     cups_device_t *dkey)
+{
+  cups_device_t	*device;
+  char		*ptr;
+
+  for (device = cupsArrayFind(devices, dkey);
        device;
        device = cupsArrayNext(devices))
   {
-    if (_cups_strcasecmp(device->name, dkey.name) ||
-        _cups_strcasecmp(device->domain, dkey.domain))
+    if (_cups_strcasecmp(device->name, dkey->name) ||
+        _cups_strcasecmp(device->domain, dkey->domain))
     {
       device = NULL;
       break;
     }
-    else if (device->type == dkey.type)
+    else if (device->type == dkey->type)
     {
      /*
       * Found it, pull out the priority and make and model from the TXT
       * record and save it...
       */
 
-      const uint8_t	*data,		/* Pointer into data */
-			*datanext,	/* Next key/value pair */
-			*dataend;	/* End of entire TXT record */
-      uint8_t		datalen;	/* Length of current key/value pair */
-      char		key[256],	/* Key string */
-			value[256],	/* Value string */
-			make_and_model[512],
+      char		make_and_model[512],
 				      	/* Manufacturer and model */
 			model[256],	/* Model */
-			device_id[2048];/* 1284 device ID */
-
+			device_id[2048]; /* 1284 device ID */
 
       device_id[0]      = '\0';
       make_and_model[0] = '\0';
 
       strcpy(model, "Unknown");
 
-      for (data = rdata, dataend = data + rdlen;
-           data < dataend;
-           data = datanext)
+      for (;;)
       {
-       /*
-        * Read a key/value pair starting with an 8-bit length.  Since the
-	* length is 8 bits and the size of the key/value buffers is 256, we
-	* don't need to check for overflow...
-	*/
-
-        datalen = *data++;
-
-        if (!datalen || (data + datalen) > dataend)
-	  break;
-
-        datanext = data + datalen;
-
-        for (ptr = key; data < datanext && *data != '='; data ++)
-	  *ptr++ = *data;
-	*ptr = '\0';
+	char *key;
+	char *value;
 
-	if (data < datanext && *data == '=')
-	{
-	  data ++;
-
-	  if (data < datanext)
-	    memcpy(value, data, datanext - data);
-	  value[datanext - data] = '\0';
+	if (parse_txt_record_pair (txt))
+	  goto next;
 
-	  fprintf(stderr, "DEBUG2: query_callback: \"%s=%s\".\n",
-	          key, value);
-	}
-	else
-	{
-	  fprintf(stderr, "DEBUG2: query_callback: \"%s\" with no value.\n",
-	          key);
-	  continue;
-	}
-
-        if (!_cups_strncasecmp(key, "usb_", 4))
+	key = txt->key;
+	value = txt->value;
+        if (!strncasecmp(key, "usb_", 4))
 	{
 	 /*
 	  * Add USB device ID information...
@@ -861,6 +1267,10 @@ query_callback(
 	  if (device->type == CUPS_DEVICE_PRINTER)
 	    device->sent = 1;
 	}
+
+      next:
+	if (next_txt_record (txt) == NULL)
+	  break;
       }
 
       if (device->device_id)
@@ -917,11 +1327,9 @@ query_callback(
     }
   }
 
-  if (!device)
-    fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
+  return device;
 }
 
-
 /*
  * 'sigterm_handler()' - Handle termination signals...
  */
diff -up cups-1.5.3/cups/http-support.c.avahi-2-backend cups-1.5.3/cups/http-support.c
--- cups-1.5.3/cups/http-support.c.avahi-2-backend	2012-02-15 02:06:12.000000000 +0100
+++ cups-1.5.3/cups/http-support.c	2012-05-15 17:04:51.045944634 +0200
@@ -43,6 +43,10 @@
  *   http_copy_decode()   - Copy and decode a URI.
  *   http_copy_encode()   - Copy and encode a URI.
  *   http_resolve_cb()    - Build a device URI for the given service name.
+ *   avahi_resolve_uri_client_cb()
+ *                        - Avahi client callback for resolving URI.
+ *   avahi_resolve_uri_resolver_cb()
+ *                        - Avahi resolver callback for resolving URI.
  */
 
 /*
@@ -60,6 +64,11 @@
 #    include <sys/select.h>
 #  endif /* WIN32 */
 #endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+#  include <avahi-client/client.h>
+#  include <avahi-client/lookup.h>
+#  include <avahi-common/simple-watch.h>
+#endif /* HAVE_AVAHI */
 
 
 /*
@@ -127,6 +136,24 @@ static void DNSSD_API	http_resolve_cb(DN
 					void *context);
 #endif /* HAVE_DNSSD */
 
+#ifdef HAVE_AVAHI
+static void	avahi_resolve_uri_client_cb(AvahiClient *client,
+					    AvahiClientState state,
+					    void *simple_poll);
+static void	avahi_resolve_uri_resolver_cb(AvahiServiceResolver *resolver,
+					      AvahiIfIndex interface,
+					      AvahiProtocol protocol,
+					      AvahiResolverEvent event,
+					      const char *name,
+					      const char *type,
+					      const char *domain,
+					      const char *host_name,
+					      const AvahiAddress *address,
+					      uint16_t port,
+					      AvahiStringList *txt,
+					      AvahiLookupResultFlags flags,
+					      void *context);
+#endif /* HAVE_AVAHI */
 
 /*
  * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
@@ -1434,6 +1461,9 @@ _httpResolveURI(
 
   if (strstr(hostname, "._tcp"))
   {
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+    char		*regtype,	/* Pointer to type in hostname */
+			*domain;	/* Pointer to domain in hostname */
 #ifdef HAVE_DNSSD
 #  ifdef WIN32
 #    pragma comment(lib, "dnssd.lib")
@@ -1452,6 +1482,17 @@ _httpResolveURI(
     fd_set		input_set;	/* Input set for select() */
     struct timeval	stimeout;	/* Timeout value for select() */
 #endif /* HAVE_POLL */
+#else /* HAVE_AVAHI */
+    AvahiSimplePoll	*simple_poll;
+    AvahiClient		*client;
+    int			error;
+    struct
+    {
+      AvahiSimplePoll	*poll;
+      _http_uribuf_t	uribuf;
+    } user_data;
+#endif /* HAVE_DNSSD */
+
 
     if (options & _HTTP_RESOLVE_STDERR)
       fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
@@ -1488,9 +1529,16 @@ _httpResolveURI(
     if (domain)
       *domain++ = '\0';
 
+#ifdef HAVE_DNSSD
     uribuf.buffer   = resolved_uri;
     uribuf.bufsize  = resolved_size;
     uribuf.options  = options;
+#else
+    user_data.uribuf.buffer = resolved_uri;
+    user_data.uribuf.bufsize = resolved_size;
+    user_data.uribuf.options = options;
+#endif
+
     resolved_uri[0] = '\0';
 
     DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
@@ -1504,6 +1552,7 @@ _httpResolveURI(
 
     uri = NULL;
 
+#ifdef HAVE_DNSSD
     if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
     {
       localref = ref;
@@ -1611,6 +1660,36 @@ _httpResolveURI(
 
       DNSServiceRefDeallocate(ref);
     }
+#else /* HAVE_AVAHI */
+    if ((simple_poll = avahi_simple_poll_new ()) != NULL)
+    {
+      if ((client = avahi_client_new (avahi_simple_poll_get (simple_poll),
+				      0, avahi_resolve_uri_client_cb,
+				      &simple_poll, &error)) != NULL)
+      {
+	user_data.poll = simple_poll;
+	if (avahi_service_resolver_new (client, AVAHI_IF_UNSPEC,
+					AVAHI_PROTO_UNSPEC, hostname,
+					regtype, domain, AVAHI_PROTO_UNSPEC, 0,
+					avahi_resolve_uri_resolver_cb,
+					&user_data) != NULL)
+	{
+	  avahi_simple_poll_loop (simple_poll);
+
+	 /*
+	  * Collect the result.
+	  */
+
+	  if (resolved_uri[0])
+	    uri = resolved_uri;
+	}
+
+	avahi_client_free (client);
+      }
+
+      avahi_simple_poll_free (simple_poll);
+    }
+#endif /* HAVE_DNSSD */
 
     if (options & _HTTP_RESOLVE_STDERR)
     {
@@ -1622,13 +1701,13 @@ _httpResolveURI(
       fputs("STATE: -connecting-to-device,offline-report\n", stderr);
     }
 
-#else
+#else /* HAVE_DNSSD || HAVE_AVAHI */
    /*
     * No DNS-SD support...
     */
 
     uri = NULL;
-#endif /* HAVE_DNSSD */
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
 
     if ((options & _HTTP_RESOLVE_STDERR) && !uri)
       _cupsLangPrintFilter(stderr, "ERROR", _("Unable to find printer."));
@@ -1916,6 +1995,115 @@ http_resolve_cb(
 }
 #endif /* HAVE_DNSSD */
 
+#ifdef HAVE_AVAHI
+/*
+ * 'avahi_resolve_uri_client_cb()' - Avahi client callback for resolving URI.
+ */
+
+static void
+avahi_resolve_uri_client_cb (AvahiClient *client,
+			     AvahiClientState state,
+			     void *simple_poll)
+{
+  DEBUG_printf(("avahi_resolve_uri_client_callback(client=%p, state=%d, "
+		"simple_poll=%p)\n", client, state, simple_poll));
+
+  /*
+   * If the connection drops, quit.
+   */
+
+  if (state == AVAHI_CLIENT_FAILURE)
+    avahi_simple_poll_quit (simple_poll);
+}
+
+
+/*
+ * 'avahi_resolve_uri_resolver_cb()' - Avahi resolver callback for resolving
+ *                                     URI.
+ */
+
+static void
+avahi_resolve_uri_resolver_cb (AvahiServiceResolver *resolver,
+			       AvahiIfIndex interface,
+			       AvahiProtocol protocol,
+			       AvahiResolverEvent event,
+			       const char *name,
+			       const char *type,
+			       const char *domain,
+			       const char *host_name,
+			       const AvahiAddress *address,
+			       uint16_t port,
+			       AvahiStringList *txt,
+			       AvahiLookupResultFlags flags,
+			       void *context)
+{
+  const char		*scheme;	/* URI scheme */
+  char			rp[256];	/* Remote printer */
+  AvahiStringList	*pair;
+  char			*value;
+  size_t		valueLen = 0;
+  char			addr[AVAHI_ADDRESS_STR_MAX];
+  struct
+  {
+    AvahiSimplePoll	*poll;
+    _http_uribuf_t	uribuf;
+  }		*poll_uribuf = context;
+
+  DEBUG_printf(("avahi_resolve_uri_resolver_callback(resolver=%p, "
+		"interface=%d, protocol=%d, event=%d, name=\"%s\", "
+		"type=\"%s\", domain=\"%s\", host_name=\"%s\", address=%p, "
+		"port=%d, txt=%p, flags=%d, context=%p)\n",
+		resolver, interface, protocol, event, name, type, domain,
+		host_name, address, port, txt, flags, context));
+
+  if (event != AVAHI_RESOLVER_FOUND)
+  {
+    avahi_service_resolver_free (resolver);
+    avahi_simple_poll_quit (poll_uribuf->poll);
+    return;
+  }
+
+ /*
+  * Figure out the scheme from the full name...
+  */
+
+  if (strstr(type, "_ipp."))
+    scheme = "ipp";
+  else if (strstr(type, "_printer."))
+    scheme = "lpd";
+  else if (strstr(type, "_pdl-datastream."))
+    scheme = "socket";
+  else
+    scheme = "riousbprint";
+
+ /*
+  * Extract the "remote printer key from the TXT record...
+  */
+
+  if ((pair = avahi_string_list_find (txt, "rp")) != NULL)
+  {
+    avahi_string_list_get_pair (pair, NULL, &value, &valueLen);
+    rp[0] = '/';
+    memcpy (rp + 1, value, valueLen);
+    rp[valueLen + 1] = '\0';
+  }
+  else
+    rp[0] = '\0';
+
+ /*
+  * Assemble the final device URI...
+  */
+
+  avahi_address_snprint (addr, AVAHI_ADDRESS_STR_MAX, address);
+  httpAssembleURI(HTTP_URI_CODING_ALL, poll_uribuf->uribuf.buffer,
+		  poll_uribuf->uribuf.bufsize, scheme, NULL,
+		  addr, port, rp);
+  DEBUG_printf(("avahi_resolve_uri_resolver_callback: Resolved URI is \"%s\"\n",
+		poll_uribuf->uribuf.buffer));
+  avahi_simple_poll_quit (poll_uribuf->poll);
+}
+#endif /* HAVE_AVAHI */
+
 
 /*
  * End of "$Id: http-support.c 10284 2012-02-15 01:06:12Z mike $".