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 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2001-2002 Nokia Corporation
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
* Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
* Copyright (C) 2002-2003 Stephen Crane <steve.crane@rococosoft.com>
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include "bluetooth.h"
#include "hci.h"
#include "hci_lib.h"
#include "l2cap.h"
#include "sdp.h"
#include "sdp_lib.h"
#define SDPINF(fmt, arg...) syslog(LOG_INFO, fmt "\n", ## arg)
#define SDPERR(fmt, arg...) syslog(LOG_ERR, "%s: " fmt "\n", __func__ , ## arg)
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#ifdef SDP_DEBUG
#define SDPDBG(fmt, arg...) syslog(LOG_DEBUG, "%s: " fmt "\n", __func__ , ## arg)
#else
#define SDPDBG(fmt...)
#endif
static const uint128_t bluetooth_base_uuid = {
.data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
};
#define SDP_MAX_ATTR_LEN 65535
/* match MTU used by RFCOMM */
#define SDP_LARGE_L2CAP_MTU 1013
static sdp_data_t *sdp_copy_seq(sdp_data_t *data);
static int sdp_attr_add_new_with_length(sdp_record_t *rec,
uint16_t attr, uint8_t dtd, const void *value, uint32_t len);
static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d);
/* Message structure. */
struct tupla {
int index;
const char *str;
};
static const struct tupla Protocol[] = {
{ SDP_UUID, "SDP" },
{ UDP_UUID, "UDP" },
{ RFCOMM_UUID, "RFCOMM" },
{ TCP_UUID, "TCP" },
{ TCS_BIN_UUID, "TCS-BIN" },
{ TCS_AT_UUID, "TCS-AT" },
{ OBEX_UUID, "OBEX" },
{ IP_UUID, "IP" },
{ FTP_UUID, "FTP" },
{ HTTP_UUID, "HTTP" },
{ WSP_UUID, "WSP" },
{ BNEP_UUID, "BNEP" },
{ UPNP_UUID, "UPNP" },
{ HIDP_UUID, "HIDP" },
{ HCRP_CTRL_UUID, "HCRP-Ctrl" },
{ HCRP_DATA_UUID, "HCRP-Data" },
{ HCRP_NOTE_UUID, "HCRP-Notify" },
{ AVCTP_UUID, "AVCTP" },
{ AVDTP_UUID, "AVDTP" },
{ CMTP_UUID, "CMTP" },
{ UDI_UUID, "UDI" },
{ MCAP_CTRL_UUID, "MCAP-Ctrl" },
{ MCAP_DATA_UUID, "MCAP-Data" },
{ L2CAP_UUID, "L2CAP" },
{ ATT_UUID, "ATT" },
{ 0 }
};
static const struct tupla ServiceClass[] = {
{ SDP_SERVER_SVCLASS_ID, "SDP Server" },
{ BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" },
{ PUBLIC_BROWSE_GROUP, "Public Browse Group" },
{ SERIAL_PORT_SVCLASS_ID, "Serial Port" },
{ LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" },
{ DIALUP_NET_SVCLASS_ID, "Dialup Networking" },
{ IRMC_SYNC_SVCLASS_ID, "IrMC Sync" },
{ OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" },
{ OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" },
{ IRMC_SYNC_CMD_SVCLASS_ID, "IrMC Sync Command" },
{ HEADSET_SVCLASS_ID, "Headset" },
{ CORDLESS_TELEPHONY_SVCLASS_ID, "Cordless Telephony" },
{ AUDIO_SOURCE_SVCLASS_ID, "Audio Source" },
{ AUDIO_SINK_SVCLASS_ID, "Audio Sink" },
{ AV_REMOTE_TARGET_SVCLASS_ID, "AV Remote Target" },
{ ADVANCED_AUDIO_SVCLASS_ID, "Advanced Audio" },
{ AV_REMOTE_SVCLASS_ID, "AV Remote" },
{ AV_REMOTE_CONTROLLER_SVCLASS_ID, "AV Remote Controller" },
{ INTERCOM_SVCLASS_ID, "Intercom" },
{ FAX_SVCLASS_ID, "Fax" },
{ HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" },
{ WAP_SVCLASS_ID, "WAP" },
{ WAP_CLIENT_SVCLASS_ID, "WAP Client" },
{ PANU_SVCLASS_ID, "PAN User" },
{ NAP_SVCLASS_ID, "Network Access Point" },
{ GN_SVCLASS_ID, "PAN Group Network" },
{ DIRECT_PRINTING_SVCLASS_ID, "Direct Printing" },
{ REFERENCE_PRINTING_SVCLASS_ID, "Reference Printing" },
{ IMAGING_SVCLASS_ID, "Imaging" },
{ IMAGING_RESPONDER_SVCLASS_ID, "Imaging Responder" },
{ IMAGING_ARCHIVE_SVCLASS_ID, "Imaging Automatic Archive" },
{ IMAGING_REFOBJS_SVCLASS_ID, "Imaging Referenced Objects" },
{ HANDSFREE_SVCLASS_ID, "Handsfree" },
{ HANDSFREE_AGW_SVCLASS_ID, "Handsfree Audio Gateway" },
{ DIRECT_PRT_REFOBJS_SVCLASS_ID, "Direct Printing Ref. Objects" },
{ REFLECTED_UI_SVCLASS_ID, "Reflected UI" },
{ BASIC_PRINTING_SVCLASS_ID, "Basic Printing" },
{ PRINTING_STATUS_SVCLASS_ID, "Printing Status" },
{ HID_SVCLASS_ID, "Human Interface Device" },
{ HCR_SVCLASS_ID, "Hardcopy Cable Replacement" },
{ HCR_PRINT_SVCLASS_ID, "HCR Print" },
{ HCR_SCAN_SVCLASS_ID, "HCR Scan" },
{ CIP_SVCLASS_ID, "Common ISDN Access" },
{ VIDEO_CONF_GW_SVCLASS_ID, "Video Conferencing Gateway" },
{ UDI_MT_SVCLASS_ID, "UDI MT" },
{ UDI_TA_SVCLASS_ID, "UDI TA" },
{ AV_SVCLASS_ID, "Audio/Video" },
{ SAP_SVCLASS_ID, "SIM Access" },
{ PBAP_PCE_SVCLASS_ID, "Phonebook Access - PCE" },
{ PBAP_PSE_SVCLASS_ID, "Phonebook Access - PSE" },
{ PBAP_SVCLASS_ID, "Phonebook Access" },
{ MAP_MSE_SVCLASS_ID, "Message Access - MAS" },
{ MAP_MCE_SVCLASS_ID, "Message Access - MNS" },
{ MAP_SVCLASS_ID, "Message Access" },
{ PNP_INFO_SVCLASS_ID, "PnP Information" },
{ GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" },
{ GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" },
{ GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" },
{ GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" },
{ UPNP_SVCLASS_ID, "UPnP" },
{ UPNP_IP_SVCLASS_ID, "UPnP IP" },
{ UPNP_PAN_SVCLASS_ID, "UPnP PAN" },
{ UPNP_LAP_SVCLASS_ID, "UPnP LAP" },
{ UPNP_L2CAP_SVCLASS_ID, "UPnP L2CAP" },
{ VIDEO_SOURCE_SVCLASS_ID, "Video Source" },
{ VIDEO_SINK_SVCLASS_ID, "Video Sink" },
{ VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" },
{ HDP_SVCLASS_ID, "HDP" },
{ HDP_SOURCE_SVCLASS_ID, "HDP Source" },
{ HDP_SINK_SVCLASS_ID, "HDP Sink" },
{ GENERIC_ACCESS_SVCLASS_ID, "Generic Access" },
{ GENERIC_ATTRIB_SVCLASS_ID, "Generic Attribute" },
{ APPLE_AGENT_SVCLASS_ID, "Apple Agent" },
{ 0 }
};
#define Profile ServiceClass
static const char *string_lookup(const struct tupla *pt0, int index)
{
const struct tupla *pt;
for (pt = pt0; pt->index; pt++)
if (pt->index == index)
return pt->str;
return "";
}
static const char *string_lookup_uuid(const struct tupla *pt0,
const uuid_t *uuid)
{
uuid_t tmp_uuid;
memcpy(&tmp_uuid, uuid, sizeof(tmp_uuid));
if (sdp_uuid128_to_uuid(&tmp_uuid)) {
switch (tmp_uuid.type) {
case SDP_UUID16:
return string_lookup(pt0, tmp_uuid.value.uuid16);
case SDP_UUID32:
return string_lookup(pt0, tmp_uuid.value.uuid32);
}
}
return "";
}
/*
* Prints into a string the Protocol UUID
* coping a maximum of n characters.
*/
static int uuid2str(const struct tupla *message, const uuid_t *uuid, char *str,
size_t n)
{
const char *str2;
if (!uuid) {
snprintf(str, n, "NULL");
return -2;
}
switch (uuid->type) {
case SDP_UUID16:
str2 = string_lookup(message, uuid->value.uuid16);
snprintf(str, n, "%s", str2);
break;
case SDP_UUID32:
str2 = string_lookup(message, uuid->value.uuid32);
snprintf(str, n, "%s", str2);
break;
case SDP_UUID128:
str2 = string_lookup_uuid(message, uuid);
snprintf(str, n, "%s", str2);
break;
default:
snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
return -1;
}
return 0;
}
int sdp_proto_uuid2strn(const uuid_t *uuid, char *str, size_t n)
{
return uuid2str(Protocol, uuid, str, n);
}
int sdp_svclass_uuid2strn(const uuid_t *uuid, char *str, size_t n)
{
return uuid2str(ServiceClass, uuid, str, n);
}
int sdp_profile_uuid2strn(const uuid_t *uuid, char *str, size_t n)
{
return uuid2str(Profile, uuid, str, n);
}
/*
* convert the UUID to string, copying a maximum of n characters.
*/
int sdp_uuid2strn(const uuid_t *uuid, char *str, size_t n)
{
if (!uuid) {
snprintf(str, n, "NULL");
return -2;
}
switch (uuid->type) {
case SDP_UUID16:
snprintf(str, n, "%.4x", uuid->value.uuid16);
break;
case SDP_UUID32:
snprintf(str, n, "%.8x", uuid->value.uuid32);
break;
case SDP_UUID128:{
unsigned int data0;
unsigned short data1;
unsigned short data2;
unsigned short data3;
unsigned int data4;
unsigned short data5;
memcpy(&data0, &uuid->value.uuid128.data[0], 4);
memcpy(&data1, &uuid->value.uuid128.data[4], 2);
memcpy(&data2, &uuid->value.uuid128.data[6], 2);
memcpy(&data3, &uuid->value.uuid128.data[8], 2);
memcpy(&data4, &uuid->value.uuid128.data[10], 4);
memcpy(&data5, &uuid->value.uuid128.data[14], 2);
snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
ntohl(data0), ntohs(data1),
ntohs(data2), ntohs(data3),
ntohl(data4), ntohs(data5));
}
break;
default:
snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
return -1; /* Enum type of UUID not set */
}
return 0;
}
#ifdef SDP_DEBUG
/*
* Function prints the UUID in hex as per defined syntax -
*
* 4bytes-2bytes-2bytes-2bytes-6bytes
*
* There is some ugly code, including hardcoding, but
* that is just the way it is converting 16 and 32 bit
* UUIDs to 128 bit as defined in the SDP doc
*/
void sdp_uuid_print(const uuid_t *uuid)
{
if (uuid == NULL) {
SDPERR("Null passed to print UUID");
return;
}
if (uuid->type == SDP_UUID16) {
SDPDBG(" uint16_t : 0x%.4x", uuid->value.uuid16);
} else if (uuid->type == SDP_UUID32) {
SDPDBG(" uint32_t : 0x%.8x", uuid->value.uuid32);
} else if (uuid->type == SDP_UUID128) {
unsigned int data0;
unsigned short data1;
unsigned short data2;
unsigned short data3;
unsigned int data4;
unsigned short data5;
memcpy(&data0, &uuid->value.uuid128.data[0], 4);
memcpy(&data1, &uuid->value.uuid128.data[4], 2);
memcpy(&data2, &uuid->value.uuid128.data[6], 2);
memcpy(&data3, &uuid->value.uuid128.data[8], 2);
memcpy(&data4, &uuid->value.uuid128.data[10], 4);
memcpy(&data5, &uuid->value.uuid128.data[14], 2);
SDPDBG(" uint128_t : 0x%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
ntohl(data0), ntohs(data1), ntohs(data2),
ntohs(data3), ntohl(data4), ntohs(data5));
} else
SDPERR("Enum type of UUID not set");
}
#endif
sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value,
uint32_t length)
{
sdp_data_t *seq;
sdp_data_t *d = bt_malloc0(sizeof(sdp_data_t));
if (!d)
return NULL;
d->dtd = dtd;
d->unitSize = sizeof(uint8_t);
switch (dtd) {
case SDP_DATA_NIL:
break;
case SDP_UINT8:
d->val.uint8 = *(uint8_t *) value;
d->unitSize += sizeof(uint8_t);
break;
case SDP_INT8:
case SDP_BOOL:
d->val.int8 = *(int8_t *) value;
d->unitSize += sizeof(int8_t);
break;
case SDP_UINT16:
d->val.uint16 = bt_get_unaligned((uint16_t *) value);
d->unitSize += sizeof(uint16_t);
break;
case SDP_INT16:
d->val.int16 = bt_get_unaligned((int16_t *) value);
d->unitSize += sizeof(int16_t);
break;
case SDP_UINT32:
d->val.uint32 = bt_get_unaligned((uint32_t *) value);
d->unitSize += sizeof(uint32_t);
break;
case SDP_INT32:
d->val.int32 = bt_get_unaligned((int32_t *) value);
d->unitSize += sizeof(int32_t);
break;
case SDP_INT64:
d->val.int64 = bt_get_unaligned((int64_t *) value);
d->unitSize += sizeof(int64_t);
break;
case SDP_UINT64:
d->val.uint64 = bt_get_unaligned((uint64_t *) value);
d->unitSize += sizeof(uint64_t);
break;
case SDP_UINT128:
memcpy(&d->val.uint128.data, value, sizeof(uint128_t));
d->unitSize += sizeof(uint128_t);
break;
case SDP_INT128:
memcpy(&d->val.int128.data, value, sizeof(uint128_t));
d->unitSize += sizeof(uint128_t);
break;
case SDP_UUID16:
sdp_uuid16_create(&d->val.uuid, bt_get_unaligned((uint16_t *) value));
d->unitSize += sizeof(uint16_t);
break;
case SDP_UUID32:
sdp_uuid32_create(&d->val.uuid, bt_get_unaligned((uint32_t *) value));
d->unitSize += sizeof(uint32_t);
break;
case SDP_UUID128:
sdp_uuid128_create(&d->val.uuid, value);
d->unitSize += sizeof(uint128_t);
break;
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
if (!value) {
free(d);
return NULL;
}
d->unitSize += length;
if (length <= USHRT_MAX) {
d->val.str = bt_malloc0(length + 1);
if (!d->val.str) {
free(d);
return NULL;
}
memcpy(d->val.str, value, length);
} else {
SDPERR("Strings of size > USHRT_MAX not supported");
free(d);
d = NULL;
}
break;
case SDP_URL_STR32:
case SDP_TEXT_STR32:
SDPERR("Strings of size > USHRT_MAX not supported");
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
if (dtd == SDP_ALT8 || dtd == SDP_SEQ8)
d->unitSize += sizeof(uint8_t);
else if (dtd == SDP_ALT16 || dtd == SDP_SEQ16)
d->unitSize += sizeof(uint16_t);
else if (dtd == SDP_ALT32 || dtd == SDP_SEQ32)
d->unitSize += sizeof(uint32_t);
seq = (sdp_data_t *)value;
d->val.dataseq = seq;
for (; seq; seq = seq->next)
d->unitSize += seq->unitSize;
break;
default:
free(d);
d = NULL;
}
return d;
}
sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value)
{
uint32_t length;
switch (dtd) {
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
if (!value)
return NULL;
length = strlen((char *) value);
break;
default:
length = 0;
break;
}
return sdp_data_alloc_with_length(dtd, value, length);
}
sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d)
{
if (seq) {
sdp_data_t *p;
for (p = seq; p->next; p = p->next);
p->next = d;
} else
seq = d;
d->next = NULL;
return seq;
}
sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length,
int len)
{
sdp_data_t *curr = NULL, *seq = NULL;
int i;
for (i = 0; i < len; i++) {
sdp_data_t *data;
uint8_t dtd = *(uint8_t *) dtds[i];
if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
data = (sdp_data_t *) values[i];
else
data = sdp_data_alloc_with_length(dtd, values[i], length[i]);
if (!data) {
sdp_data_free(seq);
return NULL;
}
if (curr)
curr->next = data;
else
seq = data;
curr = data;
}
return sdp_data_alloc(SDP_SEQ8, seq);
}
sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len)
{
sdp_data_t *curr = NULL, *seq = NULL;
int i;
for (i = 0; i < len; i++) {
sdp_data_t *data;
uint8_t dtd = *(uint8_t *) dtds[i];
if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
data = (sdp_data_t *) values[i];
else
data = sdp_data_alloc(dtd, values[i]);
if (!data) {
sdp_data_free(seq);
return NULL;
}
if (curr)
curr->next = data;
else
seq = data;
curr = data;
}
return sdp_data_alloc(SDP_SEQ8, seq);
}
static void extract_svclass_uuid(sdp_data_t *data, uuid_t *uuid)
{
sdp_data_t *d;
if (!data || !SDP_IS_SEQ(data->dtd))
return;
d = data->val.dataseq;
if (!d)
return;
if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128)
return;
*uuid = d->val.uuid;
}
int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
{
sdp_data_t *p = sdp_data_get(rec, attr);
if (p)
return -1;
if (!d)
return -1;
d->attrId = attr;
rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
extract_svclass_uuid(d, &rec->svclass);
return 0;
}
void sdp_attr_remove(sdp_record_t *rec, uint16_t attr)
{
sdp_data_t *d = sdp_data_get(rec, attr);
if (d)
rec->attrlist = sdp_list_remove(rec->attrlist, d);
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
memset(&rec->svclass, 0, sizeof(rec->svclass));
}
void sdp_set_seq_len(uint8_t *ptr, uint32_t length)
{
uint8_t dtd = *ptr++;
switch (dtd) {
case SDP_SEQ8:
case SDP_ALT8:
case SDP_TEXT_STR8:
case SDP_URL_STR8:
*ptr = (uint8_t) length;
break;
case SDP_SEQ16:
case SDP_ALT16:
case SDP_TEXT_STR16:
case SDP_URL_STR16:
bt_put_be16(length, ptr);
break;
case SDP_SEQ32:
case SDP_ALT32:
case SDP_TEXT_STR32:
case SDP_URL_STR32:
bt_put_be32(length, ptr);
break;
}
}
static int sdp_get_data_type_size(uint8_t dtd)
{
int size = sizeof(uint8_t);
switch (dtd) {
case SDP_SEQ8:
case SDP_TEXT_STR8:
case SDP_URL_STR8:
case SDP_ALT8:
size += sizeof(uint8_t);
break;
case SDP_SEQ16:
case SDP_TEXT_STR16:
case SDP_URL_STR16:
case SDP_ALT16:
size += sizeof(uint16_t);
break;
case SDP_SEQ32:
case SDP_TEXT_STR32:
case SDP_URL_STR32:
case SDP_ALT32:
size += sizeof(uint32_t);
break;
}
return size;
}
void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr)
{
uint8_t *p = buf->data;
/* data type for attr */
*p++ = SDP_UINT16;
buf->data_size = sizeof(uint8_t);
bt_put_be16(attr, p);
buf->data_size += sizeof(uint16_t);
}
static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata)
{
sdp_data_t *d;
int n = 0;
for (d = sdpdata->val.dataseq; d; d = d->next) {
if (buf->data)
n += sdp_gen_pdu(buf, d);
else
n += sdp_gen_buffer(buf, d);
}
return n;
}
static int sdp_get_data_size(sdp_buf_t *buf, sdp_data_t *d)
{
uint32_t data_size = 0;
uint8_t dtd = d->dtd;
switch (dtd) {
case SDP_DATA_NIL:
break;
case SDP_UINT8:
data_size = sizeof(uint8_t);
break;
case SDP_UINT16:
data_size = sizeof(uint16_t);
break;
case SDP_UINT32:
data_size = sizeof(uint32_t);
break;
case SDP_UINT64:
data_size = sizeof(uint64_t);
break;
case SDP_UINT128:
data_size = sizeof(uint128_t);
break;
case SDP_INT8:
case SDP_BOOL:
data_size = sizeof(int8_t);
break;
case SDP_INT16:
data_size = sizeof(int16_t);
break;
case SDP_INT32:
data_size = sizeof(int32_t);
break;
case SDP_INT64:
data_size = sizeof(int64_t);
break;
case SDP_INT128:
data_size = sizeof(uint128_t);
break;
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_TEXT_STR32:
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_URL_STR32:
data_size = d->unitSize - sizeof(uint8_t);
break;
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
data_size = get_data_size(buf, d);
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
data_size = get_data_size(buf, d);
break;
case SDP_UUID16:
data_size = sizeof(uint16_t);
break;
case SDP_UUID32:
data_size = sizeof(uint32_t);
break;
case SDP_UUID128:
data_size = sizeof(uint128_t);
break;
default:
break;
}
return data_size;
}
static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d)
{
int orig = buf->buf_size;
if (buf->buf_size == 0 && d->dtd == 0) {
/* create initial sequence */
buf->buf_size += sizeof(uint8_t);
/* reserve space for sequence size */
buf->buf_size += sizeof(uint8_t);
}
/* attribute length */
buf->buf_size += sizeof(uint8_t) + sizeof(uint16_t);
buf->buf_size += sdp_get_data_type_size(d->dtd);
buf->buf_size += sdp_get_data_size(buf, d);
if (buf->buf_size > UCHAR_MAX && d->dtd == SDP_SEQ8)
buf->buf_size += sizeof(uint8_t);
return buf->buf_size - orig;
}
int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d)
{
uint32_t pdu_size, data_size;
unsigned char *src = NULL, is_seq = 0, is_alt = 0;
uint16_t u16;
uint32_t u32;
uint64_t u64;
uint128_t u128;
uint8_t *seqp = buf->data + buf->data_size;
uint32_t orig_data_size = buf->data_size;
recalculate:
pdu_size = sdp_get_data_type_size(d->dtd);
buf->data_size += pdu_size;
data_size = sdp_get_data_size(buf, d);
if (data_size > UCHAR_MAX && d->dtd == SDP_SEQ8) {
buf->data_size = orig_data_size;
d->dtd = SDP_SEQ16;
goto recalculate;
}
*seqp = d->dtd;
switch (d->dtd) {
case SDP_DATA_NIL:
break;
case SDP_UINT8:
src = &d->val.uint8;
break;
case SDP_UINT16:
u16 = htons(d->val.uint16);
src = (unsigned char *) &u16;
break;
case SDP_UINT32:
u32 = htonl(d->val.uint32);
src = (unsigned char *) &u32;
break;
case SDP_UINT64:
u64 = hton64(d->val.uint64);
src = (unsigned char *) &u64;
break;
case SDP_UINT128:
hton128(&d->val.uint128, &u128);
src = (unsigned char *) &u128;
break;
case SDP_INT8:
case SDP_BOOL:
src = (unsigned char *) &d->val.int8;
break;
case SDP_INT16:
u16 = htons(d->val.int16);
src = (unsigned char *) &u16;
break;
case SDP_INT32:
u32 = htonl(d->val.int32);
src = (unsigned char *) &u32;
break;
case SDP_INT64:
u64 = hton64(d->val.int64);
src = (unsigned char *) &u64;
break;
case SDP_INT128:
hton128(&d->val.int128, &u128);
src = (unsigned char *) &u128;
break;
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_TEXT_STR32:
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_URL_STR32:
src = (unsigned char *) d->val.str;
sdp_set_seq_len(seqp, data_size);
break;
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
is_seq = 1;
sdp_set_seq_len(seqp, data_size);
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
is_alt = 1;
sdp_set_seq_len(seqp, data_size);
break;
case SDP_UUID16:
u16 = htons(d->val.uuid.value.uuid16);
src = (unsigned char *) &u16;
break;
case SDP_UUID32:
u32 = htonl(d->val.uuid.value.uuid32);
src = (unsigned char *) &u32;
break;
case SDP_UUID128:
src = (unsigned char *) &d->val.uuid.value.uuid128;
break;
default:
break;
}
if (!is_seq && !is_alt) {
if (src && buf->buf_size >= buf->data_size + data_size) {
memcpy(buf->data + buf->data_size, src, data_size);
buf->data_size += data_size;
} else if (d->dtd != SDP_DATA_NIL) {
SDPDBG("Gen PDU : Can't copy from invalid source or dest");
}
}
pdu_size += data_size;
return pdu_size;
}
static void sdp_attr_pdu(void *value, void *udata)
{
sdp_append_to_pdu((sdp_buf_t *)udata, (sdp_data_t *)value);
}
static void sdp_attr_size(void *value, void *udata)
{
sdp_gen_buffer((sdp_buf_t *)udata, (sdp_data_t *)value);
}
int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf)
{
memset(buf, 0, sizeof(sdp_buf_t));
sdp_list_foreach(rec->attrlist, sdp_attr_size, buf);
buf->data = bt_malloc0(buf->buf_size);
if (!buf->data)
return -ENOMEM;
buf->data_size = 0;
sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf);
return 0;
}
void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
{
sdp_data_t *p;
if (!rec)
return;
p = sdp_data_get(rec, attr);
if (p) {
rec->attrlist = sdp_list_remove(rec->attrlist, p);
sdp_data_free(p);
}
d->attrId = attr;
rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
extract_svclass_uuid(d, &rec->svclass);
}
int sdp_attrid_comp_func(const void *key1, const void *key2)
{
const sdp_data_t *d1 = (const sdp_data_t *)key1;
const sdp_data_t *d2 = (const sdp_data_t *)key2;
if (d1 && d2)
return d1->attrId - d2->attrId;
return 0;
}
static void data_seq_free(sdp_data_t *seq)
{
sdp_data_t *d = seq->val.dataseq;
while (d) {
sdp_data_t *next = d->next;
sdp_data_free(d);
d = next;
}
}
void sdp_data_free(sdp_data_t *d)
{
if (!d)
return;
switch (d->dtd) {
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
data_seq_free(d);
break;
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_URL_STR32:
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_TEXT_STR32:
free(d->val.str);
break;
}
free(d);
}
int sdp_uuid_extract(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned)
{
uint8_t type;
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
return -1;
}
type = *(const uint8_t *) p;
if (!SDP_IS_UUID(type)) {
SDPERR("Unknown data type : %d expecting a svc UUID", type);
return -1;
}
p += sizeof(uint8_t);
*scanned += sizeof(uint8_t);
bufsize -= sizeof(uint8_t);
if (type == SDP_UUID16) {
if (bufsize < (int) sizeof(uint16_t)) {
SDPERR("Not enough room for 16-bit UUID");
return -1;
}
sdp_uuid16_create(uuid, bt_get_be16(p));
*scanned += sizeof(uint16_t);
} else if (type == SDP_UUID32) {
if (bufsize < (int) sizeof(uint32_t)) {
SDPERR("Not enough room for 32-bit UUID");
return -1;
}
sdp_uuid32_create(uuid, bt_get_be32(p));
*scanned += sizeof(uint32_t);
} else {
if (bufsize < (int) sizeof(uint128_t)) {
SDPERR("Not enough room for 128-bit UUID");
return -1;
}
sdp_uuid128_create(uuid, p);
*scanned += sizeof(uint128_t);
}
return 0;
}
static sdp_data_t *extract_int(const void *p, int bufsize, int *len)
{
sdp_data_t *d;
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
return NULL;
}
d = bt_malloc0(sizeof(sdp_data_t));
if (!d)
return NULL;
SDPDBG("Extracting integer");
d->dtd = *(uint8_t *) p;
p += sizeof(uint8_t);
*len += sizeof(uint8_t);
bufsize -= sizeof(uint8_t);
switch (d->dtd) {
case SDP_DATA_NIL:
break;
case SDP_BOOL:
case SDP_INT8:
case SDP_UINT8:
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
*len += sizeof(uint8_t);
d->val.uint8 = *(uint8_t *) p;
break;
case SDP_INT16:
case SDP_UINT16:
if (bufsize < (int) sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
*len += sizeof(uint16_t);
d->val.uint16 = bt_get_be16(p);
break;
case SDP_INT32:
case SDP_UINT32:
if (bufsize < (int) sizeof(uint32_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
*len += sizeof(uint32_t);
d->val.uint32 = bt_get_be32(p);
break;
case SDP_INT64:
case SDP_UINT64:
if (bufsize < (int) sizeof(uint64_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
*len += sizeof(uint64_t);
d->val.uint64 = bt_get_be64(p);
break;
case SDP_INT128:
case SDP_UINT128:
if (bufsize < (int) sizeof(uint128_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
*len += sizeof(uint128_t);
ntoh128((uint128_t *) p, &d->val.uint128);
break;
default:
free(d);
d = NULL;
}
return d;
}
static sdp_data_t *extract_uuid(const uint8_t *p, int bufsize, int *len,
sdp_record_t *rec)
{
sdp_data_t *d = bt_malloc0(sizeof(sdp_data_t));
if (!d)
return NULL;
SDPDBG("Extracting UUID");
if (sdp_uuid_extract(p, bufsize, &d->val.uuid, len) < 0) {
free(d);
return NULL;
}
d->dtd = *p;
if (rec)
sdp_pattern_add_uuid(rec, &d->val.uuid);
return d;
}
/*
* Extract strings from the PDU (could be service description and similar info)
*/
static sdp_data_t *extract_str(const void *p, int bufsize, int *len)
{
char *s;
int n;
sdp_data_t *d;
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
return NULL;
}
d = bt_malloc0(sizeof(sdp_data_t));
if (!d)
return NULL;
d->dtd = *(uint8_t *) p;
p += sizeof(uint8_t);
*len += sizeof(uint8_t);
bufsize -= sizeof(uint8_t);
switch (d->dtd) {
case SDP_TEXT_STR8:
case SDP_URL_STR8:
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
n = *(uint8_t *) p;
p += sizeof(uint8_t);
*len += sizeof(uint8_t);
bufsize -= sizeof(uint8_t);
break;
case SDP_TEXT_STR16:
case SDP_URL_STR16:
if (bufsize < (int) sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
free(d);
return NULL;
}
n = bt_get_be16(p);
p += sizeof(uint16_t);
*len += sizeof(uint16_t);
bufsize -= sizeof(uint16_t);
break;
default:
SDPERR("Sizeof text string > UINT16_MAX");
free(d);
return NULL;
}
if (bufsize < n) {
SDPERR("String too long to fit in packet");
free(d);
return NULL;
}
s = bt_malloc0(n + 1);
if (!s) {
SDPERR("Not enough memory for incoming string");
free(d);
return NULL;
}
memcpy(s, p, n);
*len += n;
SDPDBG("Len : %d", n);
SDPDBG("Str : %s", s);
d->val.str = s;
d->unitSize = n + sizeof(uint8_t);
return d;
}
/*
* Extract the sequence type and its length, and return offset into buf
* or 0 on failure.
*/
int sdp_extract_seqtype(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size)
{
uint8_t dtd;
int scanned = sizeof(uint8_t);
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
return 0;
}
dtd = *(uint8_t *) buf;
buf += sizeof(uint8_t);
bufsize -= sizeof(uint8_t);
*dtdp = dtd;
switch (dtd) {
case SDP_SEQ8:
case SDP_ALT8:
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
return 0;
}
*size = *(uint8_t *) buf;
scanned += sizeof(uint8_t);
break;
case SDP_SEQ16:
case SDP_ALT16:
if (bufsize < (int) sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
return 0;
}
*size = bt_get_be16(buf);
scanned += sizeof(uint16_t);
break;
case SDP_SEQ32:
case SDP_ALT32:
if (bufsize < (int) sizeof(uint32_t)) {
SDPERR("Unexpected end of packet");
return 0;
}
*size = bt_get_be32(buf);
scanned += sizeof(uint32_t);
break;
default:
SDPERR("Unknown sequence type, aborting");
return 0;
}
return scanned;
}
static sdp_data_t *extract_seq(const void *p, int bufsize, int *len,
sdp_record_t *rec)
{
int seqlen, n = 0;
sdp_data_t *curr, *prev;
sdp_data_t *d = bt_malloc0(sizeof(sdp_data_t));
if (!d)
return NULL;
SDPDBG("Extracting SEQ");
*len = sdp_extract_seqtype(p, bufsize, &d->dtd, &seqlen);
SDPDBG("Sequence Type : 0x%x length : 0x%x", d->dtd, seqlen);
if (*len == 0)
return d;
if (*len > bufsize) {
SDPERR("Packet not big enough to hold sequence.");
free(d);
return NULL;
}
p += *len;
bufsize -= *len;
prev = NULL;
while (n < seqlen) {
int attrlen = 0;
curr = sdp_extract_attr(p, bufsize, &attrlen, rec);
if (curr == NULL)
break;
if (prev)
prev->next = curr;
else
d->val.dataseq = curr;
prev = curr;
p += attrlen;
n += attrlen;
bufsize -= attrlen;
SDPDBG("Extracted: %d SequenceLength: %d", n, seqlen);
}
*len += n;
return d;
}
sdp_data_t *sdp_extract_attr(const uint8_t *p, int bufsize, int *size,
sdp_record_t *rec)
{
sdp_data_t *elem;
int n = 0;
uint8_t dtd;
if (bufsize < (int) sizeof(uint8_t)) {
SDPERR("Unexpected end of packet");
return NULL;
}
dtd = *(const uint8_t *)p;
SDPDBG("extract_attr: dtd=0x%x", dtd);
switch (dtd) {
case SDP_DATA_NIL:
case SDP_BOOL:
case SDP_UINT8:
case SDP_UINT16:
case SDP_UINT32:
case SDP_UINT64:
case SDP_UINT128:
case SDP_INT8:
case SDP_INT16:
case SDP_INT32:
case SDP_INT64:
case SDP_INT128:
elem = extract_int(p, bufsize, &n);
break;
case SDP_UUID16:
case SDP_UUID32:
case SDP_UUID128:
elem = extract_uuid(p, bufsize, &n, rec);
break;
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_TEXT_STR32:
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_URL_STR32:
elem = extract_str(p, bufsize, &n);
break;
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
elem = extract_seq(p, bufsize, &n, rec);
break;
default:
SDPERR("Unknown data descriptor : 0x%x terminating", dtd);
return NULL;
}
*size += n;
return elem;
}
#ifdef SDP_DEBUG
static void attr_print_func(void *value, void *userData)
{
sdp_data_t *d = (sdp_data_t *)value;
SDPDBG("=====================================");
SDPDBG("ATTRIBUTE IDENTIFIER : 0x%x", d->attrId);
SDPDBG("ATTRIBUTE VALUE PTR : %p", value);
if (d)
sdp_data_print(d);
else
SDPDBG("NULL value");
SDPDBG("=====================================");
}
void sdp_print_service_attr(sdp_list_t *svcAttrList)
{
SDPDBG("Printing service attr list %p", svcAttrList);
sdp_list_foreach(svcAttrList, attr_print_func, NULL);
SDPDBG("Printed service attr list %p", svcAttrList);
}
#endif
sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int bufsize, int *scanned)
{
int extracted = 0, seqlen = 0;
uint8_t dtd;
uint16_t attr;
sdp_record_t *rec = sdp_record_alloc();
const uint8_t *p = buf;
*scanned = sdp_extract_seqtype(buf, bufsize, &dtd, &seqlen);
p += *scanned;
bufsize -= *scanned;
rec->attrlist = NULL;
while (extracted < seqlen && bufsize > 0) {
int n = sizeof(uint8_t), attrlen = 0;
sdp_data_t *data = NULL;
SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d",
seqlen, extracted);
if (bufsize < n + (int) sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
break;
}
dtd = *(uint8_t *) p;
attr = bt_get_be16(p + n);
n += sizeof(uint16_t);
SDPDBG("DTD of attrId : %d Attr id : 0x%x ", dtd, attr);
data = sdp_extract_attr(p + n, bufsize - n, &attrlen, rec);
SDPDBG("Attr id : 0x%x attrValueLength : %d", attr, attrlen);
n += attrlen;
if (data == NULL) {
SDPDBG("Terminating extraction of attributes");
break;
}
if (attr == SDP_ATTR_RECORD_HANDLE)
rec->handle = data->val.uint32;
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
extract_svclass_uuid(data, &rec->svclass);
extracted += n;
p += n;
bufsize -= n;
sdp_attr_replace(rec, attr, data);
SDPDBG("Extract PDU, seqLength: %d localExtractedLength: %d",
seqlen, extracted);
}
#ifdef SDP_DEBUG
SDPDBG("Successful extracting of Svc Rec attributes");
sdp_print_service_attr(rec->attrlist);
#endif
*scanned += seqlen;
return rec;
}
static void sdp_copy_pattern(void *value, void *udata)
{
uuid_t *uuid = value;
sdp_record_t *rec = udata;
sdp_pattern_add_uuid(rec, uuid);
}
static void *sdp_data_value(sdp_data_t *data, uint32_t *len)
{
void *val = NULL;
switch (data->dtd) {
case SDP_DATA_NIL:
break;
case SDP_UINT8:
val = &data->val.uint8;
break;
case SDP_INT8:
case SDP_BOOL:
val = &data->val.int8;
break;
case SDP_UINT16:
val = &data->val.uint16;
break;
case SDP_INT16:
val = &data->val.int16;
break;
case SDP_UINT32:
val = &data->val.uint32;
break;
case SDP_INT32:
val = &data->val.int32;
break;
case SDP_INT64:
val = &data->val.int64;
break;
case SDP_UINT64:
val = &data->val.uint64;
break;
case SDP_UINT128:
val = &data->val.uint128;
break;
case SDP_INT128:
val = &data->val.int128;
break;
case SDP_UUID16:
val = &data->val.uuid.value.uuid16;
break;
case SDP_UUID32:
val = &data->val.uuid.value.uuid32;
break;
case SDP_UUID128:
val = &data->val.uuid.value.uuid128;
break;
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_URL_STR32:
case SDP_TEXT_STR32:
val = data->val.str;
if (len)
*len = data->unitSize - sizeof(uint8_t);
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
val = sdp_copy_seq(data->val.dataseq);
break;
}
return val;
}
static sdp_data_t *sdp_copy_seq(sdp_data_t *data)
{
sdp_data_t *tmp, *seq = NULL, *cur = NULL;
for (tmp = data; tmp; tmp = tmp->next) {
sdp_data_t *datatmp;
void *value;
uint32_t len = 0;
value = sdp_data_value(tmp, &len);
datatmp = sdp_data_alloc_with_length(tmp->dtd, value, len);
if (!datatmp) {
sdp_data_free(seq);
return NULL;
}
if (cur)
cur->next = datatmp;
else
seq = datatmp;
cur = datatmp;
}
return seq;
}
static void sdp_copy_attrlist(void *value, void *udata)
{
sdp_data_t *data = value;
sdp_record_t *rec = udata;
void *val;
uint32_t len = 0;
val = sdp_data_value(data, &len);
if (!len)
sdp_attr_add_new(rec, data->attrId, data->dtd, val);
else
sdp_attr_add_new_with_length(rec, data->attrId,
data->dtd, val, len);
}
sdp_record_t *sdp_copy_record(sdp_record_t *rec)
{
sdp_record_t *cpy;
cpy = sdp_record_alloc();
cpy->handle = rec->handle;
sdp_list_foreach(rec->pattern, sdp_copy_pattern, cpy);
sdp_list_foreach(rec->attrlist, sdp_copy_attrlist, cpy);
cpy->svclass = rec->svclass;
return cpy;
}
#ifdef SDP_DEBUG
static void print_dataseq(sdp_data_t *p)
{
sdp_data_t *d;
for (d = p; d; d = d->next)
sdp_data_print(d);
}
#endif
void sdp_record_print(const sdp_record_t *rec)
{
sdp_data_t *d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY);
if (d && SDP_IS_TEXT_STR(d->dtd))
printf("Service Name: %.*s\n", d->unitSize, d->val.str);
d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY);
if (d && SDP_IS_TEXT_STR(d->dtd))
printf("Service Description: %.*s\n", d->unitSize, d->val.str);
d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY);
if (d && SDP_IS_TEXT_STR(d->dtd))
printf("Service Provider: %.*s\n", d->unitSize, d->val.str);
}
#ifdef SDP_DEBUG
void sdp_data_print(sdp_data_t *d)
{
switch (d->dtd) {
case SDP_DATA_NIL:
SDPDBG("NIL");
break;
case SDP_BOOL:
case SDP_UINT8:
case SDP_UINT16:
case SDP_UINT32:
case SDP_UINT64:
case SDP_UINT128:
case SDP_INT8:
case SDP_INT16:
case SDP_INT32:
case SDP_INT64:
case SDP_INT128:
SDPDBG("Integer : 0x%x", d->val.uint32);
break;
case SDP_UUID16:
case SDP_UUID32:
case SDP_UUID128:
SDPDBG("UUID");
sdp_uuid_print(&d->val.uuid);
break;
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_TEXT_STR32:
SDPDBG("Text : %s", d->val.str);
break;
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_URL_STR32:
SDPDBG("URL : %s", d->val.str);
break;
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
print_dataseq(d->val.dataseq);
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
SDPDBG("Data Sequence Alternates");
print_dataseq(d->val.dataseq);
break;
}
}
#endif
sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId)
{
if (rec && rec->attrlist) {
sdp_data_t sdpTemplate;
sdp_list_t *p;
sdpTemplate.attrId = attrId;
p = sdp_list_find(rec->attrlist, &sdpTemplate, sdp_attrid_comp_func);
if (p)
return p->data;
}
return NULL;
}
static int sdp_send_req(sdp_session_t *session, uint8_t *buf, uint32_t size)
{
uint32_t sent = 0;
while (sent < size) {
int n = send(session->sock, buf + sent, size - sent, 0);
if (n < 0)
return -1;
sent += n;
}
return 0;
}
static int sdp_read_rsp(sdp_session_t *session, uint8_t *buf, uint32_t size)
{
fd_set readFds;
struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 };
FD_ZERO(&readFds);
FD_SET(session->sock, &readFds);
SDPDBG("Waiting for response");
if (select(session->sock + 1, &readFds, NULL, NULL, &timeout) == 0) {
SDPERR("Client timed out");
errno = ETIMEDOUT;
return -1;
}
return recv(session->sock, buf, size, 0);
}
/*
* generic send request, wait for response method.
*/
int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *reqbuf,
uint8_t *rspbuf, uint32_t reqsize, uint32_t *rspsize)
{
int n;
sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *) reqbuf;
sdp_pdu_hdr_t *rsphdr = (sdp_pdu_hdr_t *) rspbuf;
SDPDBG("");
if (0 > sdp_send_req(session, reqbuf, reqsize)) {
SDPERR("Error sending data:%m");
return -1;
}
n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
if (0 > n)
return -1;
SDPDBG("Read : %d", n);
if (n == 0 || reqhdr->tid != rsphdr->tid) {
errno = EPROTO;
return -1;
}
*rspsize = n;
return 0;
}
/*
* singly-linked lists (after openobex implementation)
*/
sdp_list_t *sdp_list_append(sdp_list_t *p, void *d)
{
sdp_list_t *q, *n = malloc(sizeof(sdp_list_t));
if (!n)
return NULL;
n->data = d;
n->next = 0;
if (!p)
return n;
for (q = p; q->next; q = q->next);
q->next = n;
return p;
}
sdp_list_t *sdp_list_remove(sdp_list_t *list, void *d)
{
sdp_list_t *p, *q;
for (q = 0, p = list; p; q = p, p = p->next)
if (p->data == d) {
if (q)
q->next = p->next;
else
list = p->next;
free(p);
break;
}
return list;
}
sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *d,
sdp_comp_func_t f)
{
sdp_list_t *q, *p, *n;
n = malloc(sizeof(sdp_list_t));
if (!n)
return NULL;
n->data = d;
for (q = 0, p = list; p; q = p, p = p->next)
if (f(p->data, d) >= 0)
break;
/* insert between q and p; if !q insert at head */
if (q)
q->next = n;
else
list = n;
n->next = p;
return list;
}
/*
* Every element of the list points to things which need
* to be free()'d. This method frees the list's contents
*/
void sdp_list_free(sdp_list_t *list, sdp_free_func_t f)
{
sdp_list_t *next;
while (list) {
next = list->next;
if (f)
f(list->data);
free(list);
list = next;
}
}
static inline int __find_port(sdp_data_t *seq, int proto)
{
if (!seq || !seq->next)
return 0;
if (SDP_IS_UUID(seq->dtd) && sdp_uuid_to_proto(&seq->val.uuid) == proto) {
seq = seq->next;
switch (seq->dtd) {
case SDP_UINT8:
return seq->val.uint8;
case SDP_UINT16:
return seq->val.uint16;
}
}
return 0;
}
int sdp_get_proto_port(const sdp_list_t *list, int proto)
{
if (proto != L2CAP_UUID && proto != RFCOMM_UUID) {
errno = EINVAL;
return -1;
}
for (; list; list = list->next) {
sdp_list_t *p;
for (p = list->data; p; p = p->next) {
sdp_data_t *seq = p->data;
int port = __find_port(seq, proto);
if (port)
return port;
}
}
return 0;
}
sdp_data_t *sdp_get_proto_desc(sdp_list_t *list, int proto)
{
for (; list; list = list->next) {
sdp_list_t *p;
for (p = list->data; p; p = p->next) {
sdp_data_t *seq = p->data;
if (SDP_IS_UUID(seq->dtd) &&
sdp_uuid_to_proto(&seq->val.uuid) == proto)
return seq->next;
}
}
return NULL;
}
static int sdp_get_proto_descs(uint16_t attr_id, const sdp_record_t *rec,
sdp_list_t **pap)
{
sdp_data_t *pdlist, *curr;
sdp_list_t *ap = NULL;
pdlist = sdp_data_get(rec, attr_id);
if (pdlist == NULL) {
errno = ENODATA;
return -1;
}
SDPDBG("Attribute value type: 0x%02x", pdlist->dtd);
if (attr_id == SDP_ATTR_ADD_PROTO_DESC_LIST) {
if (!SDP_IS_SEQ(pdlist->dtd)) {
errno = EINVAL;
return -1;
}
pdlist = pdlist->val.dataseq;
}
for (; pdlist; pdlist = pdlist->next) {
sdp_list_t *pds = NULL;
if (!SDP_IS_SEQ(pdlist->dtd) && !SDP_IS_ALT(pdlist->dtd))
goto failed;
for (curr = pdlist->val.dataseq; curr; curr = curr->next) {
if (!SDP_IS_SEQ(curr->dtd)) {
sdp_list_free(pds, NULL);
goto failed;
}
pds = sdp_list_append(pds, curr->val.dataseq);
}
ap = sdp_list_append(ap, pds);
}
*pap = ap;
return 0;
failed:
sdp_list_foreach(ap, (sdp_list_func_t) sdp_list_free, NULL);
sdp_list_free(ap, NULL);
errno = EINVAL;
return -1;
}
int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
{
return sdp_get_proto_descs(SDP_ATTR_PROTO_DESC_LIST, rec, pap);
}
int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
{
return sdp_get_proto_descs(SDP_ATTR_ADD_PROTO_DESC_LIST, rec, pap);
}
int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr,
sdp_list_t **seqp)
{
sdp_data_t *sdpdata = sdp_data_get(rec, attr);
*seqp = NULL;
if (sdpdata && SDP_IS_SEQ(sdpdata->dtd)) {
sdp_data_t *d;
for (d = sdpdata->val.dataseq; d; d = d->next) {
uuid_t *u;
if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128) {
errno = EINVAL;
goto fail;
}
u = malloc(sizeof(uuid_t));
if (!u)
goto fail;
*u = d->val.uuid;
*seqp = sdp_list_append(*seqp, u);
}
return 0;
}
fail:
sdp_list_free(*seqp, free);
*seqp = NULL;
return -1;
}
int sdp_set_uuidseq_attr(sdp_record_t *rec, uint16_t aid, sdp_list_t *seq)
{
int status = 0, i, len;
void **dtds, **values;
uint8_t uuid16 = SDP_UUID16;
uint8_t uuid32 = SDP_UUID32;
uint8_t uuid128 = SDP_UUID128;
sdp_list_t *p;
len = sdp_list_len(seq);
if (!seq || len == 0)
return -1;
dtds = malloc(len * sizeof(void *));
if (!dtds)
return -1;
values = malloc(len * sizeof(void *));
if (!values) {
free(dtds);
return -1;
}
for (p = seq, i = 0; i < len; i++, p = p->next) {
uuid_t *uuid = p->data;
if (uuid)
switch (uuid->type) {
case SDP_UUID16:
dtds[i] = &uuid16;
values[i] = &uuid->value.uuid16;
break;
case SDP_UUID32:
dtds[i] = &uuid32;
values[i] = &uuid->value.uuid32;
break;
case SDP_UUID128:
dtds[i] = &uuid128;
values[i] = &uuid->value.uuid128;
break;
default:
status = -1;
break;
}
else {
status = -1;
break;
}
}
if (status == 0) {
sdp_data_t *data = sdp_seq_alloc(dtds, values, len);
sdp_attr_replace(rec, aid, data);
sdp_pattern_add_uuidseq(rec, seq);
}
free(dtds);
free(values);
return status;
}
int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq)
{
sdp_lang_attr_t *lang;
sdp_data_t *sdpdata, *curr_data;
*langSeq = NULL;
sdpdata = sdp_data_get(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST);
if (sdpdata == NULL) {
errno = ENODATA;
return -1;
}
if (!SDP_IS_SEQ(sdpdata->dtd))
goto invalid;
curr_data = sdpdata->val.dataseq;
while (curr_data) {
sdp_data_t *pCode, *pEncoding, *pOffset;
pCode = curr_data;
if (pCode->dtd != SDP_UINT16)
goto invalid;
/* LanguageBaseAttributeIDList entries are always grouped as
* triplets */
if (!pCode->next || !pCode->next->next)
goto invalid;
pEncoding = pCode->next;
if (pEncoding->dtd != SDP_UINT16)
goto invalid;
pOffset = pEncoding->next;
if (pOffset->dtd != SDP_UINT16)
goto invalid;
lang = malloc(sizeof(sdp_lang_attr_t));
if (!lang) {
sdp_list_free(*langSeq, free);
*langSeq = NULL;
return -1;
}
lang->code_ISO639 = pCode->val.uint16;
lang->encoding = pEncoding->val.uint16;
lang->base_offset = pOffset->val.uint16;
SDPDBG("code_ISO639 : 0x%02x", lang->code_ISO639);
SDPDBG("encoding : 0x%02x", lang->encoding);
SDPDBG("base_offfset : 0x%02x", lang->base_offset);
*langSeq = sdp_list_append(*langSeq, lang);
curr_data = pOffset->next;
}
return 0;
invalid:
sdp_list_free(*langSeq, free);
*langSeq = NULL;
errno = EINVAL;
return -1;
}
int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq)
{
sdp_profile_desc_t *profDesc;
sdp_data_t *sdpdata, *seq;
*profDescSeq = NULL;
sdpdata = sdp_data_get(rec, SDP_ATTR_PFILE_DESC_LIST);
if (sdpdata == NULL) {
errno = ENODATA;
return -1;
}
if (!SDP_IS_SEQ(sdpdata->dtd) || sdpdata->val.dataseq == NULL)
goto invalid;
for (seq = sdpdata->val.dataseq; seq; seq = seq->next) {
uuid_t *uuid = NULL;
uint16_t version = 0x100;
if (SDP_IS_UUID(seq->dtd)) {
/* Mac OS X 10.7.3 and old Samsung phones do not comply
* to the SDP specification for
* BluetoothProfileDescriptorList. This workaround
* allows to properly parse UUID/version from SDP
* record published by these systems. */
sdp_data_t *next = seq->next;
uuid = &seq->val.uuid;
if (next && next->dtd == SDP_UINT16) {
version = next->val.uint16;
seq = next;
}
} else if (SDP_IS_SEQ(seq->dtd)) {
sdp_data_t *puuid, *pVnum;
puuid = seq->val.dataseq;
if (puuid == NULL || !SDP_IS_UUID(puuid->dtd))
goto invalid;
uuid = &puuid->val.uuid;
pVnum = puuid->next;
if (pVnum == NULL || pVnum->dtd != SDP_UINT16)
goto invalid;
version = pVnum->val.uint16;
} else
goto invalid;
if (uuid != NULL) {
profDesc = malloc(sizeof(sdp_profile_desc_t));
if (!profDesc) {
sdp_list_free(*profDescSeq, free);
*profDescSeq = NULL;
return -1;
}
profDesc->uuid = *uuid;
profDesc->version = version;
#ifdef SDP_DEBUG
sdp_uuid_print(&profDesc->uuid);
SDPDBG("Vnum : 0x%04x", profDesc->version);
#endif
*profDescSeq = sdp_list_append(*profDescSeq, profDesc);
}
}
return 0;
invalid:
sdp_list_free(*profDescSeq, free);
*profDescSeq = NULL;
errno = EINVAL;
return -1;
}
int sdp_get_server_ver(const sdp_record_t *rec, sdp_list_t **u16)
{
sdp_data_t *d, *curr;
*u16 = NULL;
d = sdp_data_get(rec, SDP_ATTR_VERSION_NUM_LIST);
if (d == NULL) {
errno = ENODATA;
return -1;
}
if (!SDP_IS_SEQ(d->dtd) || d->val.dataseq == NULL)
goto invalid;
for (curr = d->val.dataseq; curr; curr = curr->next) {
if (curr->dtd != SDP_UINT16)
goto invalid;
*u16 = sdp_list_append(*u16, &curr->val.uint16);
}
return 0;
invalid:
sdp_list_free(*u16, NULL);
*u16 = NULL;
errno = EINVAL;
return -1;
}
/* flexible extraction of basic attributes - Jean II */
/* How do we expect caller to extract predefined data sequences? */
int sdp_get_int_attr(const sdp_record_t *rec, uint16_t attrid, int *value)
{
sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
if (sdpdata)
/* Verify that it is what the caller expects */
if (sdpdata->dtd == SDP_BOOL || sdpdata->dtd == SDP_UINT8 ||
sdpdata->dtd == SDP_UINT16 || sdpdata->dtd == SDP_UINT32 ||
sdpdata->dtd == SDP_INT8 || sdpdata->dtd == SDP_INT16 ||
sdpdata->dtd == SDP_INT32) {
*value = sdpdata->val.uint32;
return 0;
}
errno = EINVAL;
return -1;
}
int sdp_get_string_attr(const sdp_record_t *rec, uint16_t attrid, char *value,
size_t valuelen)
{
sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
/* Verify that it is what the caller expects */
if (!sdpdata || !SDP_IS_TEXT_STR(sdpdata->dtd))
goto fail;
/* Have to copy the NULL terminator too, so check len < valuelen. */
if (strlen(sdpdata->val.str) < valuelen) {
strcpy(value, sdpdata->val.str);
return 0;
}
fail:
errno = EINVAL;
return -1;
}
#define get_basic_attr(attrID, pAttrValue, fieldName) \
sdp_data_t *data = sdp_data_get(rec, attrID); \
if (data) { \
*pAttrValue = data->val.fieldName; \
return 0; \
} \
errno = EINVAL; \
return -1;
int sdp_get_service_id(const sdp_record_t *rec, uuid_t *uuid)
{
get_basic_attr(SDP_ATTR_SERVICE_ID, uuid, uuid);
}
int sdp_get_group_id(const sdp_record_t *rec, uuid_t *uuid)
{
get_basic_attr(SDP_ATTR_GROUP_ID, uuid, uuid);
}
int sdp_get_record_state(const sdp_record_t *rec, uint32_t *svcRecState)
{
get_basic_attr(SDP_ATTR_RECORD_STATE, svcRecState, uint32);
}
int sdp_get_service_avail(const sdp_record_t *rec, uint8_t *svcAvail)
{
get_basic_attr(SDP_ATTR_SERVICE_AVAILABILITY, svcAvail, uint8);
}
int sdp_get_service_ttl(const sdp_record_t *rec, uint32_t *svcTTLInfo)
{
get_basic_attr(SDP_ATTR_SVCINFO_TTL, svcTTLInfo, uint32);
}
int sdp_get_database_state(const sdp_record_t *rec, uint32_t *svcDBState)
{
get_basic_attr(SDP_ATTR_SVCDB_STATE, svcDBState, uint32);
}
/*
* NOTE that none of the setXXX() functions below will
* actually update the SDP server, unless the
* {register, update}sdp_record_t() function is invoked.
*/
int sdp_attr_add_new(sdp_record_t *rec, uint16_t attr, uint8_t dtd,
const void *value)
{
sdp_data_t *d = sdp_data_alloc(dtd, value);
if (d) {
sdp_attr_replace(rec, attr, d);
return 0;
}
return -1;
}
static int sdp_attr_add_new_with_length(sdp_record_t *rec,
uint16_t attr, uint8_t dtd, const void *value, uint32_t len)
{
sdp_data_t *d;
d = sdp_data_alloc_with_length(dtd, value, len);
if (!d)
return -1;
sdp_attr_replace(rec, attr, d);
return 0;
}
/*
* Set the information attributes of the service
* pointed to by rec. The attributes are
* service name, description and provider name
*/
void sdp_set_info_attr(sdp_record_t *rec, const char *name, const char *prov,
const char *desc)
{
if (name)
sdp_attr_add_new(rec, SDP_ATTR_SVCNAME_PRIMARY,
SDP_TEXT_STR8, name);
if (prov)
sdp_attr_add_new(rec, SDP_ATTR_PROVNAME_PRIMARY,
SDP_TEXT_STR8, prov);
if (desc)
sdp_attr_add_new(rec, SDP_ATTR_SVCDESC_PRIMARY,
SDP_TEXT_STR8, desc);
}
static sdp_data_t *access_proto_to_dataseq(sdp_record_t *rec, sdp_list_t *proto)
{
sdp_data_t *seq = NULL;
void *dtds[10], *values[10];
void **seqDTDs, **seqs;
int i, seqlen;
sdp_list_t *p;
seqlen = sdp_list_len(proto);
seqDTDs = bt_malloc0(seqlen * sizeof(void *));
if (!seqDTDs)
return NULL;
seqs = malloc(seqlen * sizeof(void *));
if (!seqs) {
free(seqDTDs);
return NULL;
}
for (i = 0, p = proto; p; p = p->next, i++) {
sdp_list_t *elt = p->data;
sdp_data_t *s;
uuid_t *uuid = NULL;
unsigned int pslen = 0;
for (; elt && pslen < ARRAY_SIZE(dtds); elt = elt->next, pslen++) {
sdp_data_t *d = elt->data;
dtds[pslen] = &d->dtd;
switch (d->dtd) {
case SDP_UUID16:
uuid = (uuid_t *) d;
values[pslen] = &uuid->value.uuid16;
break;
case SDP_UUID32:
uuid = (uuid_t *) d;
values[pslen] = &uuid->value.uuid32;
break;
case SDP_UUID128:
uuid = (uuid_t *) d;
values[pslen] = &uuid->value.uuid128;
break;
case SDP_UINT8:
values[pslen] = &d->val.uint8;
break;
case SDP_UINT16:
values[pslen] = &d->val.uint16;
break;
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
values[pslen] = d;
break;
/* FIXME: more */
}
}
s = sdp_seq_alloc(dtds, values, pslen);
if (s) {
seqDTDs[i] = &s->dtd;
seqs[i] = s;
if (uuid)
sdp_pattern_add_uuid(rec, uuid);
}
}
seq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
free(seqDTDs);
free(seqs);
return seq;
}
/*
* sets the access protocols of the service specified
* to the value specified in "access_proto"
*
* Note that if there are alternate mechanisms by
* which the service is accessed, then they should
* be specified as sequences
*
* Using a value of NULL for accessProtocols has
* effect of removing this attribute (if previously set)
*
* This function replaces the existing sdp_access_proto_t
* structure (if any) with the new one specified.
*
* returns 0 if successful or -1 if there is a failure.
*/
int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
{
const sdp_list_t *p;
sdp_data_t *protos = NULL;
for (p = ap; p; p = p->next) {
sdp_data_t *seq = access_proto_to_dataseq(rec, p->data);
protos = sdp_seq_append(protos, seq);
}
sdp_attr_add(rec, SDP_ATTR_PROTO_DESC_LIST, protos);
return 0;
}
int sdp_set_add_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
{
const sdp_list_t *p;
sdp_data_t *protos = NULL;
for (p = ap; p; p = p->next) {
sdp_data_t *seq = access_proto_to_dataseq(rec, p->data);
protos = sdp_seq_append(protos, seq);
}
sdp_attr_add(rec, SDP_ATTR_ADD_PROTO_DESC_LIST,
protos ? sdp_data_alloc(SDP_SEQ8, protos) : NULL);
return 0;
}
/*
* set the "LanguageBase" attributes of the service record
* record to the value specified in "langAttrList".
*
* "langAttrList" is a linked list of "sdp_lang_attr_t"
* objects, one for each language in which user visible
* attributes are present in the service record.
*
* Using a value of NULL for langAttrList has
* effect of removing this attribute (if previously set)
*
* This function replaces the exisiting sdp_lang_attr_t
* structure (if any) with the new one specified.
*
* returns 0 if successful or -1 if there is a failure.
*/
int sdp_set_lang_attr(sdp_record_t *rec, const sdp_list_t *seq)
{
uint8_t uint16 = SDP_UINT16;
int status = 0, i = 0, seqlen = sdp_list_len(seq);
void **dtds, **values;
const sdp_list_t *p;
dtds = malloc(3 * seqlen * sizeof(void *));
if (!dtds)
return -1;
values = malloc(3 * seqlen * sizeof(void *));
if (!values) {
free(dtds);
return -1;
}
for (p = seq; p; p = p->next) {
sdp_lang_attr_t *lang = p->data;
if (!lang) {
status = -1;
break;
}
dtds[i] = &uint16;
values[i] = &lang->code_ISO639;
i++;
dtds[i] = &uint16;
values[i] = &lang->encoding;
i++;
dtds[i] = &uint16;
values[i] = &lang->base_offset;
i++;
}
if (status == 0) {
sdp_data_t *seq = sdp_seq_alloc(dtds, values, 3 * seqlen);
sdp_attr_add(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST, seq);
}
free(dtds);
free(values);
return status;
}
/*
* set the "ServiceID" attribute of the service.
*
* This is the UUID of the service.
*
* returns 0 if successful or -1 if there is a failure.
*/
void sdp_set_service_id(sdp_record_t *rec, uuid_t uuid)
{
switch (uuid.type) {
case SDP_UUID16:
sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID16,
&uuid.value.uuid16);
break;
case SDP_UUID32:
sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID32,
&uuid.value.uuid32);
break;
case SDP_UUID128:
sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID128,
&uuid.value.uuid128);
break;
}
sdp_pattern_add_uuid(rec, &uuid);
}
/*
* set the GroupID attribute of the service record defining a group.
*
* This is the UUID of the group.
*
* returns 0 if successful or -1 if there is a failure.
*/
void sdp_set_group_id(sdp_record_t *rec, uuid_t uuid)
{
switch (uuid.type) {
case SDP_UUID16:
sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID16,
&uuid.value.uuid16);
break;
case SDP_UUID32:
sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID32,
&uuid.value.uuid32);
break;
case SDP_UUID128:
sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID128,
&uuid.value.uuid128);
break;
}
sdp_pattern_add_uuid(rec, &uuid);
}
/*
* set the ProfileDescriptorList attribute of the service record
* pointed to by record to the value specified in "profileDesc".
*
* Each element in the list is an object of type
* sdp_profile_desc_t which is a definition of the
* Bluetooth profile that this service conforms to.
*
* Using a value of NULL for profileDesc has
* effect of removing this attribute (if previously set)
*
* This function replaces the exisiting ProfileDescriptorList
* structure (if any) with the new one specified.
*
* returns 0 if successful or -1 if there is a failure.
*/
int sdp_set_profile_descs(sdp_record_t *rec, const sdp_list_t *profiles)
{
int status = 0;
uint8_t uuid16 = SDP_UUID16;
uint8_t uuid32 = SDP_UUID32;
uint8_t uuid128 = SDP_UUID128;
uint8_t uint16 = SDP_UINT16;
int i = 0, seqlen = sdp_list_len(profiles);
void **seqDTDs, **seqs;
const sdp_list_t *p;
sdp_data_t *pAPSeq;
seqDTDs = malloc(seqlen * sizeof(void *));
if (!seqDTDs)
return -1;
seqs = malloc(seqlen * sizeof(void *));
if (!seqs) {
free(seqDTDs);
return -1;
}
for (p = profiles; p; p = p->next) {
sdp_data_t *seq;
void *dtds[2], *values[2];
sdp_profile_desc_t *profile = p->data;
if (!profile) {
status = -1;
goto end;
}
switch (profile->uuid.type) {
case SDP_UUID16:
dtds[0] = &uuid16;
values[0] = &profile->uuid.value.uuid16;
break;
case SDP_UUID32:
dtds[0] = &uuid32;
values[0] = &profile->uuid.value.uuid32;
break;
case SDP_UUID128:
dtds[0] = &uuid128;
values[0] = &profile->uuid.value.uuid128;
break;
default:
status = -1;
goto end;
}
dtds[1] = &uint16;
values[1] = &profile->version;
seq = sdp_seq_alloc(dtds, values, 2);
if (seq == NULL) {
status = -1;
goto end;
}
seqDTDs[i] = &seq->dtd;
seqs[i] = seq;
sdp_pattern_add_uuid(rec, &profile->uuid);
i++;
}
pAPSeq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
sdp_attr_add(rec, SDP_ATTR_PFILE_DESC_LIST, pAPSeq);
end:
free(seqDTDs);
free(seqs);
return status;
}
/*
* sets various URL attributes of the service
* pointed to by record. The URL include
*
* client: a URL to the client's
* platform specific (WinCE, PalmOS) executable
* code that can be used to access this service.
*
* doc: a URL pointing to service documentation
*
* icon: a URL to an icon that can be used to represent
* this service.
*
* Note that you need to pass NULL for any URLs
* that you don't want to set or remove
*/
void sdp_set_url_attr(sdp_record_t *rec, const char *client, const char *doc,
const char *icon)
{
sdp_attr_add_new(rec, SDP_ATTR_CLNT_EXEC_URL, SDP_URL_STR8, client);
sdp_attr_add_new(rec, SDP_ATTR_DOC_URL, SDP_URL_STR8, doc);
sdp_attr_add_new(rec, SDP_ATTR_ICON_URL, SDP_URL_STR8, icon);
}
uuid_t *sdp_uuid16_create(uuid_t *u, uint16_t val)
{
memset(u, 0, sizeof(uuid_t));
u->type = SDP_UUID16;
u->value.uuid16 = val;
return u;
}
uuid_t *sdp_uuid32_create(uuid_t *u, uint32_t val)
{
memset(u, 0, sizeof(uuid_t));
u->type = SDP_UUID32;
u->value.uuid32 = val;
return u;
}
uuid_t *sdp_uuid128_create(uuid_t *u, const void *val)
{
memset(u, 0, sizeof(uuid_t));
u->type = SDP_UUID128;
memcpy(&u->value.uuid128, val, sizeof(uint128_t));
return u;
}
/*
* UUID comparison function
* returns 0 if uuidValue1 == uuidValue2 else -1
*/
int sdp_uuid_cmp(const void *p1, const void *p2)
{
uuid_t *u1 = sdp_uuid_to_uuid128(p1);
uuid_t *u2 = sdp_uuid_to_uuid128(p2);
int ret;
ret = sdp_uuid128_cmp(u1, u2);
bt_free(u1);
bt_free(u2);
return ret;
}
/*
* UUID comparison function
* returns 0 if uuidValue1 == uuidValue2 else -1
*/
int sdp_uuid16_cmp(const void *p1, const void *p2)
{
const uuid_t *u1 = p1;
const uuid_t *u2 = p2;
return memcmp(&u1->value.uuid16, &u2->value.uuid16, sizeof(uint16_t));
}
/*
* UUID comparison function
* returns 0 if uuidValue1 == uuidValue2 else -1
*/
int sdp_uuid128_cmp(const void *p1, const void *p2)
{
const uuid_t *u1 = p1;
const uuid_t *u2 = p2;
return memcmp(&u1->value.uuid128, &u2->value.uuid128, sizeof(uint128_t));
}
/*
* 128 to 16 bit and 32 to 16 bit UUID conversion functions
* yet to be implemented. Note that the input is in NBO in
* both 32 and 128 bit UUIDs and conversion is needed
*/
void sdp_uuid16_to_uuid128(uuid_t *uuid128, const uuid_t *uuid16)
{
/*
* We have a 16 bit value, which needs to be added to
* bytes 3 and 4 (at indices 2 and 3) of the Bluetooth base
*/
unsigned short data1;
/* allocate a 128bit UUID and init to the Bluetooth base UUID */
uuid128->value.uuid128 = bluetooth_base_uuid;
uuid128->type = SDP_UUID128;
/* extract bytes 2 and 3 of 128bit BT base UUID */
memcpy(&data1, &bluetooth_base_uuid.data[2], 2);
/* add the given UUID (16 bits) */
data1 += htons(uuid16->value.uuid16);
/* set bytes 2 and 3 of the 128 bit value */
memcpy(&uuid128->value.uuid128.data[2], &data1, 2);
}
void sdp_uuid32_to_uuid128(uuid_t *uuid128, const uuid_t *uuid32)
{
/*
* We have a 32 bit value, which needs to be added to
* bytes 1->4 (at indices 0 thru 3) of the Bluetooth base
*/
unsigned int data0;
/* allocate a 128bit UUID and init to the Bluetooth base UUID */
uuid128->value.uuid128 = bluetooth_base_uuid;
uuid128->type = SDP_UUID128;
/* extract first 4 bytes */
memcpy(&data0, &bluetooth_base_uuid.data[0], 4);
/* add the given UUID (32bits) */
data0 += htonl(uuid32->value.uuid32);
/* set the 4 bytes of the 128 bit value */
memcpy(&uuid128->value.uuid128.data[0], &data0, 4);
}
uuid_t *sdp_uuid_to_uuid128(const uuid_t *uuid)
{
uuid_t *uuid128 = bt_malloc0(sizeof(uuid_t));
if (!uuid128)
return NULL;
switch (uuid->type) {
case SDP_UUID128:
*uuid128 = *uuid;
break;
case SDP_UUID32:
sdp_uuid32_to_uuid128(uuid128, uuid);
break;
case SDP_UUID16:
sdp_uuid16_to_uuid128(uuid128, uuid);
break;
}
return uuid128;
}
/*
* converts a 128-bit uuid to a 16/32-bit one if possible
* returns true if uuid contains a 16/32-bit UUID at exit
*/
int sdp_uuid128_to_uuid(uuid_t *uuid)
{
const uint128_t *b = &bluetooth_base_uuid;
uint128_t *u = &uuid->value.uuid128;
uint32_t data;
unsigned int i;
if (uuid->type != SDP_UUID128)
return 1;
for (i = 4; i < sizeof(b->data); i++)
if (b->data[i] != u->data[i])
return 0;
memcpy(&data, u->data, 4);
data = htonl(data);
if (data <= 0xffff) {
uuid->type = SDP_UUID16;
uuid->value.uuid16 = (uint16_t) data;
} else {
uuid->type = SDP_UUID32;
uuid->value.uuid32 = data;
}
return 1;
}
/*
* convert a UUID to the 16-bit short-form
*/
int sdp_uuid_to_proto(uuid_t *uuid)
{
uuid_t u = *uuid;
if (sdp_uuid128_to_uuid(&u)) {
switch (u.type) {
case SDP_UUID16:
return u.value.uuid16;
case SDP_UUID32:
return u.value.uuid32;
}
}
return 0;
}
/*
* This function appends data to the PDU buffer "dst" from source "src".
* The data length is also computed and set.
* Should the PDU length exceed 2^8, then sequence type is
* set accordingly and the data is memmove()'d.
*/
void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len)
{
uint8_t *p = dst->data;
uint8_t dtd = *p;
SDPDBG("Append src size: %d", len);
SDPDBG("Append dst size: %d", dst->data_size);
SDPDBG("Dst buffer size: %d", dst->buf_size);
if (dst->data_size + len > dst->buf_size) {
SDPERR("Cannot append");
return;
}
if (dst->data_size == 0 && dtd == 0) {
/* create initial sequence */
*p = SDP_SEQ8;
dst->data_size += sizeof(uint8_t);
/* reserve space for sequence size */
dst->data_size += sizeof(uint8_t);
}
memcpy(dst->data + dst->data_size, data, len);
dst->data_size += len;
dtd = *(uint8_t *) dst->data;
if (dst->data_size > UCHAR_MAX && dtd == SDP_SEQ8) {
short offset = sizeof(uint8_t) + sizeof(uint8_t);
memmove(dst->data + offset + 1, dst->data + offset,
dst->data_size - offset);
*p = SDP_SEQ16;
dst->data_size += 1;
}
dtd = *(uint8_t *) p;
p += sizeof(uint8_t);
switch (dtd) {
case SDP_SEQ8:
*(uint8_t *) p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t);
break;
case SDP_SEQ16:
bt_put_be16(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t), p);
break;
case SDP_SEQ32:
bt_put_be32(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t), p);
break;
}
}
void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d)
{
sdp_buf_t append;
memset(&append, 0, sizeof(sdp_buf_t));
sdp_gen_buffer(&append, d);
append.data = malloc(append.buf_size);
if (!append.data)
return;
sdp_set_attrid(&append, d->attrId);
sdp_gen_pdu(&append, d);
sdp_append_to_buf(pdu, append.data, append.data_size);
free(append.data);
}
/*
* Registers an sdp record.
*
* It is incorrect to call this method on a record that
* has been already registered with the server.
*
* Returns zero on success, otherwise -1 (and sets errno).
*/
int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, uint8_t *data, uint32_t size, uint8_t flags, uint32_t *handle)
{
uint8_t *req, *rsp, *p;
uint32_t reqsize, rspsize;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
int status;
SDPDBG("");
if (!session->local) {
errno = EREMOTE;
return -1;
}
req = malloc(SDP_REQ_BUFFER_SIZE);
rsp = malloc(SDP_RSP_BUFFER_SIZE);
if (req == NULL || rsp == NULL) {
status = -1;
errno = ENOMEM;
goto end;
}
reqhdr = (sdp_pdu_hdr_t *)req;
reqhdr->pdu_id = SDP_SVC_REGISTER_REQ;
reqhdr->tid = htons(sdp_gen_tid(session));
reqsize = sizeof(sdp_pdu_hdr_t) + 1;
p = req + sizeof(sdp_pdu_hdr_t);
if (bacmp(device, BDADDR_ANY)) {
*p++ = flags | SDP_DEVICE_RECORD;
bacpy((bdaddr_t *) p, device);
p += sizeof(bdaddr_t);
reqsize += sizeof(bdaddr_t);
} else
*p++ = flags;
memcpy(p, data, size);
reqsize += size;
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize);
if (status < 0)
goto end;
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
SDPERR("Unexpected end of packet");
errno = EPROTO;
status = -1;
goto end;
}
rsphdr = (sdp_pdu_hdr_t *) rsp;
p = rsp + sizeof(sdp_pdu_hdr_t);
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
/* Invalid service record */
errno = EINVAL;
status = -1;
} else if (rsphdr->pdu_id != SDP_SVC_REGISTER_RSP) {
errno = EPROTO;
status = -1;
} else {
if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint32_t)) {
SDPERR("Unexpected end of packet");
errno = EPROTO;
status = -1;
goto end;
}
if (handle)
*handle = bt_get_be32(p);
}
end:
free(req);
free(rsp);
return status;
}
int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags)
{
sdp_buf_t pdu;
uint32_t handle;
int err;
SDPDBG("");
if (rec->handle && rec->handle != 0xffffffff) {
uint32_t handle = rec->handle;
sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
}
if (sdp_gen_record_pdu(rec, &pdu) < 0) {
errno = ENOMEM;
return -1;
}
err = sdp_device_record_register_binary(session, device,
pdu.data, pdu.data_size, flags, &handle);
free(pdu.data);
if (err == 0) {
sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
rec->handle = handle;
sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
}
return err;
}
int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags)
{
return sdp_device_record_register(session, BDADDR_ANY, rec, flags);
}
/*
* unregister a service record
*/
int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle)
{
uint8_t *reqbuf, *rspbuf, *p;
uint32_t reqsize = 0, rspsize = 0;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
int status;
SDPDBG("");
if (handle == SDP_SERVER_RECORD_HANDLE) {
errno = EINVAL;
return -1;
}
if (!session->local) {
errno = EREMOTE;
return -1;
}
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
if (!reqbuf || !rspbuf) {
errno = ENOMEM;
status = -1;
goto end;
}
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
reqhdr->pdu_id = SDP_SVC_REMOVE_REQ;
reqhdr->tid = htons(sdp_gen_tid(session));
p = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);
bt_put_be32(handle, p);
reqsize += sizeof(uint32_t);
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
if (status < 0)
goto end;
if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
errno = EPROTO;
status = -1;
goto end;
}
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
p = rspbuf + sizeof(sdp_pdu_hdr_t);
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
/* For this case the status always is invalid record handle */
errno = EINVAL;
status = -1;
} else if (rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) {
errno = EPROTO;
status = -1;
} else {
uint16_t tmp;
memcpy(&tmp, p, sizeof(tmp));
status = tmp;
}
end:
free(reqbuf);
free(rspbuf);
return status;
}
int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec)
{
int err;
err = sdp_device_record_unregister_binary(session, device, rec->handle);
if (err == 0)
sdp_record_free(rec);
return err;
}
int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec)
{
return sdp_device_record_unregister(session, BDADDR_ANY, rec);
}
/*
* modify an existing service record
*/
int sdp_device_record_update_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle, uint8_t *data, uint32_t size)
{
return -1;
}
int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec)
{
uint8_t *reqbuf, *rspbuf, *p;
uint32_t reqsize, rspsize;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
uint32_t handle;
sdp_buf_t pdu;
int status;
SDPDBG("");
handle = rec->handle;
if (handle == SDP_SERVER_RECORD_HANDLE) {
errno = EINVAL;
return -1;
}
if (!session->local) {
errno = EREMOTE;
return -1;
}
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
if (!reqbuf || !rspbuf) {
errno = ENOMEM;
status = -1;
goto end;
}
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
reqhdr->pdu_id = SDP_SVC_UPDATE_REQ;
reqhdr->tid = htons(sdp_gen_tid(session));
p = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);
bt_put_be32(handle, p);
reqsize += sizeof(uint32_t);
p += sizeof(uint32_t);
if (sdp_gen_record_pdu(rec, &pdu) < 0) {
errno = ENOMEM;
status = -1;
goto end;
}
memcpy(p, pdu.data, pdu.data_size);
reqsize += pdu.data_size;
free(pdu.data);
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
if (status < 0)
goto end;
if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
errno = EPROTO;
status = -1;
goto end;
}
SDPDBG("Send req status : %d", status);
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
p = rspbuf + sizeof(sdp_pdu_hdr_t);
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
/* The status can be invalid sintax or invalid record handle */
errno = EINVAL;
status = -1;
} else if (rsphdr->pdu_id != SDP_SVC_UPDATE_RSP) {
errno = EPROTO;
status = -1;
} else {
uint16_t tmp;
memcpy(&tmp, p, sizeof(tmp));
status = tmp;
}
end:
free(reqbuf);
free(rspbuf);
return status;
}
int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec)
{
return sdp_device_record_update(session, BDADDR_ANY, rec);
}
sdp_record_t *sdp_record_alloc(void)
{
sdp_record_t *rec = bt_malloc0(sizeof(sdp_record_t));
if (!rec)
return NULL;
rec->handle = 0xffffffff;
return rec;
}
/*
* Free the contents of a service record
*/
void sdp_record_free(sdp_record_t *rec)
{
sdp_list_free(rec->attrlist, (sdp_free_func_t) sdp_data_free);
sdp_list_free(rec->pattern, free);
free(rec);
}
void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid)
{
uuid_t *uuid128 = sdp_uuid_to_uuid128(uuid);
SDPDBG("Elements in target pattern : %d", sdp_list_len(rec->pattern));
SDPDBG("Trying to add : 0x%lx", (unsigned long) uuid128);
if (sdp_list_find(rec->pattern, uuid128, sdp_uuid128_cmp) == NULL)
rec->pattern = sdp_list_insert_sorted(rec->pattern, uuid128, sdp_uuid128_cmp);
else
bt_free(uuid128);
SDPDBG("Elements in target pattern : %d", sdp_list_len(rec->pattern));
}
void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq)
{
for (; seq; seq = seq->next) {
uuid_t *uuid = (uuid_t *)seq->data;
sdp_pattern_add_uuid(rec, uuid);
}
}
/*
* Extract a sequence of service record handles from a PDU buffer
* and add the entries to a sdp_list_t. Note that the service record
* handles are not in "data element sequence" form, but just like
* an array of service handles
*/
static void extract_record_handle_seq(uint8_t *pdu, int bufsize, sdp_list_t **seq, int count, unsigned int *scanned)
{
sdp_list_t *pSeq = *seq;
uint8_t *pdata = pdu;
int n;
for (n = 0; n < count; n++) {
uint32_t *pSvcRec;
if (bufsize < (int) sizeof(uint32_t)) {
SDPERR("Unexpected end of packet");
break;
}
pSvcRec = malloc(sizeof(uint32_t));
if (!pSvcRec)
break;
*pSvcRec = bt_get_be32(pdata);
pSeq = sdp_list_append(pSeq, pSvcRec);
pdata += sizeof(uint32_t);
*scanned += sizeof(uint32_t);
bufsize -= sizeof(uint32_t);
}
*seq = pSeq;
}
/*
* Generate the attribute sequence pdu form
* from sdp_list_t elements. Return length of attr seq
*/
static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd)
{
sdp_data_t *dataseq;
void **types, **values;
sdp_buf_t buf;
int i, seqlen = sdp_list_len(seq);
/* Fill up the value and the dtd arrays */
SDPDBG("");
SDPDBG("Seq length : %d", seqlen);
types = malloc(seqlen * sizeof(void *));
if (!types)
return -ENOMEM;
values = malloc(seqlen * sizeof(void *));
if (!values) {
free(types);
return -ENOMEM;
}
for (i = 0; i < seqlen; i++) {
void *data = seq->data;
types[i] = &dtd;
if (SDP_IS_UUID(dtd))
data = &((uuid_t *)data)->value;
values[i] = data;
seq = seq->next;
}
dataseq = sdp_seq_alloc(types, values, seqlen);
if (!dataseq) {
free(types);
free(values);
return -ENOMEM;
}
memset(&buf, 0, sizeof(sdp_buf_t));
sdp_gen_buffer(&buf, dataseq);
buf.data = malloc(buf.buf_size);
if (!buf.data) {
sdp_data_free(dataseq);
free(types);
free(values);
return -ENOMEM;
}
SDPDBG("Data Seq : 0x%p", seq);
seqlen = sdp_gen_pdu(&buf, dataseq);
SDPDBG("Copying : %d", buf.data_size);
memcpy(dst, buf.data, buf.data_size);
sdp_data_free(dataseq);
free(types);
free(values);
free(buf.data);
return seqlen;
}
static int gen_searchseq_pdu(uint8_t *dst, const sdp_list_t *seq)
{
uuid_t *uuid = seq->data;
return gen_dataseq_pdu(dst, seq, uuid->type);
}
static int gen_attridseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dataType)
{
return gen_dataseq_pdu(dst, seq, dataType);
}
typedef struct {
uint8_t length;
unsigned char data[16];
} __attribute__ ((packed)) sdp_cstate_t;
static int copy_cstate(uint8_t *pdata, int pdata_len, const sdp_cstate_t *cstate)
{
if (cstate) {
uint8_t len = cstate->length;
if (len >= pdata_len) {
SDPERR("Continuation state size exceeds internal buffer");
len = pdata_len - 1;
}
*pdata++ = len;
memcpy(pdata, cstate->data, len);
return len + 1;
}
*pdata = 0;
return 1;
}
/*
* This is a service search request.
*
* INPUT :
*
* sdp_list_t *search
* Singly linked list containing elements of the search
* pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
* of the service to be searched
*
* uint16_t max_rec_num
* A 16 bit integer which tells the service, the maximum
* entries that the client can handle in the response. The
* server is obliged not to return > max_rec_num entries
*
* OUTPUT :
*
* int return value
* 0:
* The request completed successfully. This does not
* mean the requested services were found
* -1:
* On any failure and sets errno
*
* sdp_list_t **rsp_list
* This variable is set on a successful return if there are
* non-zero service handles. It is a singly linked list of
* service record handles (uint16_t)
*/
int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search,
uint16_t max_rec_num, sdp_list_t **rsp)
{
int status = 0;
uint32_t reqsize = 0, _reqsize;
uint32_t rspsize = 0, rsplen;
int seqlen = 0;
int rec_count;
unsigned scanned, pdata_len;
uint8_t *pdata, *_pdata;
uint8_t *reqbuf, *rspbuf;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
sdp_cstate_t *cstate = NULL;
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
if (!reqbuf || !rspbuf) {
errno = ENOMEM;
status = -1;
goto end;
}
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);
/* add service class IDs for search */
seqlen = gen_searchseq_pdu(pdata, search);
if (seqlen < 0) {
errno = EINVAL;
status = -1;
goto end;
}
SDPDBG("Data seq added : %d", seqlen);
/* set the length and increment the pointer */
reqsize += seqlen;
pdata += seqlen;
/* specify the maximum svc rec count that client expects */
bt_put_be16(max_rec_num, pdata);
reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);
_reqsize = reqsize;
_pdata = pdata;
*rsp = NULL;
do {
/* Add continuation state or NULL (first time) */
reqsize = _reqsize + copy_cstate(_pdata,
SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
/* Set the request header's param length */
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
reqhdr->tid = htons(sdp_gen_tid(session));
/*
* Send the request, wait for response and if
* no error, set the appropriate values and return
*/
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
if (status < 0)
goto end;
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
SDPERR("Unexpected end of packet");
status = -1;
goto end;
}
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
rsplen = ntohs(rsphdr->plen);
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
SDPDBG("Status : 0x%x", rsphdr->pdu_id);
status = -1;
goto end;
}
scanned = 0;
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
if (pdata_len < sizeof(uint16_t) + sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
status = -1;
goto end;
}
/* net service record match count */
pdata += sizeof(uint16_t);
scanned += sizeof(uint16_t);
pdata_len -= sizeof(uint16_t);
rec_count = bt_get_be16(pdata);
pdata += sizeof(uint16_t);
scanned += sizeof(uint16_t);
pdata_len -= sizeof(uint16_t);
SDPDBG("Current svc count: %d", rec_count);
SDPDBG("ResponseLength: %d", rsplen);
if (!rec_count) {
status = -1;
goto end;
}
extract_record_handle_seq(pdata, pdata_len, rsp, rec_count, &scanned);
SDPDBG("BytesScanned : %d", scanned);
if (rsplen > scanned) {
uint8_t cstate_len;
if (rspsize < sizeof(sdp_pdu_hdr_t) + scanned + sizeof(uint8_t)) {
SDPERR("Unexpected end of packet: continuation state data missing");
status = -1;
goto end;
}
pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned;
cstate_len = *(uint8_t *) pdata;
if (cstate_len > 0) {
cstate = (sdp_cstate_t *)pdata;
SDPDBG("Cont state length: %d", cstate_len);
} else
cstate = NULL;
}
} while (cstate);
end:
free(reqbuf);
free(rspbuf);
return status;
}
/*
* This is a service attribute request.
*
* INPUT :
*
* uint32_t handle
* The handle of the service for which the attribute(s) are
* requested
*
* sdp_attrreq_type_t reqtype
* Attribute identifiers are 16 bit unsigned integers specified
* in one of 2 ways described below :
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
* They are the actual attribute identifiers in ascending order
*
* SDP_ATTR_REQ_RANGE - 32bit identifier range
* The high-order 16bits is the start of range
* the low-order 16bits are the end of range
* 0x0000 to 0xFFFF gets all attributes
*
* sdp_list_t *attrid
* Singly linked list containing attribute identifiers desired.
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
*
* OUTPUT :
* return sdp_record_t *
* 0:
* On any error and sets errno
* !0:
* The service record
*/
sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle,
sdp_attrreq_type_t reqtype, const sdp_list_t *attrids)
{
uint32_t reqsize = 0, _reqsize;
uint32_t rspsize = 0, rsp_count;
int attr_list_len = 0;
int seqlen = 0;
unsigned int pdata_len;
uint8_t *pdata, *_pdata;
uint8_t *reqbuf, *rspbuf;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
sdp_cstate_t *cstate = NULL;
uint8_t cstate_len = 0;
sdp_buf_t rsp_concat_buf;
sdp_record_t *rec = 0;
if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
errno = EINVAL;
return NULL;
}
memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
if (!reqbuf || !rspbuf) {
errno = ENOMEM;
goto end;
}
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);
/* add the service record handle */
bt_put_be32(handle, pdata);
reqsize += sizeof(uint32_t);
pdata += sizeof(uint32_t);
/* specify the response limit */
bt_put_be16(65535, pdata);
reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);
/* get attr seq PDU form */
seqlen = gen_attridseq_pdu(pdata, attrids,
reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
if (seqlen < 0) {
errno = EINVAL;
goto end;
}
pdata += seqlen;
reqsize += seqlen;
SDPDBG("Attr list length : %d", seqlen);
/* save before Continuation State */
_pdata = pdata;
_reqsize = reqsize;
do {
int status;
/* add NULL continuation state */
reqsize = _reqsize + copy_cstate(_pdata,
SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
/* set the request header's param length */
reqhdr->tid = htons(sdp_gen_tid(session));
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
if (status < 0)
goto end;
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
SDPERR("Unexpected end of packet");
goto end;
}
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
SDPDBG("PDU ID : 0x%x", rsphdr->pdu_id);
goto end;
}
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
if (pdata_len < sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
goto end;
}
rsp_count = bt_get_be16(pdata);
attr_list_len += rsp_count;
pdata += sizeof(uint16_t);
pdata_len -= sizeof(uint16_t);
/*
* if continuation state set need to re-issue request before
* parsing
*/
if (pdata_len < rsp_count + sizeof(uint8_t)) {
SDPERR("Unexpected end of packet: continuation state data missing");
goto end;
}
cstate_len = *(uint8_t *) (pdata + rsp_count);
SDPDBG("Response id : %d", rsphdr->pdu_id);
SDPDBG("Attrlist byte count : %d", rsp_count);
SDPDBG("sdp_cstate_t length : %d", cstate_len);
/*
* a split response: concatenate intermediate responses
* and the last one (which has cstate_len == 0)
*/
if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
uint8_t *targetPtr = NULL;
cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
/* build concatenated response buffer */
rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
memcpy(targetPtr, pdata, rsp_count);
rsp_concat_buf.data_size += rsp_count;
}
} while (cstate);
if (attr_list_len > 0) {
int scanned = 0;
if (rsp_concat_buf.data_size != 0) {
pdata = rsp_concat_buf.data;
pdata_len = rsp_concat_buf.data_size;
}
rec = sdp_extract_pdu(pdata, pdata_len, &scanned);
}
end:
free(reqbuf);
free(rsp_concat_buf.data);
free(rspbuf);
return rec;
}
/*
* SDP transaction structure for asynchronous search
*/
struct sdp_transaction {
sdp_callback_t *cb; /* called when the transaction finishes */
void *udata; /* client user data */
uint8_t *reqbuf; /* pointer to request PDU */
sdp_buf_t rsp_concat_buf;
uint32_t reqsize; /* without cstate */
int err; /* ZERO if success or the errno if failed */
};
/*
* Creates a new sdp session for asynchronous search
* INPUT:
* int sk
* non-blocking L2CAP socket
*
* RETURN:
* sdp_session_t *
* NULL - On memory allocation failure
*/
sdp_session_t *sdp_create(int sk, uint32_t flags)
{
sdp_session_t *session;
struct sdp_transaction *t;
session = bt_malloc0(sizeof(sdp_session_t));
if (!session) {
errno = ENOMEM;
return NULL;
}
session->flags = flags;
session->sock = sk;
t = bt_malloc0(sizeof(struct sdp_transaction));
if (!t) {
errno = ENOMEM;
free(session);
return NULL;
}
session->priv = t;
return session;
}
/*
* Sets the callback function/user data used to notify the application
* that the asynchronous transaction finished. This function must be
* called before request an asynchronous search.
*
* INPUT:
* sdp_session_t *session
* Current sdp session to be handled
* sdp_callback_t *cb
* callback to be called when the transaction finishes
* void *udata
* user data passed to callback
* RETURN:
* 0 - Success
* -1 - Failure
*/
int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata)
{
struct sdp_transaction *t;
if (!session || !session->priv)
return -1;
t = session->priv;
t->cb = func;
t->udata = udata;
return 0;
}
/*
* This function starts an asynchronous service search request.
* The incoming and outgoing data are stored in the transaction structure
* buffers. When there is incoming data the sdp_process function must be
* called to get the data and handle the continuation state.
*
* INPUT :
* sdp_session_t *session
* Current sdp session to be handled
*
* sdp_list_t *search
* Singly linked list containing elements of the search
* pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
* of the service to be searched
*
* uint16_t max_rec_num
* A 16 bit integer which tells the service, the maximum
* entries that the client can handle in the response. The
* server is obliged not to return > max_rec_num entries
*
* OUTPUT :
*
* int return value
* 0 - if the request has been sent properly
* -1 - On any failure and sets errno
*/
int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num)
{
struct sdp_transaction *t;
sdp_pdu_hdr_t *reqhdr;
uint8_t *pdata;
int cstate_len, seqlen = 0;
if (!session || !session->priv)
return -1;
t = session->priv;
/* clean possible allocated buffer */
free(t->rsp_concat_buf.data);
memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
if (!t->reqbuf) {
t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
if (!t->reqbuf) {
t->err = ENOMEM;
goto end;
}
}
memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
reqhdr->tid = htons(sdp_gen_tid(session));
reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
/* generate PDU */
pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
t->reqsize = sizeof(sdp_pdu_hdr_t);
/* add service class IDs for search */
seqlen = gen_searchseq_pdu(pdata, search);
if (seqlen < 0) {
t->err = EINVAL;
goto end;
}
SDPDBG("Data seq added : %d", seqlen);
/* now set the length and increment the pointer */
t->reqsize += seqlen;
pdata += seqlen;
bt_put_be16(max_rec_num, pdata);
t->reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);
/* set the request header's param length */
cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
SDPERR("Error sending data:%m");
t->err = errno;
goto end;
}
return 0;
end:
free(t->reqbuf);
t->reqbuf = NULL;
return -1;
}
/*
* This function starts an asynchronous service attribute request.
* The incoming and outgoing data are stored in the transaction structure
* buffers. When there is incoming data the sdp_process function must be
* called to get the data and handle the continuation state.
*
* INPUT :
* sdp_session_t *session
* Current sdp session to be handled
*
* uint32_t handle
* The handle of the service for which the attribute(s) are
* requested
*
* sdp_attrreq_type_t reqtype
* Attribute identifiers are 16 bit unsigned integers specified
* in one of 2 ways described below :
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
* They are the actual attribute identifiers in ascending order
*
* SDP_ATTR_REQ_RANGE - 32bit identifier range
* The high-order 16bits is the start of range
* the low-order 16bits are the end of range
* 0x0000 to 0xFFFF gets all attributes
*
* sdp_list_t *attrid_list
* Singly linked list containing attribute identifiers desired.
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
*
* OUTPUT :
* int return value
* 0 - if the request has been sent properly
* -1 - On any failure and sets errno
*/
int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list)
{
struct sdp_transaction *t;
sdp_pdu_hdr_t *reqhdr;
uint8_t *pdata;
int cstate_len, seqlen = 0;
if (!session || !session->priv)
return -1;
t = session->priv;
/* clean possible allocated buffer */
free(t->rsp_concat_buf.data);
memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
if (!t->reqbuf) {
t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
if (!t->reqbuf) {
t->err = ENOMEM;
goto end;
}
}
memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
reqhdr->tid = htons(sdp_gen_tid(session));
reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
/* generate PDU */
pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
t->reqsize = sizeof(sdp_pdu_hdr_t);
/* add the service record handle */
bt_put_be32(handle, pdata);
t->reqsize += sizeof(uint32_t);
pdata += sizeof(uint32_t);
/* specify the response limit */
bt_put_be16(65535, pdata);
t->reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);
/* get attr seq PDU form */
seqlen = gen_attridseq_pdu(pdata, attrid_list,
reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
if (seqlen < 0) {
t->err = EINVAL;
goto end;
}
/* now set the length and increment the pointer */
t->reqsize += seqlen;
pdata += seqlen;
SDPDBG("Attr list length : %d", seqlen);
/* set the request header's param length */
cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
SDPERR("Error sending data:%m");
t->err = errno;
goto end;
}
return 0;
end:
free(t->reqbuf);
t->reqbuf = NULL;
return -1;
}
/*
* This function starts an asynchronous service search attributes.
* It is a service search request combined with attribute request. The incoming
* and outgoing data are stored in the transaction structure buffers. When there
* is incoming data the sdp_process function must be called to get the data
* and handle the continuation state.
*
* INPUT:
* sdp_session_t *session
* Current sdp session to be handled
*
* sdp_list_t *search
* Singly linked list containing elements of the search
* pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
* of the service to be searched
*
* AttributeSpecification attrSpec
* Attribute identifiers are 16 bit unsigned integers specified
* in one of 2 ways described below :
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
* They are the actual attribute identifiers in ascending order
*
* SDP_ATTR_REQ_RANGE - 32bit identifier range
* The high-order 16bits is the start of range
* the low-order 16bits are the end of range
* 0x0000 to 0xFFFF gets all attributes
*
* sdp_list_t *attrid_list
* Singly linked list containing attribute identifiers desired.
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
*
* RETURN:
* 0 - if the request has been sent properly
* -1 - On any failure
*/
int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list)
{
struct sdp_transaction *t;
sdp_pdu_hdr_t *reqhdr;
uint8_t *pdata;
int cstate_len, seqlen = 0;
if (!session || !session->priv)
return -1;
t = session->priv;
/* clean possible allocated buffer */
free(t->rsp_concat_buf.data);
memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
if (!t->reqbuf) {
t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
if (!t->reqbuf) {
t->err = ENOMEM;
goto end;
}
}
memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
reqhdr->tid = htons(sdp_gen_tid(session));
reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
/* generate PDU */
pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
t->reqsize = sizeof(sdp_pdu_hdr_t);
/* add service class IDs for search */
seqlen = gen_searchseq_pdu(pdata, search);
if (seqlen < 0) {
t->err = EINVAL;
goto end;
}
SDPDBG("Data seq added : %d", seqlen);
/* now set the length and increment the pointer */
t->reqsize += seqlen;
pdata += seqlen;
bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
t->reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);
SDPDBG("Max attr byte count : %d", SDP_MAX_ATTR_LEN);
/* get attr seq PDU form */
seqlen = gen_attridseq_pdu(pdata, attrid_list,
reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
if (seqlen < 0) {
t->err = EINVAL;
goto end;
}
pdata += seqlen;
SDPDBG("Attr list length : %d", seqlen);
t->reqsize += seqlen;
/* set the request header's param length */
cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
SDPERR("Error sending data:%m");
t->err = errno;
goto end;
}
return 0;
end:
free(t->reqbuf);
t->reqbuf = NULL;
return -1;
}
/*
* Function used to get the error reason after sdp_callback_t function has been called
* and the status is 0xffff or if sdp_service_{search, attr, search_attr}_async returns -1.
* It indicates that an error NOT related to SDP_ErrorResponse happened. Get errno directly
* is not safe because multiple transactions can be triggered.
* This function must be used with asynchronous sdp functions only.
*
* INPUT:
* sdp_session_t *session
* Current sdp session to be handled
* RETURN:
* 0 = No error in the current transaction
* -1 - if the session is invalid
* positive value - the errno value
*
*/
int sdp_get_error(sdp_session_t *session)
{
struct sdp_transaction *t;
if (!session || !session->priv) {
SDPERR("Invalid session");
return -1;
}
t = session->priv;
return t->err;
}
/*
* Receive the incoming SDP PDU. This function must be called when there is data
* available to be read. On continuation state, the original request (with a new
* transaction ID) and the continuation state data will be appended in the initial PDU.
* If an error happens or the transaction finishes the callback function will be called.
*
* INPUT:
* sdp_session_t *session
* Current sdp session to be handled
* RETURN:
* 0 - if the transaction is on continuation state
* -1 - On any failure or the transaction finished
*/
int sdp_process(sdp_session_t *session)
{
struct sdp_transaction *t;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
sdp_cstate_t *pcstate;
uint8_t *pdata, *rspbuf, *targetPtr;
int rsp_count, err = -1;
size_t size = 0;
int n, plen;
uint16_t status = 0xffff;
uint8_t pdu_id = 0x00;
if (!session || !session->priv) {
SDPERR("Invalid session");
return -1;
}
rspbuf = bt_malloc0(SDP_RSP_BUFFER_SIZE);
if (!rspbuf) {
SDPERR("Response buffer alloc failure:%m (%d)", errno);
return -1;
}
t = session->priv;
reqhdr = (sdp_pdu_hdr_t *)t->reqbuf;
rsphdr = (sdp_pdu_hdr_t *)rspbuf;
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
if (n < 0) {
SDPERR("Read response:%m (%d)", errno);
t->err = errno;
goto end;
}
if (reqhdr->tid != rsphdr->tid) {
t->err = EPROTO;
SDPERR("Protocol error: transaction id does not match");
goto end;
}
if (n != (int) (ntohs(rsphdr->plen) + sizeof(sdp_pdu_hdr_t))) {
t->err = EPROTO;
SDPERR("Protocol error: invalid length");
goto end;
}
pdu_id = rsphdr->pdu_id;
switch (rsphdr->pdu_id) {
uint8_t *ssr_pdata;
uint16_t tsrc, csrc;
case SDP_SVC_SEARCH_RSP:
/*
* TSRC: Total Service Record Count (2 bytes)
* CSRC: Current Service Record Count (2 bytes)
*/
ssr_pdata = pdata;
tsrc = bt_get_be16(ssr_pdata);
ssr_pdata += sizeof(uint16_t);
csrc = bt_get_be16(ssr_pdata);
/* csrc should never be larger than tsrc */
if (csrc > tsrc) {
t->err = EPROTO;
SDPERR("Protocol error: wrong current service record count value.");
goto end;
}
SDPDBG("Total svc count: %d", tsrc);
SDPDBG("Current svc count: %d", csrc);
/* parameter length without continuation state */
plen = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
if (t->rsp_concat_buf.data_size == 0) {
/* first fragment */
rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
} else if (t->rsp_concat_buf.data_size >= sizeof(uint16_t) * 2) {
/* point to the first csrc */
uint8_t *pcsrc = t->rsp_concat_buf.data + 2;
uint16_t tcsrc, tcsrc2;
/* FIXME: update the interface later. csrc doesn't need be passed to clients */
pdata += sizeof(uint16_t); /* point to csrc */
/* the first csrc contains the sum of partial csrc responses */
memcpy(&tcsrc, pcsrc, sizeof(tcsrc));
memcpy(&tcsrc2, pdata, sizeof(tcsrc2));
tcsrc += tcsrc2;
memcpy(pcsrc, &tcsrc, sizeof(tcsrc));
pdata += sizeof(uint16_t); /* point to the first handle */
rsp_count = csrc * 4;
} else {
t->err = EPROTO;
SDPERR("Protocol error: invalid PDU size");
status = SDP_INVALID_PDU_SIZE;
goto end;
}
status = 0x0000;
break;
case SDP_SVC_ATTR_RSP:
case SDP_SVC_SEARCH_ATTR_RSP:
rsp_count = bt_get_be16(pdata);
SDPDBG("Attrlist byte count : %d", rsp_count);
/* Valid range for rsp_count is 0x0002-0xFFFF */
if (t->rsp_concat_buf.data_size == 0 && rsp_count < 0x0002) {
t->err = EPROTO;
SDPERR("Protocol error: invalid AttrList size");
status = SDP_INVALID_PDU_SIZE;
goto end;
}
/*
* Number of bytes in the AttributeLists parameter(without
* continuation state) + AttributeListsByteCount field size.
*/
plen = sizeof(uint16_t) + rsp_count;
pdata += sizeof(uint16_t); /* points to attribute list */
status = 0x0000;
break;
case SDP_ERROR_RSP:
status = bt_get_be16(pdata);
size = ntohs(rsphdr->plen);
goto end;
default:
t->err = EPROTO;
SDPERR("Illegal PDU ID: 0x%x", rsphdr->pdu_id);
goto end;
}
/* Out of bound check before using rsp_count as offset for
* continuation state, which has at least a one byte size
* field.
*/
if ((n - (int) sizeof(sdp_pdu_hdr_t)) < plen + 1) {
t->err = EPROTO;
SDPERR("Protocol error: invalid PDU size");
status = SDP_INVALID_PDU_SIZE;
goto end;
}
pcstate = (sdp_cstate_t *) (pdata + rsp_count);
SDPDBG("Cstate length : %d", pcstate->length);
/*
* Check out of bound. Continuation state must have at least
* 1 byte: ZERO to indicate that it is not a partial response.
*/
if ((n - (int) sizeof(sdp_pdu_hdr_t)) != (plen + pcstate->length + 1)) {
t->err = EPROTO;
SDPERR("Protocol error: wrong PDU size.");
status = 0xffff;
goto end;
}
/*
* This is a split response, need to concatenate intermediate
* responses and the last one which will have cstate length == 0
*/
t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count);
targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size;
t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count;
memcpy(targetPtr, pdata, rsp_count);
t->rsp_concat_buf.data_size += rsp_count;
if (pcstate->length > 0) {
int reqsize, cstate_len;
reqhdr->tid = htons(sdp_gen_tid(session));
/* add continuation state */
cstate_len = copy_cstate(t->reqbuf + t->reqsize,
SDP_REQ_BUFFER_SIZE - t->reqsize, pcstate);
reqsize = t->reqsize + cstate_len;
/* set the request header's param length */
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
if (sdp_send_req(session, t->reqbuf, reqsize) < 0) {
SDPERR("Error sending data:%m(%d)", errno);
status = 0xffff;
t->err = errno;
goto end;
}
err = 0;
}
end:
if (err) {
if (t->rsp_concat_buf.data_size != 0) {
pdata = t->rsp_concat_buf.data;
size = t->rsp_concat_buf.data_size;
}
if (t->cb)
t->cb(pdu_id, status, pdata, size, t->udata);
}
free(rspbuf);
return err;
}
/*
* This is a service search request combined with the service
* attribute request. First a service class match is done and
* for matching service, requested attributes are extracted
*
* INPUT :
*
* sdp_list_t *search
* Singly linked list containing elements of the search
* pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
* of the service to be searched
*
* AttributeSpecification attrSpec
* Attribute identifiers are 16 bit unsigned integers specified
* in one of 2 ways described below :
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
* They are the actual attribute identifiers in ascending order
*
* SDP_ATTR_REQ_RANGE - 32bit identifier range
* The high-order 16bits is the start of range
* the low-order 16bits are the end of range
* 0x0000 to 0xFFFF gets all attributes
*
* sdp_list_t *attrids
* Singly linked list containing attribute identifiers desired.
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
*
* OUTPUT :
* int return value
* 0:
* The request completed successfully. This does not
* mean the requested services were found
* -1:
* On any error and sets errno
*
* sdp_list_t **rsp
* This variable is set on a successful return to point to
* service(s) found. Each element of this list is of type
* sdp_record_t* (of the services which matched the search list)
*/
int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrids, sdp_list_t **rsp)
{
int status = 0;
uint32_t reqsize = 0, _reqsize;
uint32_t rspsize = 0;
int seqlen = 0, attr_list_len = 0;
int rsp_count = 0, cstate_len = 0;
unsigned int pdata_len;
uint8_t *pdata, *_pdata;
uint8_t *reqbuf, *rspbuf;
sdp_pdu_hdr_t *reqhdr, *rsphdr;
uint8_t dataType;
sdp_list_t *rec_list = NULL;
sdp_buf_t rsp_concat_buf;
sdp_cstate_t *cstate = NULL;
if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
errno = EINVAL;
return -1;
}
memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
if (!reqbuf || !rspbuf) {
errno = ENOMEM;
status = -1;
goto end;
}
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
/* generate PDU */
pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);
/* add service class IDs for search */
seqlen = gen_searchseq_pdu(pdata, search);
if (seqlen < 0) {
errno = EINVAL;
status = -1;
goto end;
}
SDPDBG("Data seq added : %d", seqlen);
/* now set the length and increment the pointer */
reqsize += seqlen;
pdata += seqlen;
bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);
SDPDBG("Max attr byte count : %d", SDP_MAX_ATTR_LEN);
/* get attr seq PDU form */
seqlen = gen_attridseq_pdu(pdata, attrids,
reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
if (seqlen < 0) {
errno = EINVAL;
status = -1;
goto end;
}
pdata += seqlen;
SDPDBG("Attr list length : %d", seqlen);
reqsize += seqlen;
*rsp = 0;
/* save before Continuation State */
_pdata = pdata;
_reqsize = reqsize;
do {
reqhdr->tid = htons(sdp_gen_tid(session));
/* add continuation state (can be null) */
reqsize = _reqsize + copy_cstate(_pdata,
SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
/* set the request header's param length */
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
SDPERR("Unexpected end of packet");
status = -1;
goto end;
}
if (status < 0) {
SDPDBG("Status : 0x%x", rsphdr->pdu_id);
goto end;
}
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
status = -1;
goto end;
}
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
if (pdata_len < sizeof(uint16_t)) {
SDPERR("Unexpected end of packet");
status = -1;
goto end;
}
rsp_count = bt_get_be16(pdata);
attr_list_len += rsp_count;
pdata += sizeof(uint16_t); /* pdata points to attribute list */
pdata_len -= sizeof(uint16_t);
if (pdata_len < rsp_count + sizeof(uint8_t)) {
SDPERR("Unexpected end of packet: continuation state data missing");
status = -1;
goto end;
}
cstate_len = *(uint8_t *) (pdata + rsp_count);
SDPDBG("Attrlist byte count : %d", attr_list_len);
SDPDBG("Response byte count : %d", rsp_count);
SDPDBG("Cstate length : %d", cstate_len);
/*
* This is a split response, need to concatenate intermediate
* responses and the last one which will have cstate_len == 0
*/
if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
uint8_t *targetPtr = NULL;
cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
/* build concatenated response buffer */
rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
memcpy(targetPtr, pdata, rsp_count);
rsp_concat_buf.data_size += rsp_count;
}
} while (cstate);
if (attr_list_len > 0) {
int scanned = 0;
if (rsp_concat_buf.data_size != 0) {
pdata = rsp_concat_buf.data;
pdata_len = rsp_concat_buf.data_size;
}
/*
* Response is a sequence of sequence(s) for one or
* more data element sequence(s) representing services
* for which attributes are returned
*/
scanned = sdp_extract_seqtype(pdata, pdata_len, &dataType, &seqlen);
SDPDBG("Bytes scanned : %d", scanned);
SDPDBG("Seq length : %d", seqlen);
if (scanned && seqlen) {
pdata += scanned;
pdata_len -= scanned;
do {
int recsize = 0;
sdp_record_t *rec = sdp_extract_pdu(pdata, pdata_len, &recsize);
if (rec == NULL) {
SDPERR("SVC REC is null");
status = -1;
goto end;
}
if (!recsize) {
sdp_record_free(rec);
break;
}
scanned += recsize;
pdata += recsize;
pdata_len -= recsize;
SDPDBG("Loc seq length : %d", recsize);
SDPDBG("Svc Rec Handle : 0x%x", rec->handle);
SDPDBG("Bytes scanned : %d", scanned);
SDPDBG("Attrlist byte count : %d", attr_list_len);
rec_list = sdp_list_append(rec_list, rec);
} while (scanned < attr_list_len && pdata_len > 0);
SDPDBG("Successful scan of service attr lists");
*rsp = rec_list;
}
}
end:
free(rsp_concat_buf.data);
free(reqbuf);
free(rspbuf);
return status;
}
/*
* Find devices in the piconet.
*/
int sdp_general_inquiry(inquiry_info *ii, int num_dev, int duration, uint8_t *found)
{
int n = hci_inquiry(-1, 10, num_dev, NULL, &ii, 0);
if (n < 0) {
SDPERR("Inquiry failed:%m");
return -1;
}
*found = n;
return 0;
}
int sdp_close(sdp_session_t *session)
{
struct sdp_transaction *t;
int ret;
if (!session)
return -1;
ret = close(session->sock);
t = session->priv;
if (t) {
free(t->reqbuf);
free(t->rsp_concat_buf.data);
free(t);
}
free(session);
return ret;
}
static inline int sdp_is_local(const bdaddr_t *device)
{
return memcmp(device, BDADDR_LOCAL, sizeof(bdaddr_t)) == 0;
}
static int sdp_connect_local(sdp_session_t *session)
{
struct sockaddr_un sa;
session->sock = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (session->sock < 0)
return -1;
session->local = 1;
sa.sun_family = AF_UNIX;
strcpy(sa.sun_path, SDP_UNIX_PATH);
return connect(session->sock, (struct sockaddr *) &sa, sizeof(sa));
}
static int set_l2cap_mtu(int sk, uint16_t mtu)
{
struct l2cap_options l2o;
socklen_t len;
memset(&l2o, 0, sizeof(l2o));
len = sizeof(l2o);
if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0)
return -1;
l2o.imtu = mtu;
l2o.omtu = mtu;
if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0)
return -1;
return 0;
}
static int sdp_connect_l2cap(const bdaddr_t *src,
const bdaddr_t *dst, sdp_session_t *session)
{
uint32_t flags = session->flags;
struct sockaddr_l2 sa;
int sk;
int sockflags = SOCK_SEQPACKET | SOCK_CLOEXEC;
if (flags & SDP_NON_BLOCKING)
sockflags |= SOCK_NONBLOCK;
session->sock = socket(PF_BLUETOOTH, sockflags, BTPROTO_L2CAP);
if (session->sock < 0)
return -1;
session->local = 0;
sk = session->sock;
memset(&sa, 0, sizeof(sa));
sa.l2_family = AF_BLUETOOTH;
sa.l2_psm = 0;
if (bacmp(src, BDADDR_ANY)) {
sa.l2_bdaddr = *src;
if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
return -1;
}
if (flags & SDP_WAIT_ON_CLOSE) {
struct linger l = { .l_onoff = 1, .l_linger = 1 };
if (setsockopt(sk, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
return -1;
}
if ((flags & SDP_LARGE_MTU) &&
set_l2cap_mtu(sk, SDP_LARGE_L2CAP_MTU) < 0)
return -1;
sa.l2_psm = htobs(SDP_PSM);
sa.l2_bdaddr = *dst;
do {
int ret = connect(sk, (struct sockaddr *) &sa, sizeof(sa));
if (!ret)
return 0;
if (ret < 0 && (flags & SDP_NON_BLOCKING) &&
(errno == EAGAIN || errno == EINPROGRESS))
return 0;
} while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY));
return -1;
}
sdp_session_t *sdp_connect(const bdaddr_t *src,
const bdaddr_t *dst, uint32_t flags)
{
sdp_session_t *session;
int err;
if ((flags & SDP_RETRY_IF_BUSY) && (flags & SDP_NON_BLOCKING)) {
errno = EINVAL;
return NULL;
}
session = sdp_create(-1, flags);
if (!session)
return NULL;
if (sdp_is_local(dst)) {
if (sdp_connect_local(session) < 0)
goto fail;
} else {
if (sdp_connect_l2cap(src, dst, session) < 0)
goto fail;
}
return session;
fail:
err = errno;
if (session->sock >= 0)
close(session->sock);
free(session->priv);
free(session);
errno = err;
return NULL;
}
int sdp_get_socket(const sdp_session_t *session)
{
return session->sock;
}
uint16_t sdp_gen_tid(sdp_session_t *session)
{
return session->tid++;
}
/*
* Set the supported features
*/
int sdp_set_supp_feat(sdp_record_t *rec, const sdp_list_t *sf)
{
const sdp_list_t *p, *r;
sdp_data_t *feat, *seq_feat;
int seqlen, i;
void **seqDTDs, **seqVals;
seqlen = sdp_list_len(sf);
seqDTDs = malloc(seqlen * sizeof(void *));
if (!seqDTDs)
return -1;
seqVals = malloc(seqlen * sizeof(void *));
if (!seqVals) {
free(seqDTDs);
return -1;
}
for (p = sf, i = 0; p; p = p->next, i++) {
int plen, j;
void **dtds, **vals;
int *lengths;
plen = sdp_list_len(p->data);
dtds = malloc(plen * sizeof(void *));
if (!dtds)
goto fail;
vals = malloc(plen * sizeof(void *));
if (!vals) {
free(dtds);
goto fail;
}
lengths = malloc(plen * sizeof(int));
if (!lengths) {
free(dtds);
free(vals);
goto fail;
}
for (r = p->data, j = 0; r; r = r->next, j++) {
sdp_data_t *data = (sdp_data_t *) r->data;
dtds[j] = &data->dtd;
switch (data->dtd) {
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
vals[j] = data->val.str;
lengths[j] = data->unitSize - sizeof(uint8_t);
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
vals[j] = data->val.dataseq;
lengths[j] = 0;
break;
default:
vals[j] = &data->val;
lengths[j] = 0;
break;
}
}
feat = sdp_seq_alloc_with_length(dtds, vals, lengths, plen);
free(dtds);
free(vals);
free(lengths);
if (!feat)
goto fail;
seqDTDs[i] = &feat->dtd;
seqVals[i] = feat;
}
seq_feat = sdp_seq_alloc(seqDTDs, seqVals, seqlen);
if (!seq_feat)
goto fail;
sdp_attr_replace(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST, seq_feat);
free(seqVals);
free(seqDTDs);
return 0;
fail:
free(seqVals);
free(seqDTDs);
return -1;
}
/*
* Get the supported features
* If an error occurred -1 is returned and errno is set
*/
int sdp_get_supp_feat(const sdp_record_t *rec, sdp_list_t **seqp)
{
sdp_data_t *sdpdata, *d;
sdp_list_t *tseq;
tseq = NULL;
sdpdata = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
if (!sdpdata || !SDP_IS_SEQ(sdpdata->dtd))
return sdp_get_uuidseq_attr(rec,
SDP_ATTR_SUPPORTED_FEATURES_LIST, seqp);
for (d = sdpdata->val.dataseq; d; d = d->next) {
sdp_data_t *dd;
sdp_list_t *subseq;
if (!SDP_IS_SEQ(d->dtd))
goto fail;
subseq = NULL;
for (dd = d->val.dataseq; dd; dd = dd->next) {
sdp_data_t *data;
void *val;
int length;
switch (dd->dtd) {
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
val = dd->val.str;
length = dd->unitSize - sizeof(uint8_t);
break;
case SDP_UINT8:
case SDP_UINT16:
val = &dd->val;
length = 0;
break;
default:
sdp_list_free(subseq, free);
goto fail;
}
data = sdp_data_alloc_with_length(dd->dtd, val, length);
if (data)
subseq = sdp_list_append(subseq, data);
}
tseq = sdp_list_append(tseq, subseq);
}
*seqp = tseq;
return 0;
fail:
while (tseq) {
sdp_list_t * next;
next = tseq->next;
sdp_list_free(tseq, free);
tseq = next;
}
errno = EINVAL;
return -1;
}
void sdp_add_lang_attr(sdp_record_t *rec)
{
sdp_lang_attr_t base_lang;
sdp_list_t *langs;
base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
base_lang.encoding = 106;
base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
langs = sdp_list_append(0, &base_lang);
sdp_set_lang_attr(rec, langs);
sdp_list_free(langs, NULL);
}
|