summaryrefslogtreecommitdiff
path: root/libre/cups-libre/cups-avahi.patch
blob: 7e9c8ecea66c175b8e42305aaba508aca16cacbd (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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
Description: Avahi support
Bug: http://www.cups.org/str.php?L3066
Author: Tim Waugh
Origin: git clone http://twaugh.fedorapeople.org/cups-avahi.git (Aug 11, 2011)

--- a/backend/dnssd.c
+++ b/backend/dnssd.c
@@ -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 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 @@
 		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 @@
  * Local functions...
  */
 
+#ifdef HAVE_DNSSD
 static void		browse_callback(DNSServiceRef sdRef,
 			                DNSServiceFlags flags,
 				        uint32_t interfaceIndex,
@@ -93,12 +131,6 @@
 					      const char *regtype,
 					      const char *replyDomain,
 					      void *context);
-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);
 static void		query_callback(DNSServiceRef sdRef,
 			               DNSServiceFlags flags,
 				       uint32_t interfaceIndex,
@@ -107,9 +139,118 @@
 				       uint16_t rrclass, uint16_t rdlen,
 				       const void *rdata, uint32_t ttl,
 				       void *context);
+#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);
 static void		sigterm_handler(int sig);
 static void		unquote(char *dst, const char *src, size_t dstsize);
 
+#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.
@@ -120,6 +261,13 @@
      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 */
@@ -133,12 +281,11 @@
 		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 */
@@ -198,6 +345,49 @@
   * 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");
@@ -258,6 +448,7 @@
   riousbprint_ref = main_ref;
   DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
                    "_riousbprint._tcp", NULL, browse_callback, devices);
+#endif /* HAVE_DNSSD */
 
  /*
   * Loop until we are killed...
@@ -265,6 +456,9 @@
 
   while (!job_canceled)
   {
+    int announce = 0;
+
+#ifdef HAVE_DNSSD
     FD_ZERO(&input);
     FD_SET(fd, &input);
 
@@ -284,11 +478,35 @@
     }
     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 */
@@ -302,6 +520,7 @@
         if (device->sent)
 	  sent ++;
 
+#ifdef HAVE_DNSSD
         if (device->ref)
 	  count ++;
 
@@ -333,14 +552,23 @@
 	      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;
@@ -401,6 +629,7 @@
 }
 
 
+#ifdef HAVE_DNSSD
 /*
  * 'browse_callback()' - Browse devices.
  */
@@ -489,6 +718,7 @@
 	  device->fullName);
   device->sent = 1;
 }
+#endif /* HAVE_DNSSD */
 
 
 /*
@@ -569,6 +799,41 @@
 
 
 /*
+ * '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.
  */
 
@@ -589,20 +854,7 @@
   */
 
   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;
@@ -622,8 +874,14 @@
         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);
       }
@@ -643,6 +901,9 @@
   device->domain   = strdup(replyDomain);
   device->type     = key.type;
   device->priority = 50;
+#ifdef HAVE_AVAHI
+  device->resolved = 0;
+#endif /* HAVE_AVAHI */
 
   cupsArrayAdd(devices, device);
 
@@ -650,13 +911,20 @@
   * 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.
  */
@@ -680,7 +948,7 @@
 		*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\", "
@@ -714,94 +982,233 @@
   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);
+
+  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 */
+
 
-  for (device = cupsArrayFind(devices, &dkey);
+/*
+ * '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...
@@ -856,6 +1263,10 @@
 	  if (device->type == CUPS_DEVICE_PRINTER)
 	    device->sent = 1;
 	}
+
+      next:
+	if (next_txt_record (txt) == NULL)
+	  break;
       }
 
       if (device->device_id)
@@ -905,11 +1316,9 @@
     }
   }
 
-  if (!device)
-    fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
+  return device;
 }
 
-
 /*
  * 'sigterm_handler()' - Handle termination signals...
  */
--- a/config-scripts/cups-dnssd.m4
+++ b/config-scripts/cups-dnssd.m4
@@ -23,6 +23,21 @@
 DNSSDLIBS=""
 DNSSD_BACKEND=""
 
+AC_ARG_ENABLE(avahi, [  --enable-avahi          turn on DNS Service Discovery support, default=no],
+	      [if test x$enable_avahi = xyes; then
+		       AC_MSG_CHECKING(for Avahi)
+		       if $PKGCONFIG --exists avahi-client; then
+			       AC_MSG_RESULT(yes)
+			       CFLAGS="$CFLAGS `$PKGCONFIG --cflags avahi-client`"
+			       DNSSDLIBS="`$PKGCONFIG --libs avahi-client`"
+			       DNSSD_BACKEND="dnssd"
+			       AC_DEFINE(HAVE_AVAHI)
+			       enable_dnssd=no
+		       else
+			       AC_MSG_RESULT(no)
+		       fi
+	       fi])
+
 if test x$enable_dnssd != xno; then
 	AC_CHECK_HEADER(dns_sd.h, [
 		case "$uname" in
--- a/config.h.in
+++ b/config.h.in
@@ -390,6 +390,13 @@
 
 
 /*
+ * Do we have Avahi for DNS Service Discovery?
+ */
+
+#undef HAVE_AVAHI
+
+
+/*
  * Do we have <sys/ioctl.h>?
  */
 
--- a/cups/http-support.c
+++ b/cups/http-support.c
@@ -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 @@
 					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
@@ -1431,6 +1458,9 @@
 
   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")
@@ -1449,6 +1479,17 @@
     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);
@@ -1485,9 +1526,16 @@
     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\", "
@@ -1501,6 +1549,7 @@
 
     uri = NULL;
 
+#ifdef HAVE_DNSSD
     if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
     {
       localref = ref;
@@ -1608,6 +1657,36 @@
 
       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)
     {
@@ -1619,13 +1698,13 @@
       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."));
@@ -1895,6 +1974,116 @@
 #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 9820 2011-06-10 22:06:26Z mike $".
  */
--- a/scheduler/Makefile
+++ b/scheduler/Makefile
@@ -17,6 +17,7 @@
 
 CUPSDOBJS =	\
 		auth.o \
+		avahi.o \
 		banners.o \
 		cert.o \
 		classes.o \
@@ -39,7 +40,8 @@
 		server.o \
 		statbuf.o \
 		subscriptions.o \
-		sysman.o
+		sysman.o \
+		timeout.o
 LIBOBJS =	\
 		filter.o \
 		mime.o \
--- /dev/null
+++ b/scheduler/avahi.c
@@ -0,0 +1,445 @@
+/*
+ * "$Id$"
+ *
+ *   Avahi poll implementation for the CUPS scheduler.
+ *
+ *   Copyright (C) 2010 Red Hat, Inc.
+ *   Authors:
+ *    Tim Waugh <twaugh@redhat.com>
+ *
+ *   Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   "LICENSE" which should have been included with this file.  If this
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
+ *
+ * Contents:
+ *
+ *   watch_read_cb         - Read callback for file descriptor
+ *   watch_write_cb        - Write callback for file descriptor
+ *   watched_fd_add_select() - Call cupsdAddSelect() as needed
+ *   watch_new()           - Create a new file descriptor watch
+ *   watch_free()          - Free a file descriptor watch
+ *   watch_update()        - Update watched events for a file descriptor
+ *   watch_get_events()    - Get events that happened for a file descriptor
+ *   timeout_cb()          - Run a timed Avahi callback
+ *   timeout_new()         - Set a wakeup time
+ *   timeout_update()      - Update the expiration time for a timeout
+ *   timeout_free()        - Free a timeout
+ *   compare_watched_fds() - Compare watched file descriptors for array sorting
+ *   compare_timeouts()    - Compare timeouts for array sorting
+ *   avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
+ *   avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
+ *   avahi_cups_poll_get() - Get the abstract poll API structure
+ */
+
+#include <config.h>
+
+#ifdef HAVE_AVAHI /* Applies to entire file... */
+
+/*
+ * Include necessary headers...
+ */
+
+#include "cupsd.h"
+
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#  include <malloc.h>
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
+
+#ifdef HAVE_AVAHI
+#  include <avahi-common/timeval.h>
+#endif /* HAVE_AVAHI */
+
+
+typedef struct
+{
+  AvahiCupsPoll *cups_poll;
+
+  int fd;
+  AvahiWatchEvent occurred;
+  cups_array_t *watches;
+} cupsd_watched_fd_t;
+
+struct AvahiWatch
+{
+  cupsd_watched_fd_t *watched_fd;
+
+  AvahiWatchEvent events;
+  AvahiWatchCallback callback;
+  void *userdata;
+};
+
+struct AvahiTimeout
+{
+  AvahiCupsPoll *cups_poll;
+  AvahiTimeoutCallback callback;
+  void *userdata;
+  cupsd_timeout_t *cupsd_timeout;
+};
+
+/*
+ * Local functions...
+ */
+
+static AvahiWatch *	watch_new(const AvahiPoll *api,
+				  int fd,
+				  AvahiWatchEvent events,
+				  AvahiWatchCallback callback,
+				  void *userdata);
+static void		watch_free(AvahiWatch *watch);
+static void		watch_update(AvahiWatch *watch,
+				     AvahiWatchEvent events);
+static AvahiWatchEvent	watch_get_events(AvahiWatch *watch);
+static int		compare_watches(AvahiWatch *p0,
+					AvahiWatch *p1);
+
+
+/*
+ * 'watch_read_cb' - Read callback for file descriptor
+ */
+
+static void
+watch_read_cb (void *userdata)
+{
+  AvahiWatch *watch;
+  cupsd_watched_fd_t *watched_fd = userdata;
+  watched_fd->occurred |= AVAHI_WATCH_IN;
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+       watch;
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
+    if (watch->events & watched_fd->occurred) {
+      (watch->callback) (watch, watched_fd->fd,
+			 AVAHI_WATCH_IN, watch->userdata);
+      watched_fd->occurred &= ~AVAHI_WATCH_IN;
+      break;
+    }
+  }
+}
+
+
+/*
+ * 'watch_write_cb' - Write callback for file descriptor
+ */
+
+static void
+watch_write_cb (void *userdata)
+{
+  AvahiWatch *watch;
+  cupsd_watched_fd_t *watched_fd = userdata;
+  watched_fd->occurred |= AVAHI_WATCH_OUT;
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+       watch;
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
+    if (watch->events & watched_fd->occurred) {
+      (watch->callback) (watch, watched_fd->fd,
+			 AVAHI_WATCH_OUT, watch->userdata);
+      watched_fd->occurred &= ~AVAHI_WATCH_OUT;
+      break;
+    }
+  }
+}
+
+
+/*
+ * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
+ */
+
+static int						/* O - Watches? */
+watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
+{
+  AvahiWatch *watch;
+  cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
+
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+       watch;
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
+    if (watch->events & (AVAHI_WATCH_IN |
+			     AVAHI_WATCH_ERR |
+			     AVAHI_WATCH_HUP)) {
+      read_cb = (cupsd_selfunc_t)watch_read_cb;
+      if (write_cb != NULL)
+	break;
+    }
+
+    if (watch->events & AVAHI_WATCH_OUT) {
+      write_cb = (cupsd_selfunc_t)watch_write_cb;
+      if (read_cb != NULL)
+	break;
+    }
+  }
+
+  if (read_cb || write_cb)
+    cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
+  else
+    cupsdRemoveSelect (watched_fd->fd);
+
+  return (read_cb || write_cb);
+}
+
+/*
+ * 'watch_new' - Create a new file descriptor watch
+ */
+
+static AvahiWatch *
+watch_new (const AvahiPoll *api,
+	   int fd,
+	   AvahiWatchEvent events,
+	   AvahiWatchCallback callback,
+	   void *userdata)
+{
+  cupsd_watched_fd_t key, *watched_fd;
+  AvahiCupsPoll *cups_poll = api->userdata;
+  AvahiWatch *watch = malloc(sizeof(AvahiWatch));
+  if (watch == NULL)
+    return (NULL);
+
+  watch->events = events;
+  watch->callback = callback;
+  watch->userdata = userdata;
+
+  key.fd = fd;
+  watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
+  if (watched_fd == NULL) {
+    watched_fd = malloc(sizeof(cupsd_watched_fd_t));
+    if (watched_fd == NULL) {
+      free (watch);
+      return (NULL);
+    }
+
+    watched_fd->fd = fd;
+    watched_fd->occurred = 0;
+    watched_fd->cups_poll = cups_poll;
+    watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches,
+					NULL);
+  }
+
+  watch->watched_fd = watched_fd;
+  cupsArrayAdd(watched_fd->watches, watch);
+  watched_fd_add_select (watched_fd);
+  return (watch);
+}
+
+
+/*
+ * 'watch_free' - Free a file descriptor watch
+ */
+
+static void
+watch_free (AvahiWatch *watch)
+{
+  cupsd_watched_fd_t *watched_fd = watch->watched_fd;
+  AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
+
+  cupsArrayRemove (watched_fd->watches, watch);
+  free (watch);
+
+  if (!watched_fd_add_select (watched_fd)) {
+    /* No more watches */
+    cupsArrayRemove (cups_poll->watched_fds, watched_fd);
+    free (watched_fd);
+  }
+}
+
+
+/*
+ * 'watch_update' - Update watched events for a file descriptor
+ */
+
+static void
+watch_update (AvahiWatch *watch,
+	      AvahiWatchEvent events)
+{
+  watch->events = events;
+  watched_fd_add_select (watch->watched_fd);
+}
+
+
+/*
+ * 'watch_get_events' - Get events that happened for a file descriptor
+ */
+
+static AvahiWatchEvent
+watch_get_events (AvahiWatch *watch)
+{
+  return (watch->watched_fd->occurred);
+}
+
+
+/*
+ * 'compare_watches' - Compare watches for array sorting
+ */
+
+static int
+compare_watches (AvahiWatch *p0,
+		 AvahiWatch *p1)
+{
+  if (p0->watched_fd->fd < p1->watched_fd->fd)
+    return (-1);
+
+  return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
+}
+
+
+/*
+ * 'timeout_cb()' - Run a timed Avahi callback
+ */
+
+static void
+timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
+{
+  AvahiTimeout *timeout = userdata;
+  (timeout->callback) (timeout, timeout->userdata);
+}
+
+
+/*
+ * 'timeout_new' - Set a wakeup time
+ */
+
+static AvahiTimeout *
+timeout_new (const AvahiPoll *api,
+	     const struct timeval *tv,
+	     AvahiTimeoutCallback callback,
+	     void *userdata)
+{
+  AvahiTimeout *timeout;
+  AvahiCupsPoll *cups_poll = api->userdata;
+
+  timeout = malloc(sizeof(AvahiTimeout));
+  if (timeout == NULL)
+    return (NULL);
+
+  timeout->cups_poll = cups_poll;
+  timeout->callback = callback;
+  timeout->userdata = userdata;
+  timeout->cupsd_timeout = cupsdAddTimeout (tv,
+					    (cupsd_timeoutfunc_t)timeout_cb,
+					    timeout);
+  cupsArrayAdd (cups_poll->timeouts, timeout);
+  return (timeout);
+}
+
+
+/*
+ * 'timeout_update' - Update the expiration time for a timeout
+ */
+
+static void
+timeout_update (AvahiTimeout *timeout,
+		const struct timeval *tv)
+{
+  cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
+}
+
+
+/*
+ * ' timeout_free' - Free a timeout
+ */
+
+static void
+timeout_free (AvahiTimeout *timeout)
+{
+  cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
+  cupsdRemoveTimeout (timeout->cupsd_timeout);
+  free (timeout);
+}
+
+
+/*
+ * 'compare_watched_fds' - Compare watched file descriptors for array sorting
+ */
+static int
+compare_watched_fds(cupsd_watched_fd_t *p0,
+		    cupsd_watched_fd_t *p1)
+{
+  if (p0->fd != p1->fd)
+    return (p0->fd < p1->fd ? -1 : 1);
+
+  if (p0 == p1)
+    return (0);
+
+  return (p0 < p1 ? -1 : 1);
+}
+
+
+/*
+ * 'compare_timeouts' - Compare timeouts for array sorting
+ */
+static int
+compare_timeouts(AvahiTimeout *p0,
+		 AvahiTimeout *p1)
+{
+ /*
+  * Just compare pointers to make it a stable sort.
+  */
+
+  if (p0->cupsd_timeout < p1->cupsd_timeout)
+    return (-1);
+  return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
+}
+
+
+/*
+ * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
+ */
+
+AvahiCupsPoll *
+avahi_cups_poll_new (void)
+{
+  AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
+  if (cups_poll == NULL)
+    return (NULL);
+
+  cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
+					 NULL);
+  cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
+				      NULL);
+
+  cups_poll->api.userdata = cups_poll;
+  cups_poll->api.watch_new = watch_new;
+  cups_poll->api.watch_free = watch_free;
+  cups_poll->api.watch_update = watch_update;
+  cups_poll->api.watch_get_events = watch_get_events;
+
+  cups_poll->api.timeout_new = timeout_new;
+  cups_poll->api.timeout_update = timeout_update;
+  cups_poll->api.timeout_free = timeout_free;
+
+  return (cups_poll);
+}
+
+
+/*
+ * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
+ */
+void
+avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
+{
+  cupsd_watched_fd_t *watched_fd;
+
+  for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
+       watched_fd;
+       watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){
+    cupsArrayClear (watched_fd->watches);
+  }
+
+  cupsArrayClear (cups_poll->watched_fds);
+  cupsArrayClear (cups_poll->timeouts);
+}
+
+
+/*
+ * 'avahi_cups_poll_get' - Get the abstract poll API structure
+ */
+
+const AvahiPoll *
+avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
+{
+  return (&cups_poll->api);
+}
+
+
+#endif /* HAVE_AVAHI ... from top of file */
+
+/*
+ * End of "$Id$".
+ */
--- /dev/null
+++ b/scheduler/avahi.h
@@ -0,0 +1,49 @@
+/*
+ * "$Id$"
+ *
+ *   Avahi poll implementation for the CUPS scheduler.
+ *
+ *   Copyright (C) 2010 Red Hat, Inc.
+ *   Authors:
+ *    Tim Waugh <twaugh@redhat.com>
+ *
+ *   Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
+ */
+
+#include <config.h>
+
+#ifdef HAVE_AVAHI
+#  include <avahi-client/client.h>
+#  include <avahi-client/publish.h>
+#endif /* HAVE_AVAHI */
+
+#ifdef HAVE_AUTHORIZATION_H
+#  include <Security/Authorization.h>
+#endif /* HAVE_AUTHORIZATION_H */
+
+
+#ifdef HAVE_AVAHI
+typedef struct
+{
+    AvahiPoll api;
+    cups_array_t *watched_fds;
+    cups_array_t *timeouts;
+} AvahiCupsPoll;
+#endif /* HAVE_AVAHI */
+
+/*
+ * Prototypes...
+ */
+
+#ifdef HAVE_AVAHI
+extern AvahiCupsPoll *	avahi_cups_poll_new(void);
+extern void		avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
+extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
+#endif /* HAVE_AVAHI */
+
+
+/*
+ * End of "$Id$".
+ */
--- a/scheduler/cupsd.h
+++ b/scheduler/cupsd.h
@@ -140,6 +140,15 @@
 
 typedef void (*cupsd_selfunc_t)(void *data);
 
+#ifdef HAVE_AVAHI
+/*
+ * Timeout callback function type...
+ */
+
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
+#endif /* HAVE_AVAHI */
+
 
 /*
  * Globals...
@@ -173,6 +182,11 @@
 					/* Running from launchd */
 #endif /* HAVE_LAUNCH_H */
 
+#ifdef HAVE_AVAHI
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
+#endif /* HAVE_AVAHI */
+
+
 
 /*
  * Prototypes...
@@ -242,6 +256,20 @@
 extern void		cupsdStartServer(void);
 extern void		cupsdStopServer(void);
 
+#ifdef HAVE_AVAHI
+extern void     cupsdInitTimeouts(void);
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
+					 cupsd_timeoutfunc_t cb,
+					 void *data);
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
+extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
+extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
+				    const struct timeval *tv);
+extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
+#endif /* HAVE_AVAHI */
+
+extern int	cupsdRemoveFile(const char *filename);
+
 
 /*
  * End of "$Id: cupsd.h 9766 2011-05-11 22:17:34Z mike $".
--- a/scheduler/main.c
+++ b/scheduler/main.c
@@ -122,6 +122,10 @@
   cupsd_listener_t	*lis;		/* Current listener */
   time_t		current_time,	/* Current time */
 			activity,	/* Client activity timer */
+#ifdef HAVE_AVAHI
+			avahi_client_time, /* Time for next Avahi client
+					      check */
+#endif /* HAVE_AVAHI */
 			browse_time,	/* Next browse send time */
 			senddoc_time,	/* Send-Document time */
 			expire_time,	/* Subscription expire time */
@@ -148,6 +152,10 @@
   int			launchd_idle_exit;
 					/* Idle exit on select timeout? */
 #endif	/* HAVE_LAUNCHD */
+#ifdef HAVE_AVAHI
+  cupsd_timeout_t	*tmo;		/* Next scheduled timed callback */
+  long			tmo_delay;	/* Time before it must be called */
+#endif /* HAVE_AVAHI */
 
 
 #ifdef HAVE_GETEUID
@@ -527,6 +535,14 @@
 
   httpInitialize();
 
+#ifdef HAVE_AVAHI
+ /*
+  * Initialize timed callback structures.
+  */
+
+  cupsdInitTimeouts();
+#endif /* HAVE_AVAHI */
+
   cupsdStartServer();
 
  /*
@@ -657,6 +673,9 @@
   */
 
   current_time  = time(NULL);
+#ifdef HAVE_AVAHI
+  avahi_client_time = current_time;
+#endif /* HAVE_AVAHI */
   browse_time   = current_time;
   event_time    = current_time;
   expire_time   = current_time;
@@ -871,6 +890,26 @@
     }
 #endif /* __APPLE__ */
 
+#ifdef HAVE_AVAHI
+   /*
+    * If a timed callback is due, run it.
+    */
+
+    tmo = cupsdNextTimeout (&tmo_delay);
+    if (tmo && tmo_delay == 0)
+      cupsdRunTimeout (tmo);
+
+   /*
+    * Try to restart the Avahi client every 10 seconds if needed...
+    */
+
+    if ((current_time - avahi_client_time) >= 10)
+    {
+      avahi_client_time = current_time;
+      cupsdStartAvahiClient();
+    }
+#endif /* HAVE_AVAHI */
+
 #ifndef __APPLE__
    /*
     * Update the network interfaces once a minute...
@@ -1815,6 +1854,10 @@
   cupsd_job_t		*job;		/* Job information */
   cupsd_subscription_t	*sub;		/* Subscription information */
   const char		*why;		/* Debugging aid */
+#ifdef HAVE_AVAHI
+  cupsd_timeout_t	*tmo;		/* Timed callback */
+  long			tmo_delay;	/* Seconds before calling it */
+#endif /* HAVE_AVAHI */
 
 
  /*
@@ -1857,6 +1900,19 @@
   }
 #endif /* __APPLE__ */
 
+#ifdef HAVE_AVAHI
+ /*
+  * See if there are any scheduled timed callbacks to run.
+  */
+
+  tmo = cupsdNextTimeout (&tmo_delay);
+  if (tmo)
+  {
+    timeout = tmo_delay;
+    why = "run a timed callback";
+  }
+#endif /* HAVE_AVAHI */
+
  /*
   * Check whether we are accepting new connections...
   */
--- /dev/null
+++ b/scheduler/timeout.c
@@ -0,0 +1,195 @@
+/*
+ * "$Id$"
+ *
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
+ *
+ *   Copyright (C) 2010 Red Hat, Inc.
+ *   Authors:
+ *     Tim Waugh <twaugh@redhat.com>
+ *
+ *   Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
+ *
+ * Contents:
+ *
+ *   cupsdInitTimeouts()  - Initialise timeout structure.
+ *   cupsdAddTimeout()    - Add a timed callback.
+ *   cupsdNextTimeout()   - Find the next enabled timed callback.
+ *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
+ *   cupsdRemoveTimeout() - Discard a timed callback.
+ *   compare_timeouts()   - Compare timed callbacks for array sorting.
+ */
+
+#include <config.h>
+
+#ifdef HAVE_AVAHI /* Applies to entire file... */
+
+/*
+ * Include necessary headers...
+ */
+
+#include "cupsd.h"
+
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#  include <malloc.h>
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
+
+#ifdef HAVE_AVAHI
+#  include <avahi-common/timeval.h>
+#endif /* HAVE_AVAHI */
+
+
+struct _cupsd_timeout_s
+{
+  struct timeval when;
+  int enabled;
+  cupsd_timeoutfunc_t callback;
+  void *data;
+};
+
+/*
+ * Local functions...
+ */
+
+/*
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
+ */
+
+static int
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
+{
+  if (!p0->enabled || !p1->enabled)
+  {
+    if (!p0->enabled && !p1->enabled)
+      return (0);
+
+    return (p0->enabled ? -1 : 1);
+  }
+
+  return (avahi_timeval_compare (&p0->when, &p1->when));
+}
+
+
+/*
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
+ */
+
+void
+cupsdInitTimeouts(void)
+{
+  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
+}
+
+
+/*
+ * 'cupsdAddTimeout()' - Add a timed callback.
+ */
+
+cupsd_timeout_t *				/* O - Timeout handle */
+cupsdAddTimeout(const struct timeval *tv,	/* I - Absolute time */
+		cupsd_timeoutfunc_t cb,		/* I - Callback function */
+		void *data)			/* I - User data */
+{
+  cupsd_timeout_t *timeout;
+
+  timeout = malloc (sizeof(cupsd_timeout_t));
+  if (timeout != NULL)
+  {
+    timeout->enabled = (tv != NULL);
+    if (tv)
+    {
+      timeout->when.tv_sec = tv->tv_sec;
+      timeout->when.tv_usec = tv->tv_usec;
+    }
+
+    timeout->callback = cb;
+    timeout->data = data;
+    cupsArrayAdd (Timeouts, timeout);
+  }
+
+  return timeout;
+}
+
+
+/*
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
+ */
+
+cupsd_timeout_t *		/* O - Next enabled timeout or NULL */
+cupsdNextTimeout(long *delay)	/* O - Seconds before scheduled */
+{
+  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
+  struct timeval curtime;
+
+  if (first && !first->enabled)
+    first = NULL;
+
+  if (first && delay)
+  {
+    gettimeofday (&curtime, NULL);
+    if (avahi_timeval_compare (&curtime, &first->when) > 0)
+    {
+      *delay = 0;
+    } else {
+      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
+      if (first->when.tv_usec < curtime.tv_usec)
+	(*delay)--;
+    }
+  }
+
+  return (first);
+}
+
+
+/*
+ * 'cupsdRunTimeout()' - Run a timed callback.
+ */
+
+void
+cupsdRunTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
+{
+  if (!timeout)
+    return;
+  timeout->enabled = 0;
+  if (!timeout->callback)
+    return;
+  timeout->callback (timeout, timeout->data);
+}
+
+/*
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
+ */
+
+void
+cupsdUpdateTimeout(cupsd_timeout_t *timeout,	/* I - Timeout */
+		   const struct timeval *tv)	/* I - Absolute time or NULL */
+{
+  cupsArrayRemove (Timeouts, timeout);
+  timeout->enabled = (tv != NULL);
+  if (tv)
+  {
+    timeout->when.tv_sec = tv->tv_sec;
+    timeout->when.tv_usec = tv->tv_usec;
+  }
+  cupsArrayAdd (Timeouts, timeout);
+}
+
+
+/*
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
+ */
+
+void
+cupsdRemoveTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
+{
+  cupsArrayRemove (Timeouts, timeout);
+  free (timeout);
+}
+
+
+#endif /* HAVE_AVAHI ... from top of file */
+
+/*
+ * End of "$Id$".
+ */
--- a/cgi-bin/admin.c
+++ b/cgi-bin/admin.c
@@ -1643,7 +1643,7 @@
       else
 	local_protocols[0] = '\0';
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
       if (cgiGetVariable("BROWSE_LOCAL_DNSSD"))
       {
 	if (local_protocols[0])
@@ -1651,7 +1651,7 @@
 	else
 	  strcat(local_protocols, "dnssd");
       }
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 #ifdef HAVE_LDAP
       if (cgiGetVariable("BROWSE_LOCAL_LDAP"))
@@ -2718,9 +2718,9 @@
 #endif /* HAVE_GSSAPI */
   cgiSetVariable("KERBEROS", "");
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   cgiSetVariable("HAVE_DNSSD", "1");
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 #ifdef HAVE_LDAP
   cgiSetVariable("HAVE_LDAP", "1");
--- a/scheduler/client.c
+++ b/scheduler/client.c
@@ -4987,7 +4987,7 @@
 	    !strncmp(host, "[::1]:", 6));
   }
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
  /*
   * Check if the hostname is something.local (Bonjour); if so, allow it.
   */
@@ -4996,7 +4996,7 @@
       (!_cups_strcasecmp(end, ".local") || !_cups_strncasecmp(end, ".local:", 7) ||
        !_cups_strcasecmp(end, ".local.") || !_cups_strncasecmp(end, ".local.:", 8)))
     return (1);
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
  /*
   * Check if the hostname is an IP address...
--- a/scheduler/dirsvc.c
+++ b/scheduler/dirsvc.c
@@ -27,6 +27,7 @@
  *   ldap_connect()             - Start new LDAP connection
  *   ldap_reconnect()           - Reconnect to LDAP Server
  *   ldap_disconnect()          - Disconnect from LDAP Server
+ *   cupsdStartAvahiClient()    - Start an Avahi client if needed
  *   cupsdStartBrowsing()       - Start sending and receiving broadcast
  *                                information.
  *   cupsdStartPolling()        - Start polling servers as needed.
@@ -45,6 +46,8 @@
  *                                printer.
  *   dnssdPackTxtRecord()       - Pack an array of key/value pairs into the TXT
  *                                record format.
+ *   avahiPackTxtRecord()       - Pack an array of key/value pairs into an
+ *                                AvahiStringList.
  *   dnssdRegisterCallback()    - DNSServiceRegister callback.
  *   dnssdRegisterPrinter()     - Start sending broadcast information for a
  *                                printer or update the broadcast contents.
@@ -83,6 +86,7 @@
  */
 
 #include "cupsd.h"
+#include <assert.h>
 #include <grp.h>
 
 #ifdef HAVE_DNSSD
@@ -97,6 +101,17 @@
 #    endif /* HAVE_SYSTEMCONFIGURATION */
 #  endif /* __APPLE__ */
 #endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+#  include <avahi-common/domain.h>
+#endif /* HAVE_AVAHI */
+
+
+#ifdef HAVE_DNSSD
+typedef char *cupsd_txt_record_t;
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+typedef AvahiStringList *cupsd_txt_record_t;
+#endif /* HAVE_AVAHI */
 
 
 /*
@@ -159,27 +174,39 @@
 static void	update_smb(int onoff);
 
 
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+static cupsd_txt_record_t dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
+					      int for_lpd);
+static int	dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
+static void	dnssdDeregisterPrinter(cupsd_printer_t *p);
+static void	dnssdRegisterPrinter(cupsd_printer_t *p);
+static void	dnssdStop(void);
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
+
 #ifdef HAVE_DNSSD
 #  ifdef HAVE_COREFOUNDATION
 static void	dnssdAddAlias(const void *key, const void *value,
 		              void *context);
 #  endif /* HAVE_COREFOUNDATION */
-static char	*dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
-		                     int for_lpd);
-static int	dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
-static void	dnssdDeregisterPrinter(cupsd_printer_t *p);
-static char	*dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
-		                    int count);
 static void	dnssdRegisterCallback(DNSServiceRef sdRef,
 		                      DNSServiceFlags flags,
 				      DNSServiceErrorType errorCode,
 				      const char *name, const char *regtype,
 				      const char *domain, void *context);
-static void	dnssdRegisterPrinter(cupsd_printer_t *p);
-static void	dnssdStop(void);
 static void	dnssdUpdate(void);
 #endif /* HAVE_DNSSD */
 
+#ifdef HAVE_AVAHI
+static AvahiStringList *avahiPackTxtRecord(char *keyvalue[][2],
+					   int count);
+static void	avahi_entry_group_cb (AvahiEntryGroup *group,
+				      AvahiEntryGroupState state,
+				      void *userdata);
+static void	avahi_client_cb (AvahiClient *client,
+				 AvahiClientState state,
+				 void *userdata);
+#endif /* HAVE_AVAHI */
+
 #ifdef HAVE_LDAP
 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
 		{
@@ -283,10 +310,10 @@
     ldap_dereg_printer(p);
 #endif /* HAVE_LDAP */
 
-#ifdef HAVE_DNSSD
-  if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+  if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD))
     dnssdDeregisterPrinter(p);
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 }
 
 
@@ -702,10 +729,10 @@
     slpRegisterPrinter(p); */
 #endif /* HAVE_LIBSLP */
 
-#ifdef HAVE_DNSSD
-  if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+  if ((BrowseLocalProtocols & BROWSE_DNSSD))
     dnssdRegisterPrinter(p);
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 }
 
 
@@ -1419,6 +1446,27 @@
 #endif /* HAVE_LDAP */
 
 
+#ifdef HAVE_AVAHI
+/*
+ * 'cupsdStartAvahiClient()' - Start an Avahi client if needed
+ */
+
+void
+cupsdStartAvahiClient(void)
+{
+  if (!AvahiCupsClient && !AvahiCupsClientConnecting)
+  {
+    if (!AvahiCupsPollHandle)
+      AvahiCupsPollHandle = avahi_cups_poll_new ();
+
+    if (AvahiCupsPollHandle)
+      avahi_client_new (avahi_cups_poll_get (AvahiCupsPollHandle),
+			AVAHI_CLIENT_NO_FAIL, avahi_client_cb, NULL, NULL);
+  }
+}
+#endif /* HAVE_AVAHI */
+
+  
 /*
  * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
  */
@@ -1542,13 +1590,16 @@
   else
     BrowseSocket = -1;
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
   {
+#ifdef HAVE_DNSSD
     DNSServiceErrorType error;		/* Error from service creation */
+#endif /* HAVE_DNSSD */
     cupsd_listener_t	*lis;		/* Current listening socket */
 
 
+#ifdef HAVE_DNSSD
    /*
     * First create a "master" connection for all registrations...
     */
@@ -1573,6 +1624,7 @@
       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
 
       cupsdAddSelect(fd, (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
+#endif /* HAVE_DNSSD */
 
      /*
       * Then get the port we use for registrations.  If we are not listening
@@ -1606,9 +1658,16 @@
       */
 
       cupsdUpdateDNSSDName();
+
+#ifdef HAVE_AVAHI
+      cupsdStartAvahiClient ();
+#endif /* HAVE_AVAHI */
+
+#ifdef HAVE_DNSSD
     }
-  }
 #endif /* HAVE_DNSSD */
+  }
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 #ifdef HAVE_LIBSLP
   if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
@@ -1834,10 +1893,10 @@
     BrowseSocket = -1;
   }
 
-#ifdef HAVE_DNSSD
-  if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+  if ((BrowseLocalProtocols & BROWSE_DNSSD))
     dnssdStop();
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 #ifdef HAVE_LIBSLP
   if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
@@ -1902,7 +1961,7 @@
 }
 
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
 /*
  * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
  */
@@ -1910,8 +1969,14 @@
 void
 cupsdUpdateDNSSDName(void)
 {
+#ifdef HAVE_DNSSD
   DNSServiceErrorType error;		/* Error from service creation */
   char		webif[1024];		/* Web interface share name */
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  int		ret;			/* Error from service creation */
+  char		webif[AVAHI_LABEL_MAX];	/* Web interface share name */
+#endif /* HAVE_AVAHI */
 #  ifdef HAVE_SYSTEMCONFIGURATION
   SCDynamicStoreRef sc;			/* Context for dynamic store */
   CFDictionaryRef btmm;			/* Back-to-My-Mac domains */
@@ -2042,6 +2107,7 @@
     else
       strlcpy(webif, "CUPS Web Interface", sizeof(webif));
 
+#ifdef HAVE_DNSSD
     if (WebIFRef)
       DNSServiceRefDeallocate(WebIFRef);
 
@@ -2054,9 +2120,45 @@
 				    NULL)) != kDNSServiceErr_NoError)
       cupsdLogMessage(CUPSD_LOG_ERROR,
 		      "DNS-SD web interface registration failed: %d", error);
+#endif /* HAVE_DNSSD */
+
+#ifdef HAVE_AVAHI
+    if (!AvahiCupsClient)
+     /*
+      * Client not yet running.
+      */
+      return;
+
+    if (AvahiWebIFGroup)
+      avahi_entry_group_reset (AvahiWebIFGroup);
+    else
+      AvahiWebIFGroup = avahi_entry_group_new (AvahiCupsClient,
+					       avahi_entry_group_cb,
+					       NULL);
+
+    if (AvahiWebIFGroup)
+    {
+      ret = avahi_entry_group_add_service (AvahiWebIFGroup,
+					   AVAHI_IF_UNSPEC,
+					   AVAHI_PROTO_UNSPEC,
+					   0, /* flags */
+					   webif, /* name */
+					   "_http._tcp", /* type */
+					   NULL, /* domain */
+					   NULL, /* host */
+					   DNSSDPort, /* port */
+					   "path=/", NULL);
+      if (ret == 0)
+	ret = avahi_entry_group_commit (AvahiWebIFGroup);
+
+      if (ret != 0)
+	cupsdLogMessage (CUPSD_LOG_ERROR,
+			 "Avahi web interface registration failed: %d", ret);
+    }
+#endif /* HAVE_AVAHI */
   }
 }
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 
 #ifdef HAVE_LDAP
@@ -2334,13 +2436,15 @@
                     "Bad Back to My Mac domain in dynamic store!");
 }
 #  endif /* HAVE_COREFOUNDATION */
+#endif /* HAVE_DNSSD */
 
 
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
 /*
  * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
  */
 
-static char *				/* O - TXT record */
+static cupsd_txt_record_t		/* O - TXT record */
 dnssdBuildTxtRecord(
     int             *txt_len,		/* O - TXT record length */
     cupsd_printer_t *p,			/* I - Printer information */
@@ -2379,7 +2483,12 @@
   keyvalue[i  ][0] = "ty";
   keyvalue[i++][1] = p->make_model ? p->make_model : "Unknown";
 
-  snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.", DNSSDHostName);
+  snprintf(admin_hostname, sizeof(admin_hostname),
+	   "%s.local"
+#ifdef HAVE_DNSSD
+	   "." /* terminating dot no good for Avahi */
+#endif /* HAVE_DNSSD */
+	   , DNSSDHostName);
   httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
                    "http", NULL, admin_hostname, DNSSDPort, "/%s/%s",
 		   (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
@@ -2462,7 +2571,12 @@
   * Then pack them into a proper txt record...
   */
 
+#ifdef HAVE_DNSSD
   return (dnssdPackTxtRecord(txt_len, keyvalue, i));
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  return (avahiPackTxtRecord(keyvalue, i));
+#endif /* HAVE_AVAHI */
 }
 
 
@@ -2474,7 +2588,16 @@
 dnssdComparePrinters(cupsd_printer_t *a,/* I - First printer */
                      cupsd_printer_t *b)/* I - Second printer */
 {
-  return (_cups_strcasecmp(a->reg_name, b->reg_name));
+  if (!a->reg_name)
+    if (!b->reg_name)
+      return 0;
+    else
+      return -1;
+  else
+    if (!b->reg_name)
+      return 1;
+    else
+      return (_cups_strcasecmp(a->reg_name, b->reg_name));
 }
 
 
@@ -2489,6 +2612,10 @@
 {
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
 
+#ifdef HAVE_DNSSD
+  if (!DNSSDRef)
+    return;
+
  /*
   * Closing the socket deregisters the service
   */
@@ -2524,6 +2651,24 @@
     free(p->printer_txt);
     p->printer_txt = NULL;
   }
+#endif /* HAVE_DNSSD */
+
+#ifdef HAVE_AVAHI
+  if (p->avahi_group)
+    {
+      avahi_entry_group_reset (p->avahi_group);
+      avahi_entry_group_free (p->avahi_group);
+      p->avahi_group = NULL;
+
+      if (p->ipp_txt)
+	avahi_string_list_free (p->ipp_txt);
+
+      if (p->printer_txt)
+	avahi_string_list_free (p->printer_txt);
+
+      p->ipp_txt = p->printer_txt = NULL;
+    }
+#endif /* HAVE_AVAHI */
 
  /*
   * Remove the printer from the array of DNS-SD printers, then clear the
@@ -2533,8 +2678,10 @@
   cupsArrayRemove(DNSSDPrinters, p);
   cupsdClearString(&p->reg_name);
 }
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 
+#ifdef HAVE_DNSSD
 /*
  * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
  *                          TXT record format.
@@ -2644,8 +2791,10 @@
     LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
   }
 }
+#endif /* HAVE_DNSSD */
 
 
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
 /*
  * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
  *		              or update the broadcast contents.
@@ -2654,20 +2803,40 @@
 static void
 dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
 {
+#ifdef HAVE_DNSSD
   DNSServiceErrorType	se;		/* dnssd errors */
   char			*ipp_txt,	/* IPP TXT record buffer */
 			*printer_txt,	/* LPD TXT record buffer */
-			name[1024],	/* Service name */
-			*nameptr;	/* Pointer into name */
+			name[1024];	/* Service name */
   int			ipp_len,	/* IPP TXT record length */
 			printer_len,	/* LPD TXT record length */
 			printer_port;	/* LPD port number */
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  int			ret;		/* Error code */
+  AvahiStringList	*ipp_txt,	/* IPP TXT record */
+			*printer_txt;	/* LPD TXT record */
+  char			name[AVAHI_LABEL_MAX],	/* Service name */
+			fullsubtype[AVAHI_LABEL_MAX]; /* Full subtype */
+  char			*regtype_copy,	/* Writeable copy of reg type */
+			*subtype,	/* Current service sub type */
+			*nextsubtype;	/* Next service sub type */
+#endif /* HAVE_AVAHI */
+  char			*nameptr;	/* Pointer into name */
   const char		*regtype;	/* Registration type */
 
 
+#ifdef HAVE_DNSSD
+  if (!DNSSDRef)
+    return;
+
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
                   !p->ipp_ref ? "new" : "update");
-
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
+		  !p->avahi_group ? "new" : "update");
+#endif /* HAVE_AVAHI */
  /*
   * If per-printer sharing was just disabled make sure we're not
   * registered before returning.
@@ -2686,12 +2855,36 @@
   if (p->info && strlen(p->info) > 0)
   {
     if (DNSSDComputerName)
-      snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDComputerName);
+    {
+     /*
+      * Make sure there is room for at least 15 characters of
+      * DNSSDComputerName.
+      */
+
+      assert(sizeof(name) >= 15 + 4);
+      nameptr = name + strlcpy(name, p->info,
+			       sizeof(name) - 4 -
+			       strnlen(DNSSDComputerName, 15));
+      nameptr += strlcpy(nameptr, " @ ", sizeof(name) - (nameptr - name));
+      strlcpy(nameptr, DNSSDComputerName, sizeof(name) - (nameptr - name));
+    }
     else
       strlcpy(name, p->info, sizeof(name));
   }
   else if (DNSSDComputerName)
-    snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDComputerName);
+  {
+   /*
+    * Make sure there is room for at least 15 characters of
+    * DNSSDComputerName.
+    */
+
+    assert(sizeof(name) >= 15 + 4);
+    nameptr = name + strlcpy(name, p->info,
+			     sizeof(name) - 4 -
+			     strnlen(DNSSDComputerName, 15));
+    nameptr += strlcpy(nameptr, " @ ", sizeof(name) - (nameptr - name));
+    strlcpy(nameptr, DNSSDComputerName, sizeof(name) - (nameptr - name));
+  }
   else
     strlcpy(name, p->name, sizeof(name));
 
@@ -2712,6 +2905,7 @@
   * Register IPP and (optionally) LPD...
   */
 
+#ifdef HAVE_DNSSD
   ipp_len = 0;				/* anti-compiler-warning-code */
   ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
 
@@ -2884,6 +3078,209 @@
 
   if (printer_txt)
     free(printer_txt);
+#endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  if (!AvahiCupsClient)
+   /*
+    * Client not running yet.  The client callback will call us again later.
+    */
+    return;
+
+  ipp_txt = dnssdBuildTxtRecord(NULL, p, 0);
+  printer_txt = dnssdBuildTxtRecord(NULL, p, 1);
+  regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" : DNSSDRegType;
+
+  if (p->avahi_group && p->ipp_txt && ipp_txt &&
+      !avahi_string_list_equal (p->ipp_txt, ipp_txt))
+  {
+   /*
+    * Update the existing registration...
+    */
+
+    avahi_string_list_free (p->ipp_txt);
+
+    if (p->printer_txt)
+      avahi_string_list_free (p->printer_txt);
+
+   /*
+    * Update the service group entry.
+    */
+
+    regtype_copy = strdup (regtype);
+    subtype = strchr (regtype_copy, ',');
+    if (subtype)
+      *subtype = '\0';
+
+    cupsdLogMessage (CUPSD_LOG_DEBUG,
+		     "Updating TXT record for %s (%s)", name, regtype_copy);
+    ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
+						       AVAHI_IF_UNSPEC,
+						       AVAHI_PROTO_UNSPEC,
+						       0, name,
+						       regtype_copy,
+						       NULL, ipp_txt);
+    free (regtype_copy);
+
+    if (ret < 0)
+      goto update_failed;
+
+    p->ipp_txt = ipp_txt;
+    ipp_txt = NULL;
+
+    if (BrowseLocalProtocols & BROWSE_LPD)
+    {
+      ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
+							 AVAHI_IF_UNSPEC,
+							 AVAHI_PROTO_UNSPEC,
+							 0, name,
+							 "_printer._tcp", NULL,
+							 printer_txt);
+      if (ret < 0)
+	goto update_failed;
+
+      p->printer_txt = printer_txt;
+      printer_txt = NULL;
+    }
+
+    ret = avahi_entry_group_commit (p->avahi_group);
+    if (ret < 0)
+    {
+    update_failed:
+      cupsdLogMessage (CUPSD_LOG_ERROR,
+		       "Failed to update TXT record for %s: %d",
+		       name, ret);
+      avahi_entry_group_reset (p->avahi_group);
+      avahi_entry_group_free (p->avahi_group);
+      p->avahi_group = NULL;
+      ipp_txt = p->ipp_txt;
+      p->ipp_txt = NULL;
+    }
+  }
+
+  if (!p->avahi_group)
+  {
+   /*
+    * Initial registration.  Use the _fax subtype for fax queues...
+    */
+
+    p->avahi_group = avahi_entry_group_new (AvahiCupsClient,
+					    avahi_entry_group_cb,
+					    p);
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG,
+		    "Registering Avahi printer %s with name \"%s\" and "
+		    "type \"%s\"", p->name, name, regtype);
+
+    if (!p->avahi_group)
+    {
+      ret = 0;
+      goto add_failed;
+    }
+
+   /*
+    * Add each service type (DNSSDRegType may contain several,
+    * separated by commas).
+    */
+
+    subtype = regtype_copy = strdup (regtype);
+    while (subtype && *subtype)
+    {
+      nextsubtype = strchr (subtype, ',');
+      if (nextsubtype)
+	*nextsubtype++ = '\0';
+
+      if (subtype == regtype_copy)
+      {
+       /*
+	* Main type entry.
+	*/
+
+	cupsdLogMessage (CUPSD_LOG_DEBUG,
+			 "Adding TXT record for %s (%s)", name, regtype_copy);
+	ret = avahi_entry_group_add_service_strlst (p->avahi_group,
+						    AVAHI_IF_UNSPEC,
+						    AVAHI_PROTO_UNSPEC,
+						    0, name, regtype_copy,
+						    NULL, NULL,
+						    DNSSDPort,
+						    ipp_txt);
+      }
+      else
+      {
+       /*
+	* Sub-type entry.
+	*/
+
+	snprintf (fullsubtype, sizeof(fullsubtype),
+		  "%s._sub.%s", subtype, regtype_copy);
+	cupsdLogMessage (CUPSD_LOG_DEBUG,
+			 "Adding TXT record for %s (%s)", name, fullsubtype);
+	ret = avahi_entry_group_add_service_subtype (p->avahi_group,
+						     AVAHI_IF_UNSPEC,
+						     AVAHI_PROTO_UNSPEC,
+						     0, name,
+						     regtype_copy,
+						     NULL, fullsubtype);
+      }
+
+      if (ret < 0)
+      {
+	free (regtype_copy);
+	goto add_failed;
+      }
+
+      subtype = nextsubtype;
+    }
+
+    free (regtype_copy);
+    p->ipp_txt = ipp_txt;
+    ipp_txt = NULL;
+
+    if (BrowseLocalProtocols & BROWSE_LPD)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+		      "Registering Avahi printer %s with name \"%s\" and "
+		      "type \"_printer._tcp\"", p->name, name);
+
+      ret = avahi_entry_group_add_service_strlst (p->avahi_group,
+						  AVAHI_IF_UNSPEC,
+						  AVAHI_PROTO_UNSPEC,
+						  0, name,
+						  "_printer._tcp", NULL, NULL,
+						  515,
+						  printer_txt);
+      if (ret < 0)
+	goto add_failed;
+
+      p->printer_txt = printer_txt;
+      printer_txt = NULL;
+    }
+
+    ret = avahi_entry_group_commit (p->avahi_group);
+
+    if (ret < 0)
+    {
+    add_failed:
+      cupsdLogMessage (CUPSD_LOG_ERROR,
+		       "Failed to add Avahi entry for %s: %d",
+		       name, ret);
+      if (p->avahi_group)
+      {
+	avahi_entry_group_reset (p->avahi_group);
+	avahi_entry_group_free (p->avahi_group);
+	p->avahi_group = NULL;
+      }
+      ipp_txt = p->ipp_txt;
+      p->ipp_txt = NULL;
+    }
+  }
+
+  if (ipp_txt)
+    avahi_string_list_free (ipp_txt);
+
+  if (printer_txt)
+    avahi_string_list_free (printer_txt);
+#endif /* HAVE_AVAHI */
 }
 
 
@@ -2896,6 +3293,10 @@
 {
   cupsd_printer_t	*p;		/* Current printer */
 
+#ifdef HAVE_DNSSD
+  if (!DNSSDRef)
+    return;
+#endif /* HAVE_DNSSD */
 
  /*
   * De-register the individual printers
@@ -2906,6 +3307,7 @@
        p = (cupsd_printer_t *)cupsArrayNext(Printers))
     dnssdDeregisterPrinter(p);
 
+#ifdef HAVE_DNSSD
  /*
   * Shutdown the rest of the service refs...
   */
@@ -2926,14 +3328,17 @@
 
   DNSServiceRefDeallocate(DNSSDRef);
   DNSSDRef = NULL;
+#endif /* HAVE_DNSSD */
 
   cupsArrayDelete(DNSSDPrinters);
   DNSSDPrinters = NULL;
 
   DNSSDPort = 0;
 }
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
 
+#ifdef HAVE_DNSSD
 /*
  * 'dnssdUpdate()' - Handle DNS-SD queries.
  */
@@ -2955,6 +3360,147 @@
 #endif /* HAVE_DNSSD */
 
 
+#ifdef HAVE_AVAHI
+/*
+ * 'avahiPackTxtRecord()' - Pack an array of key/value pairs into an
+ *                          AvahiStringList.
+ */
+
+static AvahiStringList *		/* O - new string list */
+avahiPackTxtRecord(char *keyvalue[][2],	/* I - Table of key value pairs */
+		   int count)		/* I - Number of items in table */
+{
+  AvahiStringList *strlst = NULL;
+  char **elements;
+  size_t len;
+  int i = 0;
+
+  elements = malloc ((1 + count) * sizeof (char *));
+  if (!elements)
+    goto cleanup;
+
+  for (i = 0; i < count; i++)
+    {
+      len = (1 + strlen (keyvalue[i][0]) +
+	     (keyvalue[i][1] ? 1 + strlen (keyvalue[i][1]) : 1));
+      elements[i] = malloc (len * sizeof (char));
+      if (!elements[i])
+	goto cleanup;
+
+      snprintf (elements[i], len, "%s=%s", keyvalue[i][0], keyvalue[i][1]);
+    }
+
+  strlst = avahi_string_list_new_from_array ((const char **) elements, count);
+
+cleanup:
+  while (--i >= 0)
+    free (elements[i]);
+
+  free (elements);
+  return (strlst);
+}
+
+
+/*
+ * 'avahi_entry_group_cb()' - Avahi entry group callback function.
+ */
+static void
+avahi_entry_group_cb (AvahiEntryGroup *group,
+		      AvahiEntryGroupState state,
+		      void *userdata)
+{
+  char *name;
+
+  if (userdata)
+    name = ((cupsd_printer_t *) userdata)->reg_name;
+  else
+    name = "CUPS web interface";
+
+  switch (state)
+  {
+  case AVAHI_ENTRY_GROUP_UNCOMMITED:
+  case AVAHI_ENTRY_GROUP_REGISTERING:
+    break;
+
+  case AVAHI_ENTRY_GROUP_ESTABLISHED:
+    cupsdLogMessage (CUPSD_LOG_DEBUG,
+		     "Avahi entry group established for %s", name);
+    break;
+
+  default:
+    cupsdLogMessage (CUPSD_LOG_DEBUG,
+		     "Avahi entry group %s has state %d",
+		     name, state);
+    break;
+  }
+}
+
+
+/*
+ * 'avahi_client_cb()' - Avahi client callback function.
+ */
+static void
+avahi_client_cb (AvahiClient *client,
+		 AvahiClientState state,
+		 void *userdata)
+{
+  cupsd_printer_t *printer;
+  switch (state)
+  {
+  case AVAHI_CLIENT_S_RUNNING:
+   /*
+    * Avahi client started successfully.
+    */
+    AvahiCupsClient = client;
+    AvahiCupsClientConnecting = 0;
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client started");
+
+    cupsdUpdateDNSSDName ();
+
+    for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
+	 printer;
+	 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
+      if (Browsing && (BrowseLocalProtocols & BROWSE_DNSSD) &&
+	  (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+			      CUPS_PRINTER_SCANNER))) && printer->shared)
+	dnssdRegisterPrinter (printer);
+
+    break;
+
+  case AVAHI_CLIENT_CONNECTING:
+   /*
+    * No Avahi daemon, client is waiting.
+    */
+    AvahiCupsClientConnecting = 1;
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client connecting");
+    break;
+
+  case AVAHI_CLIENT_FAILURE:
+   /*
+    * Avahi client failed, close it to allow a clean restart.
+    */
+    cupsdLogMessage (CUPSD_LOG_ERROR,
+		     "Avahi client failed, "
+		     "closing client to allow a clean restart");
+
+    for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
+	 printer;
+	 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
+      dnssdDeregisterPrinter (printer);
+
+    avahi_client_free(client);
+    AvahiCupsClientConnecting = 0;
+    AvahiCupsClient = NULL;
+
+    break;
+
+  default:
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client state: %d", state);
+  }
+}
+#endif /* HAVE_AVAHI */
+
+
 /*
  * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
  */
--- a/scheduler/dirsvc.h
+++ b/scheduler/dirsvc.h
@@ -31,6 +31,10 @@
 #  endif /* HAVE_LDAP_SSL_H */
 #endif /* HAVE_LDAP */
 
+#ifdef HAVE_AVAHI
+#  include <avahi-client/publish.h>
+#endif /* HAVE_AVAHI */
+
 /*
  * Browse protocols...
  */
@@ -131,19 +135,22 @@
 VAR cupsd_statbuf_t	*PollStatusBuffer VALUE(NULL);
 					/* Status buffer for pollers */
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
 VAR char		*DNSSDComputerName VALUE(NULL),
 					/* Computer/server name */
 			*DNSSDHostName	VALUE(NULL),
 					/* Hostname */
 			*DNSSDRegType VALUE(NULL);
 					/* Bonjour registration type */
-VAR cups_array_t	*DNSSDAlias	VALUE(NULL);
-					/* List of dynamic ServerAlias's */
 VAR int			DNSSDPort	VALUE(0);
 					/* Port number to register */
 VAR cups_array_t	*DNSSDPrinters	VALUE(NULL);
 					/* Printers we have registered */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
+
+#ifdef HAVE_DNSSD
+VAR cups_array_t	*DNSSDAlias	VALUE(NULL);
+					/* List of dynamic ServerAlias's */
 VAR DNSServiceRef	DNSSDRef	VALUE(NULL),
 					/* Master DNS-SD service reference */
 			WebIFRef	VALUE(NULL),
@@ -152,6 +159,17 @@
 					/* Remote printer browse reference */
 #endif /* HAVE_DNSSD */
 
+#ifdef HAVE_AVAHI
+VAR AvahiCupsPoll	*AvahiCupsPollHandle	VALUE(NULL);
+					/* AvahiCupsPoll object */
+VAR AvahiClient		*AvahiCupsClient	VALUE(NULL);
+					/* AvahiClient object */
+VAR int			AvahiCupsClientConnecting	VALUE(0);
+					/* Is AvahiClient object connecting? */
+VAR AvahiEntryGroup	*AvahiWebIFGroup	VALUE(NULL);
+					/* Web interface entry group */
+#endif /* HAVE_AVAHI */
+
 #ifdef HAVE_LIBSLP
 VAR SLPHandle		BrowseSLPHandle	VALUE(NULL);
 					/* SLP API handle */
@@ -195,13 +213,14 @@
 extern void	cupsdRestartPolling(void);
 extern void	cupsdSaveRemoteCache(void);
 extern void	cupsdSendBrowseList(void);
+extern void	cupsdStartAvahiClient(void);
 extern void	cupsdStartBrowsing(void);
 extern void	cupsdStartPolling(void);
 extern void	cupsdStopBrowsing(void);
 extern void	cupsdStopPolling(void);
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
 extern void	cupsdUpdateDNSSDName(void);
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 #ifdef HAVE_LDAP
 extern void	cupsdUpdateLDAPBrowse(void);
 #endif /* HAVE_LDAP */
--- a/scheduler/ipp.c
+++ b/scheduler/ipp.c
@@ -6087,7 +6087,7 @@
     ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
                ippTimeToDate(curtime));
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
   {
     if (printer->reg_name)
@@ -6097,7 +6097,7 @@
       ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
                    "printer-dns-sd-name", 0);
   }
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
   if (!ra || cupsArrayFind(ra, "printer-error-policy"))
     ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
--- a/scheduler/printers.c
+++ b/scheduler/printers.c
@@ -883,9 +883,9 @@
   cupsdClearString(&p->alert);
   cupsdClearString(&p->alert_description);
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   cupsdClearString(&p->pdl);
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 
   cupsArrayDelete(p->filetypes);
 
@@ -3765,7 +3765,7 @@
     attr->values[i].string.text = _cupsStrAlloc(mimetype);
   }
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   {
     char		pdl[1024];	/* Buffer to build pdl list */
     mime_filter_t	*filter;	/* MIME filter looping var */
@@ -3821,7 +3821,7 @@
 
     cupsdSetString(&p->pdl, pdl);
   }
-#endif /* HAVE_DNSSD */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
 }
 
 
--- a/scheduler/printers.h
+++ b/scheduler/printers.h
@@ -16,6 +16,9 @@
 #ifdef HAVE_DNSSD
 #  include <dns_sd.h>
 #endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+#  include "avahi.h"
+#endif /* HAVE_AVAHI */
 #include <cups/pwg-private.h>
 
 
@@ -95,16 +98,23 @@
   time_t	marker_time;		/* Last time marker attributes were updated */
   _ppd_cache_t	*pc;			/* PPD cache and mapping data */
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   char		*reg_name,		/* Name used for service registration */
-		*pdl,			/* pdl value for TXT record */
-		*ipp_txt,		/* IPP TXT record contents */
+		*pdl;			/* pdl value for TXT record */
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
+#ifdef HAVE_DNSSD
+  char		*ipp_txt,		/* IPP TXT record contents */
 		*printer_txt;		/* LPD TXT record contents */
   int		ipp_len,		/* IPP TXT record length */
 		printer_len;		/* LPD TXT record length */
   DNSServiceRef	ipp_ref,		/* Reference for _ipp._tcp,_cups */
 		printer_ref;		/* Reference for _printer._tcp */
 #endif /* HAVE_DNSSD */
+#ifdef HAVE_AVAHI
+  AvahiStringList *ipp_txt,		/* IPP TXT record */
+		*printer_txt;		/* LPD TXT record */
+  AvahiEntryGroup *avahi_group;		/* Avahi entry group */
+#endif /* HAVE_AVAHI */
 };
 
 
--- a/scheduler/conf.c
+++ b/scheduler/conf.c
@@ -650,7 +650,7 @@
   Browsing                 = CUPS_DEFAULT_BROWSING;
   DefaultShared            = CUPS_DEFAULT_DEFAULT_SHARED;
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   cupsdSetString(&DNSSDRegType, "_ipp._tcp,_cups");
 #endif /* HAVE_DNSSD */