1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <kea_version.h>

#include <asiolink/addr_utilities.h>
#include <asiolink/io_address.h>
#include <asiolink/io_service.h>
#include <asiolink/io_service_mgr.h>
#include <cc/data.h>
#include <config/http_command_mgr.h>
#include <cryptolink/cryptolink.h>
#include <dhcp/classify.h>
#include <dhcp/dhcp4.h>
#include <dhcp/docsis3_option_defs.h>
#include <dhcp/duid.h>
#include <dhcp/hwaddr.h>
#include <dhcp/iface_mgr.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/option.h>
#include <dhcp/option4_addrlst.h>
#include <dhcp/option4_client_fqdn.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_data_types.h>
#include <dhcp/option_definition.h>
#include <dhcp/option_int.h>
#include <dhcp/option_int_array.h>
#include <dhcp/option_string.h>
#include <dhcp/option_vendor.h>
#include <dhcp/option_vendor_class.h>
#include <dhcp/pkt.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt4o6.h>
#include <dhcp/socket_info.h>
#include <dhcp/std_option_defs.h>
#include <dhcp4/client_handler.h>
#include <dhcp4/dhcp4_log.h>
#include <dhcp4/dhcp4_srv.h>
#include <dhcp4/dhcp4to6_ipc.h>
#include <dhcp_ddns/ncr_io.h>
#include <dhcp_ddns/ncr_msg.h>
#include <dhcpsrv/alloc_engine.h>
#include <dhcpsrv/callout_handle_store.h>
#include <dhcpsrv/cb_ctl_dhcp4.h>
#include <dhcpsrv/cfg_expiration.h>
#include <dhcpsrv/cfg_globals.h>
#include <dhcpsrv/cfg_host_operations.h>
#include <dhcpsrv/cfg_iface.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/cfg_shared_networks.h>
#include <dhcpsrv/cfg_subnets4.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/client_class_def.h>
#include <dhcpsrv/d2_client_cfg.h>
#include <dhcpsrv/d2_client_mgr.h>
#include <dhcpsrv/dhcpsrv_exceptions.h>
#include <dhcpsrv/host.h>
#include <dhcpsrv/host_data_source_factory.h>
#include <dhcpsrv/host_mgr.h>
#include <dhcpsrv/lease.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/ncr_generator.h>
#include <dhcpsrv/network_state.h>
#include <dhcpsrv/packet_fuzzer.h>
#include <dhcpsrv/pool.h>
#include <dhcpsrv/resource_handler.h>
#include <dhcpsrv/shared_network.h>
#include <dhcpsrv/srv_config.h>
#include <dhcpsrv/subnet.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcpsrv/subnet_selector.h>
#include <dhcpsrv/utils.h>
#include <eval/evaluate.h>
#include <eval/token.h>
#include <exceptions/exceptions.h>
#include <hooks/callout_handle.h>
#include <hooks/hooks_log.h>
#include <hooks/hooks_manager.h>
#include <hooks/parking_lots.h>
#include <hooks/server_hooks.h>
#include <log/interprocess/interprocess_sync_file.h>
#include <log/log_dbglevels.h>
#include <log/log_formatter.h>
#include <log/logger.h>
#include <log/macros.h>
#include <stats/stats_mgr.h>
#include <util/multi_threading_mgr.h>
#include <util/optional.h>
#include <util/readwrite_mutex.h>
#include <util/thread_pool.h>
#include <util/triplet.h>

#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cmath><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdint><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdlib><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <exception><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <functional><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <list><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <memory><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <set><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <tuple><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <boost/foreach.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/range/adaptor/reversed.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::asiolink;
using namespace isc::cryptolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp_ddns;
using namespace isc::hooks;
using namespace isc::log;
using namespace isc::log::interprocess;
using namespace isc::stats;
using namespace isc::util;
using namespace std;
namespace ph = std::placeholders;

namespace {

/// Structure that holds registered hook indexes
struct Dhcp4Hooks {
    int hook_index_buffer4_receive_;        ///< index for "buffer4_receive" hook point
    int hook_index_pkt4_receive_;           ///< index for "pkt4_receive" hook point
    int hook_index_subnet4_select_;         ///< index for "subnet4_select" hook point
    int hook_index_leases4_committed_;      ///< index for "leases4_committed" hook point
    int hook_index_lease4_release_;         ///< index for "lease4_release" hook point
    int hook_index_pkt4_send_;              ///< index for "pkt4_send" hook point
    int hook_index_buffer4_send_;           ///< index for "buffer4_send" hook point
    int hook_index_lease4_decline_;         ///< index for "lease4_decline" hook point
    int hook_index_host4_identifier_;       ///< index for "host4_identifier" hook point
    int hook_index_ddns4_update_;           ///< index for "ddns4_update" hook point
    int hook_index_lease4_offer_;           ///< index for "lease4_offer" hook point
    int hook_index_lease4_server_decline_;  ///< index for "lease4_server_decline" hook point

    /// Constructor that registers hook points for DHCPv4 engine
    Dhcp4Hooks() {
        hook_index_buffer4_receive_         = HooksManager::registerHook("buffer4_receive");
        hook_index_pkt4_receive_            = HooksManager::registerHook("pkt4_receive");
        hook_index_subnet4_select_          = HooksManager::registerHook("subnet4_select");
        hook_index_leases4_committed_       = HooksManager::registerHook("leases4_committed");
        hook_index_lease4_release_          = HooksManager::registerHook("lease4_release");
        hook_index_pkt4_send_               = HooksManager::registerHook("pkt4_send");
        hook_index_buffer4_send_            = HooksManager::registerHook("buffer4_send");
        hook_index_lease4_decline_          = HooksManager::registerHook("lease4_decline");
        hook_index_host4_identifier_        = HooksManager::registerHook("host4_identifier");
        hook_index_ddns4_update_            = HooksManager::registerHook("ddns4_update");
        hook_index_lease4_offer_            = HooksManager::registerHook("lease4_offer");
        hook_index_lease4_server_decline_   = HooksManager::registerHook("lease4_server_decline");
    }
};

/// List of statistics which is initialized to 0 during the DHCPv4
/// server startup.
std::set<std::string> dhcp4_statistics = {
    "pkt4-received",
    "pkt4-discover-received",
    "pkt4-offer-received",
    "pkt4-request-received",
    "pkt4-ack-received",
    "pkt4-nak-received",
    "pkt4-release-received",
    "pkt4-decline-received",
    "pkt4-inform-received",
    "pkt4-unknown-received",
    "pkt4-sent",
    "pkt4-offer-sent",
    "pkt4-ack-sent",
    "pkt4-nak-sent",
    "pkt4-parse-failed",
    "pkt4-receive-drop",
    "v4-allocation-fail",
    "v4-allocation-fail-shared-network",
    "v4-allocation-fail-subnet",
    "v4-allocation-fail-no-pools",
    "v4-allocation-fail-classes",
    "v4-reservation-conflicts",
    "v4-lease-reuses",
};

} // end of anonymous namespace

// Declare a Hooks object. As this is outside any function or method, it
// will be instantiated (and the constructor run) when the module is loaded.
// As a result, the hook indexes will be defined before any method in this
// module is called.
Dhcp4Hooks Hooks;

namespace isc {
namespace dhcp {

Dhcpv4Exchange::Dhcpv4Exchange(const AllocEnginePtr& alloc_engine,
                               const Pkt4Ptr& query,
                               AllocEngine::ClientContext4Ptr& context,
                               const ConstSubnet4Ptr& subnet,
                               bool& drop)
    : alloc_engine_(alloc_engine), query_(query), resp_(),
      context_(context), ipv6_only_preferred_(false) {

    if (!alloc_engine_) {
        isc_throw(BadValue, "alloc_engine value must not be NULL"
                  " when creating an instance of the Dhcpv4Exchange");
    }

    if (!query_) {
        isc_throw(BadValue, "query value must not be NULL when"
                  " creating an instance of the Dhcpv4Exchange");
    }

    // Reset the given context argument.
    context.reset();

    // Create response message.
    initResponse();
    // Select subnet for the query message.
    context_->subnet_ = subnet;

    // If subnet found, retrieve client identifier which will be needed
    // for allocations and search for reservations associated with a
    // subnet/shared network.
    SharedNetwork4Ptr sn;
    if (subnet && !context_->early_global_reservations_lookup_) {
        OptionPtr opt_clientid = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
        if (opt_clientid) {
            context_->clientid_.reset(new ClientId(opt_clientid->getData()));
        }
    }

    if (subnet) {
        // Find static reservations if not disabled for our subnet.
        if (subnet->getReservationsInSubnet() ||
            subnet->getReservationsGlobal()) {
            // Before we can check for static reservations, we need to prepare a set
            // of identifiers to be used for this.
            if (!context_->early_global_reservations_lookup_) {
                setHostIdentifiers(context_);
            }

            // Check for static reservations.
            alloc_engine->findReservation(*context_);

            // Get shared network to see if it is set for a subnet.
            subnet->getSharedNetwork(sn);
        }
    }

    // Global host reservations are independent of a selected subnet. If the
    // global reservations contain client classes we should use them in case
    // they are meant to affect pool selection. Also, if the subnet does not
    // belong to a shared network we can use the reserved client classes
    // because there is no way our subnet could change. Such classes may
    // affect selection of a pool within the selected subnet.
    auto global_host = context_->globalHost();
    auto current_host = context_->currentHost();
    if ((!context_->early_global_reservations_lookup_ &&
         global_host && !global_host->getClientClasses4().empty()) ||
        (!sn && current_host && !current_host->getClientClasses4().empty())) {
        // We have already evaluated client classes and some of them may
        // be in conflict with the reserved classes. Suppose there are
        // two classes defined in the server configuration: first_class
        // and second_class and the test for the second_class it looks
        // like this: "not member('first_class')". If the first_class
        // initially evaluates to false, the second_class evaluates to
        // true. If the first_class is now set within the hosts reservations
        // and we don't remove the previously evaluated second_class we'd
        // end up with both first_class and second_class evaluated to
        // true. In order to avoid that, we have to remove the classes
        // evaluated in the first pass and evaluate them again. As
        // a result, the first_class set via the host reservation will
        // replace the second_class because the second_class will this
        // time evaluate to false as desired.
        removeDependentEvaluatedClasses(query);
        setReservedClientClasses(context_);
        evaluateClasses(query, false);
    }

    // Set KNOWN builtin class if something was found, UNKNOWN if not.
    if (!context_->hosts_.empty()) {
        query->addClass("KNOWN");
        LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
            .arg(query->getLabel())
            .arg("KNOWN");
    } else {
        query->addClass("UNKNOWN");
        LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
            .arg(query->getLabel())
            .arg("UNKNOWN");
    }

    // Perform second pass of classification.
    evaluateClasses(query, true);

    const ClientClasses& classes = query_->getClasses();
    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASSES_ASSIGNED_AFTER_SUBNET_SELECTION)
        .arg(query_->getLabel())
        .arg(classes.toText());

    // Check the DROP special class.
    if (query_->inClass("DROP")) {
        LOG_DEBUG(packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0013)
            .arg(query_->getHWAddrLabel())
            .arg(query_->toText());
        isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                  static_cast<int64_t>(1));
        drop = true;
    }
}

void
Dhcpv4Exchange::initResponse() {
    uint8_t resp_type = 0;
    switch (getQuery()->getType()) {
    case DHCPDISCOVER:
        resp_type = DHCPOFFER;
        break;
    case DHCPREQUEST:
    case DHCPINFORM:
        resp_type = DHCPACK;
        break;
    default:
        ;
    }
    // Only create a response if one is required.
    if (resp_type > 0) {
        resp_.reset(new Pkt4(resp_type, getQuery()->getTransid()));
        copyDefaultFields();
        copyDefaultOptions();

        if (getQuery()->isDhcp4o6()) {
            initResponse4o6();
        }
    }
}

void
Dhcpv4Exchange::initResponse4o6() {
    Pkt4o6Ptr query = boost::dynamic_pointer_cast<Pkt4o6>(getQuery());
    if (!query) {
        return;
    }
    const Pkt6Ptr& query6 = query->getPkt6();
    Pkt6Ptr resp6(new Pkt6(DHCPV6_DHCPV4_RESPONSE, query6->getTransid()));
    // Don't add client-id or server-id
    // But copy relay info
    if (!query6->relay_info_.empty()) {
        resp6->copyRelayInfo(query6);
    }
    // Copy interface, and remote address and port
    resp6->setIface(query6->getIface());
    resp6->setIndex(query6->getIndex());
    resp6->setRemoteAddr(query6->getRemoteAddr());
    resp6->setRemotePort(query6->getRemotePort());
    resp_.reset(new Pkt4o6(resp_, resp6));
}

void
Dhcpv4Exchange::copyDefaultFields() {
    resp_->setIface(query_->getIface());
    resp_->setIndex(query_->getIndex());

    // explicitly set this to 0
    resp_->setSiaddr(IOAddress::IPV4_ZERO_ADDRESS());
    // ciaddr is always 0, except for the Renew/Rebind state and for
    // Inform when it may be set to the ciaddr sent by the client.
    if (query_->getType() == DHCPINFORM) {
        resp_->setCiaddr(query_->getCiaddr());
    } else {
        resp_->setCiaddr(IOAddress::IPV4_ZERO_ADDRESS());
    }
    resp_->setHops(query_->getHops());

    // copy MAC address
    resp_->setHWAddr(query_->getHWAddr());

    // relay address
    resp_->setGiaddr(query_->getGiaddr());

    // If src/dest HW addresses are used by the packet filtering class
    // we need to copy them as well. There is a need to check that the
    // address being set is not-NULL because an attempt to set the NULL
    // HW would result in exception. If these values are not set, the
    // the default HW addresses (zeroed) should be generated by the
    // packet filtering class when creating Ethernet header for
    // outgoing packet.
    HWAddrPtr src_hw_addr = query_->getLocalHWAddr();
    if (src_hw_addr) {
        resp_->setLocalHWAddr(src_hw_addr);
    }
    HWAddrPtr dst_hw_addr = query_->getRemoteHWAddr();
    if (dst_hw_addr) {
        resp_->setRemoteHWAddr(dst_hw_addr);
    }

    // Copy flags from the request to the response per RFC 2131
    resp_->setFlags(query_->getFlags());
}

void
Dhcpv4Exchange::copyDefaultOptions() {
    // Let's copy client-id to response. See RFC6842.
    // It is possible to disable RFC6842 to keep backward compatibility
    bool echo = CfgMgr::instance().getCurrentCfg()->getEchoClientId();
    OptionPtr client_id = query_->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
    if (client_id && echo) {
        resp_->addOption(client_id);
    }

    // RFC 3011 states about the Subnet Selection Option

    // "Servers configured to support this option MUST return an
    //  identical copy of the option to any client that sends it,
    //  regardless of whether or not the client requests the option in
    //  a parameter request list. Clients using this option MUST
    //  discard DHCPOFFER or DHCPACK packets that do not contain this
    //  option."
    OptionPtr subnet_sel = query_->getOption(DHO_SUBNET_SELECTION);
    if (subnet_sel) {
        resp_->addOption(subnet_sel);
    }

    // If this packet is relayed, we want to copy Relay Agent Info option
    // when it is not empty.
    OptionPtr rai = query_->getOption(DHO_DHCP_AGENT_OPTIONS);
    if (!rai || (rai->len() <= Option::OPTION4_HDR_LEN)) {
        return;
    }
    // Do not copy recovered stashed RAI.
    ConstElementPtr sao = CfgMgr::instance().getCurrentCfg()->
        getConfiguredGlobal(CfgGlobals::STASH_AGENT_OPTIONS);
    if (sao && (sao->getType() == Element::boolean) &&
        sao->boolValue() && query_->inClass("STASH_AGENT_OPTIONS")) {
        return;
    }
    resp_->addOption(rai);
}

void
Dhcpv4Exchange::setHostIdentifiers(AllocEngine::ClientContext4Ptr context) {
    const ConstCfgHostOperationsPtr cfg =
        CfgMgr::instance().getCurrentCfg()->getCfgHostOperations4();

    // Collect host identifiers. The identifiers are stored in order of preference.
    // The server will use them in that order to search for host reservations.
    for (auto const& id_type : cfg->getIdentifierTypes()) {
        switch (id_type) {
        case Host::IDENT_HWADDR:
            if (context->hwaddr_ && !context->hwaddr_->hwaddr_.empty()) {
                context->addHostIdentifier(id_type, context->hwaddr_->hwaddr_);
            }
            break;

        case Host::IDENT_DUID:
            if (context->clientid_) {
                const std::vector<uint8_t>& vec = context->clientid_->getClientId();
                if (!vec.empty()) {
                    // Client identifier type = DUID? Client identifier holding a DUID
                    // comprises Type (1 byte), IAID (4 bytes), followed by the actual
                    // DUID. Thus, the minimal length is 6.
                    if ((vec[0] == CLIENT_ID_OPTION_TYPE_DUID) && (vec.size() > 5)) {
                        // Extract DUID, skip IAID.
                        context->addHostIdentifier(id_type,
                                                   std::vector<uint8_t>(vec.begin() + 5,
                                                                        vec.end()));
                    }
                }
            }
            break;

        case Host::IDENT_CIRCUIT_ID:
            {
                OptionPtr rai = context->query_->getOption(DHO_DHCP_AGENT_OPTIONS);
                if (rai) {
                    OptionPtr circuit_id_opt = rai->getOption(RAI_OPTION_AGENT_CIRCUIT_ID);
                    if (circuit_id_opt) {
                        const OptionBuffer& circuit_id_vec = circuit_id_opt->getData();
                        if (!circuit_id_vec.empty()) {
                            context->addHostIdentifier(id_type, circuit_id_vec);
                        }
                    }
                }
            }
            break;

        case Host::IDENT_CLIENT_ID:
            if (context->clientid_) {
                const std::vector<uint8_t>& vec = context->clientid_->getClientId();
                if (!vec.empty()) {
                    context->addHostIdentifier(id_type, vec);
                }
            }
            break;
        case Host::IDENT_FLEX:
            {
                if (!HooksManager::calloutsPresent(Hooks.hook_index_host4_identifier_)) {
                    break;
                }

                CalloutHandlePtr callout_handle = getCalloutHandle(context->query_);

                Host::IdentifierType type = Host::IDENT_FLEX;
                std::vector<uint8_t> id;

                // Use the RAII wrapper to make sure that the callout handle state is
                // reset when this object goes out of scope. All hook points must do
                // it to prevent possible circular dependency between the callout
                // handle and its arguments.
                ScopedCalloutHandleState callout_handle_state(callout_handle);

                // Pass incoming packet as argument
                callout_handle->setArgument("query4", context->query_);
                callout_handle->setArgument("id_type", type);
                callout_handle->setArgument("id_value", id);

                // Call callouts
                HooksManager::callCallouts(Hooks.hook_index_host4_identifier_,
                                           *callout_handle);

                callout_handle->getArgument("id_type", type);
                callout_handle->getArgument("id_value", id);

                if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_CONTINUE) &&
                    !id.empty()) {

                    LOG_DEBUG(packet4_logger, DBGLVL_TRACE_BASIC, DHCP4_FLEX_ID)
                        .arg(context->query_->getLabel())
                        .arg(Host::getIdentifierAsText(type, &id[0], id.size()));

                    context->addHostIdentifier(type, id);
                }
                break;
            }
        default:
            ;
        }
    }
}

void
Dhcpv4Exchange::removeDependentEvaluatedClasses(const Pkt4Ptr& query) {
    const ClientClassDictionaryPtr& dict =
        CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
    const ClientClassDefListPtr& defs_ptr = dict->getClasses();
    for (auto const& def : *defs_ptr) {
        // Only remove evaluated classes. Other classes can be
        // assigned via hooks libraries and we should not remove
        // them because there is no way they can be added back.
        if (def->getMatchExpr()) {
            query->classes_.erase(def->getName());
        }
    }
}

void
Dhcpv4Exchange::setReservedClientClasses(AllocEngine::ClientContext4Ptr context) {
    if (context->currentHost() && context->query_) {
        const ClientClasses& classes = context->currentHost()->getClientClasses4();
        for (auto const& cclass : classes) {
            context->query_->addClass(cclass);
        }
    }
}

void
Dhcpv4Exchange::conditionallySetReservedClientClasses() {
    if (context_->subnet_) {
        SharedNetwork4Ptr shared_network;
        context_->subnet_->getSharedNetwork(shared_network);
        if (shared_network) {
            ConstHostPtr host = context_->currentHost();
            if (host && (host->getIPv4SubnetID() != SUBNET_ID_GLOBAL)) {
                setReservedClientClasses(context_);
            }
        }
    }
}

void
Dhcpv4Exchange::setReservedMessageFields() {
    ConstHostPtr host = context_->currentHost();
    // Nothing to do if host reservations not specified for this client.
    if (host) {
        if (!host->getNextServer().isV4Zero()) {
            resp_->setSiaddr(host->getNextServer());
        }

        std::string sname = host->getServerHostname();
        if (!sname.empty()) {
            resp_->setSname(reinterpret_cast<const uint8_t*>(sname.c_str()),
                            sname.size());
        }

        std::string bootfile = host->getBootFileName();
        if (!bootfile.empty()) {
            resp_->setFile(reinterpret_cast<const uint8_t*>(bootfile.c_str()),
                           bootfile.size());
        }
    }
}

void Dhcpv4Exchange::classifyByVendor(const Pkt4Ptr& pkt) {
    // Built-in vendor class processing
    boost::shared_ptr<OptionString> vendor_class =
        boost::dynamic_pointer_cast<OptionString>(pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER));

    if (!vendor_class) {
        return;
    }

    pkt->addClass(Dhcpv4Srv::VENDOR_CLASS_PREFIX + vendor_class->getValue());
}

void Dhcpv4Exchange::classifyPacket(const Pkt4Ptr& pkt) {
    // All packets belong to ALL.
    pkt->addClass("ALL");

    // First: built-in vendor class processing.
    classifyByVendor(pkt);

    // Run match expressions on classes not depending on KNOWN/UNKNOWN.
    evaluateClasses(pkt, false);
}

void Dhcpv4Exchange::evaluateClasses(const Pkt4Ptr& pkt, bool depend_on_known) {
    // Note getClientClassDictionary() cannot be null
    const ClientClassDictionaryPtr& dict =
        CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
    const ClientClassDefListPtr& defs_ptr = dict->getClasses();
    for (auto const& it : *defs_ptr) {
        // Note second cannot be null
        const ExpressionPtr& expr_ptr = it->getMatchExpr();
        // Nothing to do without an expression to evaluate
        if (!expr_ptr) {
            continue;
        }
        // Not the right time if only when additional
        if (it->getAdditional()) {
            continue;
        }
        // Not the right pass.
        if (it->getDependOnKnown() != depend_on_known) {
            continue;
        }
        it->test(pkt, expr_ptr);
    }
}

const std::string Dhcpv4Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");

Dhcpv4Srv::Dhcpv4Srv(uint16_t server_port, uint16_t client_port,
                     const bool use_bcast, const bool direct_response_desired)
    : io_service_(new IOService()), server_port_(server_port),
      client_port_(client_port), shutdown_(true),
      alloc_engine_(), use_bcast_(use_bcast),
      network_state_(new NetworkState()),
      cb_control_(new CBControlDHCPv4()),
      test_send_responses_to_source_(false) {

    const char* env = std::getenv("KEA_TEST_SEND_RESPONSES_TO_SOURCE");
    if (env) {
        LOG_WARN(dhcp4_logger, DHCP4_TESTING_MODE_SEND_TO_SOURCE_ENABLED);
        test_send_responses_to_source_ = true;
    }

    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET)
        .arg(server_port);

    try {
        // Port 0 is used for testing purposes where we don't open broadcast
        // capable sockets. So, set the packet filter handling direct traffic
        // only if we are in non-test mode.
        if (server_port) {
            // First call to instance() will create IfaceMgr (it's a singleton)
            // it may throw something if things go wrong.
            // The 'true' value of the call to setMatchingPacketFilter imposes
            // that IfaceMgr will try to use the mechanism to respond directly
            // to the client which doesn't have address assigned. This capability
            // may be lacking on some OSes, so there is no guarantee that server
            // will be able to respond directly.
            IfaceMgr::instance().setMatchingPacketFilter(direct_response_desired);
        }

        // Instantiate allocation engine. The number of allocation attempts equal
        // to zero indicates that the allocation engine will use the number of
        // attempts depending on the pool size.
        alloc_engine_.reset(new AllocEngine(0));

        /// @todo call loadLibraries() when handling configuration changes

    } catch (const std::exception &e) {
        LOG_ERROR(dhcp4_logger, DHCP4_SRV_CONSTRUCT_ERROR).arg(e.what());
        return;
    }

    // Initializing all observations with default value
    setPacketStatisticsDefaults();

    // All done, so can proceed
    shutdown_ = false;
}

void Dhcpv4Srv::setPacketStatisticsDefaults() {
    isc::stats::StatsMgr& stats_mgr = isc::stats::StatsMgr::instance();

    // Iterate over set of observed statistics
    for (auto const& it : dhcp4_statistics) {
        // Initialize them with default value 0
        stats_mgr.setValue(it, static_cast<int64_t>(0));
    }
}

Dhcpv4Srv::~Dhcpv4Srv() {
    // Discard any parked packets
    discardPackets();

    try {
        stopD2();
    } catch (const std::exception& ex) {
        // Highly unlikely, but lets Report it but go on
        LOG_ERROR(dhcp4_logger, DHCP4_SRV_D2STOP_ERROR).arg(ex.what());
    }

    try {
        Dhcp4to6Ipc::instance().close();
    } catch (const std::exception& ex) {
        // Highly unlikely, but lets Report it but go on
        LOG_ERROR(dhcp4_logger, DHCP4_SRV_DHCP4O6_ERROR).arg(ex.what());
    }

    IfaceMgr::instance().closeSockets();

    // The lease manager was instantiated during DHCPv4Srv configuration,
    // so we should clean up after ourselves.
    LeaseMgrFactory::destroy();

    // Destroy the host manager before hooks unload.
    HostMgr::create();

    // Explicitly unload hooks
    HooksManager::prepareUnloadLibraries();
    if (!HooksManager::unloadLibraries()) {
        auto names = HooksManager::getLibraryNames();
        std::string msg;
        if (!names.empty()) {
            msg = names[0];
            for (size_t i = 1; i < names.size(); ++i) {
                msg += std::string(", ") + names[i];
            }
        }
        LOG_ERROR(dhcp4_logger, DHCP4_SRV_UNLOAD_LIBRARIES_ERROR).arg(msg);
    }
    IOServiceMgr::instance().clearIOServices();
    io_service_->stopAndPoll();
}

void
Dhcpv4Srv::shutdown() {
    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_SHUTDOWN_REQUEST);
    shutdown_ = true;
}

isc::dhcp::ConstSubnet4Ptr
Dhcpv4Srv::selectSubnet(const Pkt4Ptr& query, bool& drop,
                        bool sanity_only, bool allow_answer_park) {

    // DHCPv4-over-DHCPv6 is a special (and complex) case
    if (query->isDhcp4o6()) {
        return (selectSubnet4o6(query, drop, sanity_only, allow_answer_park));
    }

    ConstSubnet4Ptr subnet;

    const SubnetSelector& selector = CfgSubnets4::initSelector(query);

    CfgMgr& cfgmgr = CfgMgr::instance();
    subnet = cfgmgr.getCurrentCfg()->getCfgSubnets4()->selectSubnet(selector);

    // Let's execute all callouts registered for subnet4_select
    // (skip callouts if the selectSubnet was called to do sanity checks only)
    if (!sanity_only &&
        HooksManager::calloutsPresent(Hooks.hook_index_subnet4_select_)) {
        CalloutHandlePtr callout_handle = getCalloutHandle(query);

        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        shared_ptr<ScopedCalloutHandleState> callout_handle_state(
            std::make_shared<ScopedCalloutHandleState>(callout_handle));

        // Enable copying options from the packet within hook library.
        ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);

        // Set new arguments
        callout_handle->setArgument("query4", query);
        callout_handle->setArgument("subnet4", subnet);
        callout_handle->setArgument("subnet4collection",
                                    cfgmgr.getCurrentCfg()->
                                    getCfgSubnets4()->getAll());

        auto const tpl(parkingLimitExceeded("subnet4_select"));
        bool const exceeded(get<0>(tpl));
        if (exceeded) {
            uint32_t const limit(get<1>(tpl));
            // We can't park it so we're going to throw it on the floor.
            LOG_DEBUG(packet4_logger, DBGLVL_PKT_HANDLING,
                      DHCP4_HOOK_SUBNET4_SELECT_PARKING_LOT_FULL)
                .arg(limit)
                .arg(query->getLabel());
            return (ConstSubnet4Ptr());
        }

        // We proactively park the packet.
        HooksManager::park(
            "subnet4_select", query, [this, query, allow_answer_park, callout_handle_state]() {
                if (MultiThreadingMgr::instance().getMode()) {
                    boost::shared_ptr<function<void()>> callback(
                        boost::make_shared<function<void()>>(
                            [this, query, allow_answer_park]() mutable {
                                processLocalizedQuery4AndSendResponse(query, allow_answer_park);
                            }));
                    callout_handle_state->on_completion_ = [callback]() {
                        MultiThreadingMgr::instance().getThreadPool().add(callback);
                    };
                } else {
                    processLocalizedQuery4AndSendResponse(query, allow_answer_park);
                }
            });

        // Call user (and server-side) callouts
        try {
            HooksManager::callCallouts(Hooks.hook_index_subnet4_select_,
                                       *callout_handle);
        } catch (...) {
            // Make sure we don't orphan a parked packet.
            HooksManager::drop("subnet4_select", query);
            throw;
        }

        // Callouts parked the packet. Same as drop but callouts will resume
        // processing or drop the packet later.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_PARK) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_HOOK_SUBNET4_SELECT_PARK)
                .arg(query->getLabel());
            drop = true;
            return (ConstSubnet4Ptr());
        } else {
            HooksManager::drop("subnet4_select", query);
        }

        // Callouts decided to skip this step. This means that no subnet
        // will be selected. Packet processing will continue, but it will
        // be severely limited (i.e. only global options will be assigned)
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_HOOK_SUBNET4_SELECT_SKIP)
                .arg(query->getLabel());
            return (ConstSubnet4Ptr());
        }

        // Callouts decided to drop the packet. It is a superset of the
        // skip case so no subnet will be selected.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_HOOK_SUBNET4_SELECT_DROP)
                .arg(query->getLabel());
            drop = true;
            return (ConstSubnet4Ptr());
        }

        // Use whatever subnet was specified by the callout
        callout_handle->getArgument("subnet4", subnet);
    }

    if (subnet) {
        // Log at higher debug level that subnet has been found.
        LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA, DHCP4_SUBNET_SELECTED)
            .arg(query->getLabel())
            .arg(subnet->getID());
        // Log detailed information about the selected subnet at the
        // lower debug level.
        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_SUBNET_DATA)
            .arg(query->getLabel())
            .arg(subnet->toText());

    } else {
        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL,
                  DHCP4_SUBNET_SELECTION_FAILED)
            .arg(query->getLabel());
    }

    return (subnet);
}

isc::dhcp::ConstSubnet4Ptr
Dhcpv4Srv::selectSubnet4o6(const Pkt4Ptr& query, bool& drop,
                           bool sanity_only, bool allow_answer_park) {
    ConstSubnet4Ptr subnet;

    SubnetSelector selector;
    selector.ciaddr_ = query->getCiaddr();
    selector.giaddr_ = query->getGiaddr();
    selector.local_address_ = query->getLocalAddr();
    selector.client_classes_ = query->classes_;
    selector.iface_name_ = query->getIface();
    // Mark it as DHCPv4-over-DHCPv6
    selector.dhcp4o6_ = true;
    // Now the DHCPv6 part
    selector.remote_address_ = query->getRemoteAddr();
    selector.first_relay_linkaddr_ = IOAddress("::");

    // Handle a DHCPv6 relayed query
    Pkt4o6Ptr query4o6 = boost::dynamic_pointer_cast<Pkt4o6>(query);
    if (!query4o6) {
        isc_throw(Unexpected, "Can't get DHCP4o6 message");
    }
    const Pkt6Ptr& query6 = query4o6->getPkt6();

    // Initialize fields specific to relayed messages.
    if (query6 && !query6->relay_info_.empty()) {
        for (auto const& relay : boost::adaptors::reverse(query6->relay_info_)) {
            if (!relay.linkaddr_.isV6Zero() &&
                !relay.linkaddr_.isV6LinkLocal()) {<--- Consider using std::find_if algorithm instead of a raw loop.
                selector.first_relay_linkaddr_ = relay.linkaddr_;
                break;
            }
        }
        selector.interface_id_ =
            query6->getAnyRelayOption(D6O_INTERFACE_ID,
                                      Pkt6::RELAY_GET_FIRST);
    }

    // If the Subnet Selection option is present, extract its value.
    OptionPtr sbnsel = query->getOption(DHO_SUBNET_SELECTION);
    if (sbnsel) {
        OptionCustomPtr oc = boost::dynamic_pointer_cast<OptionCustom>(sbnsel);
        if (oc) {
            selector.option_select_ = oc->readAddress();
        }
    }

    CfgMgr& cfgmgr = CfgMgr::instance();
    subnet = cfgmgr.getCurrentCfg()->getCfgSubnets4()->selectSubnet4o6(selector);

    // Let's execute all callouts registered for subnet4_select.
    // (skip callouts if the selectSubnet was called to do sanity checks only)
    if (!sanity_only &&
        HooksManager::calloutsPresent(Hooks.hook_index_subnet4_select_)) {
        CalloutHandlePtr callout_handle = getCalloutHandle(query);

        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        shared_ptr<ScopedCalloutHandleState> callout_handle_state(
            std::make_shared<ScopedCalloutHandleState>(callout_handle));

        // Enable copying options from the packet within hook library.
        ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);

        // Set new arguments
        callout_handle->setArgument("query4", query);
        callout_handle->setArgument("subnet4", subnet);
        callout_handle->setArgument("subnet4collection",
                                    cfgmgr.getCurrentCfg()->
                                    getCfgSubnets4()->getAll());

        auto const tpl(parkingLimitExceeded("subnet4_select"));
        bool const exceeded(get<0>(tpl));
        if (exceeded) {
            uint32_t const limit(get<1>(tpl));
            // We can't park it so we're going to throw it on the floor.
            LOG_DEBUG(packet4_logger, DBGLVL_PKT_HANDLING,
                      DHCP4_HOOK_SUBNET4_SELECT_4O6_PARKING_LOT_FULL)
                .arg(limit)
                .arg(query->getLabel());
            return (ConstSubnet4Ptr());
        }

        // We proactively park the packet.
        HooksManager::park(
            "subnet4_select", query, [this, query, allow_answer_park, callout_handle_state]() {
                if (MultiThreadingMgr::instance().getMode()) {
                    boost::shared_ptr<function<void()>> callback(
                        boost::make_shared<function<void()>>(
                            [this, query, allow_answer_park]() mutable {
                                processLocalizedQuery4AndSendResponse(query, allow_answer_park);
                            }));
                    callout_handle_state->on_completion_ = [callback]() {
                        MultiThreadingMgr::instance().getThreadPool().add(callback);
                    };
                } else {
                    processLocalizedQuery4AndSendResponse(query, allow_answer_park);
                }
            });

        // Call user (and server-side) callouts
        try {
            HooksManager::callCallouts(Hooks.hook_index_subnet4_select_,
                                       *callout_handle);
        } catch (...) {
            // Make sure we don't orphan a parked packet.
            HooksManager::drop("subnet4_select", query);
            throw;
        }

        // Callouts parked the packet. Same as drop but callouts will resume
        // processing or drop the packet later.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_PARK) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_HOOK_SUBNET4_SELECT_PARK)
                .arg(query->getLabel());
            drop = true;
            return (ConstSubnet4Ptr());
        } else {
            HooksManager::drop("subnet4_select", query);
        }

        // Callouts decided to skip this step. This means that no subnet
        // will be selected. Packet processing will continue, but it will
        // be severely limited (i.e. only global options will be assigned)
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_DHCP4O6_HOOK_SUBNET4_SELECT_SKIP)
                .arg(query->getLabel());
            return (ConstSubnet4Ptr());
        }

        // Callouts decided to drop the packet. It is a superset of the
        // skip case so no subnet will be selected.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_DHCP4O6_HOOK_SUBNET4_SELECT_DROP)
                .arg(query->getLabel());
            drop = true;
            return (ConstSubnet4Ptr());
        }

        // Use whatever subnet was specified by the callout
        callout_handle->getArgument("subnet4", subnet);
    }

    if (subnet) {
        // Log at higher debug level that subnet has been found.
        LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA,
                  DHCP4_DHCP4O6_SUBNET_SELECTED)
            .arg(query->getLabel())
            .arg(subnet->getID());
        // Log detailed information about the selected subnet at the
        // lower debug level.
        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA,
                  DHCP4_DHCP4O6_SUBNET_DATA)
            .arg(query->getLabel())
            .arg(subnet->toText());

    } else {
        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL,
                  DHCP4_DHCP4O6_SUBNET_SELECTION_FAILED)
            .arg(query->getLabel());
    }

    return (subnet);
}

Pkt4Ptr
Dhcpv4Srv::receivePacket(int timeout) {
    return (IfaceMgr::instance().receive4(timeout));
}

void
Dhcpv4Srv::sendPacket(const Pkt4Ptr& packet) {
    IfaceMgr::instance().send(packet);
}

void
Dhcpv4Srv::initContext0(const Pkt4Ptr& query,
                        AllocEngine::ClientContext4Ptr ctx) {
    // Pointer to client's query.
    ctx->query_ = query;

    // Hardware address.
    ctx->hwaddr_ = query->getHWAddr();
}

bool
Dhcpv4Srv::earlyGHRLookup(const Pkt4Ptr& query,
                          AllocEngine::ClientContext4Ptr ctx) {

    // First part of context initialization.
    initContext0(query, ctx);

    // Get the early-global-reservations-lookup flag value.
    data::ConstElementPtr egrl = CfgMgr::instance().getCurrentCfg()->
        getConfiguredGlobal(CfgGlobals::EARLY_GLOBAL_RESERVATIONS_LOOKUP);
    if (egrl) {
        ctx->early_global_reservations_lookup_ = egrl->boolValue();
    }

    // Perform early global reservations lookup when wanted.
    if (ctx->early_global_reservations_lookup_) {
        // Retrieve retrieve client identifier.
        OptionPtr opt_clientid = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
        if (opt_clientid) {
            ctx->clientid_.reset(new ClientId(opt_clientid->getData()));
        }

        // Get the host identifiers.
        Dhcpv4Exchange::setHostIdentifiers(ctx);

        // Check for global host reservations.
        ConstHostPtr global_host = alloc_engine_->findGlobalReservation(*ctx);

        if (global_host && !global_host->getClientClasses4().empty()) {
            // Remove dependent evaluated classes.
            Dhcpv4Exchange::removeDependentEvaluatedClasses(query);

            // Add classes from the global reservations.
            const ClientClasses& classes = global_host->getClientClasses4();
            for (auto const& cclass : classes) {
                query->addClass(cclass);
            }

            // Evaluate classes before KNOWN.
            Dhcpv4Exchange::evaluateClasses(query, false);
        }

        if (global_host) {
            // Add the KNOWN class;
            query->addClass("KNOWN");
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
                .arg(query->getLabel())
                .arg("KNOWN");

            // Evaluate classes after KNOWN.
            Dhcpv4Exchange::evaluateClasses(query, true);

            // Check the DROP special class.
            if (query->inClass("DROP")) {
                LOG_DEBUG(packet4_logger, DBGLVL_PKT_HANDLING,
                          DHCP4_PACKET_DROP_0014)
                    .arg(query->getHWAddrLabel())
                    .arg(query->toText());
                isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                          static_cast<int64_t>(1));
                return (false);
            }

            // Store the reservation.
            ctx->hosts_[SUBNET_ID_GLOBAL] = global_host;
        }
    }

    return (true);
}

int
Dhcpv4Srv::run() {
#ifdef HAVE_AFL
    // Get the values of the environment variables used to control the
    // fuzzing.

    // Specfies the interface to be used to pass packets from AFL to Kea.
    const char* interface = getenv("KEA_AFL_INTERFACE");
    if (!interface) {
        isc_throw(FuzzInitFail, "no fuzzing interface has been set");
    }

    // The address on the interface to be used.
    const char* address = getenv("KEA_AFL_ADDRESS");
    if (!address) {
        isc_throw(FuzzInitFail, "no fuzzing address has been set");
    }

    // Set up structures needed for fuzzing.
    PacketFuzzer fuzzer(server_port_, interface, address);

    // The next line is needed as a signature for AFL to recognize that we are
    // running persistent fuzzing.  This has to be in the main image file.
    while (__AFL_LOOP(fuzzer.maxLoopCount())) {
        // Read from stdin and put the data read into an address/port on which
        // Kea is listening, read for Kea to read it via asynchronous I/O.
        fuzzer.transfer();
#else
    while (!shutdown_) {
#endif // HAVE_AFL
        try {
            runOne();
            // Handle events registered by hooks using external IOService objects.
            IOServiceMgr::instance().pollIOServices();
            getIOService()->poll();
            config::HttpCommandMgr::instance().garbageCollectListeners();
        } catch (const std::exception& e) {
            // General catch-all exception that are not caught by more specific
            // catches. This one is for exceptions derived from std::exception.
            LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_STD_EXCEPTION_MAIN)
                .arg(e.what());
        } catch (...) {
            // General catch-all exception that are not caught by more specific
            // catches. This one is for other exceptions, not derived from
            // std::exception.
            LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION_MAIN);
        }
    }

    // Stop everything before we change into single-threaded mode.
    MultiThreadingCriticalSection cs;

    // destroying the thread pool
    MultiThreadingMgr::instance().apply(false, 0, 0);

    return (getExitValue());
}

void
Dhcpv4Srv::runOne() {
    // client's message and server's response
    Pkt4Ptr query;

    try {
        // Set select() timeout to 1s. This value should not be modified
        // because it is important that the select() returns control
        // frequently so as the IOService can be polled for ready handlers.
        uint32_t timeout = 1;
        query = receivePacket(timeout);

        // Log if packet has arrived. We can't log the detailed information
        // about the DHCP message because it hasn't been unpacked/parsed
        // yet, and it can't be parsed at this point because hooks will
        // have to process it first. The only information available at this
        // point are: the interface, source address and destination addresses
        // and ports.
        if (query) {
            LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC, DHCP4_BUFFER_RECEIVED)
                .arg(query->getRemoteAddr().toText())
                .arg(query->getRemotePort())
                .arg(query->getLocalAddr().toText())
                .arg(query->getLocalPort())
                .arg(query->getIface());
        }

        // We used to log that the wait was interrupted, but this is no longer
        // the case. Our wait time is 1s now, so the lack of query packet more
        // likely means that nothing new appeared within a second, rather than
        // we were interrupted. And we don't want to print a message every
        // second.

    } catch (const SignalInterruptOnSelect&) {
        // Packet reception interrupted because a signal has been received.
        // This is not an error because we might have received a SIGTERM,
        // SIGINT, SIGHUP or SIGCHLD which are handled by the server. For
        // signals that are not handled by the server we rely on the default
        // behavior of the system.
        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL, DHCP4_BUFFER_WAIT_SIGNAL);
    } catch (const std::exception& e) {
        // Log all other errors.
        LOG_ERROR(packet4_logger, DHCP4_BUFFER_RECEIVE_FAIL)
            .arg(e.what());
    }

    // Timeout may be reached or signal received, which breaks select()
    // with no reception occurred. No need to log anything here because
    // we have logged right after the call to receivePacket().
    if (!query) {
        return;
    }

    // If the DHCP service has been globally disabled, drop the packet.
    if (!network_state_->isServiceEnabled()) {
        LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0008)
            .arg(query->getLabel());
        return;
    } else {
        if (MultiThreadingMgr::instance().getMode()) {
            query->addPktEvent("mt_queued");
            typedef function<void()> CallBack;
            boost::shared_ptr<CallBack> call_back =
                boost::make_shared<CallBack>(std::bind(&Dhcpv4Srv::processPacketAndSendResponseNoThrow,
                                                       this, query));
            if (!MultiThreadingMgr::instance().getThreadPool().add(call_back)) {
                LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_PACKET_QUEUE_FULL);
            }
        } else {
            processPacketAndSendResponse(query);
        }
    }
}

void
Dhcpv4Srv::processPacketAndSendResponseNoThrow(Pkt4Ptr query) {
    try {
        processPacketAndSendResponse(query);
    } catch (const std::exception& e) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_STD_EXCEPTION)
            .arg(query->getLabel())
            .arg(e.what());
    } catch (...) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION).arg(query->getLabel());
    }
}

void
Dhcpv4Srv::processPacketAndSendResponse(Pkt4Ptr query) {
    Pkt4Ptr rsp = processPacket(query);
    if (!rsp) {
        return;
    }

    CalloutHandlePtr callout_handle = getCalloutHandle(query);

    processPacketBufferSend(callout_handle, rsp);
}

Pkt4Ptr
Dhcpv4Srv::processPacket(Pkt4Ptr query, bool allow_answer_park) {
    query->addPktEvent("process_started");

    // All packets belong to ALL.
    query->addClass("ALL");

    // Log reception of the packet. We need to increase it early, as any
    // failures in unpacking will cause the packet to be dropped. We
    // will increase type specific statistic further down the road.
    // See processStatsReceived().
    isc::stats::StatsMgr::instance().addValue("pkt4-received",
                                              static_cast<int64_t>(1));

    bool skip_unpack = false;

    // The packet has just been received so contains the uninterpreted wire
    // data; execute callouts registered for buffer4_receive.
    if (HooksManager::calloutsPresent(Hooks.hook_index_buffer4_receive_)) {
        CalloutHandlePtr callout_handle = getCalloutHandle(query);

        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        ScopedCalloutHandleState callout_handle_state(callout_handle);

        // Enable copying options from the packet within hook library.
        ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);

        // Pass incoming packet as argument
        callout_handle->setArgument("query4", query);

        // Call callouts
        HooksManager::callCallouts(Hooks.hook_index_buffer4_receive_,
                                   *callout_handle);

        // Callouts decided to drop the received packet.
        // The response (rsp) is null so the caller (runOne) will
        // immediately return too.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
            LOG_DEBUG(hooks_logger, DBGLVL_PKT_HANDLING,
                      DHCP4_HOOK_BUFFER_RCVD_DROP)
                .arg(query->getRemoteAddr().toText())
                .arg(query->getLocalAddr().toText())
                .arg(query->getIface());
            return (Pkt4Ptr());;
        }

        // Callouts decided to skip the next processing step. The next
        // processing step would be to parse the packet, so skip at this
        // stage means that callouts did the parsing already, so server
        // should skip parsing.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_DETAIL,
                      DHCP4_HOOK_BUFFER_RCVD_SKIP)
                .arg(query->getRemoteAddr().toText())
                .arg(query->getLocalAddr().toText())
                .arg(query->getIface());
            skip_unpack = true;
        }

        callout_handle->getArgument("query4", query);
    }

    // Unpack the packet information unless the buffer4_receive callouts
    // indicated they did it
    if (!skip_unpack) {
        try {
            LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL, DHCP4_BUFFER_UNPACK)
                .arg(query->getRemoteAddr().toText())
                .arg(query->getLocalAddr().toText())
                .arg(query->getIface());
            query->unpack();
        } catch (const SkipRemainingOptionsError& e) {
            // An option failed to unpack but we are to attempt to process it
            // anyway.  Log it and let's hope for the best.
            LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL,
                      DHCP4_PACKET_OPTIONS_SKIPPED)
                .arg(query->getLabel())
                .arg(e.what());
        } catch (const std::exception& e) {
            // Failed to parse the packet.
            LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0001)
                .arg(query->getLabel())
                .arg(query->getRemoteAddr().toText())
                .arg(query->getLocalAddr().toText())
                .arg(query->getIface())
                .arg(e.what())
                .arg(query->getHWAddrLabel());

            // Increase the statistics of parse failures and dropped packets.
            isc::stats::StatsMgr::instance().addValue("pkt4-parse-failed",
                                                      static_cast<int64_t>(1));
            isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                      static_cast<int64_t>(1));
            return (Pkt4Ptr());
        }
    }

    // Classify can emit INFO logs so help to track the query.
    LOG_INFO(dhcp4_logger, DHCP4_QUERY_LABEL)
        .arg(query->getLabel());

    // Update statistics accordingly for received packet.
    processStatsReceived(query);

    // Recover stashed RAI from client address lease.
    try {
        recoverStashedAgentOption(query);
    } catch (const std::exception&) {
        // Ignore exceptions.
    }

    // Assign this packet to one or more classes if needed. We need to do
    // this before calling accept(), because getSubnet4() may need client
    // class information.
    classifyPacket(query);

    // Now it is classified the deferred unpacking can be done.
    deferredUnpack(query);

    // Check whether the message should be further processed or discarded.
    // There is no need to log anything here. This function logs by itself.
    if (!accept(query)) {
        // Increase the statistic of dropped packets.
        isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                  static_cast<int64_t>(1));
        return (Pkt4Ptr());
    }

    // We have sanity checked (in accept() that the Message Type option
    // exists, so we can safely get it here.
    int type = query->getType();
    LOG_INFO(packet4_logger, DHCP4_PACKET_RECEIVED)
        .arg(query->getLabel())
        .arg(query->getName())
        .arg(type)
        .arg(query->getRemoteAddr())
        .arg(query->getLocalAddr())
        .arg(query->getIface());
    LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_QUERY_DATA)
        .arg(query->getLabel())
        .arg(query->toText());

    // Let's execute all callouts registered for pkt4_receive
    if (HooksManager::calloutsPresent(Hooks.hook_index_pkt4_receive_)) {
        CalloutHandlePtr callout_handle = getCalloutHandle(query);

        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        ScopedCalloutHandleState callout_handle_state(callout_handle);

        // Enable copying options from the packet within hook library.
        ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);

        // Pass incoming packet as argument
        callout_handle->setArgument("query4", query);

        // Call callouts
        HooksManager::callCallouts(Hooks.hook_index_pkt4_receive_,
                                   *callout_handle);

        // Callouts decided to skip the next processing step. The next
        // processing step would be to process the packet, so skip at this
        // stage means drop.
        if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
            (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                      DHCP4_HOOK_PACKET_RCVD_SKIP)
                .arg(query->getLabel());
            return (Pkt4Ptr());
        }

        callout_handle->getArgument("query4", query);
    }

    // Check the DROP special class.
    if (query->inClass("DROP")) {
        LOG_DEBUG(packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0010)
            .arg(query->getHWAddrLabel())
            .arg(query->toText());
        isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                  static_cast<int64_t>(1));
        return (Pkt4Ptr());
    }

    return (processDhcp4Query(query, allow_answer_park));
}

void
Dhcpv4Srv::processDhcp4QueryAndSendResponse(Pkt4Ptr query,
                                            bool allow_answer_park) {
    try {
        Pkt4Ptr rsp = processDhcp4Query(query, allow_answer_park);
        if (!rsp) {
            return;
        }

        CalloutHandlePtr callout_handle = getCalloutHandle(query);
        processPacketBufferSend(callout_handle, rsp);
    } catch (const std::exception& e) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_STD_EXCEPTION)
            .arg(query->getLabel())
            .arg(e.what());
    } catch (...) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION).arg(query->getLabel());
    }
}

Pkt4Ptr
Dhcpv4Srv::processDhcp4Query(Pkt4Ptr query, bool allow_answer_park) {
    // Create a client race avoidance RAII handler.
    ClientHandler client_handler;

    // Check for lease modifier queries from the same client being processed.
    if (MultiThreadingMgr::instance().getMode() &&
        ((query->getType() == DHCPDISCOVER) ||
         (query->getType() == DHCPREQUEST) ||
         (query->getType() == DHCPRELEASE) ||
         (query->getType() == DHCPDECLINE))) {
        ContinuationPtr cont =
            makeContinuation(std::bind(&Dhcpv4Srv::processDhcp4QueryAndSendResponse,
                                       this, query, allow_answer_park));
        if (!client_handler.tryLock(query, cont)) {
            return (Pkt4Ptr());
        }
    }

    AllocEngine::ClientContext4Ptr ctx(new AllocEngine::ClientContext4());
    if (!earlyGHRLookup(query, ctx)) {
        return (Pkt4Ptr());
    }

    try {
        sanityCheck(query);
        if ((query->getType() == DHCPDISCOVER) ||
            (query->getType() == DHCPREQUEST) ||
            (query->getType() == DHCPINFORM)) {
            bool drop = false;
            ctx->subnet_ = selectSubnet(query, drop, false, allow_answer_park);
            // Stop here if selectSubnet decided to drop the packet
            if (drop) {
                return (Pkt4Ptr());
            }
        }
    } catch (const std::exception& e) {

        // Catch-all exception (we used to call only isc::Exception, but
        // std::exception could potentially be raised and if we don't catch
        // it here, it would be caught in main() and the process would
        // terminate).  Just log the problem and ignore the packet.
        // (The problem is logged as a debug message because debug is
        // disabled by default - it prevents a DDOS attack based on the
        // sending of problem packets.)
        LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0007)
            .arg(query->getLabel())
            .arg(e.what());

        // Increase the statistic of dropped packets.
        isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                  static_cast<int64_t>(1));
    }

    return (processLocalizedQuery4(ctx, allow_answer_park));
}

void
Dhcpv4Srv::processLocalizedQuery4AndSendResponse(Pkt4Ptr query,
                                                 AllocEngine::ClientContext4Ptr& ctx,
                                                 bool allow_answer_park) {
    try {
        Pkt4Ptr rsp = processLocalizedQuery4(ctx, allow_answer_park);
        if (!rsp) {
            return;
        }

        CalloutHandlePtr callout_handle = getCalloutHandle(query);

        processPacketBufferSend(callout_handle, rsp);
    } catch (const std::exception& e) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_STD_EXCEPTION)
            .arg(query->getLabel())
            .arg(e.what());
    } catch (...) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION).arg(query->getLabel());
    }
}

void
Dhcpv4Srv::processLocalizedQuery4AndSendResponse(Pkt4Ptr query,
                                                 bool allow_answer_park) {
    // Initialize context.
    AllocEngine::ClientContext4Ptr ctx(new AllocEngine::ClientContext4());
    initContext0(query, ctx);

    // Subnet is cached in the callout context associated to the query.
    try {
        CalloutHandlePtr callout_handle = getCalloutHandle(query);
        callout_handle->getContext("subnet4", ctx->subnet_);
    } catch (const Exception&) {
        // No subnet, leave it to null...
    }

    processLocalizedQuery4AndSendResponse(query, ctx, allow_answer_park);
}

Pkt4Ptr
Dhcpv4Srv::processLocalizedQuery4(AllocEngine::ClientContext4Ptr& ctx,
                                  bool allow_answer_park) {
    if (!ctx) {
        isc_throw(Unexpected, "null context");
    }
    Pkt4Ptr query = ctx->query_;
    Pkt4Ptr rsp;
    try {
        switch (query->getType()) {
        case DHCPDISCOVER:
            rsp = processDiscover(query, ctx);
            break;

        case DHCPREQUEST:
            // Note that REQUEST is used for many things in DHCPv4: for
            // requesting new leases, renewing existing ones and even
            // for rebinding.
            rsp = processRequest(query, ctx);
            break;

        case DHCPRELEASE:
            processRelease(query, ctx);
            break;

        case DHCPDECLINE:
            processDecline(query, ctx);
            break;

        case DHCPINFORM:
            rsp = processInform(query, ctx);
            break;

        default:
            // Only action is to output a message if debug is enabled,
            // and that is covered by the debug statement before the
            // "switch" statement.
            ;
        }
    } catch (const std::exception& e) {

        // Catch-all exception (we used to call only isc::Exception, but
        // std::exception could potentially be raised and if we don't catch
        // it here, it would be caught in main() and the process would
        // terminate).  Just log the problem and ignore the packet.
        // (The problem is logged as a debug message because debug is
        // disabled by default - it prevents a DDOS attack based on the
        // sending of problem packets.)
        LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0007)
            .arg(query->getLabel())
            .arg(e.what());

        // Increase the statistic of dropped packets.
        isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                  static_cast<int64_t>(1));
    }

    CalloutHandlePtr callout_handle = getCalloutHandle(query);
    if (ctx) {
        // leases4_committed and lease4_offer callouts are treated in the same way,
        // so prepare correct set of variables basing on the packet context.
        int hook_idx = Hooks.hook_index_leases4_committed_;
        std::string hook_label = "leases4_committed";
        MessageID pkt_park_msg = DHCP4_HOOK_LEASES4_COMMITTED_PARK;
        MessageID pkt_drop_msg = DHCP4_HOOK_LEASES4_COMMITTED_DROP;
        MessageID parking_lot_full_msg = DHCP4_HOOK_LEASES4_COMMITTED_PARKING_LOT_FULL;
        if (ctx->fake_allocation_) {
            hook_idx = Hooks.hook_index_lease4_offer_;
            hook_label = "lease4_offer";
            pkt_park_msg = DHCP4_HOOK_LEASE4_OFFER_PARK;
            pkt_drop_msg = DHCP4_HOOK_LEASE4_OFFER_DROP;
            parking_lot_full_msg = DHCP4_HOOK_LEASE4_OFFER_PARKING_LOT_FULL;
        }

        if (HooksManager::calloutsPresent(hook_idx)) {
            // The ScopedCalloutHandleState class which guarantees that the task
            // is added to the thread pool after the response is reset (if needed)
            // and CalloutHandle state is reset. In ST it does nothing.
            // A smart pointer is used to store the ScopedCalloutHandleState so that
            // a copy of the pointer is created by the lambda and only on the
            // destruction of the last reference the task is added.
            // In MT there are 2 cases:
            // 1. packet is unparked before current thread smart pointer to
            //    ScopedCalloutHandleState is destroyed:
            //  - the lambda uses the smart pointer to set the callout which adds the
            //    task, but the task is added after ScopedCalloutHandleState is
            //    destroyed, on the destruction of the last reference which is held
            //    by the current thread.
            // 2. packet is unparked after the current thread smart pointer to
            //    ScopedCalloutHandleState is destroyed:
            //  - the current thread reference to ScopedCalloutHandleState is
            //    destroyed, but the reference in the lambda keeps it alive until
            //    the lambda is called and the last reference is released, at which
            //    time the task is actually added.
            // Use the RAII wrapper to make sure that the callout handle state is
            // reset when this object goes out of scope. All hook points must do
            // it to prevent possible circular dependency between the callout
            // handle and its arguments.
            std::shared_ptr<ScopedCalloutHandleState> callout_handle_state =
                std::make_shared<ScopedCalloutHandleState>(callout_handle);

            ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);

            // Also pass the corresponding query packet as argument
            callout_handle->setArgument("query4", query);

            Lease4CollectionPtr new_leases(new Lease4Collection());
            // Filter out the new lease if it was reused so not committed.
            if (ctx->new_lease_ && (ctx->new_lease_->reuseable_valid_lft_ == 0)) {
                new_leases->push_back(ctx->new_lease_);
            }
            callout_handle->setArgument("leases4", new_leases);

            if (ctx->fake_allocation_) {
                // Arguments required only for lease4_offer callout.
                callout_handle->setArgument("offer_lifetime", ctx->offer_lft_);
                callout_handle->setArgument("old_lease", ctx->old_lease_);
            } else {
                // Arguments required only for leases4_committed callout.
                Lease4CollectionPtr deleted_leases(new Lease4Collection());
                if (ctx->old_lease_) {
                    if ((!ctx->new_lease_) || (ctx->new_lease_->addr_ != ctx->old_lease_->addr_)) {
                            deleted_leases->push_back(ctx->old_lease_);
                    }
                }
                callout_handle->setArgument("deleted_leases4", deleted_leases);
            }

            if (allow_answer_park) {
                auto const tpl(parkingLimitExceeded(hook_label));
                bool const exceeded(get<0>(tpl));
                if (exceeded) {
                    uint32_t const limit(get<1>(tpl));
                    // We can't park it so we're going to throw it on the floor.
                    LOG_DEBUG(packet4_logger, DBGLVL_PKT_HANDLING, parking_lot_full_msg)
                        .arg(limit)
                        .arg(query->getLabel());
                    isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                                static_cast<int64_t>(1));
                    return (Pkt4Ptr());
                }

                // We proactively park the packet. We'll unpark it without invoking
                // the callback (i.e. drop) unless the callout status is set to
                // NEXT_STEP_PARK.  Otherwise the callback we bind here will be
                // executed when the hook library unparks the packet.
                HooksManager::park(
                    hook_label, query,
                    [this, callout_handle, query, rsp, callout_handle_state, hook_idx, ctx]() mutable {
                        if (hook_idx == Hooks.hook_index_lease4_offer_) {
                            bool offer_address_in_use = false;<--- Assignment 'offer_address_in_use=false', assigned value is 0
                            try {
                                callout_handle->getArgument("offer_address_in_use", offer_address_in_use);
                            } catch (const NoSuchArgument& ex) {
                                LOG_ERROR(hooks_logger, DHCP4_HOOK_LEASE4_OFFER_ARGUMENT_MISSING)
                                          .arg(query->getLabel())
                                          .arg(ex.what());
                            }

                            if (offer_address_in_use) {<--- Condition 'offer_address_in_use' is always false
                                Lease4Ptr lease = ctx->new_lease_;
                                bool lease_exists = (ctx->offer_lft_ > 0);
                                if (MultiThreadingMgr::instance().getMode()) {
                                    typedef function<void()> CallBack;
                                    // We need to pass in the lease and flag as the callback handle state
                                    // gets reset prior to the invocation of the on_completion_ callback.
                                    boost::shared_ptr<CallBack> call_back = boost::make_shared<CallBack>(
                                        std::bind(&Dhcpv4Srv::serverDeclineNoThrow, this,
                                                  callout_handle, query, lease, lease_exists));
                                    callout_handle_state->on_completion_ = [call_back]() {
                                        MultiThreadingMgr::instance().getThreadPool().add(call_back);
                                    };
                                } else {
                                    serverDecline(callout_handle, query, lease, lease_exists);
                                }

                                return;
                            }
                        }

                        // Send the response to the client.
                        if (MultiThreadingMgr::instance().getMode()) {
                            typedef function<void()> CallBack;
                            boost::shared_ptr<CallBack> call_back = boost::make_shared<CallBack>(
                                std::bind(&Dhcpv4Srv::sendResponseNoThrow, this, callout_handle,
                                          query, rsp, ctx->subnet_));
                            callout_handle_state->on_completion_ = [call_back]() {
                                MultiThreadingMgr::instance().getThreadPool().add(call_back);
                            };
                        } else {
                            processPacketPktSend(callout_handle, query, rsp, ctx->subnet_);
                            processPacketBufferSend(callout_handle, rsp);
                        }
                    });
            }

            try {
                // Call all installed callouts
                HooksManager::callCallouts(hook_idx, *callout_handle);
            } catch (...) {
                // Make sure we don't orphan a parked packet.
                if (allow_answer_park) {
                    HooksManager::drop(hook_label, query);
                }

                throw;
            }

            if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_PARK) &&
                allow_answer_park) {
                LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS, pkt_park_msg)
                    .arg(query->getLabel());
                // Since the hook library(ies) are going to do the unparking, then
                // reset the pointer to the response to indicate to the caller that
                // it should return, as the packet processing will continue via
                // the callback.
                rsp.reset();
            } else {
                // Drop the park job on the packet, it isn't needed.
                HooksManager::drop(hook_label, query);
                if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
                    LOG_DEBUG(hooks_logger, DBGLVL_PKT_HANDLING, pkt_drop_msg)
                        .arg(query->getLabel());
                    rsp.reset();
                }
            }
        }
    }

    // If we have a response prep it for shipment.
    if (rsp) {
        ConstSubnet4Ptr subnet = (ctx ? ctx->subnet_ : ConstSubnet4Ptr());
        processPacketPktSend(callout_handle, query, rsp, subnet);
    }
    return (rsp);
}

void
Dhcpv4Srv::sendResponseNoThrow(hooks::CalloutHandlePtr& callout_handle,
                               Pkt4Ptr& query, Pkt4Ptr& rsp,
                               ConstSubnet4Ptr& subnet) {
    try {
            processPacketPktSend(callout_handle, query, rsp, subnet);
            processPacketBufferSend(callout_handle, rsp);
        } catch (const std::exception& e) {
            LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_STD_EXCEPTION)
                .arg(query->getLabel())
                .arg(e.what());
    } catch (...) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION).arg(query->getLabel());
    }
}

void
Dhcpv4Srv::processPacketPktSend(hooks::CalloutHandlePtr& callout_handle,
                                Pkt4Ptr& query, Pkt4Ptr& rsp,
                                ConstSubnet4Ptr& subnet) {
    query->addPktEvent("process_completed");
    if (!rsp) {
        return;
    }

    // Specifies if server should do the packing
    bool skip_pack = false;

    // Execute all callouts registered for pkt4_send
    if (HooksManager::calloutsPresent(Hooks.hook_index_pkt4_send_)) {

        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        ScopedCalloutHandleState callout_handle_state(callout_handle);

        // Enable copying options from the query and response packets within
        // hook library.
        ScopedEnableOptionsCopy<Pkt4> query_resp_options_copy(query, rsp);

        // Pass incoming packet as argument
        callout_handle->setArgument("query4", query);

        // Set our response
        callout_handle->setArgument("response4", rsp);

        // Pass in the selected subnet.
        callout_handle->setArgument("subnet4", subnet);

        // Call all installed callouts
        HooksManager::callCallouts(Hooks.hook_index_pkt4_send_,
                                   *callout_handle);

        // Callouts decided to skip the next processing step. The next
        // processing step would be to pack the packet (create wire data).
        // That step will be skipped if any callout sets skip flag.
        // It essentially means that the callout already did packing,
        // so the server does not have to do it again.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
            LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS, DHCP4_HOOK_PACKET_SEND_SKIP)
                .arg(query->getLabel());
            skip_pack = true;
        }

        /// Callouts decided to drop the packet.
        if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
            LOG_DEBUG(hooks_logger, DBGLVL_PKT_HANDLING, DHCP4_HOOK_PACKET_SEND_DROP)
                .arg(rsp->getLabel());
            rsp.reset();
            return;
        }
    }

    if (!skip_pack) {
        try {
            LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_PACK)
                .arg(rsp->getLabel());
            rsp->pack();
        } catch (const std::exception& e) {
            LOG_ERROR(options4_logger, DHCP4_PACKET_PACK_FAIL)
                .arg(rsp->getLabel())
                .arg(e.what());
        }
    }
}

void
Dhcpv4Srv::processPacketBufferSend(CalloutHandlePtr& callout_handle,
                                   Pkt4Ptr& rsp) {
    if (!rsp) {
        return;
    }

    try {
        // Now all fields and options are constructed into output wire buffer.
        // Option objects modification does not make sense anymore. Hooks
        // can only manipulate wire buffer at this stage.
        // Let's execute all callouts registered for buffer4_send
        if (HooksManager::calloutsPresent(Hooks.hook_index_buffer4_send_)) {

            // Use the RAII wrapper to make sure that the callout handle state is
            // reset when this object goes out of scope. All hook points must do
            // it to prevent possible circular dependency between the callout
            // handle and its arguments.
            ScopedCalloutHandleState callout_handle_state(callout_handle);

            // Enable copying options from the packet within hook library.
            ScopedEnableOptionsCopy<Pkt4> resp4_options_copy(rsp);

            // Pass incoming packet as argument
            callout_handle->setArgument("response4", rsp);

            // Call callouts
            HooksManager::callCallouts(Hooks.hook_index_buffer4_send_,
                                       *callout_handle);

            // Callouts decided to skip the next processing step. The next
            // processing step would be to parse the packet, so skip at this
            // stage means drop.
            if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
                (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
                LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
                          DHCP4_HOOK_BUFFER_SEND_SKIP)
                    .arg(rsp->getLabel());
                return;
            }

            callout_handle->getArgument("response4", rsp);
        }

        LOG_INFO(packet4_logger, DHCP4_PACKET_SEND)
            .arg(rsp->getLabel())
            .arg(rsp->getName())
            .arg(static_cast<int>(rsp->getType()))
            .arg(rsp->getLocalAddr().isV4Zero() ? "*" : rsp->getLocalAddr().toText())
            .arg(rsp->getLocalPort())
            .arg(rsp->getRemoteAddr())
            .arg(rsp->getRemotePort())
            .arg(rsp->getIface().empty() ? "to be determined from routing" :
                 rsp->getIface());

        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA,
                  DHCP4_RESPONSE_DATA)
            .arg(rsp->getLabel())
            .arg(rsp->getName())
            .arg(static_cast<int>(rsp->getType()))
            .arg(rsp->toText());
        sendPacket(rsp);

        // Update statistics accordingly for sent packet.
        processStatsSent(rsp);

    } catch (const std::exception& e) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_SEND_FAIL)
            .arg(rsp->getLabel())
            .arg(e.what());
    }
}

string
Dhcpv4Srv::srvidToString(const OptionPtr& srvid) {
    if (!srvid) {
        isc_throw(BadValue, "NULL pointer passed to srvidToString()");
    }
    boost::shared_ptr<Option4AddrLst> generated =
        boost::dynamic_pointer_cast<Option4AddrLst>(srvid);
    if (!srvid) {
        isc_throw(BadValue, "Pointer to invalid option passed to srvidToString()");
    }

    Option4AddrLst::AddressContainer addrs = generated->getAddresses();
    if (addrs.size() != 1) {
        isc_throw(BadValue, "Malformed option passed to srvidToString(). "
                  << "Expected to contain a single IPv4 address.");
    }

    return (addrs[0].toText());
}

void
Dhcpv4Srv::appendServerID(Dhcpv4Exchange& ex) {<--- Parameter 'ex' can be declared as reference to const

    // Do not append generated server identifier if there is one appended already.
    // This is when explicitly configured server identifier option is present.
    if (ex.getResponse()->getOption(DHO_DHCP_SERVER_IDENTIFIER)) {
        return;
    }

    // Use local address on which the packet has been received as a
    // server identifier. In some cases it may be a different address,
    // e.g. broadcast packet or DHCPv4o6 packet.
    IOAddress local_addr = ex.getQuery()->getLocalAddr();
    Pkt4Ptr query = ex.getQuery();

    if (local_addr.isV4Bcast() || query->isDhcp4o6()) {
        local_addr = IfaceMgr::instance().getSocket(query).addr_;
    }

    static const OptionDefinition& server_id_def = LibDHCP::DHO_DHCP_SERVER_IDENTIFIER_DEF();
    OptionCustomPtr opt_srvid(new OptionCustom(server_id_def, Option::V4));
    opt_srvid->writeAddress(local_addr);
    ex.getResponse()->addOption(opt_srvid);
}

void
Dhcpv4Srv::buildCfgOptionList(Dhcpv4Exchange& ex) {
    CfgOptionList& co_list = ex.getCfgOptionList();

    // Retrieve subnet.
    ConstSubnet4Ptr subnet = ex.getContext()->subnet_;
    if (!subnet) {
        // All methods using the CfgOptionList object return soon when
        // there is no subnet so do the same
        return;
    }

    // Firstly, host specific options.
    const ConstHostPtr& host = ex.getContext()->currentHost();
    if (host && !host->getCfgOption4()->empty()) {
        co_list.push_back(host->getCfgOption4());
    }

    // Secondly, pool specific options.
    Pkt4Ptr resp = ex.getResponse();
    IOAddress addr = IOAddress::IPV4_ZERO_ADDRESS();
    if (resp) {
        addr = resp->getYiaddr();
    }
    if (!addr.isV4Zero()) {
        PoolPtr pool = subnet->getPool(Lease::TYPE_V4, addr, false);
        if (pool && !pool->getCfgOption()->empty()) {
            co_list.push_back(pool->getCfgOption());
        }
    }

    // Thirdly, subnet configured options.
    if (!subnet->getCfgOption()->empty()) {
        co_list.push_back(subnet->getCfgOption());
    }

    // Fourthly, shared network specific options.
    SharedNetwork4Ptr network;
    subnet->getSharedNetwork(network);
    if (network && !network->getCfgOption()->empty()) {
        co_list.push_back(network->getCfgOption());
    }

    // Each class in the incoming packet
    const ClientClasses& classes = ex.getQuery()->getClasses();
    for (auto const& cclass : classes) {
        // Find the client class definition for this class
        const ClientClassDefPtr& ccdef = CfgMgr::instance().getCurrentCfg()->
            getClientClassDictionary()->findClass(cclass);
        if (!ccdef) {
            // Not found: the class is built-in or not configured
            if (!isClientClassBuiltIn(cclass)) {
                LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_UNCONFIGURED)
                    .arg(ex.getQuery()->getLabel())
                    .arg(cclass);
            }
            // Skip it
            continue;
        }

        if (ccdef->getCfgOption()->empty()) {
            // Skip classes which don't configure options
            continue;
        }

        co_list.push_back(ccdef->getCfgOption());
    }

    // Last global options
    if (!CfgMgr::instance().getCurrentCfg()->getCfgOption()->empty()) {
        co_list.push_back(CfgMgr::instance().getCurrentCfg()->getCfgOption());
    }
}

void
Dhcpv4Srv::appendRequestedOptions(Dhcpv4Exchange& ex) {
    // Get the subnet relevant for the client. We will need it
    // to get the options associated with it.
    ConstSubnet4Ptr subnet = ex.getContext()->subnet_;
    // If we can't find the subnet for the client there is no way
    // to get the options to be sent to a client. We don't log an
    // error because it will be logged by the assignLease method
    // anyway.
    if (!subnet) {
        return;
    }

    // Unlikely short cut
    const CfgOptionList& co_list = ex.getCfgOptionList();
    if (co_list.empty()) {
        return;
    }

    Pkt4Ptr query = ex.getQuery();
    Pkt4Ptr resp = ex.getResponse();
    set<uint8_t> requested_opts;

    // try to get the 'Parameter Request List' option which holds the
    // codes of requested options.
    OptionUint8ArrayPtr option_prl = boost::dynamic_pointer_cast<
        OptionUint8Array>(query->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));

    // Get the list of options that client requested.
    if (option_prl) {
        for (uint16_t code : option_prl->getValues()) {
            static_cast<void>(requested_opts.insert(code));
        }
    }

    std::set<uint8_t> cancelled_opts;
    const auto& cclasses = query->getClasses();

    // Iterate on the configured option list to add persistent and
    // cancelled options.
    for (auto const& copts : co_list) {
        const OptionContainerPtr& opts = copts->getAll(DHCP4_OPTION_SPACE);
        if (!opts) {
            continue;
        }
        // Get persistent options.
        const OptionContainerPersistIndex& pidx = opts->get<2>();
        const OptionContainerPersistRange& prange = pidx.equal_range(true);
        BOOST_FOREACH(auto const& desc, prange) {
            // Add the persistent option code to requested options.
            if (desc.option_) {
                uint8_t code = static_cast<uint8_t>(desc.option_->getType());
                static_cast<void>(requested_opts.insert(code));
            }
        }
        // Get cancelled options.
        const OptionContainerCancelIndex& cidx = opts->get<5>();
        const OptionContainerCancelRange& crange = cidx.equal_range(true);
        BOOST_FOREACH(auto const& desc, crange) {
            // Add the cancelled option code to cancelled options.
            if (desc.option_) {
                uint8_t code = static_cast<uint8_t>(desc.option_->getType());
                static_cast<void>(cancelled_opts.insert(code));
            }
        }
    }

    // For each requested option code get the first instance of the option
    // to be returned to the client.
    for (uint8_t opt : requested_opts) {
        if (cancelled_opts.count(opt) > 0) {
            continue;
        }
        // Skip special cases: DHO_VIVSO_SUBOPTIONS.
        if (opt == DHO_VIVSO_SUBOPTIONS) {
            continue;
        }
        // Add nothing when it is already there.
        if (!resp->getOption(opt)) {
            // Iterate on the configured option list
            for (auto const& copts : co_list) {
                OptionDescriptor desc = copts->get(DHCP4_OPTION_SPACE, opt);
                // Got it: add it and jump to the outer loop
                if (desc.option_ && desc.allowedForClientClasses(cclasses)) {
                    resp->addOption(desc.option_);
                    break;
                }
            }
        }
    }

    // Special cases for vendor class and options which are identified
    // by the code/type and the vendor/enterprise id vs. the code/type only.
    if ((requested_opts.count(DHO_VIVCO_SUBOPTIONS) > 0) &&
        (cancelled_opts.count(DHO_VIVCO_SUBOPTIONS) == 0)) {
        // Keep vendor ids which are already in the response to insert
        // VIVCO options at most once per vendor.
        set<uint32_t> vendor_ids;
        // Get what already exists in the response.
        for (auto const& opt : resp->getOptions(DHO_VIVCO_SUBOPTIONS)) {
            OptionVendorClassPtr vendor_opts;
            vendor_opts = boost::dynamic_pointer_cast<OptionVendorClass>(opt.second);
            if (vendor_opts) {
                uint32_t vendor_id = vendor_opts->getVendorId();
                static_cast<void>(vendor_ids.insert(vendor_id));
            }
        }
        // Iterate on the configured option list.
        for (auto const& copts : co_list) {
            for (auto const& desc : copts->getList(DHCP4_OPTION_SPACE,
                                                        DHO_VIVCO_SUBOPTIONS)) {
                if (!desc.option_ || !desc.allowedForClientClasses(cclasses)) {
                    continue;
                }
                OptionVendorClassPtr vendor_opts =
                    boost::dynamic_pointer_cast<OptionVendorClass>(desc.option_);
                if (!vendor_opts) {
                    continue;
                }
                // Is the vendor id already in the response?
                uint32_t vendor_id = vendor_opts->getVendorId();
                if (vendor_ids.count(vendor_id) > 0) {
                    continue;
                }
                // Got it: add it.
                resp->Pkt::addOption(desc.option_);
                static_cast<void>(vendor_ids.insert(vendor_id));
            }
        }
    }

    if ((requested_opts.count(DHO_VIVSO_SUBOPTIONS) > 0) &&
        (cancelled_opts.count(DHO_VIVSO_SUBOPTIONS) == 0)) {
        // Keep vendor ids which are already in the response to insert
        // VIVSO options at most once per vendor.
        set<uint32_t> vendor_ids;
        // Get what already exists in the response.
        for (auto const& opt : resp->getOptions(DHO_VIVSO_SUBOPTIONS)) {
            OptionVendorPtr vendor_opts;
            vendor_opts = boost::dynamic_pointer_cast<OptionVendor>(opt.second);
            if (vendor_opts) {
                uint32_t vendor_id = vendor_opts->getVendorId();
                static_cast<void>(vendor_ids.insert(vendor_id));
            }
        }
        // Iterate on the configured option list
        for (auto const& copts : co_list) {
            for (auto const& desc : copts->getList(DHCP4_OPTION_SPACE,
                                                   DHO_VIVSO_SUBOPTIONS)) {
                if (!desc.option_ || !desc.allowedForClientClasses(cclasses)) {
                    continue;
                }
                OptionVendorPtr vendor_opts =
                    boost::dynamic_pointer_cast<OptionVendor>(desc.option_);
                if (!vendor_opts) {
                    continue;
                }
                // Is the vendor id already in the response?
                uint32_t vendor_id = vendor_opts->getVendorId();
                if (vendor_ids.count(vendor_id) > 0) {
                    continue;
                }
                // Append a fresh vendor option as the next method should
                // add suboptions to it.
                vendor_opts.reset(new OptionVendor(Option::V4, vendor_id));
                resp->Pkt::addOption(vendor_opts);
                static_cast<void>(vendor_ids.insert(vendor_id));
            }
        }
    }
}

void
Dhcpv4Srv::appendRequestedVendorOptions(Dhcpv4Exchange& ex) {
    // Get the configured subnet suitable for the incoming packet.
    ConstSubnet4Ptr subnet = ex.getContext()->subnet_;

    const CfgOptionList& co_list = ex.getCfgOptionList();

    // Leave if there is no subnet matching the incoming packet.
    // There is no need to log the error message here because
    // it will be logged in the assignLease() when it fails to
    // pick the suitable subnet. We don't want to duplicate
    // error messages in such case.
    //
    // Also, if there's no options to possibly assign, give up.
    if (!subnet || co_list.empty()) {
        return;
    }

    Pkt4Ptr query = ex.getQuery();
    Pkt4Ptr resp = ex.getResponse();
    set<uint32_t> vendor_ids;

    // The server could have provided the option using client classification or
    // hooks. If there're vendor info options in the response already, use them.
    map<uint32_t, OptionVendorPtr> vendor_rsps;
    for (auto const& opt : resp->getOptions(DHO_VIVSO_SUBOPTIONS)) {
        OptionVendorPtr vendor_rsp;
        vendor_rsp = boost::dynamic_pointer_cast<OptionVendor>(opt.second);
        if (vendor_rsp) {
            uint32_t vendor_id = vendor_rsp->getVendorId();
            vendor_rsps[vendor_id] = vendor_rsp;
            static_cast<void>(vendor_ids.insert(vendor_id));
        }
    }

    // Next, try to get the vendor-id from the client packet's
    // vendor-specific information option (125).
    map<uint32_t, OptionVendorPtr> vendor_reqs;
    for (auto const& opt : query->getOptions(DHO_VIVSO_SUBOPTIONS)) {
        OptionVendorPtr vendor_req;
        vendor_req = boost::dynamic_pointer_cast<OptionVendor>(opt.second);
        if (vendor_req) {
            uint32_t vendor_id = vendor_req->getVendorId();
            vendor_reqs[vendor_id] = vendor_req;
            static_cast<void>(vendor_ids.insert(vendor_id));
        }
    }

    // Finally, try to get the vendor-id from the client packet's
    // vendor-specific class option (124).
    for (auto const& opt : query->getOptions(DHO_VIVCO_SUBOPTIONS)) {
        OptionVendorClassPtr vendor_class;
        vendor_class = boost::dynamic_pointer_cast<OptionVendorClass>(opt.second);
        if (vendor_class) {
            uint32_t vendor_id = vendor_class->getVendorId();
            static_cast<void>(vendor_ids.insert(vendor_id));
        }
    }

    // If there's no vendor option in either request or response, then there's no way
    // to figure out what the vendor-id values are and we give up.
    if (vendor_ids.empty()) {
        return;
    }

    map<uint32_t, set<uint8_t> > requested_opts;

    // Let's try to get ORO within that vendor-option.
    // This is specific to vendor-id=4491 (Cable Labs). Other vendors may have
    // different policies.
    OptionUint8ArrayPtr oro;
    if (vendor_reqs.count(VENDOR_ID_CABLE_LABS) > 0) {
        OptionVendorPtr vendor_req = vendor_reqs[VENDOR_ID_CABLE_LABS];
        OptionPtr oro_generic = vendor_req->getOption(DOCSIS3_V4_ORO);
        if (oro_generic) {
            // Vendor ID 4491 makes Kea look at DOCSIS3_V4_OPTION_DEFINITIONS
            // when parsing options. Based on that, oro_generic will have been
            // created as an OptionUint8Array, but might not be for other
            // vendor IDs.
            oro = boost::dynamic_pointer_cast<OptionUint8Array>(oro_generic);
        }
        if (oro) {
            set<uint8_t> oro_req_opts;
            for (uint8_t code : oro->getValues()) {
                static_cast<void>(oro_req_opts.insert(code));
            }
            requested_opts[VENDOR_ID_CABLE_LABS] = oro_req_opts;
        }
    }

    const auto& cclasses = query->getClasses();
    for (uint32_t vendor_id : vendor_ids) {

        std::set<uint8_t> cancelled_opts;

        // Iterate on the configured option list to add persistent and
        // cancelled options,
        for (auto const& copts : co_list) {
            const OptionContainerPtr& opts = copts->getAll(vendor_id);
            if (!opts) {
                continue;
            }

            // Get persistent options.
            const OptionContainerPersistIndex& pidx = opts->get<2>();
            const OptionContainerPersistRange& prange = pidx.equal_range(true);
            BOOST_FOREACH(auto const& desc, prange) {
                // Add the persistent option code to requested options.
                if (desc.option_) {
                    uint8_t code = static_cast<uint8_t>(desc.option_->getType());
                    static_cast<void>(requested_opts[vendor_id].insert(code));
                }
            }

            // Get cancelled options.
            const OptionContainerCancelIndex& cidx = opts->get<5>();
            const OptionContainerCancelRange& crange = cidx.equal_range(true);
            BOOST_FOREACH(auto const& desc, crange) {
                // Add the cancelled option code to cancelled options.
                if (desc.option_) {
                    uint8_t code = static_cast<uint8_t>(desc.option_->getType());
                    static_cast<void>(cancelled_opts.insert(code));
                }
            }
        }

        // If there is nothing to add don't do anything with this vendor.
        // This will explicitly not echo back vendor options from the request
        // that either correspond to a vendor not known to Kea even if the
        // option encapsulates data or there are no persistent options
        // configured for this vendor so Kea does not send any option back.
        if (requested_opts[vendor_id].empty()) {
            continue;
        }


        // It's possible that vivso was inserted already by client class or
        // a hook. If that is so, let's use it.
        OptionVendorPtr vendor_rsp;
        if (vendor_rsps.count(vendor_id) > 0) {
            vendor_rsp = vendor_rsps[vendor_id];
        } else {
            vendor_rsp.reset(new OptionVendor(Option::V4, vendor_id));
        }

        // Get the list of options that client requested.
        bool added = false;

        for (uint8_t opt : requested_opts[vendor_id]) {
            if (cancelled_opts.count(opt) > 0) {
                continue;
            }
            if (!vendor_rsp->getOption(opt)) {
                for (auto const& copts : co_list) {
                    OptionDescriptor desc = copts->get(vendor_id, opt);
                    if (desc.option_ && desc.allowedForClientClasses(cclasses)) {
                        vendor_rsp->addOption(desc.option_);
                        added = true;
                        break;
                    }
                }
            }
        }

        // If we added some sub-options and the vendor opts option is not in
        // the response already, then add it.
        if (added && (vendor_rsps.count(vendor_id) == 0)) {
            resp->Pkt::addOption(vendor_rsp);
        }
    }
}

void
Dhcpv4Srv::appendBasicOptions(Dhcpv4Exchange& ex) {
    // Identify options that we always want to send to the
    // client (if they are configured).
    static const std::vector<uint16_t> required_options = {
        DHO_ROUTERS,
        DHO_DOMAIN_NAME_SERVERS,
        DHO_DOMAIN_NAME,
        DHO_DHCP_SERVER_IDENTIFIER };

    // Get the subnet.
    ConstSubnet4Ptr subnet = ex.getContext()->subnet_;
    if (!subnet) {
        return;
    }

    // Unlikely short cut
    const CfgOptionList& co_list = ex.getCfgOptionList();
    if (co_list.empty()) {
        return;
    }

    Pkt4Ptr resp = ex.getResponse();
    const auto& cclasses = ex.getQuery()->getClasses();

    // Try to find all 'required' options in the outgoing
    // message. Those that are not present will be added.
    for (auto const& required : required_options) {
        OptionPtr opt = resp->getOption(required);
        if (!opt) {
            // Check whether option has been configured.
            for (auto const& copts : co_list) {
                OptionDescriptor desc = copts->get(DHCP4_OPTION_SPACE, required);
                if (desc.option_ && desc.allowedForClientClasses(cclasses)) {
                    resp->addOption(desc.option_);
                    break;
                }
            }
        }
    }
}

void
Dhcpv4Srv::processClientName(Dhcpv4Exchange& ex) {
    // It is possible that client has sent both Client FQDN and Hostname
    // option. In that the server should prefer Client FQDN option and
    // ignore the Hostname option.
    try {
        Pkt4Ptr query = ex.getQuery();
        Pkt4Ptr resp = ex.getResponse();
        Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>
            (query->getOption(DHO_FQDN));
        if (fqdn) {
            LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_CLIENT_FQDN_PROCESS)
                .arg(query->getLabel());
            processClientFqdnOption(ex);

        } else {
            LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL,
                      DHCP4_CLIENT_HOSTNAME_PROCESS)
                    .arg(query->getLabel());
            processHostnameOption(ex);
        }

        // Based on the output option added to the response above, we figure out
        // the values for the hostname and dns flags to set in the context.  These
        // will be used to populate the lease.
        std::string hostname;
        bool fqdn_fwd = false;
        bool fqdn_rev = false;

        OptionStringPtr opt_hostname;
        fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>(resp->getOption(DHO_FQDN));
        if (fqdn) {
            hostname = fqdn->getDomainName();
            CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*fqdn, fqdn_fwd, fqdn_rev);
        } else {
            opt_hostname = boost::dynamic_pointer_cast<OptionString>
                           (resp->getOption(DHO_HOST_NAME));

            if (opt_hostname) {
                hostname = opt_hostname->getValue();
                // DHO_HOST_NAME is string option which cannot be blank,
                // we use "." to know we should replace it with a fully
                // generated name. The local string variable needs to be
                // blank in logic below.
                if (hostname == ".") {
                    hostname = "";
                }

                /// @todo It could be configurable what sort of updates the
                /// server is doing when Hostname option was sent.
                if (ex.getContext()->getDdnsParams()->getEnableUpdates()) {
                    fqdn_fwd = true;
                    fqdn_rev = true;
                }
            }
        }

        // Optionally, call a hook that may possibly override the decisions made
        // earlier.
        if (HooksManager::calloutsPresent(Hooks.hook_index_ddns4_update_)) {
            CalloutHandlePtr callout_handle = getCalloutHandle(query);

            // Use the RAII wrapper to make sure that the callout handle state is
            // reset when this object goes out of scope. All hook points must do
            // it to prevent possible circular dependency between the callout
            // handle and its arguments.
            ScopedCalloutHandleState callout_handle_state(callout_handle);

            // Setup the callout arguments.
            ConstSubnet4Ptr subnet = ex.getContext()->subnet_;
            callout_handle->setArgument("query4", query);
            callout_handle->setArgument("response4", resp);
            callout_handle->setArgument("subnet4", subnet);
            callout_handle->setArgument("hostname", hostname);
            callout_handle->setArgument("fwd-update", fqdn_fwd);
            callout_handle->setArgument("rev-update", fqdn_rev);
            callout_handle->setArgument("ddns-params", ex.getContext()->getDdnsParams());

            // Call callouts
            HooksManager::callCallouts(Hooks.hook_index_ddns4_update_, *callout_handle);

            // Let's get the parameters returned by hook.
            string hook_hostname;
            bool hook_fqdn_fwd = false;
            bool hook_fqdn_rev = false;
            callout_handle->getArgument("hostname", hook_hostname);
            callout_handle->getArgument("fwd-update", hook_fqdn_fwd);
            callout_handle->getArgument("rev-update", hook_fqdn_rev);

            // If there's anything changed by the hook, log it and then update
            // the parameters.
            if ((hostname != hook_hostname) || (fqdn_fwd != hook_fqdn_fwd) ||
                (fqdn_rev != hook_fqdn_rev)) {
                LOG_DEBUG(hooks_logger, DBGLVL_PKT_HANDLING, DHCP4_HOOK_DDNS_UPDATE)
                    .arg(hostname).arg(hook_hostname).arg(fqdn_fwd).arg(hook_fqdn_fwd)
                    .arg(fqdn_rev).arg(hook_fqdn_rev);
                hostname = hook_hostname;
                fqdn_fwd = hook_fqdn_fwd;
                fqdn_rev = hook_fqdn_rev;

                // If there's an outbound host-name option in the response we
                // need to updated it with the new host name.
                OptionStringPtr hostname_opt = boost::dynamic_pointer_cast<OptionString>
                                              (resp->getOption(DHO_HOST_NAME));
                if (hostname_opt) {
                    hostname_opt->setValue(hook_hostname);
                }

                // If there's an outbound FQDN option in the response we need
                // to update it with the new host name.
                fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>(resp->getOption(DHO_FQDN));
                if (fqdn) {
                    fqdn->setDomainName(hook_hostname, Option4ClientFqdn::FULL);
                    // Hook disabled updates, Set flags back to client accordingly.
                    fqdn->setFlag(Option4ClientFqdn::FLAG_S, 0);
                    fqdn->setFlag(Option4ClientFqdn::FLAG_N, 1);
                }
            }
        }

        // Update the context
        auto ctx = ex.getContext();
        ctx->fwd_dns_update_ = fqdn_fwd;
        ctx->rev_dns_update_ = fqdn_rev;
        ctx->hostname_ = hostname;

    } catch (const Exception& e) {
        // In some rare cases it is possible that the client's name processing
        // fails. For example, the Hostname option may be malformed, or there
        // may be an error in the server's logic which would cause multiple
        // attempts to add the same option to the response message. This
        // error message aggregates all these errors so they can be diagnosed
        // from the log. We don't want to throw an exception here because,
        // it will impact the processing of the whole packet. We rather want
        // the processing to continue, even if the client's name is wrong.
        LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_NAME_PROC_FAIL)
            .arg(ex.getQuery()->getLabel())
            .arg(e.what());
    }
}

void
Dhcpv4Srv::processClientFqdnOption(Dhcpv4Exchange& ex) {
    // Obtain the FQDN option from the client's message.
    Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
        Option4ClientFqdn>(ex.getQuery()->getOption(DHO_FQDN));

    LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_FQDN_DATA)
        .arg(ex.getQuery()->getLabel())
        .arg(fqdn->toText());

    // Create the DHCPv4 Client FQDN Option to be included in the server's
    // response to a client.
    Option4ClientFqdnPtr fqdn_resp(new Option4ClientFqdn(*fqdn));

    // Set the server S, N, and O flags based on client's flags and
    // current configuration.
    D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
    d2_mgr.adjustFqdnFlags<Option4ClientFqdn>(*fqdn, *fqdn_resp,
                                              *(ex.getContext()->getDdnsParams()));
    // Carry over the client's E flag.
    fqdn_resp->setFlag(Option4ClientFqdn::FLAG_E,
                       fqdn->getFlag(Option4ClientFqdn::FLAG_E));

    if (ex.getContext()->currentHost() &&
        !ex.getContext()->currentHost()->getHostname().empty()) {
        fqdn_resp->setDomainName(d2_mgr.qualifyName(ex.getContext()->currentHost()->getHostname(),
                                                    *(ex.getContext()->getDdnsParams()), true),
                                 Option4ClientFqdn::FULL);

    } else {
        // Adjust the domain name based on domain name value and type sent by the
        // client and current configuration.
        d2_mgr.adjustDomainName<Option4ClientFqdn>(*fqdn, *fqdn_resp,
                                                   *(ex.getContext()->getDdnsParams()));
    }

    // Add FQDN option to the response message. Note that, there may be some
    // cases when server may choose not to include the FQDN option in a
    // response to a client. In such cases, the FQDN should be removed from the
    // outgoing message. In theory we could cease to include the FQDN option
    // in this function until it is confirmed that it should be included.
    // However, we include it here for simplicity. Functions used to acquire
    // lease for a client will scan the response message for FQDN and if it
    // is found they will take necessary actions to store the FQDN information
    // in the lease database as well as to generate NameChangeRequests to DNS.
    // If we don't store the option in the response message, we will have to
    // propagate it in the different way to the functions which acquire the
    // lease. This would require modifications to the API of this class.
    LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_RESPONSE_FQDN_DATA)
        .arg(ex.getQuery()->getLabel())
        .arg(fqdn_resp->toText());
    ex.getResponse()->addOption(fqdn_resp);
}

void
Dhcpv4Srv::processHostnameOption(Dhcpv4Exchange& ex) {<--- Parameter 'ex' can be declared as reference to const
    // Fetch D2 configuration.
    D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();<--- Variable 'd2_mgr' can be declared as reference to const

    // Obtain the Hostname option from the client's message.
    OptionStringPtr opt_hostname = boost::dynamic_pointer_cast<OptionString>
        (ex.getQuery()->getOption(DHO_HOST_NAME));

    if (opt_hostname) {
        LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_HOSTNAME_DATA)
            .arg(ex.getQuery()->getLabel())
            .arg(opt_hostname->getValue());
    }

    AllocEngine::ClientContext4Ptr ctx = ex.getContext();

    // Hostname reservations take precedence over any other configuration,
    // i.e. DDNS configuration.  If we have a reserved hostname we should
    // use it and send it back.
    if (ctx->currentHost() && !ctx->currentHost()->getHostname().empty()) {
        // Qualify if there is a suffix configured.
        std::string hostname = d2_mgr.qualifyName(ctx->currentHost()->getHostname(),
                                                  *(ex.getContext()->getDdnsParams()), false);
        // Convert it to lower case.
        boost::algorithm::to_lower(hostname);
        LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_RESERVED_HOSTNAME_ASSIGNED)
                .arg(ex.getQuery()->getLabel())
                .arg(hostname);

        // Add it to the response
        OptionStringPtr opt_hostname_resp(new OptionString(Option::V4, DHO_HOST_NAME, hostname));
        ex.getResponse()->addOption(opt_hostname_resp);

        // We're done here.
        return;
    }

    // There is no reservation for this client however there is still a
    // possibility that we'll have to send hostname option to this client
    // if the client has included hostname option or the configuration of
    // the server requires that we send the option regardless.
    D2ClientConfig::ReplaceClientNameMode replace_name_mode =
        ex.getContext()->getDdnsParams()->getReplaceClientNameMode();

    // If we don't have a hostname then either we'll supply it or do nothing.
    if (!opt_hostname) {
        // If we're configured to supply it then add it to the response.
        // Use the root domain to signal later on that we should replace it.
        if (replace_name_mode == D2ClientConfig::RCM_ALWAYS ||
            replace_name_mode == D2ClientConfig::RCM_WHEN_NOT_PRESENT) {
            LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA,
                      DHCP4_GENERATE_FQDN)
                .arg(ex.getQuery()->getLabel());
            OptionStringPtr opt_hostname_resp(new OptionString(Option::V4,
                                                               DHO_HOST_NAME,
                                                               "."));
            ex.getResponse()->addOption(opt_hostname_resp);
        }

        return;
    }

    // Client sent us a hostname option so figure out what to do with it.
    LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_HOSTNAME_DATA)
        .arg(ex.getQuery()->getLabel())
        .arg(opt_hostname->getValue());

    std::string hostname = isc::util::str::trim(opt_hostname->getValue());
    unsigned int label_count;

    try  {
        // Parsing into labels can throw on malformed content so we're
        // going to explicitly catch that here.
        label_count = OptionDataTypeUtil::getLabelCount(hostname);
    } catch (const std::exception& exc) {
        LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_CLIENT_HOSTNAME_MALFORMED)
            .arg(ex.getQuery()->getLabel())
            .arg(exc.what());
        return;
    }

    // The hostname option sent by the client should be at least 1 octet long.
    // If it isn't we ignore this option. (Per RFC 2131, section 3.14)
    /// @todo It would be more liberal to accept this and let it fall into
    /// the case  of replace or less than two below.
    if (label_count == 0) {
        LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_EMPTY_HOSTNAME)
            .arg(ex.getQuery()->getLabel());
        return;
    }

    // Stores the value we eventually use, so we can send it back.
    OptionStringPtr opt_hostname_resp;

    // The hostname option may be unqualified or fully qualified. The lab_count
    // holds the number of labels for the name. The number of 1 means that
    // there is only root label "." (even for unqualified names, as the
    // getLabelCount function treats each name as a fully qualified one).
    // By checking the number of labels present in the hostname we may infer
    // whether client has sent the fully qualified or unqualified hostname.

    if ((replace_name_mode == D2ClientConfig::RCM_ALWAYS ||
         replace_name_mode == D2ClientConfig::RCM_WHEN_PRESENT)
        || label_count < 2) {
        // Set to root domain to signal later on that we should replace it.
        // DHO_HOST_NAME is a string option which cannot be empty.
        /// @todo We may want to reconsider whether it is appropriate for the
        /// client to send a root domain name as a Hostname. There are
        /// also extensions to the auto generation of the client's name,
        /// e.g. conversion to the puny code which may be considered at some
        /// point.
        /// For now, we just remain liberal and expect that the DNS will handle
        /// conversion if needed and possible.
        opt_hostname_resp.reset(new OptionString(Option::V4, DHO_HOST_NAME, "."));
    } else {
        // Sanitize the name the client sent us, if we're configured to do so.
        isc::util::str::StringSanitizerPtr sanitizer =
            ex.getContext()->getDdnsParams()->getHostnameSanitizer();

        if (sanitizer) {
            hostname = sanitizer->scrub(hostname);
        }

        // Convert hostname to lower case.
        boost::algorithm::to_lower(hostname);

        if (label_count == 2) {
            // If there are two labels, it means that the client has specified
            // the unqualified name. We have to concatenate the unqualified name
            // with the domain name. The false value passed as a second argument
            // indicates that the trailing dot should not be appended to the
            // hostname. We don't want to append the trailing dot because
            // we don't know whether the hostname is partial or not and some
            // clients do not handle the hostnames with the trailing dot.
            opt_hostname_resp.reset(
                new OptionString(Option::V4, DHO_HOST_NAME,
                                 d2_mgr.qualifyName(hostname, *(ex.getContext()->getDdnsParams()),
                                                    false)));
        } else {
            opt_hostname_resp.reset(new OptionString(Option::V4, DHO_HOST_NAME, hostname));
        }
    }

    LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_RESPONSE_HOSTNAME_DATA)
        .arg(ex.getQuery()->getLabel())
        .arg(opt_hostname_resp->getValue());
    ex.getResponse()->addOption(opt_hostname_resp);
}

void
Dhcpv4Srv::createNameChangeRequests(const Lease4Ptr& lease,
                                    const Lease4Ptr& old_lease,
                                    const DdnsParams& ddns_params) {
    if (!lease) {
        isc_throw(isc::Unexpected,
                  "NULL lease specified when creating NameChangeRequest");
    }

    // Nothing to do if updates are not enabled.
    if (!ddns_params.getEnableUpdates()) {
        return;
    }

    if ((lease->reuseable_valid_lft_ == 0) &&
        (!old_lease || ddns_params.getUpdateOnRenew() || 
         !lease->hasIdenticalFqdn(*old_lease))) {
        if (old_lease) {
            // Queue's up a remove of the old lease's DNS (if needed)
            queueNCR(CHG_REMOVE, old_lease);
        }

        // We may need to generate the NameChangeRequest for the new lease. It
        // will be generated only if hostname is set and if forward or reverse
        // update has been requested.
        queueNCR(CHG_ADD, lease);
    }
}

bool
Dhcpv4Srv::assignZero(ConstSubnet4Ptr& subnet,
                      const ClientClasses& client_classes) {
    ConstSubnet4Ptr current_subnet = subnet;
    // Try subnets.
    while (current_subnet) {
        const ConstCfgOptionPtr& co = current_subnet->getCfgOption();
        if (!co->empty()) {
            OptionDescriptor desc = co->get(DHCP4_OPTION_SPACE,
                                            DHO_V6_ONLY_PREFERRED);
            if (desc.option_) {
                subnet = current_subnet;
                return (true);
            }
        }
        current_subnet = current_subnet->getNextSubnet(subnet, client_classes);
    }
    // Try the shared network.
    SharedNetwork4Ptr network;
    subnet->getSharedNetwork(network);
    if (network) {
        const ConstCfgOptionPtr& co = network->getCfgOption();
        if (!co->empty()) {
            OptionDescriptor desc = co->get(DHCP4_OPTION_SPACE,
                                            DHO_V6_ONLY_PREFERRED);
            if (desc.option_) {
                return (true);
            }
        }
    }
    return (false);
}

void
Dhcpv4Srv::assignLease(Dhcpv4Exchange& ex) {<--- Shadowed declaration
    // Get the pointers to the query and the response messages.
    Pkt4Ptr query = ex.getQuery();
    Pkt4Ptr resp = ex.getResponse();

    // Get the context.
    AllocEngine::ClientContext4Ptr ctx = ex.getContext();

    // Subnet should have been already selected when the context was created.
    ConstSubnet4Ptr subnet = ctx->subnet_;

    // "Fake" allocation is processing of DISCOVER message. We pretend to do an
    // allocation, but we do not put the lease in the database. That is ok,
    // because we do not guarantee that the user will get that exact lease. If
    // the user selects this server to do actual allocation (i.e. sends REQUEST)
    // it should include this hint. That will help us during the actual lease
    // allocation.
    bool fake_allocation = (query->getType() == DHCPDISCOVER);

    // Check if IPv6-Only Preferred was requested.
    OptionUint8ArrayPtr option_prl = boost::dynamic_pointer_cast<
        OptionUint8Array>(query->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
    if (option_prl) {
        auto const& requested_opts = option_prl->getValues();
        if ((std::find(requested_opts.cbegin(), requested_opts.cend(),
                       DHO_V6_ONLY_PREFERRED) != requested_opts.cend()) &&
            assignZero(subnet, query->getClasses())) {
            ex.setIPv6OnlyPreferred(true);
            ctx->subnet_ = subnet;
            resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());
            if (!fake_allocation) {
                resp->setCiaddr(query->getCiaddr());
            }
            return;
        }
    }

    // Get the server identifier. It will be used to determine the state
    // of the client.
    OptionCustomPtr opt_serverid = boost::dynamic_pointer_cast<
        OptionCustom>(query->getOption(DHO_DHCP_SERVER_IDENTIFIER));

    // Check if the client has sent a requested IP address option or
    // ciaddr.
    OptionCustomPtr opt_requested_address = boost::dynamic_pointer_cast<
        OptionCustom>(query->getOption(DHO_DHCP_REQUESTED_ADDRESS));
    IOAddress hint(IOAddress::IPV4_ZERO_ADDRESS());
    if (opt_requested_address) {
        hint = opt_requested_address->readAddress();

    } else if (!query->getCiaddr().isV4Zero()) {
        hint = query->getCiaddr();

    }

    // This flag controls whether or not the server should respond to the clients
    // in the INIT-REBOOT state. We will initialize it to a configured value only
    // when the client is in that state.
    auto authoritative = false;

    // If there is no server id and there is a Requested IP Address option
    // the client is in the INIT-REBOOT state in which the server has to
    // determine whether the client's notion of the address is correct
    // and whether the client is known, i.e., has a lease.
    auto init_reboot = (!fake_allocation && !opt_serverid && opt_requested_address);
    if (init_reboot) {
        LOG_INFO(lease4_logger, DHCP4_INIT_REBOOT)
            .arg(query->getLabel())
            .arg(hint.toText());

        // Find the authoritative flag configuration.
        if (subnet) {
            authoritative = subnet->getAuthoritative();
        } else {
            // If there is no subnet, use the global value.
            auto flag = CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals()->
                get(CfgGlobals::AUTHORITATIVE);
            if (flag && (flag->getType() == data::Element::boolean)) {
                authoritative = flag->boolValue();
            }
        }
    } else if (fake_allocation) {
        LOG_DEBUG(lease4_logger, DBG_DHCP4_DETAIL, DHCP4_DISCOVER)
            .arg(query->getLabel())
            .arg(hint != IOAddress::IPV4_ZERO_ADDRESS() ? hint.toText() : "(no hint)");
    } else {
        LOG_DEBUG(lease4_logger, DBG_DHCP4_DETAIL, DHCP4_REQUEST)
            .arg(query->getLabel())
            .arg(hint != IOAddress::IPV4_ZERO_ADDRESS() ? hint.toText() : "(no hint)");
    }

    // If there is no subnet configuration for that client we ignore the
    // request from the INIT-REBOOT client if we're not authoritative, because
    // we don't know whether the network configuration is correct for this
    // client. We return DHCPNAK if we're authoritative, though.
    if (!subnet && (!init_reboot || authoritative)) {
        // This particular client is out of luck today. We do not have
        // information about the subnet he is connected to. This likely means
        // misconfiguration of the server (or some relays).

        // Perhaps this should be logged on some higher level?
        LOG_ERROR(bad_packet4_logger, DHCP4_PACKET_NAK_0001)
            .arg(query->getLabel())
            .arg(query->getRemoteAddr().toText())
            .arg(query->getName());
        resp->setType(DHCPNAK);
        resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());
        return;
    }

    HWAddrPtr hwaddr = query->getHWAddr();

    ConstSubnet4Ptr original_subnet = subnet;

    // Get client-id. It is not mandatory in DHCPv4.
    ClientIdPtr client_id = ex.getContext()->clientid_;

    // In the INIT-REBOOT state, a client remembering its previously assigned
    // address is trying to confirm whether or not this address is still usable.
    if (init_reboot) {
        Lease4Ptr lease;

        auto const& classes = query->getClasses();

        // We used to issue a separate query (two actually: one for client-id
        // and another one for hw-addr for) each subnet in the shared network.
        // That was horribly inefficient if the client didn't have any lease
        // (or there were many subnets and the client happened to be in one
        // of the last subnets).
        //
        // We now issue at most two queries: get all the leases for specific
        // client-id and then get all leases for specific hw-address.
        if (original_subnet && client_id) {

            // Get all the leases for this client-id
            Lease4Collection leases_client_id = LeaseMgrFactory::instance().getLease4(*client_id);
            if (!leases_client_id.empty()) {
                ConstSubnet4Ptr s = original_subnet;

                // Among those returned try to find a lease that belongs to
                // current shared network.
                while (s) {
                    for (auto const& l : leases_client_id) {
                        if (l->subnet_id_ == s->getID()) {<--- Consider using std::find_if algorithm instead of a raw loop.
                            lease = l;
                            break;
                        }
                    }

                    if (lease) {
                        break;

                    } else {
                        s = s->getNextSubnet(original_subnet, classes);
                    }
                }
            }
        }

        // If we haven't found a lease yet, try again by hardware-address.
        // The logic is the same.
        if (original_subnet && !lease && hwaddr) {

            // Get all leases for this particular hw-address.
            Lease4Collection leases_hwaddr = LeaseMgrFactory::instance().getLease4(*hwaddr);
            if (!leases_hwaddr.empty()) {
                ConstSubnet4Ptr s = original_subnet;

                // Pick one that belongs to a subnet in this shared network.
                while (s) {
                    for (auto const& l : leases_hwaddr) {
                        if (l->subnet_id_ == s->getID()) {<--- Consider using std::find_if algorithm instead of a raw loop.
                            lease = l;
                            break;
                        }
                    }

                    if (lease) {
                        break;

                    } else {
                        s = s->getNextSubnet(original_subnet, classes);
                    }
                }
            }
        }

        // Check the first error case: unknown client. We check this before
        // validating the address sent because we don't want to respond if
        // we don't know this client, except if we're authoritative.
        bool known_client = lease && lease->belongsToClient(hwaddr, client_id);
        if (!authoritative && !known_client) {
            LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
                      DHCP4_NO_LEASE_INIT_REBOOT)
                .arg(query->getLabel())
                .arg(hint.toText());

            ex.deleteResponse();
            return;
        }

        // If we know this client, check if his notion of the IP address is
        // correct, if we don't know him, check if we are authoritative.
        if ((known_client && (lease->addr_ != hint)) ||
            (!known_client && authoritative) ||
            (!original_subnet)) {
            LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
                      DHCP4_PACKET_NAK_0002)
                .arg(query->getLabel())
                .arg(hint.toText());

            resp->setType(DHCPNAK);
            resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());
            return;
        }
    }

    CalloutHandlePtr callout_handle = getCalloutHandle(query);

    // We need to set these values in the context as they haven't been set yet.
    ctx->requested_address_ = hint;
    ctx->fake_allocation_ = fake_allocation;
    ctx->callout_handle_ = callout_handle;

    // If client query contains an FQDN or Hostname option, server
    // should respond to the client with the appropriate FQDN or Hostname
    // option to indicate if it takes responsibility for the DNS updates.
    // This is also the source for the hostname and dns flags that are
    // initially added to the lease.  In most cases, this information is
    // good now.  If we end up changing subnets in allocation we'll have to
    // do it again and then update the lease.
    processClientName(ex);

    // Get a lease.
    Lease4Ptr lease = alloc_engine_->allocateLease4(*ctx);

    // Tracks whether or not the client name (FQDN or host) has changed since
    // the lease was allocated.
    bool client_name_changed = false;

    // Subnet may be modified by the allocation engine, if the initial subnet
    // belongs to a shared network.
    if (subnet && ctx->subnet_ && subnet->getID() != ctx->subnet_->getID()) {
        SharedNetwork4Ptr network;
        subnet->getSharedNetwork(network);
        LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA, DHCP4_SUBNET_DYNAMICALLY_CHANGED)
                .arg(query->getLabel())
                .arg(subnet->toText())
                .arg(ctx->subnet_->toText())
                .arg(network ? network->getName() : "<no network?>");

        subnet = ctx->subnet_;

        if (lease) {
            // We changed subnets and that means DDNS parameters might be different
            // so we need to rerun client name processing logic.  Arguably we could
            // compare DDNS parameters for both subnets and then decide if we need
            // to rerun the name logic, but that's not likely to be any faster than
            // just re-running the name logic.  @todo When inherited parameter
            // performance is improved this argument could be revisited.
            // Another case is the new subnet has a reserved hostname.

            // First, we need to remove the prior values from the response and reset
            // those in context, to give processClientName a clean slate.
            resp->delOption(DHO_FQDN);
            resp->delOption(DHO_HOST_NAME);
            ctx->hostname_ = "";
            ctx->fwd_dns_update_  = false;
            ctx->rev_dns_update_ = false;

            // Regenerate the name and dns flags.
            processClientName(ex);

            // If the results are different from the values already on the
            // lease, flag it so the lease gets updated down below.
            if ((lease->hostname_ != ctx->hostname_) ||
                (lease->fqdn_fwd_ != ctx->fwd_dns_update_) ||
                (lease->fqdn_rev_ != ctx->rev_dns_update_)) {
                lease->hostname_ = ctx->hostname_;
                lease->fqdn_fwd_ = ctx->fwd_dns_update_;
                lease->fqdn_rev_ = ctx->rev_dns_update_;
                client_name_changed = true;
            }
        }
    }

    if (lease) {
        // We have a lease! Let's set it in the packet and send it back to
        // the client.
        if (fake_allocation) {
            LOG_INFO(lease4_logger, DHCP4_LEASE_OFFER)
                .arg(query->getLabel())
                .arg(lease->addr_.toText());
        } else {
            LOG_INFO(lease4_logger, DHCP4_LEASE_ALLOC)
                .arg(query->getLabel())
                .arg(lease->addr_.toText())
                .arg(Lease::lifetimeToText(lease->valid_lft_));
        }

        // We're logging this here, because this is the place where we know
        // which subnet has been actually used for allocation. If the
        // client identifier matching is disabled, we want to make sure that
        // the user is notified.
        if (!ctx->subnet_->getMatchClientId()) {
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_CLIENTID_IGNORED_FOR_LEASES)
                .arg(ctx->query_->getLabel())
                .arg(ctx->subnet_->getID());
        }

        resp->setYiaddr(lease->addr_);

        /// @todo The server should check what ciaddr the client has supplied
        /// in ciaddr. Currently the ciaddr is ignored except for the subnet
        /// selection. If the client supplied an invalid address, the server
        /// will also return an invalid address here.
        if (!fake_allocation) {
            // If this is a renewing client it will set a ciaddr which the
            // server may include in the response. If this is a new allocation
            // the client will set ciaddr to 0 and this will also be propagated
            // to the server's resp.
            resp->setCiaddr(query->getCiaddr());
        }

        // We may need to update FQDN or hostname if the server is to generate
        // a new name from the allocated IP address or if the allocation engine
        // switched to a different subnet within a shared network.
        postAllocateNameUpdate(ctx, lease, query, resp, client_name_changed);

        // Reuse the lease if possible.
        if (lease->reuseable_valid_lft_ > 0) {
            lease->valid_lft_ = lease->reuseable_valid_lft_;
            LOG_INFO(lease4_logger, DHCP4_LEASE_REUSE)
                .arg(query->getLabel())
                .arg(lease->addr_.toText())
                .arg(Lease::lifetimeToText(lease->valid_lft_));

            // Increment the reuse statistics.
            StatsMgr::instance().addValue("v4-lease-reuses", int64_t(1));
            StatsMgr::instance().addValue(StatsMgr::generateName("subnet", lease->subnet_id_,
                                                                 "v4-lease-reuses"),
                                          int64_t(1));
        }

        // IP Address Lease time (type 51)
        // If we're not allocating on discover  then we just sent the lifetime on the lease.
        // Otherwise (i.e. offer_lft > 0), the lease's lifetime has been set to offer_lft but
        // we want to send the client the proper valid lifetime so we have to fetch it.
        auto send_lft = (ctx->offer_lft_ ? AllocEngine::getValidLft(*ctx) : lease->valid_lft_);
        OptionPtr opt(new OptionUint32(Option::V4, DHO_DHCP_LEASE_TIME, send_lft));

        resp->addOption(opt);

        // Subnet mask (type 1)
        resp->addOption(getNetmaskOption(subnet));

        // Set T1 and T2 per configuration.
        setTeeTimes(lease, subnet, resp);

        // Create NameChangeRequests if this is a real allocation.
        if (!fake_allocation) {
            try {
                createNameChangeRequests(lease, ctx->old_lease_,
                                         *ex.getContext()->getDdnsParams());
            } catch (const Exception& ex) {<--- Shadow variable
                LOG_ERROR(ddns4_logger, DHCP4_NCR_CREATION_FAILED)
                    .arg(query->getLabel())
                    .arg(ex.what());
            }
        }

    } else {
        // Allocation engine did not allocate a lease. The engine logged
        // cause of that failure.
        if (ctx->unknown_requested_addr_) {
            ConstSubnet4Ptr s = original_subnet;
            // Address might have been rejected via class guard (i.e. not
            // allowed for this client). We need to determine if we truly
            // do not know about the address or whether this client just
            // isn't allowed to have that address. We should only DHCPNAK
            // For the latter.
            while (s) {
                if (s->inPool(Lease::TYPE_V4, hint)) {
                    break;
                }

                s = s->getNextSubnet(original_subnet);
            }

            // If we didn't find a subnet, it's not an address we know about
            // so we drop the DHCPNAK.
            if (!s) {
                LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
                          DHCP4_UNKNOWN_ADDRESS_REQUESTED)
                          .arg(query->getLabel())
                          .arg(query->getCiaddr().toText())
                          .arg(opt_requested_address ?
                          opt_requested_address->readAddress().toText() : "(no address)");
                ex.deleteResponse();
                return;
            }
        }

        LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, fake_allocation ?
                  DHCP4_PACKET_NAK_0003 : DHCP4_PACKET_NAK_0004)
            .arg(query->getLabel())
            .arg(query->getCiaddr().toText())
            .arg(opt_requested_address ?
                 opt_requested_address->readAddress().toText() : "(no address)");

        resp->setType(DHCPNAK);
        resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());

        resp->delOption(DHO_FQDN);
        resp->delOption(DHO_HOST_NAME);
    }
}

void
Dhcpv4Srv::postAllocateNameUpdate(const AllocEngine::ClientContext4Ptr& ctx, const Lease4Ptr& lease,
                                 const Pkt4Ptr& query, const Pkt4Ptr& resp, bool client_name_changed) {
    // We may need to update FQDN or hostname if the server is to generate
    // new name from the allocated IP address or if the allocation engine
    // has switched to a different subnet within a shared network.  Get
    // FQDN and hostname options from the response.
    OptionStringPtr opt_hostname;
    Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
                Option4ClientFqdn>(resp->getOption(DHO_FQDN));
    if (!fqdn) {
        opt_hostname = boost::dynamic_pointer_cast<OptionString>(resp->getOption(DHO_HOST_NAME));
        if (!opt_hostname) {
            // We don't have either one, nothing to do.
            return;
        }
    }

    // Empty hostname on the lease means we need to generate it.
    if (lease->hostname_.empty()) {
        // Note that if we have received the hostname option, rather than
        // Client FQDN the trailing dot is not appended to the generated
        // hostname because some clients don't handle the trailing dot in
        // the hostname. Whether the trailing dot is appended or not is
        // controlled by the second argument to the generateFqdn().
        lease->hostname_ = CfgMgr::instance().getD2ClientMgr()
                    .generateFqdn(lease->addr_, *(ctx->getDdnsParams()), static_cast<bool>(fqdn));

        LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_RESPONSE_HOSTNAME_GENERATE)
            .arg(query->getLabel())
            .arg(lease->hostname_);

        client_name_changed = true;
    }

    if (client_name_changed) {
        // The operations below are rather safe, but we want to catch
        // any potential exceptions (e.g. invalid lease database backend
        // implementation) and log an error.
        try {
            /// TKM - do this on committed-discover
            if (!ctx->fake_allocation_ || (ctx->offer_lft_ > 0)) {
                // The lease can't be reused.
                lease->reuseable_valid_lft_ = 0;

                // The lease update should be safe, because the lease should
                // be already in the database. In most cases the exception
                // would be thrown if the lease was missing.
                LeaseMgrFactory::instance().updateLease4(lease);
            }

            // The name update in the outbound option should be also safe,
            // because the generated name is well formed.
            if (fqdn) {
                fqdn->setDomainName(lease->hostname_, Option4ClientFqdn::FULL);
            } else {
                opt_hostname->setValue(lease->hostname_);
            }
        } catch (const Exception& ex) {
            LOG_ERROR(ddns4_logger, DHCP4_POST_ALLOCATION_NAME_UPDATE_FAIL)
                .arg(query->getLabel())
                .arg(lease->hostname_)
                .arg(ex.what());
        }
    }
}

/// @todo This logic to be modified if we decide to support infinite lease times.
void
Dhcpv4Srv::setTeeTimes(const Lease4Ptr& lease, const ConstSubnet4Ptr& subnet, Pkt4Ptr resp) {

    uint32_t t2_time = 0;
    // If T2 is explicitly configured we'll use try value.
    if (!subnet->getT2().unspecified()) {
        t2_time = subnet->getT2();
    } else if (subnet->getCalculateTeeTimes()) {
        // Calculating tee times is enabled, so calculated it.
        t2_time = static_cast<uint32_t>(round(subnet->getT2Percent() * (lease->valid_lft_)));
    }

    // Send the T2 candidate value only if it's sane: to be sane it must be less than
    // the valid life time.
    uint32_t timer_ceiling = lease->valid_lft_;
    if (t2_time > 0 && t2_time < timer_ceiling) {
        OptionUint32Ptr t2(new OptionUint32(Option::V4, DHO_DHCP_REBINDING_TIME, t2_time));
        resp->addOption(t2);
        // When we send T2, timer ceiling for T1 becomes T2.
        timer_ceiling = t2_time;
    }

    uint32_t t1_time = 0;
    // If T1 is explicitly configured we'll use try value.
    if (!subnet->getT1().unspecified()) {
        t1_time = subnet->getT1();
    } else if (subnet->getCalculateTeeTimes()) {
        // Calculating tee times is enabled, so calculate it.
        t1_time = static_cast<uint32_t>(round(subnet->getT1Percent() * (lease->valid_lft_)));
    }

    // Send T1 if it's sane: If we sent T2, T1 must be less than that.  If not it must be
    // less than the valid life time.
    if (t1_time > 0 && t1_time < timer_ceiling) {
        OptionUint32Ptr t1(new OptionUint32(Option::V4, DHO_DHCP_RENEWAL_TIME, t1_time));
        resp->addOption(t1);
    }
}

uint16_t
Dhcpv4Srv::checkRelayPort(const Dhcpv4Exchange& ex) {

    // Look for a relay-port RAI sub-option in the query.
    const Pkt4Ptr& query = ex.getQuery();
    const OptionPtr& rai = query->getOption(DHO_DHCP_AGENT_OPTIONS);
    if (rai && rai->getOption(RAI_OPTION_RELAY_PORT)) {
        // Got the sub-option so use the remote port set by the relay.
        return (query->getRemotePort());
    }
    return (0);
}

void
Dhcpv4Srv::adjustIfaceData(Dhcpv4Exchange& ex) {
    adjustRemoteAddr(ex);

    // Initialize the pointers to the client's message and the server's
    // response.
    Pkt4Ptr query = ex.getQuery();
    Pkt4Ptr response = ex.getResponse();

    // The DHCPINFORM is generally unicast to the client. The only situation
    // when the server is unable to unicast to the client is when the client
    // doesn't include ciaddr and the message is relayed. In this case the
    // server has to reply via relay agent. For other messages we send back
    // through relay if message is relayed, and unicast to the client if the
    // message is not relayed.
    // If client port was set from the command line enforce all responses
    // to it. Of course it is only for testing purposes.
    // Note that the call to this function may throw if invalid combination
    // of hops and giaddr is found (hops = 0 if giaddr = 0 and hops != 0 if
    // giaddr != 0). The exception will propagate down and eventually cause the
    // packet to be discarded.
    if (client_port_) {
        response->setRemotePort(client_port_);
    } else if (((query->getType() == DHCPINFORM) &&
         ((!query->getCiaddr().isV4Zero()) ||
          (!query->isRelayed() && !query->getRemoteAddr().isV4Zero()))) ||
        ((query->getType() != DHCPINFORM) && !query->isRelayed())) {
        response->setRemotePort(DHCP4_CLIENT_PORT);

    } else {
        // RFC 8357 section 5.1
        uint16_t relay_port = checkRelayPort(ex);
        response->setRemotePort(relay_port ? relay_port : DHCP4_SERVER_PORT);
    }

    CfgIfacePtr cfg_iface = CfgMgr::instance().getCurrentCfg()->getCfgIface();
    if (query->isRelayed() &&
        (cfg_iface->getSocketType() == CfgIface::SOCKET_UDP) &&
        (cfg_iface->getOutboundIface() == CfgIface::USE_ROUTING)) {

        // Mark the response to follow routing
        response->setLocalAddr(IOAddress::IPV4_ZERO_ADDRESS());
        response->resetIndex();
        // But keep the interface name
        response->setIface(query->getIface());

    } else {

        IOAddress local_addr = query->getLocalAddr();

        // In many cases the query is sent to a broadcast address. This address
        // appears as a local address in the query message. We can't simply copy
        // this address to a response message and use it as a source address.
        // Instead we will need to use the address assigned to the interface
        // on which the query has been received. In other cases, we will just
        // use this address as a source address for the response.
        // Do the same for DHCPv4-over-DHCPv6 exchanges.
        if (local_addr.isV4Bcast() || query->isDhcp4o6()) {
            local_addr = IfaceMgr::instance().getSocket(query).addr_;
        }

        // We assume that there is an appropriate socket bound to this address
        // and that the address is correct. This is safe assumption because
        // the local address of the query is set when the query is received.
        // The query sent to an incorrect address wouldn't have been received.
        // However, if socket is closed for this address between the reception
        // of the query and sending a response, the IfaceMgr should detect it
        // and return an error.
        response->setLocalAddr(local_addr);
        // In many cases the query is sent to a broadcast address. This address
        // appears as a local address in the query message. Therefore we can't
        // simply copy local address from the query and use it as a source
        // address for the response. Instead, we have to check what address our
        // socket is bound to and use it as a source address. This operation
        // may throw if for some reason the socket is closed.
        /// @todo Consider an optimization that we use local address from
        /// the query if this address is not broadcast.
        response->setIndex(query->getIndex());
        response->setIface(query->getIface());
    }

    if (server_port_) {
        response->setLocalPort(server_port_);
    } else {
        response->setLocalPort(DHCP4_SERVER_PORT);
    }
}

void
Dhcpv4Srv::adjustRemoteAddr(Dhcpv4Exchange& ex) {<--- Parameter 'ex' can be declared as reference to const
    // Initialize the pointers to the client's message and the server's
    // response.
    Pkt4Ptr query = ex.getQuery();
    Pkt4Ptr response = ex.getResponse();

    // DHCPv4-over-DHCPv6 is simple
    if (query->isDhcp4o6()) {
        response->setRemoteAddr(query->getRemoteAddr());
        return;
    }

    // The DHCPINFORM is slightly different than other messages in a sense
    // that the server should always unicast the response to the ciaddr.
    // It appears however that some clients don't set the ciaddr. We still
    // want to provision these clients and we do what we can't to send the
    // packet to the address where client can receive it.
    if (query->getType() == DHCPINFORM) {
        // If client adheres to RFC2131 it will set the ciaddr and in this
        // case we always unicast our response to this address.
        if (!query->getCiaddr().isV4Zero()) {
            response->setRemoteAddr(query->getCiaddr());

        // If we received DHCPINFORM via relay and the ciaddr is not set we
        // will try to send the response via relay. The caveat is that the
        // relay will not have any idea where to forward the packet because
        // the yiaddr is likely not set. So, the broadcast flag is set so
        // as the response may be broadcast.
        } else if (query->isRelayed()) {
            response->setRemoteAddr(query->getGiaddr());
            response->setFlags(response->getFlags() | BOOTP_BROADCAST);

        // If there is no ciaddr and no giaddr the only thing we can do is
        // to use the source address of the packet.
        } else {
            response->setRemoteAddr(query->getRemoteAddr());
        }
        // Remote address is now set so return.
        return;
    }

    // If received relayed message, server responds to the relay address.
    if (query->isRelayed()) {
        // The client should set the ciaddr when sending the DHCPINFORM
        // but in case he didn't, the relay may not be able to determine the
        // address of the client, because yiaddr is not set when responding
        // to Confirm and the only address available was the source address
        // of the client. The source address is however not used here because
        // the message is relayed. Therefore, we set the BROADCAST flag so
        // as the relay can broadcast the packet.
        if ((query->getType() == DHCPINFORM) &&
            query->getCiaddr().isV4Zero()) {
            response->setFlags(BOOTP_BROADCAST);
        }
        response->setRemoteAddr(query->getGiaddr());

    // If giaddr is 0 but client set ciaddr, server should unicast the
    // response to ciaddr.
    } else if (!query->getCiaddr().isV4Zero()) {
        response->setRemoteAddr(query->getCiaddr());

    // We can't unicast the response to the client when sending DHCPNAK,
    // because we haven't allocated address for him. Therefore,
    // DHCPNAK is broadcast.
    } else if (response->getType() == DHCPNAK) {
        response->setRemoteAddr(IOAddress::IPV4_BCAST_ADDRESS());

    // If yiaddr is set it means that we have created a lease for a client.
    } else if (!response->getYiaddr().isV4Zero()) {
        // If the broadcast bit is set in the flags field, we have to
        // send the response to broadcast address. Client may have requested it
        // because it doesn't support reception of messages on the interface
        // which doesn't have an address assigned. The other case when response
        // must be broadcasted is when our server does not support responding
        // directly to a client without address assigned.
        const bool bcast_flag = ((query->getFlags() & Pkt4::FLAG_BROADCAST_MASK) != 0);
        if (!IfaceMgr::instance().isDirectResponseSupported() || bcast_flag) {
            response->setRemoteAddr(IOAddress::IPV4_BCAST_ADDRESS());

        // Client cleared the broadcast bit and we support direct responses
        // so we should unicast the response to a newly allocated address -
        // yiaddr.
        } else {
            response->setRemoteAddr(response ->getYiaddr());

        }

    // In most cases, we should have the remote address found already. If we
    // found ourselves at this point, the rational thing to do is to respond
    // to the address we got the query from.
    } else {
        response->setRemoteAddr(query->getRemoteAddr());
    }

    // For testing *only*.
    if (getSendResponsesToSource()) {
        response->setRemoteAddr(query->getRemoteAddr());
    }
}

void
Dhcpv4Srv::setFixedFields(Dhcpv4Exchange& ex) {
    Pkt4Ptr query = ex.getQuery();
    Pkt4Ptr response = ex.getResponse();

    // Step 1: Start with fixed fields defined on subnet level.
    ConstSubnet4Ptr subnet = ex.getContext()->subnet_;
    if (subnet) {
        IOAddress subnet_next_server = subnet->getSiaddr();
        if (!subnet_next_server.isV4Zero()) {
            response->setSiaddr(subnet_next_server);
        }

        const string& sname = subnet->getSname();
        if (!sname.empty()) {
            // Converting string to (const uint8_t*, size_t len) format is
            // tricky. reinterpret_cast is not the most elegant solution,
            // but it does avoid us making unnecessary copy. We will convert
            // sname and file fields in Pkt4 to string one day and life
            // will be easier.
            response->setSname(reinterpret_cast<const uint8_t*>(sname.c_str()),
                               sname.size());
        }

        const string& filename = subnet->getFilename();
        if (!filename.empty()) {
            // Converting string to (const uint8_t*, size_t len) format is
            // tricky. reinterpret_cast is not the most elegant solution,
            // but it does avoid us making unnecessary copy. We will convert
            // sname and file fields in Pkt4 to string one day and life
            // will be easier.
            response->setFile(reinterpret_cast<const uint8_t*>(filename.c_str()),
                              filename.size());
        }
    }

    // Step 2: Try to set the values based on classes.
    // Any values defined in classes will override those from subnet level.
    const ClientClasses& classes = query->getClasses();
    if (!classes.empty()) {

        // Let's get class definitions
        const ClientClassDictionaryPtr& dict =
            CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();

        // Now we need to iterate over the classes assigned to the
        // query packet and find corresponding class definitions for it.
        // We want the first value found for each field.  We track how
        // many we've found so we can stop if we have all three.
        IOAddress next_server = IOAddress::IPV4_ZERO_ADDRESS();
        string sname;
        string filename;
        size_t found_cnt = 0;  // How many fields we have found.
        for (auto const& name : classes) {

            if (found_cnt >= 3) {
                break;
            }

            ClientClassDefPtr cl = dict->findClass(name);
            if (!cl) {
                // Let's skip classes that don't have definitions. Currently
                // these are automatic classes VENDOR_CLASS_something, but there
                // may be other classes assigned under other circumstances, e.g.
                // by hooks.
                continue;
            }

            if (next_server == IOAddress::IPV4_ZERO_ADDRESS()) {
                next_server = cl->getNextServer();
                if (!next_server.isV4Zero()) {
                    response->setSiaddr(next_server);
                    found_cnt++;
                }
            }

            if (sname.empty()) {
                sname = cl->getSname();
                if (!sname.empty()) {
                    // Converting string to (const uint8_t*, size_t len) format is
                    // tricky. reinterpret_cast is not the most elegant solution,
                    // but it does avoid us making unnecessary copy. We will convert
                    // sname and file fields in Pkt4 to string one day and life
                    // will be easier.
                    response->setSname(reinterpret_cast<const uint8_t*>(sname.c_str()),
                                       sname.size());
                    found_cnt++;
                }
            }

            if (filename.empty()) {
                filename = cl->getFilename();
                if (!filename.empty()) {
                    // Converting string to (const uint8_t*, size_t len) format is
                    // tricky. reinterpret_cast is not the most elegant solution,
                    // but it does avoid us making unnecessary copy. We will convert
                    // sname and file fields in Pkt4 to string one day and life
                    // will be easier.
                    response->setFile(reinterpret_cast<const uint8_t*>(filename.c_str()),
                                     filename.size());
                    found_cnt++;
                }
            }
        }
    }

    // Step 3: try to set values using HR. Any values coming from there will override
    // the subnet or class values.
    ex.setReservedMessageFields();
}

OptionPtr
Dhcpv4Srv::getNetmaskOption(const ConstSubnet4Ptr& subnet) {
    uint32_t netmask = getNetmask4(subnet->get().second).toUint32();

    OptionPtr opt(new OptionInt<uint32_t>(Option::V4,
                  DHO_SUBNET_MASK, netmask));

    return (opt);
}

tuple<bool, uint32_t>
Dhcpv4Srv::parkingLimitExceeded(string const& hook_label) {
    // Get the parking limit. Parsing should ensure the value is present.
    uint32_t parked_packet_limit(0);
    ConstElementPtr const& ppl(
        CfgMgr::instance().getCurrentCfg()->getConfiguredGlobal(CfgGlobals::PARKED_PACKET_LIMIT));
    if (ppl) {
        parked_packet_limit = ppl->intValue();
    }

    if (parked_packet_limit) {
        ParkingLotPtr const& parking_lot(
            ServerHooks::getServerHooks().getParkingLotPtr(hook_label));

        if (parking_lot && parked_packet_limit <= parking_lot->size()) {
            return make_tuple(true, parked_packet_limit);
        }
    }
    return make_tuple(false, parked_packet_limit);
}

Pkt4Ptr
Dhcpv4Srv::processDiscover(Pkt4Ptr& discover, AllocEngine::ClientContext4Ptr& context) {
    bool drop = false;
    Dhcpv4Exchange ex(alloc_engine_, discover, context, context->subnet_, drop);

    // Stop here if Dhcpv4Exchange constructor decided to drop the packet
    if (drop) {
        return (Pkt4Ptr());
    }

    if (MultiThreadingMgr::instance().getMode()) {
        // The lease reclamation cannot run at the same time.
        ReadLockGuard share(alloc_engine_->getReadWriteMutex());

        assignLease(ex);
    } else {
        assignLease(ex);
    }

    if (!ex.getResponse()) {
        // The offer is empty so return it *now*!
        return (Pkt4Ptr());
    }

    // Adding any other options makes sense only when we got the lease
    // or it is for an IPv6-Only client.
    if (!ex.getResponse()->getYiaddr().isV4Zero() || ex.getIPv6OnlyPreferred()) {
        // If this is global reservation or the subnet doesn't belong to a shared
        // network we have already fetched it and evaluated the classes.
        ex.conditionallySetReservedClientClasses();

        // Evaluate additional classes.
        evaluateAdditionalClasses(ex);

        LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASSES_ASSIGNED)
            .arg(discover->getLabel())
            .arg(discover->getName())
            .arg(discover->getClasses().toText());

        buildCfgOptionList(ex);
        appendRequestedOptions(ex);
        // Sanity check for IPv6-Only clients.
        if (ex.getIPv6OnlyPreferred()) {
            if (!ex.getResponse()->getOption(DHO_V6_ONLY_PREFERRED)) {
                // Better to drop the packet than to send an insane response.
                LOG_ERROR(packet4_logger, DHCP4_V6_ONLY_PREFERRED_MISSING_IN_OFFER)
                    .arg(discover->getLabel());
                return (Pkt4Ptr());
            }
        }
        appendRequestedVendorOptions(ex);
        // There are a few basic options that we always want to
        // include in the response. If client did not request
        // them we append them for him.
        appendBasicOptions(ex);

        // Set fixed fields (siaddr, sname, filename) if defined in
        // the reservation, class or subnet specific configuration.
        setFixedFields(ex);

    } else {
        // If the server can't offer an address, it drops the packet.
        return (Pkt4Ptr());

    }

    // Set the src/dest IP address, port and interface for the outgoing
    // packet.
    adjustIfaceData(ex);

    appendServerID(ex);

    // Return the pointer to the context, which will be required by the
    // lease4_offer callouts.
    context = ex.getContext();

    return (ex.getResponse());
}

Pkt4Ptr
Dhcpv4Srv::processRequest(Pkt4Ptr& request, AllocEngine::ClientContext4Ptr& context) {
    bool drop = false;
    Dhcpv4Exchange ex(alloc_engine_, request, context, context->subnet_, drop);

    // Stop here if Dhcpv4Exchange constructor decided to drop the packet
    if (drop) {
        return (Pkt4Ptr());
    }

    // Note that we treat REQUEST message uniformly, regardless if this is a
    // first request (requesting for new address), renewing existing address
    // or even rebinding.
    if (MultiThreadingMgr::instance().getMode()) {
        // The lease reclamation cannot run at the same time.
        ReadLockGuard share(alloc_engine_->getReadWriteMutex());

        assignLease(ex);
    } else {
        assignLease(ex);
    }

    Pkt4Ptr response = ex.getResponse();
    if (!response) {
        // The ack is empty so return it *now*!
        return (Pkt4Ptr());
    } else if (request->inClass("BOOTP")) {
        // Put BOOTP responses in the BOOTP class.
        response->addClass("BOOTP");
    }

    // Adding any other options makes sense only when we got the lease
    // or it is for an IPv6-Only client.
    if (!response->getYiaddr().isV4Zero() || ex.getIPv6OnlyPreferred()) {
        // If this is global reservation or the subnet doesn't belong to a shared
        // network we have already fetched it and evaluated the classes.
        ex.conditionallySetReservedClientClasses();

        // Evaluate additional classes.
        evaluateAdditionalClasses(ex);

        LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASSES_ASSIGNED)
            .arg(request->getLabel())
            .arg(request->getName())
            .arg(request->getClasses().toText());

        buildCfgOptionList(ex);
        appendRequestedOptions(ex);
        // Sanity check for IPv6-Only clients.
        if (ex.getIPv6OnlyPreferred()) {
            if (!response->getOption(DHO_V6_ONLY_PREFERRED)) {
                // Better to drop the packet than to send an insane response.
                LOG_ERROR(packet4_logger, DHCP4_V6_ONLY_PREFERRED_MISSING_IN_ACK)
                    .arg(request->getLabel());
                return (Pkt4Ptr());
            }
        }
        appendRequestedVendorOptions(ex);
        // There are a few basic options that we always want to
        // include in the response. If client did not request
        // them we append them for him.
        appendBasicOptions(ex);

        // Set fixed fields (siaddr, sname, filename) if defined in
        // the reservation, class or subnet specific configuration.
        setFixedFields(ex);
    }

    // Set the src/dest IP address, port and interface for the outgoing
    // packet.
    adjustIfaceData(ex);

    appendServerID(ex);

    // Return the pointer to the context, which will be required by the
    // leases4_committed callouts.
    context = ex.getContext();

    return (ex.getResponse());
}

void
Dhcpv4Srv::processRelease(Pkt4Ptr& release, AllocEngine::ClientContext4Ptr& context) {
    // Try to find client-id. Note that for the DHCPRELEASE we don't check if the
    // match-client-id configuration parameter is disabled because this parameter
    // is configured for subnets and we don't select subnet for the DHCPRELEASE.
    // Bogus clients usually generate new client identifiers when they first
    // connect to the network, so whatever client identifier has been used to
    // acquire the lease, the client identifier carried in the DHCPRELEASE is
    // likely to be the same and the lease will be correctly identified in the
    // lease database. If supplied client identifier differs from the one used
    // to acquire the lease then the lease will remain in the database and
    // simply expire.
    ClientIdPtr client_id;
    OptionPtr opt = release->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
    if (opt) {
        client_id = ClientIdPtr(new ClientId(opt->getData()));
    }

    try {
        // Do we have a lease for that particular address?
        Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(release->getCiaddr());

        if (!lease) {
            // No such lease - bogus release
            LOG_DEBUG(lease4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_NO_LEASE)
                .arg(release->getLabel())
                .arg(release->getCiaddr().toText());
            return;
        }

        if (!lease->belongsToClient(release->getHWAddr(), client_id)) {
            LOG_DEBUG(lease4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_WRONG_CLIENT)
                .arg(release->getLabel())
                .arg(release->getCiaddr().toText());
            return;
        }

        bool skip = false;

        // Execute all callouts registered for lease4_release
        if (HooksManager::calloutsPresent(Hooks.hook_index_lease4_release_)) {
            CalloutHandlePtr callout_handle = getCalloutHandle(release);

            // Use the RAII wrapper to make sure that the callout handle state is
            // reset when this object goes out of scope. All hook points must do
            // it to prevent possible circular dependency between the callout
            // handle and its arguments.
            ScopedCalloutHandleState callout_handle_state(callout_handle);

            // Enable copying options from the packet within hook library.
            ScopedEnableOptionsCopy<Pkt4> query4_options_copy(release);

            // Pass the original packet
            callout_handle->setArgument("query4", release);

            // Pass the lease to be updated
            callout_handle->setArgument("lease4", lease);

            // Call all installed callouts
            HooksManager::callCallouts(Hooks.hook_index_lease4_release_,
                                       *callout_handle);

            // Callouts decided to skip the next processing step. The next
            // processing step would be to send the packet, so skip at this
            // stage means "drop response".
            if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
                (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
                skip = true;
                LOG_DEBUG(hooks_logger, DBGLVL_PKT_HANDLING,
                          DHCP4_HOOK_LEASE4_RELEASE_SKIP)
                    .arg(release->getLabel());
            }
        }

        // Callout didn't indicate to skip the release process. Let's release
        // the lease.
        if (!skip) {
            // Ok, we've passed all checks. Let's release this address.
            bool success = false; // was the removal operation successful?
            bool expired = false; // explicitly expired instead of removed?
            auto expiration_cfg = CfgMgr::instance().getCurrentCfg()->getCfgExpiration();

            // Delete lease only if affinity is disabled.
            if (expiration_cfg->getFlushReclaimedTimerWaitTime() &&
                expiration_cfg->getHoldReclaimedTime() &&
                lease->valid_lft_ != Lease::INFINITY_LFT) {
                // Expire the lease.
                lease->valid_lft_ = 0;
                // Set the lease state to released to indicate that this lease
                // must be preserved in the database. It is particularly useful
                // in HA to differentiate between the leases that should be
                // updated in the partner's database and deleted from the partner's
                // database.
                lease->state_ = Lease4::STATE_RELEASED;
                LeaseMgrFactory::instance().updateLease4(lease);
                expired = true;
                success = true;
            } else {
                success = LeaseMgrFactory::instance().deleteLease(lease);
            }

            if (success) {
                context.reset(new AllocEngine::ClientContext4());
                context->old_lease_ = lease;

                // Release successful
                LOG_INFO(lease4_logger, DHCP4_RELEASE)
                    .arg(release->getLabel())
                    .arg(lease->addr_.toText());

                if (expired) {
                    LOG_INFO(lease4_logger, DHCP4_RELEASE_EXPIRED)
                                            .arg(release->getLabel())
                                            .arg(lease->addr_.toText());
                } else {
                    LOG_INFO(lease4_logger, DHCP4_RELEASE_DELETED)
                        .arg(release->getLabel())
                        .arg(lease->addr_.toText());

                    // Remove existing DNS entries for the lease, if any.
                    queueNCR(CHG_REMOVE, lease);
                }

                // Need to decrease statistic for assigned addresses.
                StatsMgr::instance().addValue(
                    StatsMgr::generateName("subnet", lease->subnet_id_, "assigned-addresses"),
                    static_cast<int64_t>(-1));

                auto const& subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getBySubnetId(lease->subnet_id_);
                if (subnet) {
                    auto const& pool = subnet->getPool(Lease::TYPE_V4, lease->addr_, false);
                    if (pool) {
                        StatsMgr::instance().addValue(
                            StatsMgr::generateName("subnet", subnet->getID(),
                                                   StatsMgr::generateName("pool", pool->getID(), "assigned-addresses")),
                            static_cast<int64_t>(-1));
                    }
                }

            } else {
                // Release failed
                LOG_ERROR(lease4_logger, DHCP4_RELEASE_FAIL)
                    .arg(release->getLabel())
                    .arg(lease->addr_.toText());
            }
        }
    } catch (const isc::Exception& ex) {
        LOG_ERROR(lease4_logger, DHCP4_RELEASE_EXCEPTION)
            .arg(release->getLabel())
            .arg(release->getCiaddr())
            .arg(ex.what());
    }
}

void
Dhcpv4Srv::processDecline(Pkt4Ptr& decline, AllocEngine::ClientContext4Ptr& context) {
    // Client is supposed to specify the address being declined in
    // Requested IP address option, but must not set its ciaddr.
    // (again, see table 5 in RFC2131).

    OptionCustomPtr opt_requested_address = boost::dynamic_pointer_cast<
        OptionCustom>(decline->getOption(DHO_DHCP_REQUESTED_ADDRESS));
    if (!opt_requested_address) {

        isc_throw(RFCViolation, "Mandatory 'Requested IP address' option missing"
                  " in DHCPDECLINE sent from " << decline->getLabel());
    }
    IOAddress addr(opt_requested_address->readAddress());

    // We could also extract client's address from ciaddr, but that's clearly
    // against RFC2131.

    // Now we need to check whether this address really belongs to the client
    // that attempts to decline it.
    const Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(addr);

    if (!lease) {
        // Client tried to decline an address, but we don't have a lease for
        // that address. Let's ignore it.
        //
        // We could assume that we're recovering from a mishandled migration
        // to a new server and mark the address as declined, but the window of
        // opportunity for that to be useful is small and the attack vector
        // would be pretty severe.
        LOG_WARN(dhcp4_logger, DHCP4_DECLINE_LEASE_NOT_FOUND)
            .arg(addr.toText()).arg(decline->getLabel());
        return;
    }

    // Get client-id, if available.
    OptionPtr opt_clientid = decline->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
    ClientIdPtr client_id;
    if (opt_clientid) {
        client_id.reset(new ClientId(opt_clientid->getData()));
    }

    // Check if the client attempted to decline an expired lease or a lease
    // it doesn't own. Declining expired leases is typically a client
    // misbehavior and may lead to pool exhaustion in case of a storm of
    // such declines. Only decline the lease if the lease has been recently
    // allocated to the client.
    if (lease->expired() || lease->state_ != Lease::STATE_DEFAULT ||
        !lease->belongsToClient(decline->getHWAddr(), client_id)) {

        // Get printable hardware addresses
        string client_hw = decline->getHWAddr() ?
            decline->getHWAddr()->toText(false) : "(none)";
        string lease_hw = lease->hwaddr_ ?
            lease->hwaddr_->toText(false) : "(none)";

        // Get printable client-ids
        string client_id_txt = client_id ? client_id->toText() : "(none)";
        string lease_id_txt = lease->client_id_ ?
            lease->client_id_->toText() : "(none)";

        // Print the warning and we're done here.
        LOG_WARN(dhcp4_logger, DHCP4_DECLINE_LEASE_MISMATCH)
            .arg(addr.toText()).arg(decline->getLabel())
            .arg(client_hw).arg(lease_hw).arg(client_id_txt).arg(lease_id_txt);

        return;
    }

    // Ok, all is good. The client is reporting its own address. Let's
    // process it.
    declineLease(lease, decline, context);
}

void
Dhcpv4Srv::declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline,
                        AllocEngine::ClientContext4Ptr& context) {

    // Let's check if there are hooks installed for decline4 hook point.
    // If they are, let's pass the lease and client's packet. If the hook
    // sets status to drop, we reject this Decline.
    if (HooksManager::calloutsPresent(Hooks.hook_index_lease4_decline_)) {
        CalloutHandlePtr callout_handle = getCalloutHandle(decline);

        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        ScopedCalloutHandleState callout_handle_state(callout_handle);

        // Enable copying options from the packet within hook library.
        ScopedEnableOptionsCopy<Pkt4> query4_options_copy(decline);

        // Pass the original packet
        callout_handle->setArgument("query4", decline);

        // Pass the lease to be updated
        callout_handle->setArgument("lease4", lease);

        // Call callouts
        HooksManager::callCallouts(Hooks.hook_index_lease4_decline_,
                                   *callout_handle);

        // Check if callouts decided to skip the next processing step.
        // If any of them did, we will drop the packet.
        if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
            (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
            LOG_DEBUG(hooks_logger, DBGLVL_PKT_HANDLING, DHCP4_HOOK_DECLINE_SKIP)
                .arg(decline->getLabel()).arg(lease->addr_.toText());
            return;
        }
    }

    Lease4Ptr old_values = boost::make_shared<Lease4>(*lease);

    // @todo: Call hooks.

    // We need to disassociate the lease from the client. Once we move a lease
    // to declined state, it is no longer associated with the client in any
    // way.
    lease->decline(CfgMgr::instance().getCurrentCfg()->getDeclinePeriod());

    try {
        LeaseMgrFactory::instance().updateLease4(lease);
    } catch (const Exception& ex) {
        // Update failed.
        LOG_ERROR(lease4_logger, DHCP4_DECLINE_FAIL)
            .arg(decline->getLabel())
            .arg(lease->addr_.toText())
            .arg(ex.what());
        return;
    }

    // Remove existing DNS entries for the lease, if any.
    // queueNCR will do the necessary checks and will skip the update, if not needed.
    queueNCR(CHG_REMOVE, old_values);

    // Bump up the statistics.

    // Per subnet declined addresses counter.
    StatsMgr::instance().addValue(
        StatsMgr::generateName("subnet", lease->subnet_id_, "declined-addresses"),
        static_cast<int64_t>(1));

    auto const& subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getBySubnetId(lease->subnet_id_);
    if (subnet) {
        auto const& pool = subnet->getPool(Lease::TYPE_V4, lease->addr_, false);
        if (pool) {
            StatsMgr::instance().addValue(
                StatsMgr::generateName("subnet", subnet->getID(),
                                       StatsMgr::generateName("pool", pool->getID(), "declined-addresses")),
                static_cast<int64_t>(1));
        }
    }

    // Global declined addresses counter.
    StatsMgr::instance().addValue("declined-addresses", static_cast<int64_t>(1));

    // We do not want to decrease the assigned-addresses at this time. While
    // technically a declined address is no longer allocated, the primary usage
    // of the assigned-addresses statistic is to monitor pool utilization. Most
    // people would forget to include declined-addresses in the calculation,
    // and simply do assigned-addresses/total-addresses. This would have a bias
    // towards under-representing pool utilization, if we decreased allocated
    // immediately after receiving DHCPDECLINE, rather than later when we recover
    // the address.

    context.reset(new AllocEngine::ClientContext4());
    context->new_lease_ = lease;

    LOG_INFO(lease4_logger, DHCP4_DECLINE_LEASE).arg(lease->addr_.toText())
        .arg(decline->getLabel()).arg(lease->valid_lft_);
}

void
Dhcpv4Srv::serverDecline(hooks::CalloutHandlePtr& callout_handle, Pkt4Ptr& query,
                         Lease4Ptr lease, bool lease_exists) {
    LOG_INFO(lease4_logger, DHCP4_SERVER_INITIATED_DECLINE)
            .arg(query->getLabel())
            .arg(lease->addr_.toText())
            .arg(lease->valid_lft_);

    {
        // Check if the resource is busy i.e. can be modified by another thread
        // for another client. Highly unlikely.
        ResourceHandler4 resource_handler;
        if (MultiThreadingMgr::instance().getMode() && !resource_handler.tryLock4(lease->addr_)) {
            LOG_ERROR(lease4_logger, DHCP4_SERVER_INITIATED_DECLINE_RESOURCE_BUSY)
                .arg(query->getLabel())
                .arg(lease->addr_.toText());
            return;
        }

        // We need to disassociate the lease from the client. Once we move a lease
        // to declined state, it is no longer associated with the client in any
        // way.
        lease->decline(CfgMgr::instance().getCurrentCfg()->getDeclinePeriod());

        // If the lease already exists, update it in the database.
        if (lease_exists) {
            try {
                LeaseMgrFactory::instance().updateLease4(lease);
            } catch (const NoSuchLease& ex) {
                // We expected the lease to exist but it doesn't so let's try
                // to add it.
                lease_exists = false;
            } catch (const Exception& ex) {
                // Update failed.
                LOG_ERROR(lease4_logger, DHCP4_SERVER_INITIATED_DECLINE_UPDATE_FAILED)
                    .arg(query->getLabel())
                    .arg(lease->addr_.toText());
                return;
            }
        }

        if (!lease_exists) {
            try {
                LeaseMgrFactory::instance().addLease(lease);
            } catch (const Exception& ex) {
                LOG_ERROR(lease4_logger, DHCP4_SERVER_INITIATED_DECLINE_ADD_FAILED)
                    .arg(query->getLabel())
                    .arg(lease->addr_.toText());
                return;
            }
        }
    }

    // Bump up the statistics.  If the lease does not exist (i.e. offer-lifetime == 0) we
    // need to increment assigned address stats, otherwise the accounting will be off.
    // This saves us from having to determine later, when declined leases are reclaimed,
    // whether or not we need to decrement assigned stats.  In other words, this keeps
    // a declined lease always counted also as an assigned lease, regardless of how
    // it was declined, until it is reclaimed at which point both groups of stats
    // are decremented.

    // Per subnet declined addresses counter.
    StatsMgr::instance().addValue(
        StatsMgr::generateName("subnet", lease->subnet_id_, "declined-addresses"),
        static_cast<int64_t>(1));

    if (!lease_exists) {
        StatsMgr::instance().addValue(
            StatsMgr::generateName("subnet", lease->subnet_id_, "assigned-addresses"),
            static_cast<int64_t>(1));
    }

    auto const& subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getBySubnetId(lease->subnet_id_);
    if (subnet) {
        auto const& pool = subnet->getPool(Lease::TYPE_V4, lease->addr_, false);
        if (pool) {
            StatsMgr::instance().addValue(
                StatsMgr::generateName("subnet", subnet->getID(),
                                       StatsMgr::generateName("pool", pool->getID(), "declined-addresses")),
                static_cast<int64_t>(1));
            if (!lease_exists) {
                StatsMgr::instance().addValue(
                    StatsMgr::generateName("subnet", subnet->getID(),
                                           StatsMgr::generateName("pool", pool->getID(), "assigned-addresses")),
                    static_cast<int64_t>(1));
            }
        }
    }

    // Global declined addresses counter.
    StatsMgr::instance().addValue("declined-addresses", static_cast<int64_t>(1));
    if (!lease_exists) {
        StatsMgr::instance().addValue("assigned-addresses", static_cast<int64_t>(1));
    }

    // Let's check if there are hooks installed for server decline hook point.
    // If there are, let's pass the DHCPDISCOVER and the declined lease .
    if (HooksManager::calloutsPresent(Hooks.hook_index_lease4_server_decline_)) {
        // Use the RAII wrapper to make sure that the callout handle state is
        // reset when this object goes out of scope. All hook points must do
        // it to prevent possible circular dependency between the callout
        // handle and its arguments.
        ScopedCalloutHandleState callout_handle_state(callout_handle);

        // Pass in the original DHCPDISCOVER
        callout_handle->setArgument("query4", query);

        // Pass in the declined lease.
        callout_handle->setArgument("lease4", lease);

        // Call callouts
        HooksManager::callCallouts(Hooks.hook_index_lease4_server_decline_,
                                   *callout_handle);
    }
}

void
Dhcpv4Srv::serverDeclineNoThrow(hooks::CalloutHandlePtr& callout_handle, Pkt4Ptr& query,
                                Lease4Ptr lease, bool lease_exists) {
    try {
        serverDecline(callout_handle, query, lease, lease_exists);
    } catch (...) {
        LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION).arg(query->getLabel());
    }
}

Pkt4Ptr
Dhcpv4Srv::processInform(Pkt4Ptr& inform, AllocEngine::ClientContext4Ptr& context) {
    bool drop = false;
    Dhcpv4Exchange ex(alloc_engine_, inform, context, context->subnet_, drop);

    // Stop here if Dhcpv4Exchange constructor decided to drop the packet
    if (drop) {
        return (Pkt4Ptr());
    }

    Pkt4Ptr ack = ex.getResponse();

    // If this is global reservation or the subnet doesn't belong to a shared
    // network we have already fetched it and evaluated the classes.
    ex.conditionallySetReservedClientClasses();

    // Evaluate additional classes.
    evaluateAdditionalClasses(ex);

    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASSES_ASSIGNED)
        .arg(inform->getLabel())
        .arg(inform->getName())
        .arg(inform->getClasses().toText());

    buildCfgOptionList(ex);
    appendRequestedOptions(ex);
    appendRequestedVendorOptions(ex);
    appendBasicOptions(ex);
    adjustIfaceData(ex);

    // Set fixed fields (siaddr, sname, filename) if defined in
    // the reservation, class or subnet specific configuration.
    setFixedFields(ex);

    // There are cases for the DHCPINFORM that the server receives it via
    // relay but will send the response to the client's unicast address
    // carried in the ciaddr. In this case, the giaddr and hops field should
    // be cleared (these fields were copied by the copyDefaultFields function).
    // Also Relay Agent Options should be removed if present.
    if (ack->getRemoteAddr() != inform->getGiaddr()) {
        LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL, DHCP4_INFORM_DIRECT_REPLY)
            .arg(inform->getLabel())
            .arg(ack->getRemoteAddr())
            .arg(ack->getIface());
        ack->setHops(0);
        ack->setGiaddr(IOAddress::IPV4_ZERO_ADDRESS());
        ack->delOption(DHO_DHCP_AGENT_OPTIONS);
    }

    // The DHCPACK must contain server id.
    appendServerID(ex);

    return (ex.getResponse());
}

void
Dhcpv4Srv::recoverStashedAgentOption(const Pkt4Ptr& query) {
    if (query->getCiaddr().isV4Zero() || !query->getGiaddr().isV4Zero()) {
        return;
    }
    ConstElementPtr sao = CfgMgr::instance().getCurrentCfg()->
        getConfiguredGlobal(CfgGlobals::STASH_AGENT_OPTIONS);
    if (!sao || (sao->getType() != Element::boolean) || !sao->boolValue()) {
        return;
    }
    if (query->getType() != DHCPREQUEST) {
        return;
    }
    OptionPtr rai_opt = query->getOption(DHO_DHCP_AGENT_OPTIONS);
    if (rai_opt && (rai_opt->len() > Option::OPTION4_HDR_LEN)) {
        return;
    }
    // Should not happen but makes sense to check and gives a trivial way
    // to disable the feature from previous callout points.
    if (query->inClass("STASH_AGENT_OPTIONS")) {
        return;
    }
    Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(query->getCiaddr());
    if (!lease || lease->expired()) {
        return;
    }
    ConstElementPtr user_context = lease->getContext();
    if (!user_context || (user_context->getType() != Element::map)) {
        return;
    }
    ConstElementPtr isc = user_context->get("ISC");
    if (!isc || (isc->getType() != Element::map)) {
        return;
    }
    ConstElementPtr relay_agent_info = isc->get("relay-agent-info");
    if (!relay_agent_info) {
        return;
    }
    // Compatibility with the old layout.
    if (relay_agent_info->getType() == Element::map) {
        relay_agent_info = relay_agent_info->get("sub-options");
        if (!relay_agent_info) {
            return;
        }
    }
    if (relay_agent_info->getType() != Element::string) {
        return;
    }
    // Check ownership before going further.
    ClientIdPtr client_id;
    OptionPtr opt_clientid = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
    if (opt_clientid) {
        client_id.reset(new ClientId(opt_clientid->getData()));
    }
    if (!lease->belongsToClient(query->getHWAddr(), client_id)) {
        return;
    }
    // Extract the RAI.
    string rai_hex = relay_agent_info->stringValue();
    if (rai_hex.empty()) {
        return;
    }
    vector<uint8_t> rai_data;
    str::decodeFormattedHexString(rai_hex, rai_data);
    static const OptionDefinition& rai_def = LibDHCP::DHO_DHCP_AGENT_OPTIONS_DEF();
    OptionCustomPtr rai(new OptionCustom(rai_def, Option::V4, rai_data));
    // unpackOptions is a bit too flexible so check if it got something...
    if (!rai || rai->getOptions().empty()) {
        return;
    }
    // Remove an existing empty RAI.
    if (rai_opt) {
        query->delOption(DHO_DHCP_AGENT_OPTIONS);
    }
    query->addOption(rai);
    query->addClass("STASH_AGENT_OPTIONS");
    LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA,
              DHCP4_RECOVERED_STASHED_RELAY_AGENT_INFO)
      .arg(query->getLabel())
      .arg(query->getCiaddr())
      .arg(rai->toText());
}

bool
Dhcpv4Srv::accept(const Pkt4Ptr& query) {
    // Check that the message type is accepted by the server. We rely on the
    // function called to log a message if needed.
    if (!acceptMessageType(query)) {
        return (false);
    }
    // Check if the message from directly connected client (if directly
    // connected) should be dropped or processed.
    if (!acceptDirectRequest(query)) {
        LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0002)
            .arg(query->getLabel())
            .arg(query->getIface());
        return (false);
    }

    // Check if the DHCPv4 packet has been sent to us or to someone else.
    // If it hasn't been sent to us, drop it!
    if (!acceptServerId(query)) {
        LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0003)
            .arg(query->getLabel())
            .arg(query->getIface());
        return (false);
    }

    return (true);
}

bool
Dhcpv4Srv::acceptDirectRequest(const Pkt4Ptr& pkt) {
    // Accept all relayed messages.
    if (pkt->isRelayed()) {
        return (true);
    }

    // Accept all DHCPv4-over-DHCPv6 messages.
    if (pkt->isDhcp4o6()) {
        return (true);
    }

    // The source address must not be zero for the DHCPINFORM message from
    // the directly connected client because the server will not know where
    // to respond if the ciaddr was not present.
    try {
        if (pkt->getType() == DHCPINFORM) {
            if (pkt->getRemoteAddr().isV4Zero() &&
                pkt->getCiaddr().isV4Zero()) {
                return (false);
            }
        }
    } catch (...) {
        // If we got here, it is probably because the message type hasn't
        // been set. But, this should not really happen assuming that
        // we validate the message type prior to calling this function.
        return (false);
    }
    bool drop = false;
    bool result = (!pkt->getLocalAddr().isV4Bcast() ||
                   selectSubnet(pkt, drop, true));
    if (drop) {
        // The packet must be dropped but as sanity_only is true it is dead code.
        return (false);
    }
    return (result);
}

bool
Dhcpv4Srv::acceptMessageType(const Pkt4Ptr& query) const {
    // When receiving a packet without message type option, getType() will
    // throw.
    int type;
    try {
        type = query->getType();

    } catch (...) {
        LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0004)
            .arg(query->getLabel())
            .arg(query->getIface());
        return (false);
    }

    // Once we know that the message type is within a range of defined DHCPv4
    // messages, we do a detailed check to make sure that the received message
    // is targeted at server. Note that we could have received some Offer
    // message broadcasted by the other server to a relay. Even though, the
    // server would rather unicast its response to a relay, let's be on the
    // safe side. Also, we want to drop other messages which we don't support.
    // All these valid messages that we are not going to process are dropped
    // silently.

    switch(type) {
        case DHCPDISCOVER:
        case DHCPREQUEST:
        case DHCPRELEASE:
        case DHCPDECLINE:
        case DHCPINFORM:
            return (true);
            break;

        case DHCP_NOTYPE:
            LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0009)
                     .arg(query->getLabel());
            break;

        default:
            // If we receive a message with a non-existing type, we are logging it.
            if (type >= DHCP_TYPES_EOF) {
                LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0005)
                          .arg(query->getLabel())
                          .arg(type);
            } else {
                // Exists but we don't support it.
                LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0006)
                      .arg(query->getLabel())
                      .arg(type);
            }
            break;
    }

    return (false);
}

bool
Dhcpv4Srv::acceptServerId(const Pkt4Ptr& query) const {
    // This function is meant to be called internally by the server class, so
    // we rely on the caller to sanity check the pointer and we don't check
    // it here.

    // Check if server identifier option is present. If it is not present
    // we accept the message because it is targeted to all servers.
    // Note that we don't check cases that server identifier is mandatory
    // but not present. This is meant to be sanity checked in other
    // functions.
    OptionPtr option = query->getOption(DHO_DHCP_SERVER_IDENTIFIER);
    if (!option) {
        return (true);
    }
    // Server identifier is present. Let's convert it to 4-byte address
    // and try to match with server identifiers used by the server.
    OptionCustomPtr option_custom =
        boost::dynamic_pointer_cast<OptionCustom>(option);
    // Unable to convert the option to the option type which encapsulates it.
    // We treat this as non-matching server id.
    if (!option_custom) {
        return (false);
    }
    // The server identifier option should carry exactly one IPv4 address.
    // If the option definition for the server identifier doesn't change,
    // the OptionCustom object should have exactly one IPv4 address and
    // this check is somewhat redundant. On the other hand, if someone
    // breaks option it may be better to check that here.
    if (option_custom->getDataFieldsNum() != 1) {
        return (false);
    }

    // The server identifier MUST be an IPv4 address. If given address is
    // v6, it is wrong.
    IOAddress server_id = option_custom->readAddress();
    if (!server_id.isV4()) {
        return (false);
    }

    // According to RFC5107, the RAI_OPTION_SERVER_ID_OVERRIDE option if
    // present, should match DHO_DHCP_SERVER_IDENTIFIER option.
    OptionPtr rai_option = query->getOption(DHO_DHCP_AGENT_OPTIONS);
    if (rai_option) {
        OptionPtr rai_suboption = rai_option->getOption(RAI_OPTION_SERVER_ID_OVERRIDE);
        if (rai_suboption && (server_id.toBytes() == rai_suboption->toBinary())) {
            return (true);
        }
    }

    // Skip address check if configured to ignore the server id.
    SrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
    if (cfg->getIgnoreServerIdentifier()) {
        return (true);
    }

    // This function iterates over all interfaces on which the
    // server is listening to find the one which has a socket bound
    // to the address carried in the server identifier option.
    // This has some performance implications. However, given that
    // typically there will be just a few active interfaces the
    // performance hit should be acceptable. If it turns out to
    // be significant, we will have to cache server identifiers
    // when sockets are opened.
    if (IfaceMgr::instance().hasOpenSocket(server_id)) {
        return (true);
    }

    // There are some cases when an administrator explicitly sets server
    // identifier (option 54) that should be used for a given, subnet,
    // network etc. It doesn't have to be an address assigned to any of
    // the server interfaces. Thus, we have to check if the server
    // identifier received is the one that we explicitly set in the
    // server configuration. At this point, we don't know which subnet
    // the client belongs to so we can't match the server id with any
    // subnet. We simply check if this server identifier is configured
    // anywhere. This should be good enough to eliminate exchanges
    // with other servers in the same network.

    /// @todo Currently we only check server identifiers configured at the
    /// subnet, shared network, client class and global levels.
    /// This should be sufficient for most of cases. At this point, trying to
    /// support server identifiers on the host reservations level seems to be an
    /// overkill and is probably not needed. In fact, at this point we don't
    /// know the reservations for the client communicating with the server.
    /// We may revise some of these choices in the future.

    // Check if there is at least one subnet configured with this server
    // identifier.
    ConstCfgSubnets4Ptr cfg_subnets = cfg->getCfgSubnets4();
    if (cfg_subnets->hasSubnetWithServerId(server_id)) {
        return (true);
    }

    // This server identifier is not configured for any of the subnets, so
    // check on the shared network level.
    CfgSharedNetworks4Ptr cfg_networks = cfg->getCfgSharedNetworks4();
    if (cfg_networks->hasNetworkWithServerId(server_id)) {
        return (true);
    }

    // Check if the server identifier is configured at client class level.
    const ClientClasses& classes = query->getClasses();
    for (auto const& cclass : classes) {<--- Consider using std::any_of algorithm instead of a raw loop.
        // Find the client class definition for this class
        const ClientClassDefPtr& ccdef = CfgMgr::instance().getCurrentCfg()->
            getClientClassDictionary()->findClass(cclass);
        if (!ccdef) {
            continue;
        }

        if (ccdef->getCfgOption()->empty()) {
            // Skip classes which don't configure options
            continue;
        }

        OptionCustomPtr context_opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
                (ccdef->getCfgOption()->get(DHCP4_OPTION_SPACE, DHO_DHCP_SERVER_IDENTIFIER).option_);
        if (context_opt_server_id && (context_opt_server_id->readAddress() == server_id)) {
            return (true);
        }
    }

    // Finally, it is possible that the server identifier is specified
    // on the global level.
    ConstCfgOptionPtr cfg_global_options = cfg->getCfgOption();
    OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
        (cfg_global_options->get(DHCP4_OPTION_SPACE, DHO_DHCP_SERVER_IDENTIFIER).option_);

    return (opt_server_id && (opt_server_id->readAddress() == server_id));
}

void
Dhcpv4Srv::sanityCheck(const Pkt4Ptr& query) {
    switch (query->getType()) {
    case DHCPDISCOVER:
        // server-id is forbidden.
        sanityCheck(query, FORBIDDEN);
        break;
    case DHCPREQUEST:
        // Since we cannot distinguish  between client states
        // we'll make server-id is optional for REQUESTs.
        sanityCheck(query, OPTIONAL);
        break;
    case DHCPRELEASE:
        // Server-id is mandatory in DHCPRELEASE (see table 5, RFC2131)
        // but ISC DHCP does not enforce this, so we'll follow suit.
        sanityCheck(query, OPTIONAL);
        break;
    case DHCPDECLINE:
        // Server-id is mandatory in DHCPDECLINE (see table 5, RFC2131)
        // but ISC DHCP does not enforce this, so we'll follow suit.
        sanityCheck(query, OPTIONAL);
        break;
    case DHCPINFORM:
        // server-id is supposed to be forbidden (as is requested address)
        // but ISC DHCP does not enforce either. So neither will we.
        sanityCheck(query, OPTIONAL);
        break;
    }
}

void
Dhcpv4Srv::sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid) {
    OptionPtr server_id = query->getOption(DHO_DHCP_SERVER_IDENTIFIER);
    switch (serverid) {
    case FORBIDDEN:
        if (server_id) {
            isc_throw(RFCViolation, "Server-id option was not expected, but"
                      << " received in message "
                      << query->getName());
        }
        break;

    case MANDATORY:
        if (!server_id) {
            isc_throw(RFCViolation, "Server-id option was expected, but not"
                      " received in message "
                      << query->getName());
        }
        break;

    case OPTIONAL:
        // do nothing here
        ;
    }

    // If there is HWAddress set and it is non-empty, then we're good
    if (query->getHWAddr() && !query->getHWAddr()->hwaddr_.empty()) {
        return;
    }

    // There has to be something to uniquely identify the client:
    // either non-zero MAC address or client-id option present (or both)
    OptionPtr client_id = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);

    // If there's no client-id (or a useless one is provided, i.e. 0 length)
    if (!client_id || client_id->len() == client_id->getHeaderLen()) {
        isc_throw(RFCViolation, "Missing or useless client-id and no HW address"
                  " provided in message "
                  << query->getName());
    }
}

void Dhcpv4Srv::classifyPacket(const Pkt4Ptr& pkt) {
    Dhcpv4Exchange::classifyPacket(pkt);
}

void Dhcpv4Srv::evaluateAdditionalClasses(Dhcpv4Exchange& ex) {<--- Shadowed declaration<--- Parameter 'ex' can be declared as reference to const
    // First collect required classes
    Pkt4Ptr query = ex.getQuery();
    ClientClasses classes = query->getAdditionalClasses();
    ConstSubnet4Ptr subnet = ex.getContext()->subnet_;

    if (subnet) {
        // host reservation???

        // Begin by the pool
        Pkt4Ptr resp = ex.getResponse();
        IOAddress addr = IOAddress::IPV4_ZERO_ADDRESS();
        if (resp) {
            addr = resp->getYiaddr();
        }
        if (!addr.isV4Zero()) {
            PoolPtr pool = subnet->getPool(Lease::TYPE_V4, addr, false);
            if (pool) {
                const ClientClasses& pool_to_add = pool->getAdditionalClasses();
                for (auto const& cclass : pool_to_add) {
                    classes.insert(cclass);
                }
            }
        }

        // Followed by the subnet
        const ClientClasses& to_add = subnet->getAdditionalClasses();
        for (auto const& cclass : to_add) {
            classes.insert(cclass);
        }

        // And finish by the shared-network
        SharedNetwork4Ptr network;
        subnet->getSharedNetwork(network);
        if (network) {
            const ClientClasses& net_to_add = network->getAdditionalClasses();
            for (auto const& cclass : net_to_add) {
                classes.insert(cclass);
            }
        }
    }

    // Run match expressions
    // Note getClientClassDictionary() cannot be null
    const ClientClassDictionaryPtr& dict =
        CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
    for (auto const& cclass : classes) {
        const ClientClassDefPtr class_def = dict->findClass(cclass);
        if (!class_def) {
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC,
                      DHCP4_ADDITIONAL_CLASS_UNDEFINED)
                .arg(cclass);
            // Ignore it as it can't have an attached action
            continue;
        }
        const ExpressionPtr& expr_ptr = class_def->getMatchExpr();
        // Add a class without an expression to evaluate
        if (!expr_ptr) {
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC,
                      DHCP4_ADDITIONAL_CLASS_NO_TEST)
                .arg(cclass);
            query->addClass(cclass);
            continue;
        }
        // Evaluate the expression which can return false (no match),
        // true (match) or raise an exception (error)
        try {
            bool status = evaluateBool(*expr_ptr, *query);
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_ADDITIONAL_CLASS_EVAL_RESULT)
                .arg(query->getLabel())
                .arg(cclass)
                .arg(status ? "true" : "false");
            if (status) {
                // Matching: add the class
                query->addClass(cclass);
            }
        } catch (const Exception& ex) {<--- Shadow variable
            LOG_ERROR(dhcp4_logger, DHCP4_ADDITIONAL_CLASS_EVAL_ERROR)
                .arg(query->getLabel())
                .arg(cclass)
                .arg(ex.what());
        }
    }
}

void
Dhcpv4Srv::deferredUnpack(Pkt4Ptr& query) {
    // Iterate on the list of deferred option codes
    for (auto const& code : query->getDeferredOptions()) {
        OptionDefinitionPtr def;
        // Iterate on client classes
        const ClientClasses& classes = query->getClasses();
        for (auto const& cclass : classes) {
            // Get the client class definition for this class
            const ClientClassDefPtr& ccdef =
                CfgMgr::instance().getCurrentCfg()->
                getClientClassDictionary()->findClass(cclass);
            // If not found skip it
            if (!ccdef) {
                continue;
            }
            // If there is no option definition skip it
            if (!ccdef->getCfgOptionDef()) {
                continue;
            }
            def = ccdef->getCfgOptionDef()->get(DHCP4_OPTION_SPACE, code);
            // Stop at the first client class with a definition
            if (def) {
                break;
            }
        }
        // If not found try the global definition
        if (!def) {
            def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, code);
        }
        if (!def) {
            def = LibDHCP::getRuntimeOptionDef(DHCP4_OPTION_SPACE, code);
        }
        // Finish by last resort definition
        if (!def) {
            def = LibDHCP::getLastResortOptionDef(DHCP4_OPTION_SPACE, code);
        }
        // If not defined go to the next option
        if (!def) {
            continue;
        }
        // Get the existing option for its content and remove all
        OptionPtr opt = query->getOption(code);
        if (!opt) {
            // should not happen but do not crash anyway
            LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
                      DHCP4_DEFERRED_OPTION_MISSING)
                .arg(query->getLabel())
                .arg(code);
            continue;
        }
        // Because options have already been fused, the buffer contains entire
        // data.
        const OptionBuffer buf = opt->getData();
        try {
            // Unpack the option
            opt = def->optionFactory(Option::V4, code, buf);
        } catch (const std::exception& e) {
            // Failed to parse the option.
            LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
                      DHCP4_DEFERRED_OPTION_UNPACK_FAIL)
                .arg(query->getLabel())
                .arg(code)
                .arg(e.what());
            continue;
        }
        while (query->delOption(code)) {
            // continue
        }
        // Add the unpacked option.
        query->addOption(opt);
    }
}

void
Dhcpv4Srv::startD2() {
    D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
    if (d2_mgr.ddnsEnabled()) {
        // Updates are enabled, so lets start the sender, passing in
        // our error handler.
        // This may throw so wherever this is called needs to ready.
        d2_mgr.startSender(std::bind(&Dhcpv4Srv::d2ClientErrorHandler,
                                     this, ph::_1, ph::_2));
    }
}

void
Dhcpv4Srv::stopD2() {
    D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
    if (d2_mgr.ddnsEnabled()) {
        // Updates are enabled, so lets stop the sender
        d2_mgr.stop();
        d2_mgr.stopSender();
    }
}

void
Dhcpv4Srv::d2ClientErrorHandler(const
                                dhcp_ddns::NameChangeSender::Result result,
                                dhcp_ddns::NameChangeRequestPtr& ncr) {
    LOG_ERROR(ddns4_logger, DHCP4_DDNS_REQUEST_SEND_FAILED).
              arg(result).arg((ncr ? ncr->toText() : " NULL "));
    // We cannot communicate with kea-dhcp-ddns, suspend further updates.
    /// @todo We may wish to revisit this, but for now we will simply turn
    /// them off.
    CfgMgr::instance().getD2ClientMgr().suspendUpdates();
}

std::string
Dhcpv4Srv::getVersion(bool extended) {<--- Derived function 'Dhcpv4Srv::getVersion'
    std::stringstream tmp;

    tmp << VERSION;
    if (extended) {
        tmp << " (" << EXTENDED_VERSION << ")" << endl;
        tmp << "premium: " << PREMIUM_EXTENDED_VERSION << endl;
        tmp << "linked with:" << endl;
        tmp << "- " << Logger::getVersion() << endl;
        tmp << "- " << CryptoLink::getVersion();
        auto info = LeaseMgrFactory::getDBVersions();
        if (info.size()) {
            tmp << endl << "lease backends:";
            for (auto const& version : info) {
                tmp << endl << "- " << version;
            }
        }
        info = HostDataSourceFactory::getDBVersions();
        if (info.size()) {
            tmp << endl << "host backends:";
            for (auto const& version : info) {
                tmp << endl << "- " << version;
            }
        }
        // @todo: more details about database runtime
    }

    return (tmp.str());
}

void Dhcpv4Srv::processStatsReceived(const Pkt4Ptr& query) {
    // Note that we're not bumping pkt4-received statistic as it was
    // increased early in the packet reception code.

    string stat_name = "pkt4-unknown-received";
    try {
        switch (query->getType()) {
        case DHCPDISCOVER:
            stat_name = "pkt4-discover-received";
            break;
        case DHCPOFFER:
            // Should not happen, but let's keep a counter for it
            stat_name = "pkt4-offer-received";
            break;
        case DHCPREQUEST:
            stat_name = "pkt4-request-received";
            break;
        case DHCPACK:
            // Should not happen, but let's keep a counter for it
            stat_name = "pkt4-ack-received";
            break;
        case DHCPNAK:
            // Should not happen, but let's keep a counter for it
            stat_name = "pkt4-nak-received";
            break;
        case DHCPRELEASE:
            stat_name = "pkt4-release-received";
        break;
        case DHCPDECLINE:
            stat_name = "pkt4-decline-received";
            break;
        case DHCPINFORM:
            stat_name = "pkt4-inform-received";
            break;
        default:
            ; // do nothing
        }
    }
    catch (...) {
        // If the incoming packet doesn't have option 53 (message type)
        // or a hook set pkt4_receive_skip, then Pkt4::getType() may
        // throw an exception. That's ok, we'll then use the default
        // name of pkt4-unknown-received.
    }

    isc::stats::StatsMgr::instance().addValue(stat_name,
                                              static_cast<int64_t>(1));
}

void Dhcpv4Srv::processStatsSent(const Pkt4Ptr& response) {
    // Increase generic counter for sent packets.
    isc::stats::StatsMgr::instance().addValue("pkt4-sent",
                                              static_cast<int64_t>(1));

    // Increase packet type specific counter for packets sent.
    string stat_name;
    switch (response->getType()) {
    case DHCPOFFER:
        stat_name = "pkt4-offer-sent";
        break;
    case DHCPACK:
        stat_name = "pkt4-ack-sent";
        break;
    case DHCPNAK:
        stat_name = "pkt4-nak-sent";
        break;
    default:
        // That should never happen
        return;
    }

    isc::stats::StatsMgr::instance().addValue(stat_name,
                                              static_cast<int64_t>(1));
}

int Dhcpv4Srv::getHookIndexBuffer4Receive() {
    return (Hooks.hook_index_buffer4_receive_);
}

int Dhcpv4Srv::getHookIndexPkt4Receive() {
    return (Hooks.hook_index_pkt4_receive_);
}

int Dhcpv4Srv::getHookIndexSubnet4Select() {
    return (Hooks.hook_index_subnet4_select_);
}

int Dhcpv4Srv::getHookIndexLease4Release() {
    return (Hooks.hook_index_lease4_release_);
}

int Dhcpv4Srv::getHookIndexPkt4Send() {
    return (Hooks.hook_index_pkt4_send_);
}

int Dhcpv4Srv::getHookIndexBuffer4Send() {
    return (Hooks.hook_index_buffer4_send_);
}

int Dhcpv4Srv::getHookIndexLease4Decline() {
    return (Hooks.hook_index_lease4_decline_);
}

void Dhcpv4Srv::discardPackets() {
    // Dump all of our current packets, anything that is mid-stream
    HooksManager::clearParkingLots();
}

uint16_t Dhcpv4Srv::getServerPort() const {
#ifdef FUZZING
    char const* const rotate(getenv("KEA_DHCP4_FUZZING_ROTATE_PORT"));
    if (rotate) {
        InterprocessSyncFile file("kea-dhcp4-fuzzing-rotate-port");
        InterprocessSyncLocker locker(file);
        while (!locker.lock()) {
            this_thread::sleep_for(1s);
        }
        fstream port_file;
        port_file.open("/tmp/port4.txt", ios::in);
        string line;
        int port;
        getline(port_file, line);
        port_file.close();
        if (line.empty()) {
            port = 2000;
        } else {
            port = stoi(line);
            if (port < 3000) {
                ++port;
            } else {
                port = 2000;
            }
        }
        port_file.open("/tmp/port4.txt", ios::out | ios::trunc);
        port_file << to_string(port) << endl;
        port_file.close();
        locker.unlock();
        return port;
    }
#endif  // FUZZING
    return server_port_;
}

std::list<std::list<std::string>> Dhcpv4Srv::jsonPathsToRedact() const {
    static std::list<std::list<std::string>> const list({
        {"config-control", "config-databases", "[]"},
        {"hooks-libraries", "[]", "parameters", "*"},
        {"hosts-database"},
        {"hosts-databases", "[]"},
        {"lease-database"},
    });
    return list;
}

}  // namespace dhcp
}  // namespace isc