File: nbd.py

package info (click to toggle)
libnbd 1.22.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,620 kB
  • sloc: ansic: 53,724; ml: 12,270; sh: 8,499; python: 4,581; makefile: 2,901; perl: 165; cpp: 24
file content (4095 lines) | stat: -rw-r--r-- 164,736 bytes parent folder | download | duplicates (2)
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
# NBD client library in userspace
# WARNING: THIS FILE IS GENERATED FROM
# generator/generator
# ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.
#
# Copyright Red Hat
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

'''
Python bindings for libnbd

import nbd
h = nbd.NBD()
h.connect_tcp("localhost", "nbd")
buf = h.pread(512, 0)

Read the libnbd(3) man page to find out how to use the API.
'''

import contextlib
import libnbdmod

# Re-export Error exception as nbd.Error, adding some methods.
from libnbdmod import Error

Error.__doc__ = '''
Exception thrown when the underlying libnbd call fails.

This exception has three properties to query the error.  Use
the .string property to return a printable string containing
the error message.  Use the .errnum property for the associated
numeric error value (which may be 0 if the error did not
correspond to a system call failure), or the .errno property to
return a string containing the Python errno name if one is known
(which may be None if the numeric value does not correspond to
a known errno name).
'''

Error.string = property(lambda self: self.args[0])


def _errno(self):
    import errno
    try:
        return errno.errorcode[self.args[1]]
    except KeyError:
        return None


Error.errno = property(_errno)

Error.errnum = property(lambda self: self.args[1])


def _str(self):
    if self.errno:
        return "%s (%s)" % (self.string, self.errno)
    else:
        return "%s" % self.string


Error.__str__ = _str


class ClosedHandle(ValueError):
    '''This exception is thrown when any method is called on an
    nbd handle after you have called h.close() on the same handle.'''
    pass


@contextlib.contextmanager
def nbd():
    '''
    This is a context manager function.  Python will close the handle
    automatically even if the body throws an exception:

    with nbd.nbd() as h:
        # use the handle 'h'
    '''
    h = NBD()
    yield h
    h.close()


TLS_DISABLE = 0
TLS_ALLOW = 1
TLS_REQUIRE = 2

SIZE_MINIMUM = 0
SIZE_PREFERRED = 1
SIZE_MAXIMUM = 2
SIZE_PAYLOAD = 3

CMD_FLAG_FUA = 0x01
CMD_FLAG_NO_HOLE = 0x02
CMD_FLAG_DF = 0x04
CMD_FLAG_REQ_ONE = 0x08
CMD_FLAG_FAST_ZERO = 0x10
CMD_FLAG_PAYLOAD_LEN = 0x20
CMD_FLAG_MASK = 0x3f

HANDSHAKE_FLAG_FIXED_NEWSTYLE = 0x01
HANDSHAKE_FLAG_NO_ZEROES = 0x02
HANDSHAKE_FLAG_MASK = 0x03

STRICT_COMMANDS = 0x01
STRICT_FLAGS = 0x02
STRICT_BOUNDS = 0x04
STRICT_ZERO_SIZE = 0x08
STRICT_ALIGN = 0x10
STRICT_PAYLOAD = 0x20
STRICT_AUTO_FLAG = 0x40
STRICT_MASK = 0x7f

ALLOW_TRANSPORT_TCP = 0x01
ALLOW_TRANSPORT_UNIX = 0x02
ALLOW_TRANSPORT_VSOCK = 0x04
ALLOW_TRANSPORT_SSH = 0x08
ALLOW_TRANSPORT_MASK = 0x0f

SHUTDOWN_ABANDON_PENDING = 0x10000
SHUTDOWN_MASK = 0x10000

AIO_DIRECTION_READ = 1
AIO_DIRECTION_WRITE = 2
AIO_DIRECTION_BOTH = 3
READ_DATA = 1
READ_HOLE = 2
READ_ERROR = 3
NAMESPACE_BASE = "base:"
CONTEXT_BASE_ALLOCATION = "base:allocation"
STATE_HOLE = 1
STATE_ZERO = 2
NAMESPACE_QEMU = "qemu:"
CONTEXT_QEMU_DIRTY_BITMAP = "qemu:dirty-bitmap:"
STATE_DIRTY = 1
CONTEXT_QEMU_ALLOCATION_DEPTH = "qemu:allocation-depth"


class Buffer(object):
    '''Asynchronous I/O persistent buffer'''

    def __init__(self, len):
        '''Allocate an uninitialized AIO buffer used for nbd.aio_pread.'''
        self._o = libnbdmod.alloc_aio_buffer(len)

    @classmethod
    def from_buffer(cls, buf):
        '''Create an AIO buffer that shares an existing buffer-like object.

        Because the buffer is shared, changes to the original are visible
        to nbd.aio_pwrite, and changes in nbd.aio_pread are visible to the
        original.
        '''
        self = cls(0)
        # Ensure that buf is already buffer-like
        with memoryview(buf):
            self._o = buf
        self._init = True
        return self

    @classmethod
    def from_bytearray(cls, ba):
        '''Create an AIO buffer from a bytearray or other buffer-like object.

        If ba is not a buffer, it is tried as the parameter to the
        bytearray constructor.  Otherwise, ba is copied.  Either way, the
        resulting AIO buffer is independent from the original.
        '''
        return cls.from_buffer(bytearray(ba))

    def to_buffer(self):
        '''Return a shared view of the AIO buffer contents.

        This exposes the underlying buffer; changes to the buffer are
        visible to nbd.aio_pwrite, and changes from nbd.aio_pread are
        visible in the buffer.
        '''
        if not hasattr(self, '_init'):
            self._o = bytearray(len(self._o))
            self._init = True
        return self._o

    def to_bytearray(self):
        '''Copy an AIO buffer into a bytearray.

        This copies the contents of an AIO buffer to a new bytearray, which
        remains independent from the original.
        '''
        if not hasattr(self, '_init'):
            return bytearray(len(self._o))
        return bytearray(self._o)

    def size(self):
        '''Return the size of an AIO buffer.'''
        return len(self)

    def __len__(self):
        '''Return the size of an AIO buffer.'''
        return len(self._o)

    def is_zero(self, offset=0, size=-1):
        '''Returns true if and only if all bytes in the buffer are zeroes.

        Note that although a freshly allocated buffer is uninitialized,
        this will report it as all zeroes, as it will be force-initialized
        to zero before any code that can access the buffer's contents.

        By default this tests the whole buffer, but you can restrict
        the test to a sub-range of the buffer using the optional
        offset and size parameters.  If size = -1 then we check from
        offset to the end of the buffer.  If size = 0, the function
        always returns true.  If size > 0, we check the interval
        [offset..offset+size-1].
        '''
        return libnbdmod.aio_buffer_is_zero(self._o, offset, size,
                                            hasattr(self, '_init'))


class NBD(object):
    '''NBD handle'''

    def __init__(self):
        '''Create a new NBD handle.'''
        self._o = libnbdmod.create()

    def __del__(self):
        '''Close the NBD handle and underlying connection.'''
        if self._o:
            libnbdmod.close(self._o)
            self._o = None

    def _check_not_closed(self):
        if not self._o:
            raise ClosedHandle("libnbd: method called on closed handle")

    def close(self):
        '''Explicitly close the NBD handle and underlying connection.

        The handle is closed implicitly when its reference count goes
        to zero (eg. when it goes out of scope or the program ends).

        This call is only needed if you want to force the handle to
        close now.  After calling this, the program must not call
        any method on the handle (except the implicit call to
        __del__ which happens when the final reference is cleaned up).
        '''
        self._check_not_closed()
        libnbdmod.close(self._o)
        self._o = None

    def set_debug(self, debug):
        u'''▶ set or clear the debug flag

    Set or clear the debug flag. When debugging is enabled,
    debugging messages from the library are printed to
    stderr, unless a debugging callback has been defined too
    (see "nbd.set_debug_callback") in which case they are
    sent to that function. This flag defaults to false on
    newly created handles, except if "LIBNBD_DEBUG=1" is set
    in the environment in which case it defaults to true.
'''
        self._check_not_closed()
        return libnbdmod.set_debug(self._o, debug)

    def get_debug(self):
        u'''▶ return the state of the debug flag

    Return the state of the debug flag on this handle.
'''
        self._check_not_closed()
        return libnbdmod.get_debug(self._o)

    def set_debug_callback(self, debug):
        u'''▶ set the debug callback

    Set the debug callback. This function is called when the
    library emits debug messages, when debugging is enabled
    on a handle. The callback parameters are "user_data"
    passed to this function, the name of the libnbd function
    emitting the debug message ("context"), and the message
    itself ("msg"). If no debug callback is set on a handle
    then messages are printed on "stderr".

    The callback should not call "nbd_*" APIs on the same
    handle since it can be called while holding the handle
    lock and will cause a deadlock.
'''
        self._check_not_closed()
        return libnbdmod.set_debug_callback(self._o, debug)

    def clear_debug_callback(self):
        u'''▶ clear the debug callback

    Remove the debug callback if one was previously
    associated with the handle (with
    "nbd.set_debug_callback"). If no callback was associated
    this does nothing.
'''
        self._check_not_closed()
        return libnbdmod.clear_debug_callback(self._o)

    def stats_bytes_sent(self):
        u'''▶ statistics of bytes sent over connection so far

    Return the number of bytes that the client has sent to
    the server.

    This tracks the plaintext bytes utilized by the NBD
    protocol; it may differ from the number of bytes
    actually sent over the connection, particularly when TLS
    is in use.
'''
        self._check_not_closed()
        return libnbdmod.stats_bytes_sent(self._o)

    def stats_chunks_sent(self):
        u'''▶ statistics of chunks sent over connection so far

    Return the number of chunks that the client has sent to
    the server, where a chunk is a group of bytes delineated
    by a magic number that cannot be further subdivided
    without breaking the protocol.

    This number does not necessarily relate to the number of
    API calls made, nor to the number of TCP packets sent
    over the connection.
'''
        self._check_not_closed()
        return libnbdmod.stats_chunks_sent(self._o)

    def stats_bytes_received(self):
        u'''▶ statistics of bytes received over connection so far

    Return the number of bytes that the client has received
    from the server.

    This tracks the plaintext bytes utilized by the NBD
    protocol; it may differ from the number of bytes
    actually received over the connection, particularly when
    TLS is in use.
'''
        self._check_not_closed()
        return libnbdmod.stats_bytes_received(self._o)

    def stats_chunks_received(self):
        u'''▶ statistics of chunks received over connection so far

    Return the number of chunks that the client has received
    from the server, where a chunk is a group of bytes
    delineated by a magic number that cannot be further
    subdivided without breaking the protocol.

    This number does not necessarily relate to the number of
    API calls made, nor to the number of TCP packets
    received over the connection.
'''
        self._check_not_closed()
        return libnbdmod.stats_chunks_received(self._o)

    def set_handle_name(self, handle_name):
        u'''▶ set the handle name

    Handles have a name which is unique within the current
    process. The handle name is used in debug output.

    Handle names are normally generated automatically and
    have the form "nbd1", "nbd2", etc., but you can
    optionally use this call to give the handles a name
    which is meaningful for your application to make
    debugging output easier to understand.
'''
        self._check_not_closed()
        return libnbdmod.set_handle_name(self._o, handle_name)

    def get_handle_name(self):
        u'''▶ get the handle name

    Get the name of the handle. If it was previously set by
    calling "nbd.set_handle_name" then this returns the name
    that was set. Otherwise it will return a generic name
    like "nbd1", "nbd2", etc.
'''
        self._check_not_closed()
        return libnbdmod.get_handle_name(self._o)

    def set_private_data(self, private_data):
        u'''▶ set the per-handle private data

    Handles contain a private data field for applications to
    use for any purpose.

    When calling libnbd from C, the type of this field is
    "uintptr_t" so it can be used to store an unsigned
    integer or a pointer.

    In non-C bindings it can be used to store an unsigned
    integer.

    This function sets the value of this field and returns
    the old value (or 0 if it was not previously set).
'''
        self._check_not_closed()
        return libnbdmod.set_private_data(self._o, private_data)

    def get_private_data(self):
        u'''▶ get the per-handle private data

    Return the value of the private data field set
    previously by a call to "nbd.set_private_data" (or 0 if
    it was not previously set).
'''
        self._check_not_closed()
        return libnbdmod.get_private_data(self._o)

    def set_export_name(self, export_name):
        u'''▶ set the export name

    For servers which require an export name or can serve
    different content on different exports, set the
    "export_name" to connect to. The default is the empty
    string "".

    This is only relevant when connecting to servers using
    the newstyle protocol as the oldstyle protocol did not
    support export names. The NBD protocol limits export
    names to 4096 bytes, but servers may not support the
    full length. The encoding of export names is always
    UTF-8.

    When option mode is not in use, the export name must be
    set before beginning a connection. However, when
    "nbd.set_opt_mode" has enabled option mode, it is
    possible to change the export name prior to
    "nbd.opt_go". In particular, the use of "nbd.opt_list"
    during negotiation can be used to determine a name the
    server is likely to accept, and "nbd.opt_info" can be
    used to learn details about an export before connecting.

    This call may be skipped if using "nbd.connect_uri" to
    connect to a URI that includes an export name.
'''
        self._check_not_closed()
        return libnbdmod.set_export_name(self._o, export_name)

    def get_export_name(self):
        u'''▶ get the export name

    Get the export name associated with the handle. This is
    the name that libnbd requests; see
    "nbd.get_canonical_export_name" for determining if the
    server has a different canonical name for the given
    export (most common when requesting the default export
    name of an empty string "")
'''
        self._check_not_closed()
        return libnbdmod.get_export_name(self._o)

    def set_request_block_size(self, request):
        u'''▶ control whether NBD_OPT_GO requests block size

    By default, when connecting to an export, libnbd
    requests that the server report any block size
    restrictions. The NBD protocol states that a server may
    supply block sizes regardless of whether the client
    requests them, and libnbd will report those block sizes
    (see "nbd.get_block_size"); conversely, if a client does
    not request block sizes, the server may reject the
    connection instead of dealing with a client sending
    unaligned requests. This function makes it possible to
    test server behavior by emulating older clients.

    Note that even when block size is requested, the server
    is not obligated to provide any. Furthermore, if block
    sizes are provided (whether or not the client requested
    them), libnbd enforces alignment to those sizes unless
    "nbd.set_strict_mode" is used to bypass client-side
    safety checks.
'''
        self._check_not_closed()
        return libnbdmod.set_request_block_size(self._o, request)

    def get_request_block_size(self):
        u'''▶ see if NBD_OPT_GO requests block size

    Return the state of the block size request flag on this
    handle.
'''
        self._check_not_closed()
        return libnbdmod.get_request_block_size(self._o)

    def set_full_info(self, request):
        u'''▶ control whether NBD_OPT_GO requests extra details

    By default, when connecting to an export, libnbd only
    requests the details it needs to service data
    operations. The NBD protocol says that a server can
    supply optional information, such as a canonical name of
    the export (see "nbd.get_canonical_export_name") or a
    description of the export (see
    "nbd.get_export_description"), but that a hint from the
    client makes it more likely for this extra information
    to be provided. This function controls whether libnbd
    will provide that hint.

    Note that even when full info is requested, the server
    is not obligated to reply with all information that
    libnbd requested. Similarly, libnbd will ignore any
    optional server information that libnbd has not yet been
    taught to recognize. Furthermore, the hint to request
    block sizes is independently controlled via
    "nbd.set_request_block_size".
'''
        self._check_not_closed()
        return libnbdmod.set_full_info(self._o, request)

    def get_full_info(self):
        u'''▶ see if NBD_OPT_GO requests extra details

    Return the state of the full info request flag on this
    handle.
'''
        self._check_not_closed()
        return libnbdmod.get_full_info(self._o)

    def get_canonical_export_name(self):
        u'''▶ return the canonical export name, if the server has one

    The NBD protocol permits a server to report an optional
    canonical export name, which may differ from the
    client's request (as set by "nbd.set_export_name" or
    "nbd.connect_uri"). This function accesses any name
    returned by the server; it may be the same as the client
    request, but is more likely to differ when the client
    requested a connection to the default export name (an
    empty string "").

    Some servers are unlikely to report a canonical name
    unless the client specifically hinted about wanting it,
    via "nbd.set_full_info".
'''
        self._check_not_closed()
        return libnbdmod.get_canonical_export_name(self._o)

    def get_export_description(self):
        u'''▶ return the export description, if the server has one

    The NBD protocol permits a server to report an optional
    export description. This function reports any
    description returned by the server.

    Some servers are unlikely to report a description unless
    the client specifically hinted about wanting it, via
    "nbd.set_full_info". For qemu-nbd(8), a description is
    set with *-D*.
'''
        self._check_not_closed()
        return libnbdmod.get_export_description(self._o)

    def set_tls(self, tls):
        u'''▶ enable or require TLS (authentication and encryption)

    Enable or require TLS (authenticated and encrypted
    connections) to the NBD server. The possible settings
    are:

    "TLS_DISABLE"
        Disable TLS. (The default setting, unless using
        "nbd.connect_uri" with a URI that requires TLS).

        This setting is also necessary if you use
        "nbd.set_opt_mode" and want to interact in plaintext
        with a server that implements the NBD protocol's
        "SELECTIVETLS" mode, prior to enabling TLS with
        "nbd.opt_starttls". Most NBD servers with TLS
        support prefer the NBD protocol's "FORCEDTLS" mode,
        so this sort of manual interaction tends to be
        useful mainly during integration testing.

    "TLS_ALLOW"
        Enable TLS if possible.

        This option is insecure (or best effort) in that in
        some cases it will fall back to an unencrypted
        and/or unauthenticated connection if TLS could not
        be established. Use "TLS_REQUIRE" below if the
        connection must be encrypted.

        Some servers will drop the connection if TLS fails
        so fallback may not be possible.

    "TLS_REQUIRE"
        Require an encrypted and authenticated TLS
        connection. Always fail to connect if the connection
        is not encrypted and authenticated.

    As well as calling this you may also need to supply the
    path to the certificates directory
    ("nbd.set_tls_certificates"), the username
    ("nbd.set_tls_username") and/or the Pre-Shared Keys
    (PSK) file ("nbd.set_tls_psk_file"). For now, when using
    "nbd.connect_uri", any URI query parameters related to
    TLS are not handled automatically. Setting the level
    higher than zero will fail if libnbd was not compiled
    against gnutls; you can test whether this is the case
    with "nbd.supports_tls".
'''
        self._check_not_closed()
        return libnbdmod.set_tls(self._o, tls)

    def get_tls(self):
        u'''▶ get the TLS request setting

    Get the TLS request setting.

    Note: If you want to find out if TLS was actually
    negotiated on a particular connection use
    "nbd.get_tls_negotiated" instead.
'''
        self._check_not_closed()
        return libnbdmod.get_tls(self._o)

    def get_tls_negotiated(self):
        u'''▶ find out if TLS was negotiated on a connection

    After connecting you may call this to find out if the
    connection is using TLS.

    This is normally useful only if you set the TLS request
    mode to "TLS_ALLOW" (see "nbd.set_tls"), because in this
    mode we try to use TLS but fall back to unencrypted if
    it was not available. This function will tell you if TLS
    was negotiated or not.

    In "TLS_REQUIRE" mode (the most secure) the connection
    would have failed if TLS could not be negotiated. With
    "TLS_DISABLE" mode, TLS is not tried automatically; but
    if the NBD server uses the less-common "SELECTIVETLS"
    mode, this function reports whether a manual
    "nbd.opt_starttls" enabled TLS or if the connection is
    still plaintext.
'''
        self._check_not_closed()
        return libnbdmod.get_tls_negotiated(self._o)

    def set_tls_certificates(self, dir):
        u'''▶ set the path to the TLS certificates directory

    Set the path to the TLS certificates directory. If not
    set and TLS is used then a compiled in default is used.
    For root this is "/etc/pki/libnbd/". For non-root this
    is "$HOME/.pki/libnbd" and "$HOME/.config/pki/libnbd".
    If none of these directories can be found then the
    system trusted CAs are used.

    This function may be called regardless of whether TLS is
    supported, but will have no effect unless "nbd.set_tls"
    is also used to request or require TLS.
'''
        self._check_not_closed()
        return libnbdmod.set_tls_certificates(self._o, dir)

    def set_tls_verify_peer(self, verify):
        u'''▶ set whether we verify the identity of the server

    Set this flag to control whether libnbd will verify the
    identity of the server from the server's certificate and
    the certificate authority. This defaults to true when
    connecting to TCP servers using TLS certificate
    authentication, and false otherwise.

    This function may be called regardless of whether TLS is
    supported, but will have no effect unless "nbd.set_tls"
    is also used to request or require TLS.
'''
        self._check_not_closed()
        return libnbdmod.set_tls_verify_peer(self._o, verify)

    def get_tls_verify_peer(self):
        u'''▶ get whether we verify the identity of the server

    Get the verify peer flag.
'''
        self._check_not_closed()
        return libnbdmod.get_tls_verify_peer(self._o)

    def set_tls_username(self, username):
        u'''▶ set the TLS username

    Set the TLS client username. This is used if
    authenticating with PSK over TLS is enabled. If not set
    then the local username is used.

    This function may be called regardless of whether TLS is
    supported, but will have no effect unless "nbd.set_tls"
    is also used to request or require TLS.
'''
        self._check_not_closed()
        return libnbdmod.set_tls_username(self._o, username)

    def get_tls_username(self):
        u'''▶ get the current TLS username

    Get the current TLS username.
'''
        self._check_not_closed()
        return libnbdmod.get_tls_username(self._o)

    def set_tls_hostname(self, hostname):
        u'''▶ set the TLS hostname

    Set the TLS server hostname. This is used in preference
    to the hostname supplied when connecting (eg. through
    "nbd.connect_tcp"), or when there is no explicit
    hostname at all ("nbd.connect_unix"). It can be useful
    when you are connecting to a proxy which is forwarding
    the data to the final server, to specify the name of the
    final server so that the server's certificate can be
    checked.

    This function may be called regardless of whether TLS is
    supported, but will have no effect unless "nbd.set_tls"
    is also used to request or require TLS.
'''
        self._check_not_closed()
        return libnbdmod.set_tls_hostname(self._o, hostname)

    def get_tls_hostname(self):
        u'''▶ get the effective TLS hostname

    Get the TLS server hostname in effect. If not set, this
    returns the empty string (not an error).
'''
        self._check_not_closed()
        return libnbdmod.get_tls_hostname(self._o)

    def set_tls_psk_file(self, filename):
        u'''▶ set the TLS Pre-Shared Keys (PSK) filename

    Set the TLS Pre-Shared Keys (PSK) filename. This is used
    if trying to authenticate to the server using with a
    pre-shared key. There is no default so if this is not
    set then PSK authentication cannot be used to connect to
    the server.

    This function may be called regardless of whether TLS is
    supported, but will have no effect unless "nbd.set_tls"
    is also used to request or require TLS.
'''
        self._check_not_closed()
        return libnbdmod.set_tls_psk_file(self._o, filename)

    def set_request_extended_headers(self, request):
        u'''▶ control use of extended headers

    By default, libnbd tries to negotiate extended headers
    with the server, as this protocol extension permits the
    use of 64-bit zero, trim, and block status actions.
    However, for integration testing, it can be useful to
    clear this flag rather than find a way to alter the
    server to fail the negotiation request.

    For backwards compatibility, the setting of this knob is
    ignored if "nbd.set_request_structured_replies" is also
    set to false, since the use of extended headers implies
    structured replies.
'''
        self._check_not_closed()
        return libnbdmod.set_request_extended_headers(self._o, request)

    def get_request_extended_headers(self):
        u'''▶ see if extended headers are attempted

    Return the state of the request extended headers flag on
    this handle.

    Note: If you want to find out if extended headers were
    actually negotiated on a particular connection use
    "nbd.get_extended_headers_negotiated" instead.
'''
        self._check_not_closed()
        return libnbdmod.get_request_extended_headers(self._o)

    def get_extended_headers_negotiated(self):
        u'''▶ see if extended headers are in use

    After connecting you may call this to find out if the
    connection is using extended headers. Note that this
    setting is sticky; this can return true even after a
    second "nbd.opt_extended_headers" returns false because
    the server detected a duplicate request.

    When extended headers are not in use, commands are
    limited to a 32-bit length, even when the libnbd API
    uses a 64-bit parameter to express the length. But even
    when extended headers are supported, the server may
    enforce other limits, visible through
    "nbd.get_block_size".

    Note that when extended headers are negotiated, you
    should prefer the use of "nbd.block_status_64" instead
    of "nbd.block_status" if any of the meta contexts you
    requested via "nbd.add_meta_context" might return 64-bit
    status values; however, all of the well-known meta
    contexts covered by current "LIBNBD_CONTEXT_*" constants
    only return 32-bit status.
'''
        self._check_not_closed()
        return libnbdmod.get_extended_headers_negotiated(self._o)

    def set_request_structured_replies(self, request):
        u'''▶ control use of structured replies

    By default, libnbd tries to negotiate structured replies
    with the server, as this protocol extension must be in
    use before "nbd.can_meta_context" or "nbd.can_df" can
    return true. However, for integration testing, it can be
    useful to clear this flag rather than find a way to
    alter the server to fail the negotiation request. It is
    also useful to set this to false prior to using
    "nbd.set_opt_mode" if it is desired to control when to
    send "nbd.opt_structured_reply" during negotiation.

    Note that setting this knob to false also disables any
    automatic request for extended headers.
'''
        self._check_not_closed()
        return libnbdmod.set_request_structured_replies(self._o, request)

    def get_request_structured_replies(self):
        u'''▶ see if structured replies are attempted

    Return the state of the request structured replies flag
    on this handle.

    Note: If you want to find out if structured replies were
    actually negotiated on a particular connection use
    "nbd.get_structured_replies_negotiated" instead.
'''
        self._check_not_closed()
        return libnbdmod.get_request_structured_replies(self._o)

    def get_structured_replies_negotiated(self):
        u'''▶ see if structured replies are in use

    After connecting you may call this to find out if the
    connection is using structured replies. Note that this
    setting is sticky; this can return true even after a
    second "nbd.opt_structured_reply" returns false because
    the server detected a duplicate request.

    Note that if the connection negotiates extended headers,
    this function returns true (as extended headers imply
    structured replies) even if no explicit request for
    structured replies was attempted.
'''
        self._check_not_closed()
        return libnbdmod.get_structured_replies_negotiated(self._o)

    def set_request_meta_context(self, request):
        u'''▶ control whether connect automatically requests meta contexts

    This function controls whether the act of connecting to
    an export (all "nbd_connect_*" calls when
    "nbd.set_opt_mode" is false, or "nbd.opt_go" and
    "nbd.opt_info" when option mode is enabled) will also
    try to issue NBD_OPT_SET_META_CONTEXT when the server
    supports structured replies or extended headers and any
    contexts were registered by "nbd.add_meta_context". The
    default setting is true; however the extra step of
    negotiating meta contexts is not always desirable:
    performing both info and go on the same export works
    without needing to re-negotiate contexts on the second
    call; integration testing of other servers may benefit
    from manual invocation of "nbd.opt_set_meta_context" at
    other times in the negotiation sequence; and even when
    using just "nbd.opt_info", it can be faster to collect
    the server's results by relying on the callback function
    passed to "nbd.opt_list_meta_context" than a series of
    post-process calls to "nbd.can_meta_context".

    Note that this control has no effect if the server does
    not negotiate structured replies or extended headers, or
    if the client did not request any contexts via
    "nbd.add_meta_context". Setting this control to false
    may cause "nbd.block_status" to fail.
'''
        self._check_not_closed()
        return libnbdmod.set_request_meta_context(self._o, request)

    def get_request_meta_context(self):
        u'''▶ see if connect automatically requests meta contexts

    Return the state of the automatic meta context request
    flag on this handle.
'''
        self._check_not_closed()
        return libnbdmod.get_request_meta_context(self._o)

    def set_handshake_flags(self, flags):
        u'''▶ control use of handshake flags

    By default, libnbd tries to negotiate all possible
    handshake flags that are also supported by the server,
    since omitting a handshake flag can prevent the use of
    other functionality such as TLS encryption or structured
    replies. However, for integration testing, it can be
    useful to reduce the set of flags supported by the
    client to test that a particular server can handle
    various clients that were compliant to older versions of
    the NBD specification.

    The "flags" argument is a bitmask, including zero or
    more of the following handshake flags:

    "HANDSHAKE_FLAG_FIXED_NEWSTYLE" = 1
        The server gracefully handles unknown option
        requests from the client, rather than disconnecting.
        Without this flag, a client cannot safely request to
        use extensions such as TLS encryption or structured
        replies, as the request may cause an older server to
        drop the connection.

    "HANDSHAKE_FLAG_NO_ZEROES" = 2
        If the client is forced to use "NBD_OPT_EXPORT_NAME"
        instead of the preferred "NBD_OPT_GO", this flag
        allows the server to send fewer all-zero padding
        bytes over the connection.

    For convenience, the constant "HANDSHAKE_FLAG_MASK" is
    available to describe all flags supported by this build
    of libnbd. Future NBD extensions may add further flags,
    which in turn may be enabled by default in newer libnbd.
    As such, when attempting to disable only one specific
    bit, it is wiser to first call "nbd.get_handshake_flags"
    and modify that value, rather than blindly setting a
    constant value.
'''
        self._check_not_closed()
        return libnbdmod.set_handshake_flags(self._o, flags)

    def get_handshake_flags(self):
        u'''▶ see which handshake flags are supported

    Return the state of the handshake flags on this handle.
    When the handle has not yet completed a connection (see
    "nbd.aio_is_created"), this returns the flags that the
    client is willing to use, provided the server also
    advertises those flags. After the connection is ready
    (see "nbd.aio_is_ready"), this returns the flags that
    were actually agreed on between the server and client.
    If the NBD protocol defines new handshake flags, then
    the return value from a newer library version may
    include bits that were undefined at the time of
    compilation.
'''
        self._check_not_closed()
        return libnbdmod.get_handshake_flags(self._o)

    def set_pread_initialize(self, request):
        u'''▶ control whether libnbd pre-initializes read buffers

    By default, libnbd will pre-initialize the contents of a
    buffer passed to calls such as "nbd.pread" to all zeroes
    prior to checking for any other errors, so that even if
    a client application passed in an uninitialized buffer
    but fails to check for errors, it will not result in a
    potential security risk caused by an accidental leak of
    prior heap contents (see CVE-2022-0485 in
    libnbd-security(3) for an example of a security hole in
    an application built against an earlier version of
    libnbd that lacked consistent pre-initialization).
    However, for a client application that has audited that
    an uninitialized buffer is never dereferenced, or which
    performs its own pre-initialization, libnbd's
    sanitization efforts merely pessimize performance
    (although the time spent in pre-initialization may pale
    in comparison to time spent waiting on network packets).

    Calling this function with "request" set to false tells
    libnbd to skip the buffer initialization step in read
    commands.
'''
        self._check_not_closed()
        return libnbdmod.set_pread_initialize(self._o, request)

    def get_pread_initialize(self):
        u'''▶ see whether libnbd pre-initializes read buffers

    Return whether libnbd performs a pre-initialization of a
    buffer passed to "nbd.pread" and similar to all zeroes,
    as set by "nbd.set_pread_initialize".
'''
        self._check_not_closed()
        return libnbdmod.get_pread_initialize(self._o)

    def set_strict_mode(self, flags):
        u'''▶ control how strictly to follow NBD protocol

    By default, libnbd tries to detect requests that would
    trigger undefined behavior in the NBD protocol, and
    rejects them client side without causing any network
    traffic, rather than risking undefined server behavior.
    However, for integration testing, it can be handy to
    relax the strictness of libnbd, to coerce it into
    sending such requests over the network for testing the
    robustness of the server in dealing with such traffic.

    The "flags" argument is a bitmask, including zero or
    more of the following strictness flags:

    "STRICT_COMMANDS" = 0x1
        If set, this flag rejects client requests that do
        not comply with the set of advertised server flags
        (for example, attempting a write on a read-only
        server, or attempting to use "CMD_FLAG_FUA" when
        "nbd.can_fua" returned false). If clear, this flag
        relies on the server to reject unexpected commands.

    "STRICT_FLAGS" = 0x2
        If set, this flag rejects client requests that
        attempt to set a command flag not recognized by
        libnbd (those outside of "CMD_FLAG_MASK"), or a flag
        not normally associated with a command (such as
        using "CMD_FLAG_FUA" on a read command). If clear,
        all flags are sent on to the server, even if sending
        such a flag may cause the server to change its reply
        in a manner that confuses libnbd, perhaps causing
        deadlock or ending the connection.

        Flags that are known by libnbd as associated with a
        given command (such as "CMD_FLAG_DF" for
        "nbd.pread_structured" gated by "nbd.can_df") are
        controlled by "STRICT_COMMANDS" instead; and
        "CMD_FLAG_PAYLOAD_LEN" is managed automatically by
        libnbd unless "STRICT_AUTO_FLAG" is disabled.

        Note that the NBD protocol only supports 16 bits of
        command flags, even though the libnbd API uses
        "uint32_t"; bits outside of the range permitted by
        the protocol are always a client-side error.

    "STRICT_BOUNDS" = 0x4
        If set, this flag rejects client requests that would
        exceed the export bounds without sending any traffic
        to the server. If clear, this flag relies on the
        server to detect out-of-bounds requests.

    "STRICT_ZERO_SIZE" = 0x8
        If set, this flag rejects client requests with
        length 0. If clear, this permits zero-length
        requests to the server, which may produce undefined
        results.

    "STRICT_ALIGN" = 0x10
        If set, and the server provided minimum block sizes
        (see "SIZE_MINIMUM" for "nbd.get_block_size"), this
        flag rejects client requests that do not have length
        and offset aligned to the server's minimum
        requirements. If clear, unaligned requests are sent
        to the server, where it is up to the server whether
        to honor or reject the request.

    "STRICT_PAYLOAD" = 0x20
        If set, the client refuses to send a command to the
        server with more than libnbd's outgoing payload
        maximum (see "SIZE_PAYLOAD" for
        "nbd.get_block_size"), whether or not the server
        advertised a block size maximum. If clear, oversize
        requests up to 64MiB may be attempted, although
        requests larger than 32MiB are liable to cause some
        servers to disconnect.

    "STRICT_AUTO_FLAG" = 0x40
        If set, commands that accept the
        "CMD_FLAG_PAYLOAD_LEN" flag (such as "nbd.pwrite"
        and nbd_block_status_filter(3)) ignore the presence
        or absence of that flag from the caller, instead
        sending the value over the wire that matches the
        server's expectations based on whether extended
        headers were negotiated when the connection was
        made. If clear, the caller takes on the
        responsibility for whether the payload length flag
        is set or clear during the affected command, which
        can be useful during integration testing but is more
        likely to lead to undefined behavior.

    For convenience, the constant "STRICT_MASK" is available
    to describe all strictness flags supported by this build
    of libnbd. Future versions of libnbd may add further
    flags, which are likely to be enabled by default for
    additional client-side filtering. As such, when
    attempting to relax only one specific bit while keeping
    remaining checks at the client side, it is wiser to
    first call "nbd.get_strict_mode" and modify that value,
    rather than blindly setting a constant value.
'''
        self._check_not_closed()
        return libnbdmod.set_strict_mode(self._o, flags)

    def get_strict_mode(self):
        u'''▶ see which strictness flags are in effect

    Return flags indicating which protocol strictness items
    are being enforced locally by libnbd rather than the
    server. The return value from a newer library version
    may include bits that were undefined at the time of
    compilation.
'''
        self._check_not_closed()
        return libnbdmod.get_strict_mode(self._o)

    def set_opt_mode(self, enable):
        u'''▶ control option mode, for pausing during option negotiation

    Set this flag to true in order to request that a
    connection command "nbd_connect_*" will pause for
    negotiation options rather than proceeding all the way
    to the ready state, when communicating with a newstyle
    server. This setting has no effect when connecting to an
    oldstyle server.

    Note that libnbd defaults to attempting
    "NBD_OPT_STARTTLS", "NBD_OPT_EXTENDED_HEADERS", and
    "NBD_OPT_STRUCTURED_REPLY" before letting you control
    remaining negotiation steps; if you need control over
    these steps as well, first set "nbd.set_tls" to
    "TLS_DISABLE", and "nbd.set_request_extended_headers" or
    "nbd.set_request_structured_replies" to false, before
    starting the connection attempt.

    When option mode is enabled, you have fine-grained
    control over which options are negotiated, compared to
    the default of the server negotiating everything on your
    behalf using settings made before starting the
    connection. To leave the mode and proceed on to the
    ready state, you must use "nbd.opt_go" successfully; a
    failed "nbd.opt_go" returns to the negotiating state to
    allow a change of export name before trying again. You
    may also use "nbd.opt_abort" or "nbd.shutdown" to end
    the connection without finishing negotiation.
'''
        self._check_not_closed()
        return libnbdmod.set_opt_mode(self._o, enable)

    def get_opt_mode(self):
        u'''▶ return whether option mode was enabled

    Return true if option negotiation mode was enabled on
    this handle.
'''
        self._check_not_closed()
        return libnbdmod.get_opt_mode(self._o)

    def opt_go(self):
        u'''▶ end negotiation and move on to using an export

    Request that the server finish negotiation and move on
    to serving the export previously specified by the most
    recent "nbd.set_export_name" or "nbd.connect_uri". This
    can only be used if "nbd.set_opt_mode" enabled option
    mode.

    By default, libnbd will automatically request all meta
    contexts registered by "nbd.add_meta_context" as part of
    this call; but this can be suppressed with
    "nbd.set_request_meta_context", particularly if
    "nbd.opt_set_meta_context" was used earlier in the
    negotiation sequence.

    If this fails, the server may still be in negotiation,
    where it is possible to attempt another option such as a
    different export name; although older servers will
    instead have killed the connection.
'''
        self._check_not_closed()
        return libnbdmod.opt_go(self._o)

    def opt_abort(self):
        u'''▶ end negotiation and close the connection

    Request that the server finish negotiation, gracefully
    if possible, then close the connection. This can only be
    used if "nbd.set_opt_mode" enabled option mode.
'''
        self._check_not_closed()
        return libnbdmod.opt_abort(self._o)

    def opt_starttls(self):
        u'''▶ request the server to initiate TLS

    Request that the server initiate a secure TLS
    connection, by sending "NBD_OPT_STARTTLS". This can only
    be used if "nbd.set_opt_mode" enabled option mode;
    furthermore, if you use "nbd.set_tls" to request
    anything other than the default of "TLS_DISABLE", then
    libnbd will have already attempted a TLS connection
    prior to allowing you control over option negotiation.
    This command is disabled if "nbd.supports_tls" reports
    false.

    This function is mainly useful for integration testing
    of corner cases in server handling; in particular,
    misuse of this function when coupled with a server that
    is not careful about resetting stateful commands such as
    "nbd.opt_structured_reply" could result in a security
    hole (see CVE-2021-3716 against nbdkit, for example).
    Thus, when security is a concern, you should instead
    prefer to use "nbd.set_tls" with "TLS_REQUIRE" and let
    libnbd negotiate TLS automatically.

    This function returns true if the server replies with
    success, false if the server replies with an error, and
    fails only if the server does not reply (such as for a
    loss of connection, which can include when the server
    rejects credentials supplied during the TLS handshake).
    Note that the NBD protocol documents that requesting TLS
    after it is already enabled is a client error; most
    servers will gracefully fail a second request, but that
    does not downgrade a TLS session that has already been
    established, as reported by "nbd.get_tls_negotiated".
'''
        self._check_not_closed()
        return libnbdmod.opt_starttls(self._o)

    def opt_extended_headers(self):
        u'''▶ request the server to enable extended headers

    Request that the server use extended headers, by sending
    "NBD_OPT_EXTENDED_HEADERS". This can only be used if
    "nbd.set_opt_mode" enabled option mode; furthermore,
    libnbd defaults to automatically requesting this unless
    you use "nbd.set_request_extended_headers" or
    "nbd.set_request_structured_replies" prior to
    connecting. This function is mainly useful for
    integration testing of corner cases in server handling.

    This function returns true if the server replies with
    success, false if the server replies with an error, and
    fails only if the server does not reply (such as for a
    loss of connection). Note that some servers fail a
    second request as redundant; libnbd assumes that once
    one request has succeeded, then extended headers are
    supported (as visible by
    "nbd.get_extended_headers_negotiated") regardless if
    later calls to this function return false. If this
    function returns true, the use of structured replies is
    implied.
'''
        self._check_not_closed()
        return libnbdmod.opt_extended_headers(self._o)

    def opt_structured_reply(self):
        u'''▶ request the server to enable structured replies

    Request that the server use structured replies, by
    sending "NBD_OPT_STRUCTURED_REPLY". This can only be
    used if "nbd.set_opt_mode" enabled option mode;
    furthermore, libnbd defaults to automatically requesting
    this unless you use "nbd.set_request_structured_replies"
    prior to connecting. This function is mainly useful for
    integration testing of corner cases in server handling.

    This function returns true if the server replies with
    success, false if the server replies with an error, and
    fails only if the server does not reply (such as for a
    loss of connection). Note that some servers fail a
    second request as redundant; libnbd assumes that once
    one request has succeeded, then structured replies are
    supported (as visible by
    "nbd.get_structured_replies_negotiated") regardless if
    later calls to this function return false. Similarly, a
    server may fail this request if extended headers are
    already negotiated, since extended headers take
    priority.
'''
        self._check_not_closed()
        return libnbdmod.opt_structured_reply(self._o)

    def opt_list(self, list):
        u'''▶ request the server to list all exports during negotiation

    Request that the server list all exports that it
    supports. This can only be used if "nbd.set_opt_mode"
    enabled option mode.

    The "list" function is called once per advertised
    export, with any "user_data" passed to this function,
    and with "name" and "description" supplied by the
    server. Many servers omit descriptions, in which case
    "description" will be an empty string. Remember that it
    is not safe to call "nbd.set_export_name" from within
    the context of the callback function; rather, your code
    must copy any "name" needed for later use after this
    function completes. At present, the return value of the
    callback is ignored, although a return of -1 should be
    avoided.

    For convenience, when this function succeeds, it returns
    the number of exports that were advertised by the
    server.

    Not all servers understand this request, and even when
    it is understood, the server might intentionally send an
    empty list to avoid being an information leak, may
    encounter a failure after delivering partial results, or
    may refuse to answer more than one query per connection
    in the interest of avoiding negotiation that does not
    resolve. Thus, this function may succeed even when no
    exports are reported, or may fail but have a non-empty
    list. Likewise, the NBD protocol does not specify an
    upper bound for the number of exports that might be
    advertised, so client code should be aware that a server
    may send a lengthy list.

    For nbd-server(1) you will need to allow clients to make
    list requests by adding "allowlist=true" to the
    "[generic]" section of /etc/nbd-server/config. For
    qemu-nbd(8), a description is set with *-D*.
'''
        self._check_not_closed()
        return libnbdmod.opt_list(self._o, list)

    def opt_info(self):
        u'''▶ request the server for information about an export

    Request that the server supply information about the
    export name previously specified by the most recent
    "nbd.set_export_name" or "nbd.connect_uri". This can
    only be used if "nbd.set_opt_mode" enabled option mode.

    If successful, functions like "nbd.is_read_only" and
    "nbd.get_size" will report details about that export. If
    "nbd.set_request_meta_context" is set (the default) and
    structured replies or extended headers were negotiated,
    it is also valid to use "nbd.can_meta_context" after
    this call. However, it may be more efficient to clear
    that setting and manually utilize
    "nbd.opt_list_meta_context" with its callback approach,
    for learning which contexts an export supports. In
    general, if "nbd.opt_go" is called next, that call will
    likely succeed with the details remaining the same,
    although this is not guaranteed by all servers.

    Not all servers understand this request, and even when
    it is understood, the server might fail the request even
    when a corresponding "nbd.opt_go" would succeed.
'''
        self._check_not_closed()
        return libnbdmod.opt_info(self._o)

    def opt_list_meta_context(self, context):
        u'''▶ list available meta contexts, using implicit query list

    Request that the server list available meta contexts
    associated with the export previously specified by the
    most recent "nbd.set_export_name" or "nbd.connect_uri",
    and with a list of queries from prior calls to
    "nbd.add_meta_context" (see
    "nbd.opt_list_meta_context_queries" if you want to
    supply an explicit query list instead). This can only be
    used if "nbd.set_opt_mode" enabled option mode.

    The NBD protocol allows a client to decide how many
    queries to ask the server. Rather than taking that list
    of queries as a parameter to this function, libnbd
    reuses the current list of requested meta contexts as
    set by "nbd.add_meta_context"; you can use
    "nbd.clear_meta_contexts" to set up a different list of
    queries. When the list is empty, a server will typically
    reply with all contexts that it supports; when the list
    is non-empty, the server will reply only with supported
    contexts that match the client's request. Note that a
    reply by the server might be encoded to represent
    several feasible contexts within one string, rather than
    multiple strings per actual context name that would
    actually succeed during "nbd.opt_go"; so it is still
    necessary to use "nbd.can_meta_context" after connecting
    to see which contexts are actually supported.

    The "context" function is called once per server reply,
    with any "user_data" passed to this function, and with
    "name" supplied by the server. Remember that it is not
    safe to call "nbd.add_meta_context" from within the
    context of the callback function; rather, your code must
    copy any "name" needed for later use after this function
    completes. At present, the return value of the callback
    is ignored, although a return of -1 should be avoided.

    For convenience, when this function succeeds, it returns
    the number of replies returned by the server.

    Not all servers understand this request, and even when
    it is understood, the server might intentionally send an
    empty list because it does not support the requested
    context, or may encounter a failure after delivering
    partial results. Thus, this function may succeed even
    when no contexts are reported, or may fail but have a
    non-empty list. Likewise, the NBD protocol does not
    specify an upper bound for the number of replies that
    might be advertised, so client code should be aware that
    a server may send a lengthy list.
'''
        self._check_not_closed()
        return libnbdmod.opt_list_meta_context(self._o, context)

    def opt_list_meta_context_queries(self, queries, context):
        u'''▶ list available meta contexts, using explicit query list

    Request that the server list available meta contexts
    associated with the export previously specified by the
    most recent "nbd.set_export_name" or "nbd.connect_uri",
    and with an explicit list of queries provided as a
    parameter (see "nbd.opt_list_meta_context" if you want
    to reuse an implicit query list instead). This can only
    be used if "nbd.set_opt_mode" enabled option mode.

    The NBD protocol allows a client to decide how many
    queries to ask the server. For this function, the list
    is explicit in the "queries" parameter. When the list is
    empty, a server will typically reply with all contexts
    that it supports; when the list is non-empty, the server
    will reply only with supported contexts that match the
    client's request. Note that a reply by the server might
    be encoded to represent several feasible contexts within
    one string, rather than multiple strings per actual
    context name that would actually succeed during
    "nbd.opt_go"; so it is still necessary to use
    "nbd.can_meta_context" after connecting to see which
    contexts are actually supported.

    The "context" function is called once per server reply,
    with any "user_data" passed to this function, and with
    "name" supplied by the server. Remember that it is not
    safe to call "nbd.add_meta_context" from within the
    context of the callback function; rather, your code must
    copy any "name" needed for later use after this function
    completes. At present, the return value of the callback
    is ignored, although a return of -1 should be avoided.

    For convenience, when this function succeeds, it returns
    the number of replies returned by the server.

    Not all servers understand this request, and even when
    it is understood, the server might intentionally send an
    empty list because it does not support the requested
    context, or may encounter a failure after delivering
    partial results. Thus, this function may succeed even
    when no contexts are reported, or may fail but have a
    non-empty list. Likewise, the NBD protocol does not
    specify an upper bound for the number of replies that
    might be advertised, so client code should be aware that
    a server may send a lengthy list.
'''
        self._check_not_closed()
        return libnbdmod.opt_list_meta_context_queries(self._o, queries,
                                                       context)

    def opt_set_meta_context(self, context):
        u'''▶ select specific meta contexts, using implicit query list

    Request that the server supply all recognized meta
    contexts registered through prior calls to
    "nbd.add_meta_context", in conjunction with the export
    previously specified by the most recent
    "nbd.set_export_name" or "nbd.connect_uri". This can
    only be used if "nbd.set_opt_mode" enabled option mode.
    Normally, this function is redundant, as "nbd.opt_go"
    automatically does the same task if structured replies
    or extended headers have already been negotiated. But
    manual control over meta context requests can be useful
    for fine-grained testing of how a server handles unusual
    negotiation sequences. Often, use of this function is
    coupled with "nbd.set_request_meta_context" to bypass
    the automatic context request normally performed by
    "nbd.opt_go".

    The NBD protocol allows a client to decide how many
    queries to ask the server. Rather than taking that list
    of queries as a parameter to this function, libnbd
    reuses the current list of requested meta contexts as
    set by "nbd.add_meta_context"; you can use
    "nbd.clear_meta_contexts" to set up a different list of
    queries (see "nbd.opt_set_meta_context_queries" to pass
    an explicit list of contexts instead). Since this
    function is primarily designed for testing servers,
    libnbd does not prevent the use of this function on an
    empty list or when "nbd.set_request_structured_replies"
    has disabled structured replies, in order to see how a
    server behaves.

    The "context" function is called once per server reply,
    with any "user_data" passed to this function, and with
    "name" supplied by the server. Additionally, each server
    name will remain visible through "nbd.can_meta_context"
    until the next attempt at "nbd.set_export_name" or
    "nbd.opt_set_meta_context", as well as "nbd.opt_go" or
    "nbd.opt_info" that trigger an automatic meta context
    request. Remember that it is not safe to call any
    "nbd_*" APIs from within the context of the callback
    function. At present, the return value of the callback
    is ignored, although a return of -1 should be avoided.

    For convenience, when this function succeeds, it returns
    the number of replies returned by the server.

    Not all servers understand this request, and even when
    it is understood, the server might intentionally send an
    empty list because it does not support the requested
    context, or may encounter a failure after delivering
    partial results. Thus, this function may succeed even
    when no contexts are reported, or may fail but have a
    non-empty list.
'''
        self._check_not_closed()
        return libnbdmod.opt_set_meta_context(self._o, context)

    def opt_set_meta_context_queries(self, queries, context):
        u'''▶ select specific meta contexts, using explicit query list

    Request that the server supply all recognized meta
    contexts passed in through "queries", in conjunction
    with the export previously specified by the most recent
    "nbd.set_export_name" or "nbd.connect_uri". This can
    only be used if "nbd.set_opt_mode" enabled option mode.
    Normally, this function is redundant, as "nbd.opt_go"
    automatically does the same task if structured replies
    or extended headers have already been negotiated. But
    manual control over meta context requests can be useful
    for fine-grained testing of how a server handles unusual
    negotiation sequences. Often, use of this function is
    coupled with "nbd.set_request_meta_context" to bypass
    the automatic context request normally performed by
    "nbd.opt_go".

    The NBD protocol allows a client to decide how many
    queries to ask the server. This function takes an
    explicit list of queries; to instead reuse an implicit
    list, see "nbd.opt_set_meta_context". Since this
    function is primarily designed for testing servers,
    libnbd does not prevent the use of this function on an
    empty list or when "nbd.set_request_structured_replies"
    has disabled structured replies, in order to see how a
    server behaves.

    The "context" function is called once per server reply,
    with any "user_data" passed to this function, and with
    "name" supplied by the server. Additionally, each server
    name will remain visible through "nbd.can_meta_context"
    until the next attempt at "nbd.set_export_name" or
    "nbd.opt_set_meta_context", as well as "nbd.opt_go" or
    "nbd.opt_info" that trigger an automatic meta context
    request. Remember that it is not safe to call any
    "nbd_*" APIs from within the context of the callback
    function. At present, the return value of the callback
    is ignored, although a return of -1 should be avoided.

    For convenience, when this function succeeds, it returns
    the number of replies returned by the server.

    Not all servers understand this request, and even when
    it is understood, the server might intentionally send an
    empty list because it does not support the requested
    context, or may encounter a failure after delivering
    partial results. Thus, this function may succeed even
    when no contexts are reported, or may fail but have a
    non-empty list.
'''
        self._check_not_closed()
        return libnbdmod.opt_set_meta_context_queries(self._o, queries,
                                                      context)

    def add_meta_context(self, name):
        u'''▶ ask server to negotiate metadata context

    During connection libnbd can negotiate zero or more
    metadata contexts with the server. Metadata contexts are
    features (such as "base:allocation") which describe
    information returned by the "nbd.block_status_64"
    command (for "base:allocation" this is whether blocks of
    data are allocated, zero or sparse).

    This call adds one metadata context to the list to be
    negotiated. You can call it as many times as needed. The
    list is initially empty when the handle is created; you
    can check the contents of the list with
    "nbd.get_nr_meta_contexts" and "nbd.get_meta_context",
    or clear it with "nbd.clear_meta_contexts".

    The NBD protocol limits meta context names to 4096
    bytes, but servers may not support the full length. The
    encoding of meta context names is always UTF-8.

    Not all servers support all metadata contexts. To learn
    if a context was actually negotiated, call
    "nbd.can_meta_context" after connecting.

    The single parameter is the name of the metadata
    context, for example "CONTEXT_BASE_ALLOCATION".
    <libnbd.h> includes defined constants beginning with
    "CONTEXT_" for some well-known contexts, but you are
    free to pass in other contexts.

    Other metadata contexts are server-specific, but include
    "qemu:dirty-bitmap:..." and "qemu:allocation-depth" for
    qemu-nbd (see qemu-nbd *-B* and *-A* options).
'''
        self._check_not_closed()
        return libnbdmod.add_meta_context(self._o, name)

    def get_nr_meta_contexts(self):
        u'''▶ return the current number of requested meta contexts

    During connection libnbd can negotiate zero or more
    metadata contexts with the server. Metadata contexts are
    features (such as "base:allocation") which describe
    information returned by the "nbd.block_status_64"
    command (for "base:allocation" this is whether blocks of
    data are allocated, zero or sparse).

    This command returns how many meta contexts have been
    added to the list to request from the server via
    "nbd.add_meta_context". The server is not obligated to
    honor all of the requests; to see what it actually
    supports, see "nbd.can_meta_context".
'''
        self._check_not_closed()
        return libnbdmod.get_nr_meta_contexts(self._o)

    def get_meta_context(self, i):
        u'''▶ return the i'th meta context request

    During connection libnbd can negotiate zero or more
    metadata contexts with the server. Metadata contexts are
    features (such as "base:allocation") which describe
    information returned by the "nbd.block_status_64"
    command (for "base:allocation" this is whether blocks of
    data are allocated, zero or sparse).

    This command returns the i'th meta context request, as
    added by "nbd.add_meta_context", and bounded by
    "nbd.get_nr_meta_contexts".
'''
        self._check_not_closed()
        return libnbdmod.get_meta_context(self._o, i)

    def clear_meta_contexts(self):
        u'''▶ reset the list of requested meta contexts

    During connection libnbd can negotiate zero or more
    metadata contexts with the server. Metadata contexts are
    features (such as "base:allocation") which describe
    information returned by the "nbd.block_status_64"
    command (for "base:allocation" this is whether blocks of
    data are allocated, zero or sparse).

    This command resets the list of meta contexts to request
    back to an empty list, for re-population by further use
    of "nbd.add_meta_context". It is primarily useful when
    option negotiation mode is selected (see
    "nbd.set_opt_mode"), for altering the list of attempted
    contexts between subsequent export queries.
'''
        self._check_not_closed()
        return libnbdmod.clear_meta_contexts(self._o)

    def set_uri_allow_transports(self, mask):
        u'''▶ set the allowed transports in NBD URIs

    Set which transports are allowed to appear in NBD URIs.
    The default is to allow any transport.

    The "mask" parameter may contain any of the following
    flags ORed together:

    "ALLOW_TRANSPORT_TCP" = 0x1
    "ALLOW_TRANSPORT_UNIX" = 0x2
    "ALLOW_TRANSPORT_VSOCK" = 0x4
    "ALLOW_TRANSPORT_SSH" = 0x8

    For convenience, the constant "ALLOW_TRANSPORT_MASK" is
    available to describe all transports recognized by this
    build of libnbd. A future version of the library may add
    new flags.
'''
        self._check_not_closed()
        return libnbdmod.set_uri_allow_transports(self._o, mask)

    def set_uri_allow_tls(self, tls):
        u'''▶ set the allowed TLS settings in NBD URIs

    Set which TLS settings are allowed to appear in NBD
    URIs. The default is to allow either non-TLS or TLS
    URIs.

    The "tls" parameter can be:

    "TLS_DISABLE"
        TLS URIs are not permitted, ie. a URI such as
        "nbds://..." will be rejected.

    "TLS_ALLOW"
        This is the default. TLS may be used or not,
        depending on whether the URI uses "nbds" or "nbd".

    "TLS_REQUIRE"
        TLS URIs are required. All URIs must use "nbds".
'''
        self._check_not_closed()
        return libnbdmod.set_uri_allow_tls(self._o, tls)

    def set_uri_allow_local_file(self, allow):
        u'''▶ set the allowed transports in NBD URIs

    Allow NBD URIs to reference local files. This is
    *disabled* by default.

    Currently this setting only controls whether the
    "tls-psk-file" parameter in NBD URIs is allowed.
'''
        self._check_not_closed()
        return libnbdmod.set_uri_allow_local_file(self._o, allow)

    def connect_uri(self, uri):
        u'''▶ connect to NBD URI

    Connect (synchronously) to an NBD server and export by
    specifying the NBD URI. NBD URIs are a standard way to
    specify a network block device endpoint, using a syntax
    like "nbd://example.com" which is convenient, well
    defined and future proof.

    This call works by parsing the URI parameter and calling
    "nbd.set_export_name" and "nbd.set_tls" and other calls
    as needed, followed by "nbd.connect_tcp",
    "nbd.connect_unix" or "nbd.connect_vsock".

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.

  Example URIs supported
    "nbd://example.com"
        Connect over TCP, unencrypted, to "example.com" port
        10809.

    "nbds://example.com"
        Connect over TCP with TLS, to "example.com" port
        10809. If the server does not support TLS then this
        will fail.

    "nbd+unix:///foo?socket=/tmp/nbd.sock"
        Connect over the Unix domain socket /tmp/nbd.sock to
        an NBD server running locally. The export name is
        set to "foo" (note without any leading "/"
        character).

    "nbds+unix://alice@/?socket=/tmp/nbd.sock&tls-certificat
    es=certs"
        Connect over a Unix domain socket, enabling TLS and
        setting the path to a directory containing
        certificates and keys.

    "nbd+vsock:///"
        In this scenario libnbd is running in a virtual
        machine. Connect over "AF_VSOCK" to an NBD server
        running on the hypervisor.

    "nbd+ssh://server/"
        Connect to remote "server" using Secure Shell, and
        tunnel NBD to an NBD server listening on port 10809.

  Supported URI formats
    The following schemes are supported in the current
    version of libnbd:

    "nbd:"
        Connect over TCP without using TLS.

    "nbds:"
        Connect over TCP. TLS is required and the connection
        will fail if the server does not support TLS.

    "nbd+unix:"
    "nbds+unix:"
        Connect over a Unix domain socket, without or with
        TLS respectively. The "socket" parameter is
        required.

    "nbd+vsock:"
    "nbds+vsock:"
        Connect over the "AF_VSOCK" transport, without or
        with TLS respectively. You can use
        "nbd.supports_vsock" to see if this build of libnbd
        supports "AF_VSOCK".

    "nbd+ssh:"
    "nbds+ssh:"
        *Experimental*

        Tunnel NBD over a Secure Shell connection. This
        requires that ssh(1) is installed locally, and that
        nc(1) (from the nmap project) is installed on the
        remote server.

    The authority part of the URI
    ("[username@][servername][:port]") is parsed depending
    on the transport. For TCP it specifies the server to
    connect to and optional port number. For "+unix" it
    should not be present. For "+vsock" the server name is
    the numeric CID (eg. 2 to connect to the host), and the
    optional port number may be present. For "+ssh" the
    Secure Shell server and optional port. If the "username"
    is present it is used for TLS authentication.

    For all transports, an export name may be present,
    parsed in accordance with the NBD URI specification.

    Finally the query part of the URI can contain:

    socket=SOCKET
        Specifies the Unix domain socket to connect on. Must
        be present for the "+unix" transport, optional for
        "+ssh", and must not be present for the other
        transports.

    tls-certificates=DIR
        Set the certificates directory. See
        "nbd.set_tls_certificates". Note this is not allowed
        by default - see next section.

    tls-psk-file=PSKFILE
        Set the PSK file. See "nbd.set_tls_psk_file". Note
        this is not allowed by default - see next section.

    tls-hostname="SERVER"
        Set the TLS hostname. See "nbd.set_tls_hostname".

    tls-verify-peer=false
        Do not verify the server certificate. See
        "nbd.set_tls_verify_peer". The default is "true".

  Disable URI features
    For security reasons you might want to disable certain
    URI features. Pre-filtering URIs is error-prone and
    should not be attempted. Instead use the libnbd APIs
    below to control what can appear in URIs. Note you must
    call these functions on the same handle before calling
    "nbd.connect_uri" or "nbd.aio_connect_uri".

    TCP, Unix domain socket, "AF_VSOCK" or SSH transports
        Default: all allowed

        To select which transports are allowed call
        "nbd.set_uri_allow_transports".

    TLS Default: both non-TLS and TLS connections allowed

        To force TLS off or on in URIs call
        "nbd.set_uri_allow_tls".

    Connect to Unix domain socket in the local filesystem
        Default: allowed

        To prevent this you must disable the "+unix"
        transport using "nbd.set_uri_allow_transports".

    Read from local files
        Default: denied

        To allow URIs to contain references to local files
        (eg. for parameters like "tls-psk-file") call
        "nbd.set_uri_allow_local_file".

  Overriding the export name
    It is possible to override the export name portion of a
    URI by using "nbd.set_opt_mode" to enable option mode,
    then using "nbd.set_export_name" and "nbd.opt_go" as
    part of subsequent negotiation.

  Optional features
    This call will fail if libnbd was not compiled with
    libxml2; you can test whether this is the case with
    "nbd.supports_uri".

    Support for URIs that require TLS will fail if libnbd
    was not compiled with gnutls; you can test whether this
    is the case with "nbd.supports_tls".

  Constructing a URI from an existing connection
    See "nbd.get_uri".

  See if a string is an NBD URI
    See "nbd.is_uri".

  Differences from qemu and glib parsing of NBD URIs
    qemu(1) also supports NBD URIs and has a separate URI
    parser. In qemu ≤ 9.0 this was done using their own
    parser. In qemu ≥ 9.1 this is done using glib "g_uri"
    functions. The current (glib-based) parser does not
    parse the export name part of the URI in exactly the
    same way as libnbd, which may cause URIs to work in
    libnbd but not in qemu or *vice versa*. Only URIs using
    exportnames should be affected. For details see
    <https://gitlab.com/qemu-project/qemu/-/issues/2584>.

  Limitations on vsock port numbers
    The vsock(7) protocol allows 32 bit unsigned ports,
    reserving ports 0, 1 and 2 for special purposes. In
    Linux, ports < 1024 are reserved for privileged
    processes.

    libxml2 (used to parse the URI) imposes additional
    restrictions. libxml2 < 2.9 limited port numbers to
    99,999,999. libxml2 ≥ 2.9 limits port numbers to ≤
    0x7fff_ffff (31 bits).
'''
        self._check_not_closed()
        return libnbdmod.connect_uri(self._o, uri)

    def connect_unix(self, unixsocket):
        u'''▶ connect to NBD server over a Unix domain socket

    Connect (synchronously) over the named Unix domain
    socket ("unixsocket") to an NBD server running on the
    same machine.

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.
'''
        self._check_not_closed()
        return libnbdmod.connect_unix(self._o, unixsocket)

    def connect_vsock(self, cid, port):
        u'''▶ connect to NBD server over AF_VSOCK protocol

    Connect (synchronously) over the "AF_VSOCK" protocol
    from a virtual machine to an NBD server, usually running
    on the host. The "cid" and "port" parameters specify the
    server address. Usually "cid" should be 2 (to connect to
    the host), and "port" might be 10809 or another port
    number assigned to you by the host administrator.

    Not all systems support "AF_VSOCK"; to determine if
    libnbd was built on a system with vsock support, see
    "nbd.supports_vsock".

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.
'''
        self._check_not_closed()
        return libnbdmod.connect_vsock(self._o, cid, port)

    def connect_tcp(self, hostname, port):
        u'''▶ connect to NBD server over a TCP port

    Connect (synchronously) to the NBD server listening on
    "hostname:port". The "port" may be a port name such as
    "nbd", or it may be a port number as a string such as
    "10809".

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.
'''
        self._check_not_closed()
        return libnbdmod.connect_tcp(self._o, hostname, port)

    def connect_socket(self, sock):
        u'''▶ connect directly to a connected socket

    Pass a connected socket "sock" through which libnbd will
    talk to the NBD server.

    The caller is responsible for creating and connecting
    this socket by some method, before passing it to libnbd.

    If this call returns without error then socket ownership
    is passed to libnbd. Libnbd will close the socket when
    the handle is closed. The caller must not use the socket
    in any way.

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.
'''
        self._check_not_closed()
        return libnbdmod.connect_socket(self._o, sock)

    def connect_command(self, argv):
        u'''▶ connect to NBD server command

    Run the command as a subprocess and connect to it over
    stdin/stdout. This is for use with NBD servers which can
    behave like inetd clients, such as nbdkit(1) using the
    *-s*/*--single* flag, and nbd-server(1) with port number
    set to 0.

    To run qemu-nbd(1), use
    "nbd.connect_systemd_socket_activation" instead.

  Subprocess
    Libnbd will fork the "argv" command and pass the NBD
    socket to it using file descriptors 0 and 1
    (stdin/stdout):

     ┌─────────┬─────────┐    ┌────────────────┐
     │ program │ libnbd  │    │   NBD server   │
     │         │         │    │       (argv)   │
     │         │ socket ╍╍╍╍╍╍╍╍▶ stdin/stdout │
     └─────────┴─────────┘    └────────────────┘

    When the NBD handle is closed the server subprocess is
    killed.

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.
'''
        self._check_not_closed()
        return libnbdmod.connect_command(self._o, argv)

    def connect_systemd_socket_activation(self, argv):
        u'''▶ connect using systemd socket activation

    Run the command as a subprocess and connect to it using
    systemd socket activation.

    This is especially useful for running qemu-nbd(1) as a
    subprocess of libnbd, for example to use it to open
    qcow2 files.

    To run nbdkit as a subprocess, this function can be
    used, or "nbd.connect_command".

    To run nbd-server(1) as a subprocess, this function
    cannot be used, you must use "nbd.connect_command".

  Socket activation
    Libnbd will fork the "argv" command and pass an NBD
    socket to it using special "LISTEN_*" environment
    variables (as defined by the systemd socket activation
    protocol).

     ┌─────────┬─────────┐    ┌───────────────┐
     │ program │ libnbd  │    │  qemu-nbd or  │
     │         │         │    │  other server │
     │         │ socket ╍╍╍╍╍╍╍╍▶             │
     └─────────┴─────────┘    └───────────────┘

    When the NBD handle is closed the server subprocess is
    killed.

   Socket name
    The socket activation protocol lets you optionally give
    the socket a name. If used, the name is passed to the
    NBD server using the "LISTEN_FDNAMES" environment
    variable. To provide a socket name, call
    "nbd.set_socket_activation_name" before calling the
    connect function.

    This call returns when the connection has been made. By
    default, this proceeds all the way to transmission
    phase, but "nbd.set_opt_mode" can be used for manual
    control over option negotiation performed before
    transmission phase.
'''
        self._check_not_closed()
        return libnbdmod.connect_systemd_socket_activation(self._o, argv)

    def set_socket_activation_name(self, socket_name):
        u'''▶ set the socket activation name

    When running an NBD server using
    "nbd.connect_systemd_socket_activation" you can
    optionally name the socket. Call this function before
    connecting to the server.

    Some servers such as qemu-storage-daemon(1) can use this
    information to associate the socket with a name used on
    the command line, but most servers will ignore it. The
    name is passed through the "LISTEN_FDNAMES" environment
    variable.

    The parameter "socket_name" can be a short alphanumeric
    string. If it is set to the empty string (also the
    default when the handle is created) then the name
    "unknown" will be seen by the server.
'''
        self._check_not_closed()
        return libnbdmod.set_socket_activation_name(self._o, socket_name)

    def get_socket_activation_name(self):
        u'''▶ get the socket activation name

    Return the socket name used when you call
    "nbd.connect_systemd_socket_activation" on the same
    handle. By default this will return the empty string
    meaning that the server will see the name "unknown".
'''
        self._check_not_closed()
        return libnbdmod.get_socket_activation_name(self._o)

    def is_read_only(self):
        u'''▶ is the NBD export read-only?

    Returns true if the NBD export is read-only; writes and
    write-like operations will fail.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.is_read_only(self._o)

    def can_flush(self):
        u'''▶ does the server support the flush command?

    Returns true if the server supports the flush command
    (see "nbd.flush", "nbd.aio_flush"). Returns false if the
    server does not.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_flush(self._o)

    def can_fua(self):
        u'''▶ does the server support the FUA flag?

    Returns true if the server supports the FUA flag on
    certain commands (see "nbd.pwrite").

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_fua(self._o)

    def is_rotational(self):
        u'''▶ is the NBD disk rotational (like a disk)?

    Returns true if the disk exposed over NBD is rotational
    (like a traditional floppy or hard disk). Returns false
    if the disk has no penalty for random access (like an
    SSD or RAM disk).

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.is_rotational(self._o)

    def can_trim(self):
        u'''▶ does the server support the trim command?

    Returns true if the server supports the trim command
    (see "nbd.trim", "nbd.aio_trim"). Returns false if the
    server does not.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_trim(self._o)

    def can_zero(self):
        u'''▶ does the server support the zero command?

    Returns true if the server supports the zero command
    (see "nbd.zero", "nbd.aio_zero"). Returns false if the
    server does not.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_zero(self._o)

    def can_fast_zero(self):
        u'''▶ does the server support the fast zero flag?

    Returns true if the server supports the use of the
    "CMD_FLAG_FAST_ZERO" flag to the zero command (see
    "nbd.zero", "nbd.aio_zero"). Returns false if the server
    does not.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_fast_zero(self._o)

    def can_block_status_payload(self):
        u'''▶ does the server support the block status payload flag?

    Returns true if the server supports the use of the
    "CMD_FLAG_PAYLOAD_LEN" flag to allow filtering of the
    block status command (see "nbd.block_status_filter").
    Returns false if the server does not. Note that this
    will never return true if
    "nbd.get_extended_headers_negotiated" is false.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_block_status_payload(self._o)

    def can_df(self):
        u'''▶ does the server support the don't fragment flag to pread?

    Returns true if the server supports structured reads
    with an ability to request a non-fragmented read (see
    "nbd.pread_structured", "nbd.aio_pread_structured").
    Returns false if the server either lacks structured
    reads or if it does not support a non-fragmented read
    request.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_df(self._o)

    def can_multi_conn(self):
        u'''▶ does the server support multi-conn?

    Returns true if the server supports multi-conn. Returns
    false if the server does not.

    It is not safe to open multiple handles connecting to
    the same server if you will write to the server and the
    server does not advertise multi-conn support. The safe
    way to check for this is to open one connection, check
    this flag is true, then open further connections as
    required.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_multi_conn(self._o)

    def can_cache(self):
        u'''▶ does the server support the cache command?

    Returns true if the server supports the cache command
    (see "nbd.cache", "nbd.aio_cache"). Returns false if the
    server does not.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_cache(self._o)

    def can_meta_context(self, metacontext):
        u'''▶ does the server support a specific meta context?

    Returns true if the server supports the given meta
    context (see "nbd.add_meta_context"). Returns false if
    the server does not. It is possible for this command to
    fail if meta contexts were requested but there is a
    missing or failed attempt at NBD_OPT_SET_META_CONTEXT
    during option negotiation.

    If the server supports block status filtering (see
    "nbd.can_block_status_payload", this function must
    return true for any filter name passed to
    "nbd.block_status_filter".

    The single parameter is the name of the metadata
    context, for example "CONTEXT_BASE_ALLOCATION".
    <libnbd.h> includes defined constants for well-known
    namespace contexts beginning with "CONTEXT_", but you
    are free to pass in other contexts.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.can_meta_context(self._o, metacontext)

    def get_protocol(self):
        u'''▶ return the NBD protocol variant

    Return the NBD protocol variant in use on the
    connection. At the moment this returns one of the
    strings "oldstyle", "newstyle" or "newstyle-fixed".
    Other strings might be returned in the future. Most
    modern NBD servers use "newstyle-fixed".

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.get_protocol(self._o)

    def get_size(self):
        u'''▶ return the export size

    Returns the size in bytes of the NBD export.

    Note that this call fails with "EOVERFLOW" for an
    unlikely server that advertises a size which cannot fit
    in a 64-bit signed integer.

    nbdinfo(1) *--size* option is a way to access this API
    from shell scripts.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.get_size(self._o)

    def get_block_size(self, size_type):
        u'''▶ return a specific server block size constraint

    Returns a specific block size constraint advertised by
    the server. If zero is returned it means the server did
    not advertise a constraint.

    Constraints are hints. Servers differ in their behaviour
    as to whether they enforce constraints or not.

    The "size_type" parameter selects which constraint to
    read. It can be one of:

    "SIZE_MINIMUM" = 0
        If non-zero, this will be a power of 2 between 1 and
        64k; any client request that is not aligned in
        length or offset to this size is likely to fail with
        "EINVAL". The image size will generally also be a
        multiple of this value (if not, the final few bytes
        are inaccessible while obeying alignment
        constraints).

        If zero (meaning no information was returned by the
        server), it is safest to assume a minimum block size
        of 512, although many servers support a minimum
        block size of 1.

        If the server provides a constraint, then libnbd
        defaults to honoring that constraint client-side
        unless "STRICT_ALIGN" is cleared in
        nbd_set_strict_mode(3).

    "SIZE_PREFERRED" = 1
        If non-zero, this is a power of 2 representing the
        preferred size for efficient I/O. Smaller requests
        may incur overhead such as read-modify-write cycles
        that will not be present when using I/O that is a
        multiple of this value. This value may be larger
        than the size of the export.

        If zero (meaning no information was returned by the
        server), using 4k as a preferred block size tends to
        give decent performance.

    "SIZE_MAXIMUM" = 2
        If non-zero, this represents the maximum length that
        the server is willing to handle during "nbd.pread"
        or "nbd.pwrite". Other functions like "nbd.zero" may
        still be able to use larger sizes. Note that this
        function returns what the server advertised, but
        libnbd itself imposes a maximum of 64M.

        If zero (meaning no information was returned by the
        server), some NBD servers will abruptly disconnect
        if a transaction sends or receives more than 32M of
        data.

    "SIZE_PAYLOAD" = 3
        This value is not advertised by the server, but
        rather represents the maximum outgoing payload size
        for a given connection that libnbd will enforce
        unless "STRICT_PAYLOAD" is cleared in
        nbd_set_strict_mode(3). It is always non-zero: never
        smaller than 1M, never larger than 64M, and matches
        "SIZE_MAXIMUM" when possible.

    Future NBD extensions may result in additional
    "size_type" values. Note that by default, libnbd
    requests all available block sizes, but that a server
    may differ in what sizes it chooses to report if
    "nbd.set_request_block_size" alters whether the client
    requests sizes.

    This call does not block, because it returns data that
    is saved in the handle from the NBD protocol handshake.
'''
        self._check_not_closed()
        return libnbdmod.get_block_size(self._o, size_type)

    def pread(self, count, offset, flags=0):
        u'''▶ read from the NBD server

    Issue a read command to the NBD server for the range
    starting at "offset" and ending at "offset" + "count" -
    1. NBD can only read all or nothing using this call. The
    call returns when the data has been read fully into
    "buf" or there is an error. See also
    "nbd.pread_structured", if finer visibility is required
    into the server's replies, or if you want to use
    "CMD_FLAG_DF".

    Note that libnbd currently enforces a maximum read
    buffer of 64MiB, even if the server would permit a
    larger buffer in a single transaction; attempts to
    exceed this will result in an "ERANGE" error. The server
    may enforce a smaller limit, which can be learned with
    "nbd.get_block_size".

    The "flags" parameter must be 0 for now (it exists for
    future NBD protocol extensions).

    Note that if this command fails, and
    "nbd.get_pread_initialize" returns true, then libnbd
    sanitized "buf", but it is unspecified whether the
    contents of "buf" will read as zero or as partial
    results from the server. If "nbd.get_pread_initialize"
    returns false, then libnbd did not sanitize "buf", and
    the contents are undefined on failure.

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.pread(self._o, count, offset, flags)

    def pread_structured(self, count, offset, chunk, flags=0):
        u'''▶ read from the NBD server

    Issue a read command to the NBD server for the range
    starting at "offset" and ending at "offset" + "count" -
    1. The server's response may be subdivided into chunks
    which may arrive out of order before reassembly into the
    original buffer; the "chunk" callback is used for
    notification after each chunk arrives, and may perform
    additional sanity checking on the server's reply. The
    callback cannot call "nbd_*" APIs on the same handle
    since it holds the handle lock and will cause a
    deadlock. If the callback returns -1, and no earlier
    error has been detected, then the overall read command
    will fail with any non-zero value stored into the
    callback's "error" parameter (with a default of
    "EPROTO"); but any further chunks will still invoke the
    callback.

    The "chunk" function is called once per chunk of data
    received, with the "user_data" passed to this function.
    The "subbuf" and "count" parameters represent the subset
    of the original buffer which has just been populated by
    results from the server (in C, "subbuf" always points
    within the original "buf"; but this guarantee may not
    extend to other language bindings). The "offset"
    parameter represents the absolute offset at which
    "subbuf" begins within the image (note that this is not
    the relative offset of "subbuf" within the original
    buffer "buf"). Changes to "error" on output are ignored
    unless the callback fails. The input meaning of the
    "error" parameter is controlled by the "status"
    parameter, which is one of

    "READ_DATA" = 1
        "subbuf" was populated with "count" bytes of data.
        On input, "error" contains the errno value of any
        earlier detected error, or zero.

    "READ_HOLE" = 2
        "subbuf" represents a hole, and contains "count" NUL
        bytes. On input, "error" contains the errno value of
        any earlier detected error, or zero.

    "READ_ERROR" = 3
        "count" is 0, so "subbuf" is unusable. On input,
        "error" contains the errno value reported by the
        server as occurring while reading that "offset",
        regardless if any earlier error has been detected.

    Future NBD extensions may permit other values for
    "status", but those will not be returned to a client
    that has not opted in to requesting such extensions. If
    the server is non-compliant, it is possible for the
    "chunk" function to be called more times than you expect
    or with "count" 0 for "READ_DATA" or "READ_HOLE". It is
    also possible that the "chunk" function is not called at
    all (in particular, "READ_ERROR" is used only when an
    error is associated with a particular offset, and not
    when the server reports a generic error), but you are
    guaranteed that the callback was called at least once if
    the overall read succeeds. Libnbd does not validate that
    the server obeyed the requirement that a read call must
    not have overlapping chunks and must not succeed without
    enough chunks to cover the entire request.

    Note that libnbd currently enforces a maximum read
    buffer of 64MiB, even if the server would permit a
    larger buffer in a single transaction; attempts to
    exceed this will result in an "ERANGE" error. The server
    may enforce a smaller limit, which can be learned with
    "nbd.get_block_size".

    The "flags" parameter may be 0 for no flags, or may
    contain "CMD_FLAG_DF" meaning that the server should not
    reply with more than one fragment (if that is supported
    - some servers cannot do this, see "nbd.can_df"). Libnbd
    does not validate that the server actually obeys the
    flag.

    Note that if this command fails, and
    "nbd.get_pread_initialize" returns true, then libnbd
    sanitized "buf", but it is unspecified whether the
    contents of "buf" will read as zero or as partial
    results from the server. If "nbd.get_pread_initialize"
    returns false, then libnbd did not sanitize "buf", and
    the contents are undefined on failure.

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.pread_structured(self._o, count, offset, chunk,
                                          flags)

    def pwrite(self, buf, offset, flags=0):
        u'''▶ write to the NBD server

    Issue a write command to the NBD server, writing the
    data in "buf" to the range starting at "offset" and
    ending at "offset" + "count" - 1. NBD can only write all
    or nothing using this call. The call returns when the
    command has been acknowledged by the server, or there is
    an error. Note this will generally return an error if
    "nbd.is_read_only" is true.

    Note that libnbd defaults to enforcing a maximum write
    buffer of the lesser of 64MiB or any maximum payload
    size advertised by the server; attempts to exceed this
    will generally result in a client-side "ERANGE" error,
    rather than a server-side disconnection. The actual
    limit can be learned with "nbd.get_block_size".

    The "flags" parameter may be 0 for no flags, or may
    contain "CMD_FLAG_FUA" meaning that the server should
    not return until the data has been committed to
    permanent storage (if that is supported - some servers
    cannot do this, see "nbd.can_fua"). For convenience,
    unless nbd_set_strict_flags(3) was used to disable
    "STRICT_AUTO_FLAG", libnbd ignores the presence or
    absence of the flag "CMD_FLAG_PAYLOAD_LEN" in "flags",
    while correctly using the flag over the wire according
    to whether extended headers were negotiated.

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.pwrite(self._o, buf, offset, flags)

    def shutdown(self, flags=0):
        u'''▶ disconnect from the NBD server

    Issue the disconnect command to the NBD server. This is
    a nice way to tell the server we are going away, but
    from the client's point of view has no advantage over
    abruptly closing the connection (see "nbd.close").

    This function works whether or not the handle is ready
    for transmission of commands. If more fine-grained
    control is needed, see "nbd.aio_opt_abort" and
    "nbd.aio_disconnect".

    The "flags" argument is a bitmask, including zero or
    more of the following shutdown flags:

    "SHUTDOWN_ABANDON_PENDING" = 0x10000
        If there are any pending requests which have not yet
        been sent to the server (see "nbd.aio_in_flight"),
        abandon them without sending them to the server,
        rather than the usual practice of issuing those
        commands before informing the server of the intent
        to disconnect.

    For convenience, the constant "SHUTDOWN_MASK" is
    available to describe all shutdown flags recognized by
    this build of libnbd. A future version of the library
    may add new flags.
'''
        self._check_not_closed()
        return libnbdmod.shutdown(self._o, flags)

    def flush(self, flags=0):
        u'''▶ send flush command to the NBD server

    Issue the flush command to the NBD server. The function
    should return when all write commands which have
    completed have been committed to permanent storage on
    the server. Note this will generally return an error if
    "nbd.can_flush" is false.

    The "flags" parameter must be 0 for now (it exists for
    future NBD protocol extensions).

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.flush(self._o, flags)

    def trim(self, count, offset, flags=0):
        u'''▶ send trim command to the NBD server

    Issue a trim command to the NBD server, which if
    supported by the server causes a hole to be punched in
    the backing store starting at "offset" and ending at
    "offset" + "count" - 1. The call returns when the
    command has been acknowledged by the server, or there is
    an error. Note this will generally return an error if
    "nbd.can_trim" is false or "nbd.is_read_only" is true.

    Note that not all servers can support a "count" of 4GiB
    or larger; "nbd.get_extended_headers_negotiated"
    indicates which servers will parse a request larger than
    32 bits. The NBD protocol does not yet have a way for a
    client to learn if the server will enforce an even
    smaller maximum trim size, although a future extension
    may add a constraint visible in "nbd.get_block_size".

    The "flags" parameter may be 0 for no flags, or may
    contain "CMD_FLAG_FUA" meaning that the server should
    not return until the data has been committed to
    permanent storage (if that is supported - some servers
    cannot do this, see "nbd.can_fua").

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.trim(self._o, count, offset, flags)

    def cache(self, count, offset, flags=0):
        u'''▶ send cache (prefetch) command to the NBD server

    Issue the cache (prefetch) command to the NBD server,
    which if supported by the server causes data to be
    prefetched into faster storage by the server, speeding
    up a subsequent "nbd.pread" call. The server can also
    silently ignore this command. Note this will generally
    return an error if "nbd.can_cache" is false.

    Note that not all servers can support a "count" of 4GiB
    or larger; "nbd.get_extended_headers_negotiated"
    indicates which servers will parse a request larger than
    32 bits. The NBD protocol does not yet have a way for a
    client to learn if the server will enforce an even
    smaller maximum cache size, although a future extension
    may add a constraint visible in "nbd.get_block_size".

    The "flags" parameter must be 0 for now (it exists for
    future NBD protocol extensions).

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.cache(self._o, count, offset, flags)

    def zero(self, count, offset, flags=0):
        u'''▶ send write zeroes command to the NBD server

    Issue a write zeroes command to the NBD server, which if
    supported by the server causes a zeroes to be written
    efficiently starting at "offset" and ending at "offset"
    + "count" - 1. The call returns when the command has
    been acknowledged by the server, or there is an error.
    Note this will generally return an error if
    "nbd.can_zero" is false or "nbd.is_read_only" is true.

    Note that not all servers can support a "count" of 4GiB
    or larger; "nbd.get_extended_headers_negotiated"
    indicates which servers will parse a request larger than
    32 bits. The NBD protocol does not yet have a way for a
    client to learn if the server will enforce an even
    smaller maximum zero size, although a future extension
    may add a constraint visible in "nbd.get_block_size".
    Also, some servers may permit a larger zero request only
    when the "CMD_FLAG_FAST_ZERO" is in use.

    The "flags" parameter may be 0 for no flags, or may
    contain "CMD_FLAG_FUA" meaning that the server should
    not return until the data has been committed to
    permanent storage (if that is supported - some servers
    cannot do this, see "nbd.can_fua"), "CMD_FLAG_NO_HOLE"
    meaning that the server should favor writing actual
    allocated zeroes over punching a hole, and/or
    "CMD_FLAG_FAST_ZERO" meaning that the server must fail
    quickly if writing zeroes is no faster than a normal
    write (if that is supported - some servers cannot do
    this, see "nbd.can_fast_zero").

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.zero(self._o, count, offset, flags)

    def block_status(self, count, offset, extent, flags=0):
        u'''▶ send block status command, with 32-bit callback

    Issue the block status command to the NBD server. If
    supported by the server, this causes metadata context
    information about blocks beginning from the specified
    offset to be returned. The "count" parameter is a hint:
    the server may choose to return less status, or the
    final block may extend beyond the requested range. If
    multiple contexts are supported, the number of blocks
    and cumulative length of those blocks need not be
    identical between contexts.

    Note that not all servers can support a "count" of 4GiB
    or larger; "nbd.get_extended_headers_negotiated"
    indicates which servers will parse a request larger than
    32 bits. The NBD protocol does not yet have a way for a
    client to learn if the server will enforce an even
    smaller maximum block status size, although a future
    extension may add a constraint visible in
    "nbd.get_block_size". Furthermore, this function is
    inherently limited to 32-bit values. If the server
    replies with a larger extent, the length of that extent
    will be truncated to just below 32 bits and any further
    extents from the server will be ignored. If the server
    replies with a status value larger than 32 bits (only
    possible when extended headers are in use), the callback
    function will be passed an "EOVERFLOW" error. To get the
    full extent information from a server that supports
    64-bit extents, you must use "nbd.block_status_64".

    Depending on which metadata contexts were enabled before
    connecting (see "nbd.add_meta_context") and which are
    supported by the server (see "nbd.can_meta_context")
    this call returns information about extents by calling
    back to the "extent" function. The callback cannot call
    "nbd_*" APIs on the same handle since it holds the
    handle lock and will cause a deadlock. If the callback
    returns -1, and no earlier error has been detected, then
    the overall block status command will fail with any
    non-zero value stored into the callback's "error"
    parameter (with a default of "EPROTO"); but any further
    contexts will still invoke the callback.

    The "extent" function is called once per type of
    metadata available, with the "user_data" passed to this
    function. The "metacontext" parameter is a string such
    as "base:allocation". The "entries" array is an array of
    pairs of integers with the first entry in each pair
    being the length (in bytes) of the block and the second
    entry being a status/flags field which is specific to
    the metadata context. The number of pairs passed to the
    function is "nr_entries/2". The NBD protocol document in
    the section about "NBD_REPLY_TYPE_BLOCK_STATUS"
    describes the meaning of this array; for contexts known
    to libnbd, <libnbd.h> contains constants beginning with
    "STATE_" that may help decipher the values. On entry to
    the callback, the "error" parameter contains the errno
    value of any previously detected error, but even if an
    earlier error was detected, the current "metacontext"
    and "entries" are valid.

    It is possible for the extent function to be called more
    times than you expect (if the server is buggy), so
    always check the "metacontext" field to ensure you are
    receiving the data you expect. It is also possible that
    the extent function is not called at all, even for
    metadata contexts that you requested. This indicates
    either that the server doesn't support the context or
    for some other reason cannot return the data.

    The "flags" parameter may be 0 for no flags, or may
    contain "CMD_FLAG_REQ_ONE" meaning that the server
    should return only one extent per metadata context where
    that extent does not exceed "count" bytes; however,
    libnbd does not validate that the server obeyed the
    flag.

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.block_status(self._o, count, offset, extent, flags)

    def block_status_64(self, count, offset, extent64, flags=0):
        u'''▶ send block status command, with 64-bit callback

    Issue the block status command to the NBD server. If
    supported by the server, this causes metadata context
    information about blocks beginning from the specified
    offset to be returned. The "count" parameter is a hint:
    the server may choose to return less status, or the
    final block may extend beyond the requested range. When
    multiple contexts are supported, the number of blocks
    and cumulative length of those blocks need not be
    identical between contexts; this command generally
    returns the status of all negotiated contexts, while
    some servers also support a filtered request (see
    "nbd.can_block_status_payload",
    "nbd.block_status_filter").

    Note that not all servers can support a "count" of 4GiB
    or larger; "nbd.get_extended_headers_negotiated"
    indicates which servers will parse a request larger than
    32 bits. The NBD protocol does not yet have a way for a
    client to learn if the server will enforce an even
    smaller maximum block status size, although a future
    extension may add a constraint visible in
    "nbd.get_block_size".

    Depending on which metadata contexts were enabled before
    connecting (see "nbd.add_meta_context") and which are
    supported by the server (see "nbd.can_meta_context")
    this call returns information about extents by calling
    back to the "extent64" function. The callback cannot
    call "nbd_*" APIs on the same handle since it holds the
    handle lock and will cause a deadlock. If the callback
    returns -1, and no earlier error has been detected, then
    the overall block status command will fail with any
    non-zero value stored into the callback's "error"
    parameter (with a default of "EPROTO"); but any further
    contexts will still invoke the callback.

    The "extent64" function is called once per type of
    metadata available, with the "user_data" passed to this
    function. The "metacontext" parameter is a string such
    as "base:allocation". The "entries" array is an array of
    nbd_extent structs, containing length (in bytes) of the
    block and a status/flags field which is specific to the
    metadata context. The number of array entries passed to
    the function is "nr_entries". The NBD protocol document
    in the section about "NBD_REPLY_TYPE_BLOCK_STATUS"
    describes the meaning of this array; for contexts known
    to libnbd, <libnbd.h> contains constants beginning with
    "STATE_" that may help decipher the values. On entry to
    the callback, the "error" parameter contains the errno
    value of any previously detected error.

    It is possible for the extent function to be called more
    times than you expect (if the server is buggy), so
    always check the "metacontext" field to ensure you are
    receiving the data you expect. It is also possible that
    the extent function is not called at all, even for
    metadata contexts that you requested. This indicates
    either that the server doesn't support the context or
    for some other reason cannot return the data.

    The "flags" parameter may be 0 for no flags, or may
    contain "CMD_FLAG_REQ_ONE" meaning that the server
    should return only one extent per metadata context where
    that extent does not exceed "count" bytes; however,
    libnbd does not validate that the server obeyed the
    flag.

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.block_status_64(self._o, count, offset, extent64,
                                         flags)

    def block_status_filter(self, count, offset, contexts, extent64,
                            flags=0):
        u'''▶ send filtered block status command, with 64-bit callback

    Issue a filtered block status command to the NBD server.
    If supported by the server (see
    "nbd.can_block_status_payload"), this causes metadata
    context information about blocks beginning from the
    specified offset to be returned, and with the result
    limited to just the contexts specified in "filter". Note
    that all strings in "filter" must be supported by
    "nbd.can_meta_context".

    All other parameters to this function have the same
    semantics as in "nbd.block_status_64"; except that for
    convenience, unless <nbd_set_strict_flags(3)> was used
    to disable "STRICT_AUTO_FLAG", libnbd ignores the
    presence or absence of the flag "CMD_FLAG_PAYLOAD_LEN"
    in "flags", while correctly using the flag over the
    wire.

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.block_status_filter(self._o, count, offset,
                                             contexts, extent64, flags)

    def poll(self, timeout):
        u'''▶ poll the handle once

    This is a simple implementation of poll(2) which is used
    internally by synchronous API calls. On success, it
    returns 0 if the "timeout" (in milliseconds) occurs, or
    1 if the poll completed and the state machine
    progressed. Set "timeout" to -1 to block indefinitely
    (but be careful that eventual action is actually
    expected - for example, if the connection is established
    but there are no commands in flight, using an infinite
    timeout will permanently block).

    This function is mainly useful as an example of how you
    might integrate libnbd with your own main loop, rather
    than being intended as something you would use.
'''
        self._check_not_closed()
        return libnbdmod.poll(self._o, timeout)

    def poll2(self, fd, timeout):
        u'''▶ poll the handle once, with fd

    This is the same as "nbd.poll", but an additional file
    descriptor parameter is passed. The additional fd is
    also polled (using "POLLIN"). One use for this is to
    wait for an eventfd(2).
'''
        self._check_not_closed()
        return libnbdmod.poll2(self._o, fd, timeout)

    def aio_connect(self, addr):
        u'''▶ connect to the NBD server

    Begin connecting to the NBD server. The "addr" and
    "addrlen" parameters specify the address of the socket
    to connect to.

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect(self._o, addr)

    def aio_connect_uri(self, uri):
        u'''▶ connect to an NBD URI

    Begin connecting to the NBD URI "uri". Parameters behave
    as documented in "nbd.connect_uri".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_uri(self._o, uri)

    def aio_connect_unix(self, unixsocket):
        u'''▶ connect to the NBD server over a Unix domain socket

    Begin connecting to the NBD server over Unix domain
    socket ("unixsocket"). Parameters behave as documented
    in "nbd.connect_unix".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_unix(self._o, unixsocket)

    def aio_connect_vsock(self, cid, port):
        u'''▶ connect to the NBD server over AF_VSOCK socket

    Begin connecting to the NBD server over the "AF_VSOCK"
    protocol to the server "cid:port". Parameters behave as
    documented in "nbd.connect_vsock".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_vsock(self._o, cid, port)

    def aio_connect_tcp(self, hostname, port):
        u'''▶ connect to the NBD server over a TCP port

    Begin connecting to the NBD server listening on
    "hostname:port". Parameters behave as documented in
    "nbd.connect_tcp".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_tcp(self._o, hostname, port)

    def aio_connect_socket(self, sock):
        u'''▶ connect directly to a connected socket

    Begin connecting to the connected socket "fd".
    Parameters behave as documented in "nbd.connect_socket".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_socket(self._o, sock)

    def aio_connect_command(self, argv):
        u'''▶ connect to the NBD server

    Run the command as a subprocess and begin connecting to
    it over stdin/stdout. Parameters behave as documented in
    "nbd.connect_command".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_command(self._o, argv)

    def aio_connect_systemd_socket_activation(self, argv):
        u'''▶ connect using systemd socket activation

    Run the command as a subprocess and begin connecting to
    it using systemd socket activation. Parameters behave as
    documented in "nbd.connect_systemd_socket_activation".

    You can check if the connection attempt is still
    underway by calling "nbd.aio_is_connecting". If
    "nbd.set_opt_mode" is enabled, the connection is ready
    for manual option negotiation once
    "nbd.aio_is_negotiating" returns true; otherwise, the
    connection attempt will include the NBD handshake, and
    is ready for use once "nbd.aio_is_ready" returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_connect_systemd_socket_activation(self._o,
                                                               argv)

    def aio_opt_go(self, completion=None):
        u'''▶ end negotiation and move on to using an export

    Request that the server finish negotiation and move on
    to serving the export previously specified by the most
    recent "nbd.set_export_name" or "nbd.connect_uri". This
    can only be used if "nbd.set_opt_mode" enabled option
    mode.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that directly detecting whether the server
    returns an error (as is done by the return value of the
    synchronous counterpart) is only possible with a
    completion callback; however it is also possible to
    indirectly detect an error when "nbd.aio_is_negotiating"
    returns true.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_go(self._o, completion)

    def aio_opt_abort(self):
        u'''▶ end negotiation and close the connection

    Request that the server finish negotiation, gracefully
    if possible, then close the connection. This can only be
    used if "nbd.set_opt_mode" enabled option mode.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_abort(self._o)

    def aio_opt_starttls(self, completion=None):
        u'''▶ request the server to initiate TLS

    Request that the server initiate a secure TLS
    connection, by sending "NBD_OPT_STARTTLS". This behaves
    like the synchronous counterpart "nbd.opt_starttls",
    except that it does not wait for the server's response.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_starttls(self._o, completion)

    def aio_opt_extended_headers(self, completion=None):
        u'''▶ request the server to enable extended headers

    Request that the server use extended headers, by sending
    "NBD_OPT_EXTENDED_HEADERS". This behaves like the
    synchronous counterpart "nbd.opt_extended_headers",
    except that it does not wait for the server's response.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_extended_headers(self._o, completion)

    def aio_opt_structured_reply(self, completion=None):
        u'''▶ request the server to enable structured replies

    Request that the server use structured replies, by
    sending "NBD_OPT_STRUCTURED_REPLY". This behaves like
    the synchronous counterpart "nbd.opt_structured_reply",
    except that it does not wait for the server's response.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_structured_reply(self._o, completion)

    def aio_opt_list(self, list, completion=None):
        u'''▶ request the server to list all exports during negotiation

    Request that the server list all exports that it
    supports. This can only be used if "nbd.set_opt_mode"
    enabled option mode.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_list(self._o, list, completion)

    def aio_opt_info(self, completion=None):
        u'''▶ request the server for information about an export

    Request that the server supply information about the
    export name previously specified by the most recent
    "nbd.set_export_name" or "nbd.connect_uri". This can
    only be used if "nbd.set_opt_mode" enabled option mode.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_info(self._o, completion)

    def aio_opt_list_meta_context(self, context, completion=None):
        u'''▶ request list of available meta contexts, using implicit query

    Request that the server list available meta contexts
    associated with the export previously specified by the
    most recent "nbd.set_export_name" or "nbd.connect_uri",
    and with a list of queries from prior calls to
    "nbd.add_meta_context" (see
    "nbd.aio_opt_list_meta_context_queries" if you want to
    supply an explicit query list instead). This can only be
    used if "nbd.set_opt_mode" enabled option mode.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_list_meta_context(self._o, context,
                                                   completion)

    def aio_opt_list_meta_context_queries(self, queries, context,
                                          completion=None):
        u'''▶ request list of available meta contexts, using explicit query

    Request that the server list available meta contexts
    associated with the export previously specified by the
    most recent "nbd.set_export_name" or "nbd.connect_uri",
    and with an explicit list of queries provided as a
    parameter (see "nbd.aio_opt_list_meta_context" if you
    want to reuse an implicit query list instead). This can
    only be used if "nbd.set_opt_mode" enabled option mode.

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_list_meta_context_queries(self._o, queries,
                                                           context,
                                                           completion)

    def aio_opt_set_meta_context(self, context, completion=None):
        u'''▶ select specific meta contexts, with implicit query list

    Request that the server supply all recognized meta
    contexts registered through prior calls to
    "nbd.add_meta_context", in conjunction with the export
    previously specified by the most recent
    "nbd.set_export_name" or "nbd.connect_uri". This can
    only be used if "nbd.set_opt_mode" enabled option mode.
    Normally, this function is redundant, as "nbd.opt_go"
    automatically does the same task if structured replies
    or extended headers have already been negotiated. But
    manual control over meta context requests can be useful
    for fine-grained testing of how a server handles unusual
    negotiation sequences. Often, use of this function is
    coupled with "nbd.set_request_meta_context" to bypass
    the automatic context request normally performed by
    "nbd.opt_go".

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_set_meta_context(self._o, context,
                                                  completion)

    def aio_opt_set_meta_context_queries(self, queries, context,
                                         completion=None):
        u'''▶ select specific meta contexts, with explicit query list

    Request that the server supply all recognized meta
    contexts passed in through "queries", in conjunction
    with the export previously specified by the most recent
    "nbd.set_export_name" or "nbd.connect_uri". This can
    only be used if "nbd.set_opt_mode" enabled option mode.
    Normally, this function is redundant, as "nbd.opt_go"
    automatically does the same task if structured replies
    or extended headers have already been negotiated. But
    manual control over meta context requests can be useful
    for fine-grained testing of how a server handles unusual
    negotiation sequences. Often, use of this function is
    coupled with "nbd.set_request_meta_context" to bypass
    the automatic context request normally performed by
    "nbd.opt_go".

    To determine when the request completes, wait for
    "nbd.aio_is_connecting" to return false. Or supply the
    optional "completion_callback" which will be invoked as
    described in "Completion callbacks" in libnbd(3), except
    that it is automatically retired regardless of return
    value. Note that detecting whether the server returns an
    error (as is done by the return value of the synchronous
    counterpart) is only possible with a completion
    callback.
'''
        self._check_not_closed()
        return libnbdmod.aio_opt_set_meta_context_queries(self._o, queries,
                                                          context,
                                                          completion)

    def aio_pread(self, buf, offset, completion=None, flags=0):
        u'''▶ read from the NBD server

    Issue a read command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Note that you must ensure "buf" is valid until the
    command has completed. Furthermore, if the "error"
    parameter to "completion_callback" is set or if
    "nbd.aio_command_completed" reports failure, and if
    "nbd.get_pread_initialize" returns true, then libnbd
    sanitized "buf", but it is unspecified whether the
    contents of "buf" will read as zero or as partial
    results from the server. If "nbd.get_pread_initialize"
    returns false, then libnbd did not sanitize "buf", and
    the contents are undefined on failure.

    Other parameters behave as documented in "nbd.pread".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_pread(self._o, buf, offset, completion, flags)

    def aio_pread_structured(self, buf, offset, chunk, completion=None,
                             flags=0):
        u'''▶ read from the NBD server

    Issue a read command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Note that you must ensure "buf" is valid until the
    command has completed. Furthermore, if the "error"
    parameter to "completion_callback" is set or if
    "nbd.aio_command_completed" reports failure, and if
    "nbd.get_pread_initialize" returns true, then libnbd
    sanitized "buf", but it is unspecified whether the
    contents of "buf" will read as zero or as partial
    results from the server. If "nbd.get_pread_initialize"
    returns false, then libnbd did not sanitize "buf", and
    the contents are undefined on failure.

    Other parameters behave as documented in
    "nbd.pread_structured".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_pread_structured(self._o, buf, offset, chunk,
                                              completion, flags)

    def aio_pwrite(self, buf, offset, completion=None, flags=0):
        u'''▶ write to the NBD server

    Issue a write command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Note that you must ensure "buf" is valid until the
    command has completed. Other parameters behave as
    documented in "nbd.pwrite".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_pwrite(self._o, buf, offset, completion, flags)

    def aio_disconnect(self, flags=0):
        u'''▶ disconnect from the NBD server

    Issue the disconnect command to the NBD server. This is
    not a normal command because NBD servers are not obliged
    to send a reply. Instead you should wait for
    "nbd.aio_is_closed" to become true on the connection.
    Once this command is issued, you cannot issue any
    further commands.

    Although libnbd does not prevent you from issuing this
    command while still waiting on the replies to previous
    commands, the NBD protocol recommends that you wait
    until there are no other commands in flight (see
    "nbd.aio_in_flight"), to give the server a better chance
    at a clean shutdown.

    The "flags" parameter must be 0 for now (it exists for
    future NBD protocol extensions). There is no direct
    synchronous counterpart; however, "nbd.shutdown" will
    call this function if appropriate.
'''
        self._check_not_closed()
        return libnbdmod.aio_disconnect(self._o, flags)

    def aio_flush(self, completion=None, flags=0):
        u'''▶ send flush command to the NBD server

    Issue the flush command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in "nbd.flush".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_flush(self._o, completion, flags)

    def aio_trim(self, count, offset, completion=None, flags=0):
        u'''▶ send trim command to the NBD server

    Issue a trim command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in "nbd.trim".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_trim(self._o, count, offset, completion, flags)

    def aio_cache(self, count, offset, completion=None, flags=0):
        u'''▶ send cache (prefetch) command to the NBD server

    Issue the cache (prefetch) command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in "nbd.cache".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_cache(self._o, count, offset, completion,
                                   flags)

    def aio_zero(self, count, offset, completion=None, flags=0):
        u'''▶ send write zeroes command to the NBD server

    Issue a write zeroes command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in "nbd.zero".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_zero(self._o, count, offset, completion, flags)

    def aio_block_status(self, count, offset, extent, completion=None,
                         flags=0):
        u'''▶ send block status command, with 32-bit callback

    Send the block status command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in
    "nbd.block_status".

    This function is inherently limited to 32-bit values. If
    the server replies with a larger extent, the length of
    that extent will be truncated to just below 32 bits and
    any further extents from the server will be ignored. If
    the server replies with a status value larger than 32
    bits (only possible when extended headers are in use),
    the callback function will be passed an "EOVERFLOW"
    error. To get the full extent information from a server
    that supports 64-bit extents, you must use
    "nbd.aio_block_status_64".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_block_status(self._o, count, offset, extent,
                                          completion, flags)

    def aio_block_status_64(self, count, offset, extent64, completion=None,
                            flags=0):
        u'''▶ send block status command, with 64-bit callback

    Send the block status command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in
    "nbd.block_status_64".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_block_status_64(self._o, count, offset,
                                             extent64, completion, flags)

    def aio_block_status_filter(self, count, offset, contexts, extent64,
                                completion=None, flags=0):
        u'''▶ send filtered block status command to the NBD server

    Send a filtered block status command to the NBD server.

    To check if the command completed, call
    "nbd.aio_command_completed". Or supply the optional
    "completion_callback" which will be invoked as described
    in "Completion callbacks" in libnbd(3).

    Other parameters behave as documented in
    "nbd.block_status_filter".

    By default, libnbd will reject attempts to use this
    function with parameters that are likely to result in
    server failure, such as requesting an unknown command
    flag. The "nbd.set_strict_mode" function can be used to
    alter which scenarios should await a server reply rather
    than failing fast.
'''
        self._check_not_closed()
        return libnbdmod.aio_block_status_filter(self._o, count, offset,
                                                 contexts, extent64,
                                                 completion, flags)

    def aio_get_fd(self):
        u'''▶ return file descriptor associated with this connection

    Return the underlying file descriptor associated with
    this connection. You can use this to check if the file
    descriptor is ready for reading or writing and call
    "nbd.aio_notify_read" or "nbd.aio_notify_write". See
    also "nbd.aio_get_direction". Do not do anything else
    with the file descriptor.
'''
        self._check_not_closed()
        return libnbdmod.aio_get_fd(self._o)

    def aio_get_direction(self):
        u'''▶ return the read or write direction

    Return the current direction of this connection, which
    means whether we are next expecting to read data from
    the server, write data to the server, or both. It
    returns

    0   We are not expected to interact with the server file
        descriptor from the current state. It is not worth
        attempting to use poll(2); if the connection is not
        dead, then state machine progress must instead come
        from some other means such as "nbd.aio_connect".

    "AIO_DIRECTION_READ" = 1
        We are expected next to read from the server. If
        using poll(2) you would set "events = POLLIN". If
        "revents" returns "POLLIN" or "POLLHUP" you would
        then call "nbd.aio_notify_read".

        Note that once libnbd reaches "nbd.aio_is_ready",
        this direction is returned even when there are no
        commands in flight (see "nbd.aio_in_flight"). In a
        single-threaded use of libnbd, it is not worth
        polling until after issuing a command, as otherwise
        the server will never wake up the poll. In a
        multi-threaded scenario, you can have one thread
        begin a polling loop prior to any commands, but any
        other thread that issues a command will need a way
        to kick the polling thread out of poll in case
        issuing the command changes the needed polling
        direction. Possible ways to do this include polling
        for activity on a pipe-to-self, or using
        pthread_kill(3) to send a signal that is masked
        except during ppoll(2).

    "AIO_DIRECTION_WRITE" = 2
        We are expected next to write to the server. If
        using poll(2) you would set "events = POLLOUT". If
        "revents" returns "POLLOUT" you would then call
        "nbd.aio_notify_write".

    "AIO_DIRECTION_BOTH" = 3
        We are expected next to either read or write to the
        server. If using poll(2) you would set "events =
        POLLIN|POLLOUT". If only one of "POLLIN" or
        "POLLOUT" is returned, then see above. However, if
        both are returned, it is better to call only
        "nbd.aio_notify_read", as processing the server's
        reply may change the state of the connection and
        invalidate the need to write more commands.
'''
        self._check_not_closed()
        return libnbdmod.aio_get_direction(self._o)

    def aio_notify_read(self):
        u'''▶ notify that the connection is readable

    Send notification to the state machine that the
    connection is readable. Typically this is called after
    your main loop has detected that the file descriptor
    associated with this connection is readable.
'''
        self._check_not_closed()
        return libnbdmod.aio_notify_read(self._o)

    def aio_notify_write(self):
        u'''▶ notify that the connection is writable

    Send notification to the state machine that the
    connection is writable. Typically this is called after
    your main loop has detected that the file descriptor
    associated with this connection is writable.
'''
        self._check_not_closed()
        return libnbdmod.aio_notify_write(self._o)

    def aio_is_created(self):
        u'''▶ check if the connection has just been created

    Return true if this connection has just been created.
    This is the state before the handle has started
    connecting to a server. In this state the handle can
    start to be connected by calling functions such as
    "nbd.aio_connect".
'''
        self._check_not_closed()
        return libnbdmod.aio_is_created(self._o)

    def aio_is_connecting(self):
        u'''▶ check if the connection is connecting or handshaking

    Return true if this connection is connecting to the
    server or in the process of handshaking and negotiating
    options which happens before the handle becomes ready to
    issue commands (see "nbd.aio_is_ready").
'''
        self._check_not_closed()
        return libnbdmod.aio_is_connecting(self._o)

    def aio_is_negotiating(self):
        u'''▶ check if connection is ready to send handshake option

    Return true if this connection is ready to start another
    option negotiation command while handshaking with the
    server. An option command will move back to the
    connecting state (see "nbd.aio_is_connecting"). Note
    that this state cannot be reached unless requested by
    "nbd.set_opt_mode", and even then it only works with
    newstyle servers; an oldstyle server will skip straight
    to "nbd.aio_is_ready".
'''
        self._check_not_closed()
        return libnbdmod.aio_is_negotiating(self._o)

    def aio_is_ready(self):
        u'''▶ check if the connection is in the ready state

    Return true if this connection is connected to the NBD
    server, the handshake has completed, and the connection
    is idle or waiting for a reply. In this state the handle
    is ready to issue commands.
'''
        self._check_not_closed()
        return libnbdmod.aio_is_ready(self._o)

    def aio_is_processing(self):
        u'''▶ check if the connection is processing a command

    Return true if this connection is connected to the NBD
    server, the handshake has completed, and the connection
    is processing commands (either writing out a request or
    reading a reply).

    Note the ready state ("nbd.aio_is_ready") is not
    included. In the ready state commands may be *in flight*
    (the *server* is processing them), but libnbd is not
    processing them.
'''
        self._check_not_closed()
        return libnbdmod.aio_is_processing(self._o)

    def aio_is_dead(self):
        u'''▶ check if the connection is dead

    Return true if the connection has encountered a fatal
    error and is dead. In this state the handle may only be
    closed. There is no way to recover a handle from the
    dead state.
'''
        self._check_not_closed()
        return libnbdmod.aio_is_dead(self._o)

    def aio_is_closed(self):
        u'''▶ check if the connection is closed

    Return true if the connection has closed. There is no
    way to reconnect a closed connection. Instead you must
    close the whole handle.
'''
        self._check_not_closed()
        return libnbdmod.aio_is_closed(self._o)

    def aio_command_completed(self, cookie):
        u'''▶ check if the command completed

    Return true if the command completed. If this function
    returns true then the command was successful and it has
    been retired. Return false if the command is still in
    flight. This can also fail with an error in case the
    command failed (in this case the command is also
    retired). A command is retired either via this command,
    or by using a completion callback which returns 1.

    The "cookie" parameter is the positive unique 64 bit
    cookie for the command, as returned by a call such as
    "nbd.aio_pread".
'''
        self._check_not_closed()
        return libnbdmod.aio_command_completed(self._o, cookie)

    def aio_peek_command_completed(self):
        u'''▶ check if any command has completed

    Return the unique positive 64 bit cookie of the first
    non-retired but completed command, 0 if there are
    in-flight commands but none of them are awaiting
    retirement, or -1 on error including when there are no
    in-flight commands. Any cookie returned by this function
    must still be passed to "nbd.aio_command_completed" to
    actually retire the command and learn whether the
    command was successful.
'''
        self._check_not_closed()
        return libnbdmod.aio_peek_command_completed(self._o)

    def aio_in_flight(self):
        u'''▶ check how many aio commands are still in flight

    Return the number of in-flight aio commands that are
    still awaiting a response from the server before they
    can be retired. If this returns a non-zero value when
    requesting a disconnect from the server (see
    "nbd.aio_disconnect" and "nbd.shutdown"), libnbd does
    not try to wait for those commands to complete
    gracefully; if the server strands commands while
    shutting down, "nbd.aio_command_completed" will report
    those commands as failed with a status of "ENOTCONN".
'''
        self._check_not_closed()
        return libnbdmod.aio_in_flight(self._o)

    def connection_state(self):
        u'''▶ return string describing the state of the connection

    Returns a descriptive string for the state of the
    connection. This can be used for debugging or
    troubleshooting, but you should not rely on the state of
    connections since it may change in future versions.
'''
        self._check_not_closed()
        return libnbdmod.connection_state(self._o)

    def get_package_name(self):
        u'''▶ return the name of the library

    Returns the name of the library, always "libnbd" unless
    the library was modified with another name at compile
    time.
'''
        self._check_not_closed()
        return libnbdmod.get_package_name(self._o)

    def get_version(self):
        u'''▶ return the version of the library

    Return the version of libnbd. This is returned as a
    string in the form "major.minor.release" where each of
    major, minor and release is a small positive integer.
    For example:

         minor
        "1.0.3"
         ↑   ↑
     major   release

    major = 0
        The major number was 0 for the early experimental
        versions of libnbd where we still had an unstable
        API.

    major = 1
        The major number is 1 for the versions of libnbd
        with a long-term stable API and ABI. It is not
        anticipated that major will be any number other than
        1.

    minor = 0, 2, ... (even)
        The minor number is even for stable releases.

    minor = 1, 3, ... (odd)
        The minor number is odd for development versions.
        Note that new APIs added in a development version
        remain experimental and subject to change in that
        branch until they appear in a stable release.

    release
        The release number is incremented for each release
        along a particular branch.
'''
        self._check_not_closed()
        return libnbdmod.get_version(self._o)

    def kill_subprocess(self, signum):
        u'''▶ kill server running as a subprocess

    This call may be used to kill the server running as a
    subprocess that was previously created using
    "nbd.connect_command". You do not need to use this call.
    It is only needed if the server does not exit when the
    socket is closed.

    The "signum" parameter is the optional signal number to
    send (see signal(7)). If "signum" is 0 then "SIGTERM" is
    sent.
'''
        self._check_not_closed()
        return libnbdmod.kill_subprocess(self._o, signum)

    def get_subprocess_pid(self):
        u'''▶ get the process ID of the subprocess

    For connections which create a subprocess such as
    "nbd.connect_command", this returns the process ID (PID)
    of the subprocess. This is only supported on some
    platforms.

    This is mainly useful in debugging cases. For example,
    this could be used to learn where to attach gdb(1) to
    diagnose a crash in the NBD server subprocess.
'''
        self._check_not_closed()
        return libnbdmod.get_subprocess_pid(self._o)

    def supports_tls(self):
        u'''▶ true if libnbd was compiled with support for TLS

    Returns true if libnbd was compiled with gnutls which is
    required to support TLS encryption, or false if not.
'''
        self._check_not_closed()
        return libnbdmod.supports_tls(self._o)

    def supports_vsock(self):
        u'''▶ true if libnbd was compiled with support for AF_VSOCK

    Returns true if libnbd was compiled with support for the
    "AF_VSOCK" family of sockets, or false if not.

    Note that on the Linux operating system, this returns
    true if there is compile-time support, but you may still
    need runtime support for some aspects of AF_VSOCK usage;
    for example, use of "VMADDR_CID_LOCAL" as the server
    name requires that the *vsock_loopback* kernel module is
    loaded.
'''
        self._check_not_closed()
        return libnbdmod.supports_vsock(self._o)

    def supports_uri(self):
        u'''▶ true if libnbd was compiled with support for NBD URIs

    Returns true if libnbd was compiled with libxml2 which
    is required to support NBD URIs, or false if not.
'''
        self._check_not_closed()
        return libnbdmod.supports_uri(self._o)

    def get_uri(self):
        u'''▶ construct an NBD URI for a connection

    This makes a best effort attempt to construct an NBD URI
    which could be used to connect back to the same server
    (using "nbd.connect_uri").

    In some cases there is not enough information in the
    handle to successfully create a URI (eg. if you
    connected with "nbd.connect_socket"). In such cases the
    call returns "NULL" and further diagnostic information
    is available via "nbd.get_errno" and "nbd.get_error" as
    usual.

    Even if a URI is returned it is not guaranteed to work,
    and it may not be optimal.

    nbdinfo(1) *--uri* option is a way to access this API
    from shell scripts.
'''
        self._check_not_closed()
        return libnbdmod.get_uri(self._o)

    def is_uri(self, uri):
        u'''▶ detect if a string could be an NBD URI

    Detect if the parameter "uri" could be an NBD URI or
    not. The function returns true if "uri" is likely to be
    an NBD URI, or false if not.

    This can be used to write programs that take either a
    URI or something else like a filename as a parameter.
    nbdcopy(1) is one such program.

    The current test is heuristic. In particular it *does
    not* guarantee that "nbd.connect_uri" will work.
'''
        self._check_not_closed()
        return libnbdmod.is_uri(self._o, uri)


package_name = NBD().get_package_name()
__version__ = NBD().get_version()

if __name__ == "__main__":
    import nbdsh

    nbdsh.shell()