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
// Copyright (C) 2012-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 <perfdhcp/test_control.h>
#include <perfdhcp/receiver.h>
#include <perfdhcp/command_options.h>
#include <perfdhcp/perf_pkt4.h>
#include <perfdhcp/perf_pkt6.h>

#include <exceptions/exceptions.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/iface_mgr.h>
#include <dhcp/dhcp4.h>
#include <dhcp/option6_ia.h>
#include <dhcp/option6_iaaddr.h>
#include <dhcp/option6_iaprefix.h>
#include <dhcp/option_int.h>
#include <util/unittests/check_valgrind.h>

#include <boost/date_time/posix_time/posix_time.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdio.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdlib.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdint.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <signal.h><--- 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 <sys/wait.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace boost::posix_time;
using namespace isc;
using namespace isc::dhcp;
using namespace isc::asiolink;

namespace isc {
namespace perfdhcp {

bool TestControl::interrupted_ = false;

bool
TestControl::waitToExit() {
    uint32_t const wait_time = options_.getExitWaitTime();

    // If we care and not all packets are in yet
    if (wait_time && !haveAllPacketsBeenReceived()) {
        const ptime now = microsec_clock::universal_time();

        // Init the end time if it hasn't started yet
        if (exit_time_.is_not_a_date_time()) {
            exit_time_ = now + time_duration(microseconds(wait_time));
        }

        // If we're not at end time yet, return true
        return (now < exit_time_);
    }

    // No need to wait, return false;
    return (false);
}

bool
TestControl::haveAllPacketsBeenReceived() const {
    const uint8_t& ipversion = options_.getIpVersion();
    const std::vector<int>& num_request = options_.getNumRequests();
    const size_t& num_request_size = num_request.size();

    if (num_request_size == 0) {
        return (false);
    }

    uint32_t responses = 0;
    uint32_t requests = num_request[0];
    if (num_request_size >= 2) {
        requests += num_request[1];
    }

    if (ipversion == 4) {
        responses = stats_mgr_.getRcvdPacketsNum(ExchangeType::DO) +
                    stats_mgr_.getRcvdPacketsNum(ExchangeType::RA);
    } else {
        responses = stats_mgr_.getRcvdPacketsNum(ExchangeType::SA) +
                    stats_mgr_.getRcvdPacketsNum(ExchangeType::RR);
    }

    return (responses == requests);
}

void
TestControl::cleanCachedPackets() {
    // When Renews are not sent, Reply packets are not cached so there
    // is nothing to do.
    if (options_.getRenewRate() == 0) {
        return;
    }

    static boost::posix_time::ptime last_clean =
        microsec_clock::universal_time();

    // Check how much time has passed since last cleanup.
    time_period time_since_clean(last_clean,
                                 microsec_clock::universal_time());
    // Cleanup every 1 second.
    if (time_since_clean.length().total_seconds() >= 1) {
        // Calculate how many cached packets to remove. Actually we could
        // just leave enough packets to handle Renews for 1 second but
        // since we want to randomize leases to be renewed so leave 5
        // times more packets to randomize from.
        /// @todo The cache size might be controlled from the command line.
        if (reply_storage_.size() > 5 * size_t(options_.getRenewRate())) {
            reply_storage_.clear(reply_storage_.size() -
                                 5 * options_.getRenewRate());
        }
        // Remember when we performed a cleanup for the last time.
        // We want to do the next cleanup not earlier than in one second.
        last_clean = microsec_clock::universal_time();
    }
}

void
TestControl::copyIaOptions(const Pkt6Ptr& pkt_from, Pkt6Ptr& pkt_to) {
    if (!pkt_from || !pkt_to) {
        isc_throw(BadValue, "NULL pointers must not be specified as arguments"
                  " for the copyIaOptions function");
    }
    // IA_NA
    if (options_.getLeaseType()
        .includes(CommandOptions::LeaseType::ADDRESS)) {
        OptionPtr option = pkt_from->getOption(D6O_IA_NA);
        if (!option) {
            isc_throw(NotFound, "IA_NA option not found in the"
                      " server's response");
        }
        pkt_to->addOption(option);
    }
    // IA_PD
    if (options_.getLeaseType()
        .includes(CommandOptions::LeaseType::PREFIX)) {
        OptionPtr option = pkt_from->getOption(D6O_IA_PD);
        if (!option) {
            isc_throw(NotFound, "IA_PD option not found in the"
                      " server's response");
        }
        pkt_to->addOption(option);
    }
}

std::string
TestControl::byte2Hex(const uint8_t b) {
    const int b1 = b / 16;
    const int b0 = b % 16;
    ostringstream stream;
    stream << std::hex << b1 << b0 << std::dec;
    return (stream.str());
}

Pkt4Ptr
TestControl::createMessageFromAck(const uint16_t msg_type,
                                  const dhcp::Pkt4Ptr& ack) {
    // Restrict messages to Release and Renew.
    if (msg_type != DHCPREQUEST && msg_type != DHCPRELEASE) {
        isc_throw(isc::BadValue, "invalid message type " << msg_type
                  << " to be created from Reply, expected DHCPREQUEST or"
                  " DHCPRELEASE");
    }

    // Get the string representation of the message - to be used for error
    // logging purposes.
    auto msg_type_str = [=]() -> const char* {
        return (msg_type == DHCPREQUEST ? "Request" : "Release");
    };

    if (!ack) {
        isc_throw(isc::BadValue, "Unable to create "
                                     << msg_type_str()
                                     << " from a null DHCPACK message");
    } else if (ack->getYiaddr().isV4Zero()) {
        isc_throw(isc::BadValue,
                  "Unable to create "
                      << msg_type_str()
                      << " from a DHCPACK message containing yiaddr of 0");
    }
    Pkt4Ptr msg(new Pkt4(msg_type, generateTransid()));
    msg->setCiaddr(ack->getYiaddr());
    msg->setHWAddr(ack->getHWAddr());
    msg->addOption(generateClientId(msg->getHWAddr()));
    if (msg_type == DHCPRELEASE) {
        // RFC 2132: DHCPRELEASE MUST include server ID.
        if (options_.isUseFirst()) {
            // Honor the '-1' flag if it exists.
            if (first_packet_serverid_.empty()) {
                isc_throw(isc::BadValue,
                          "Unable to create "
                              << msg_type_str()
                              << "from the first packet which lacks the server "
                                 "identifier option");
            }
            msg->addOption(Option::factory(Option::V4,
                                           DHO_DHCP_SERVER_IDENTIFIER,
                                           first_packet_serverid_));
        } else {
            // Otherwise take it from the DHCPACK message.
            OptionPtr server_identifier(
                ack->getOption(DHO_DHCP_SERVER_IDENTIFIER));
            if (!server_identifier) {
                isc_throw(isc::BadValue,
                          "Unable to create "
                              << msg_type_str()
                              << "from a DHCPACK message without the server "
                                 "identifier option");
            }
            msg->addOption(server_identifier);
        }
    }
    return (msg);
}

Pkt6Ptr
TestControl::createMessageFromReply(const uint16_t msg_type,
                                    const dhcp::Pkt6Ptr& reply) {
    // Restrict messages to Release and Renew.
    if (msg_type != DHCPV6_RENEW && msg_type != DHCPV6_RELEASE) {
        isc_throw(isc::BadValue, "invalid message type " << msg_type
                  << " to be created from Reply, expected DHCPV6_RENEW or"
                  " DHCPV6_RELEASE");
    }

    // Get the string representation of the message - to be used for error
    // logging purposes.
    auto msg_type_str = [=]() -> const char* {
        return (msg_type == DHCPV6_RENEW ? "Renew" : "Release");
    };

    // Reply message must be specified.
    if (!reply) {
        isc_throw(isc::BadValue, "Unable to create " << msg_type_str()
                  << " message from the Reply message because the instance of"
                  " the Reply message is NULL");
    }

    Pkt6Ptr msg(new Pkt6(msg_type, generateTransid()));
    // Client id.
    OptionPtr opt_clientid = reply->getOption(D6O_CLIENTID);
    if (!opt_clientid) {
        isc_throw(isc::Unexpected, "failed to create " << msg_type_str()
                  << " message because client id option has not been found"
                  " in the Reply message");
    }
    msg->addOption(opt_clientid);
    // Server id.
    OptionPtr opt_serverid = reply->getOption(D6O_SERVERID);
    if (!opt_serverid) {
        isc_throw(isc::Unexpected, "failed to create " << msg_type_str()
                  << " because server id option has not been found in the"
                  " Reply message");
    }
    msg->addOption(opt_serverid);
    copyIaOptions(reply, msg);
    return (msg);
}

OptionPtr
TestControl::factoryElapsedTime6(Option::Universe, uint16_t,
                                 const OptionBuffer& buf) {
    if (buf.size() == 2) {
        return (OptionPtr(new Option(Option::V6, D6O_ELAPSED_TIME, buf)));
    } else if (buf.size() == 0) {
        return (OptionPtr(new Option(Option::V6, D6O_ELAPSED_TIME,
                                     OptionBuffer(2, 0))));
    }
    isc_throw(isc::BadValue,
              "elapsed time option buffer size has to be 0 or 2");
}

OptionPtr
TestControl::factoryGeneric(Option::Universe u, uint16_t type,
                            const OptionBuffer& buf) {
    OptionPtr opt(new Option(u, type, buf));
    return (opt);
}

OptionPtr
TestControl::factoryIana6(Option::Universe, uint16_t,
                          const OptionBuffer& buf) {
    /// @todo allow different values of T1, T2 and IAID.
    const uint8_t buf_array[] = {
        0, 0, 0, 1,                     // IAID = 1
        0, 0, 3600 >> 8, 3600 & 0xff,  // T1 = 3600
        0, 0, 5400 >> 8, 5400 & 0xff,   // T2 = 5400
    };
    OptionBuffer buf_ia_na(buf_array, buf_array + sizeof(buf_array));
    for (size_t i = 0;  i < buf.size(); ++i) {
        buf_ia_na.push_back(buf[i]);
    }
    return (OptionPtr(new Option(Option::V6, D6O_IA_NA, buf_ia_na)));
}

OptionPtr
TestControl::factoryIapd6(Option::Universe, uint16_t,
                          const OptionBuffer& buf) {
    /// @todo allow different values of T1, T2 and IAID.
    static const uint8_t buf_array[] = {
        0, 0, 0, 1,                     // IAID = 1
        0, 0, 3600 >> 8, 3600 & 0xff,   // T1 = 3600
        0, 0, 5400 >> 8, 5400 & 0xff,   // T2 = 5400
    };
    OptionBuffer buf_ia_pd(buf_array, buf_array + sizeof(buf_array));
    // Append sub-options to IA_PD.
    buf_ia_pd.insert(buf_ia_pd.end(), buf.begin(), buf.end());
    return (OptionPtr(new Option(Option::V6, D6O_IA_PD, buf_ia_pd)));
}


OptionPtr
TestControl::factoryRapidCommit6(Option::Universe, uint16_t,
                                 const OptionBuffer&) {
    return (OptionPtr(new Option(Option::V6, D6O_RAPID_COMMIT, OptionBuffer())));
}

OptionPtr
TestControl::factoryOptionRequestOption6(Option::Universe,
                                         uint16_t,
                                         const OptionBuffer&) {
    const uint8_t buf_array[] = {
        0, D6O_NAME_SERVERS,
        0, D6O_DOMAIN_SEARCH,
    };
    OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
    return (OptionPtr(new Option(Option::V6, D6O_ORO, buf_with_options)));
}


OptionPtr
TestControl::factoryRequestList4(Option::Universe u,
                                 uint16_t type,
                                 const OptionBuffer& buf) {
    const uint8_t buf_array[] = {
        DHO_SUBNET_MASK,
        DHO_BROADCAST_ADDRESS,
        DHO_TIME_OFFSET,
        DHO_ROUTERS,
        DHO_DOMAIN_NAME,
        DHO_DOMAIN_NAME_SERVERS,
        DHO_HOST_NAME
    };

    OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
    OptionPtr opt(new Option(u, type, buf));
    opt->setData(buf_with_options.begin(), buf_with_options.end());
    return (opt);
}

std::vector<uint8_t>
TestControl::generateMacAddress(uint8_t& randomized) {
    const CommandOptions::MacAddrsVector& macs = options_.getMacsFromFile();
    // if we are using the -M option return a random one from the list...
    if (macs.size() > 0) {
      uint16_t r = random_generator_->generate();
      if (r >= macs.size()) {
        r = 0;
      }
      return (macs[r]);

    } else {
      // ... otherwise use the standard behavior
      uint32_t clients_num = options_.getClientsNum();
      if (clients_num < 2) {
          return (options_.getMacTemplate());
      }
      // Get the base MAC address. We are going to randomize part of it.
      std::vector<uint8_t> mac_addr(options_.getMacTemplate());
      if (mac_addr.size() != HW_ETHER_LEN) {
          isc_throw(BadValue, "invalid MAC address template specified");
      }
      uint32_t r = macaddr_gen_->generate();
      randomized = 0;
      // Randomize MAC address octets.
      for (std::vector<uint8_t>::iterator it = mac_addr.end() - 1;
           it >= mac_addr.begin();
           --it) {
          // Add the random value to the current octet.
          (*it) += r;
          ++randomized;
          if (r < 256) {
              // If we are here it means that there is no sense
              // to randomize the remaining octets of MAC address
              // because the following bytes of random value
              // are zero and it will have no effect.
              break;
          }
          // Randomize the next octet with the following
          // byte of random value.
          r >>= 8;
      }
      return (mac_addr);
    }
}

OptionPtr
TestControl::generateClientId(const dhcp::HWAddrPtr& hwaddr) const {
    vector<uint8_t> client_id;
    client_id.push_back(static_cast<uint8_t>(hwaddr->htype_));
    for (uint8_t const& byte : hwaddr->hwaddr_) {
        client_id.push_back(byte);<--- Consider using std::copy algorithm instead of a raw loop.
    }
    return (OptionPtr(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
                                 client_id)));
}

std::vector<uint8_t>
TestControl::generateDuid(uint8_t& randomized) {
    std::vector<uint8_t> mac_addr(generateMacAddress(randomized));
    const CommandOptions::MacAddrsVector& macs = options_.getMacsFromFile();
    // pick a random mac address if we are using option -M..
    if (macs.size() > 0) {
      uint16_t r = random_generator_->generate();
      if (r >= macs.size()) {
        r = 0;
      }
      std::vector<uint8_t> mac = macs[r];
      // DUID_LL is in this format
      //  0                   1                   2                   3
      //  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      // |               3               |    hardware type (16 bits)    |
      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      // .                                                               .
      // .             link-layer address (variable length)              .
      // .                                                               .
      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

      // No C++11 so initializer list support, building a vector<uint8_t> is a
      // pain...
      uint8_t duid_ll[] = {0, 3, 0, 1, 0, 0, 0, 0, 0, 0};
      // copy duid_ll array into the vector
      std::vector<uint8_t> duid(duid_ll,
                                duid_ll + sizeof(duid_ll) / sizeof(duid_ll[0]));
      // put the mac address bytes at the end
      std::copy(mac.begin(), mac.end(), duid.begin() + 4);
      return (duid);
    } else {
      uint32_t clients_num = options_.getClientsNum();
      if ((clients_num == 0) || (clients_num == 1)) {
          return (options_.getDuidTemplate());
      }
      // Get the base DUID. We are going to randomize part of it.
      std::vector<uint8_t> duid(options_.getDuidTemplate());
      /// @todo: add support for DUIDs of different sizes.
      duid.resize(duid.size());
      std::copy(mac_addr.begin(), mac_addr.end(),
                duid.begin() + duid.size() - mac_addr.size());
      return (duid);
    }
}

int
TestControl::getElapsedTimeOffset() const {
    int elp_offset = options_.getIpVersion() == 4 ?
        DHCPV4_ELAPSED_TIME_OFFSET : DHCPV6_ELAPSED_TIME_OFFSET;
    if (options_.getElapsedTimeOffset() > 0) {
        elp_offset = options_.getElapsedTimeOffset();
    }
    return (elp_offset);
}

template<class T>
uint32_t
TestControl::getElapsedTime(const T& pkt1, const T& pkt2) {
    using namespace boost::posix_time;
    ptime pkt1_time = pkt1->getTimestamp();
    ptime pkt2_time = pkt2->getTimestamp();
    if (pkt1_time.is_not_a_date_time() ||
        pkt2_time.is_not_a_date_time()) {
        isc_throw(InvalidOperation, "packet timestamp not set");;
    }
    time_period elapsed_period(pkt1_time, pkt2_time);
    return (elapsed_period.is_null() ? 0 :
            elapsed_period.length().total_milliseconds());
}

int
TestControl::getRandomOffset(const int arg_idx) const {
    int rand_offset = options_.getIpVersion() == 4 ?
        DHCPV4_RANDOMIZATION_OFFSET : DHCPV6_RANDOMIZATION_OFFSET;
    if (options_.getRandomOffset().size() > size_t(arg_idx)) {
        rand_offset = options_.getRandomOffset()[arg_idx];
    }
    return (rand_offset);
}

int
TestControl::getRequestedIpOffset() const {
    int rip_offset = options_.getIpVersion() == 4 ?
        DHCPV4_REQUESTED_IP_OFFSET : DHCPV6_IA_NA_OFFSET;
    if (options_.getRequestedIpOffset() > 0) {
        rip_offset = options_.getRequestedIpOffset();
    }
    return (rip_offset);
}

int
TestControl::getServerIdOffset() const {
    int srvid_offset = options_.getIpVersion() == 4 ?
        DHCPV4_SERVERID_OFFSET : DHCPV6_SERVERID_OFFSET;
    if (options_.getServerIdOffset() > 0) {
        srvid_offset = options_.getServerIdOffset();
    }
    return (srvid_offset);
}

TestControl::TemplateBuffer
TestControl::getTemplateBuffer(const size_t idx) const {
    if (template_buffers_.size() > idx) {
        return (template_buffers_[idx]);
    }
    isc_throw(OutOfRange, "invalid buffer index");
}

int
TestControl::getTransactionIdOffset(const int arg_idx) const {
    int xid_offset = options_.getIpVersion() == 4 ?
        DHCPV4_TRANSID_OFFSET : DHCPV6_TRANSID_OFFSET;
    if (options_.getTransactionIdOffset().size() > size_t(arg_idx)) {
        xid_offset = options_.getTransactionIdOffset()[arg_idx];
    }
    return (xid_offset);
}

void
TestControl::handleChild(int) {
    int status = 0;
    while (wait3(&status, WNOHANG, NULL) > 0) {
        // continue
    }
}

void
TestControl::handleInterrupt(int) {
    interrupted_ = true;
}

void
TestControl::initPacketTemplates() {
    template_packets_v4_.clear();
    template_packets_v6_.clear();
    template_buffers_.clear();
    std::vector<std::string> template_files = options_.getTemplateFiles();
    for (auto const& it : template_files) {
        readPacketTemplate(it);
    }
}

void
TestControl::sendPackets(const uint64_t packets_num,
                         const bool preload /* = false */) {
    for (uint64_t i = packets_num; i > 0; --i) {
        if (options_.getIpVersion() == 4) {
            // No template packets means that no -T option was specified.
            // We have to build packets ourselves.
            if (template_buffers_.empty()) {
                sendDiscover4(preload);
            } else {
                /// @todo add defines for packet type index that can be
                /// used to access template_buffers_.
                sendDiscover4(template_buffers_[0], preload);
            }
        } else {
            // No template packets means that no -T option was specified.
            // We have to build packets ourselves.
            if (template_buffers_.empty()) {
                sendSolicit6(preload);
            } else {
                /// @todo add defines for packet type index that can be
                /// used to access template_buffers_.
                sendSolicit6(template_buffers_[0], preload);
            }
        }
    }
}

uint64_t
TestControl::sendMultipleMessages4(const uint32_t msg_type,
                                   const uint64_t msg_num) {
    for (uint64_t i = 0; i < msg_num; ++i) {
        if (!sendMessageFromAck(msg_type)) {
            return (i);
        }
    }
    return (msg_num);
}

uint64_t
TestControl::sendMultipleMessages6(const uint32_t msg_type,
                                   const uint64_t msg_num) {
    for (uint64_t i = 0; i < msg_num; ++i) {
        if (!sendMessageFromReply(msg_type)) {
            return (i);
        }
    }
    return (msg_num);
}

void
TestControl::printDiagnostics() const {
    if (options_.testDiags('a')) {
        // Print all command line parameters.
        options_.printCommandLine();
        // Print MAC and DUID.
        std::cout << "Set MAC to " << vector2Hex(options_.getMacTemplate(), "::")
                  << std::endl;
        if (options_.getDuidTemplate().size() > 0) {
            std::cout << "Set DUID to " << vector2Hex(options_.getDuidTemplate()) << std::endl;
        }
    }
}

void
TestControl::printTemplate(const uint8_t packet_type) const {
    std::string hex_buf;
    int arg_idx = 0;
    if (options_.getIpVersion() == 4) {
        if (packet_type == DHCPREQUEST) {
            arg_idx = 1;
        }
        std::map<uint8_t, dhcp::Pkt4Ptr>::const_iterator pkt_it =
            template_packets_v4_.find(packet_type);
        if ((pkt_it != template_packets_v4_.end()) &&
            pkt_it->second) {
            hex_buf = vector2Hex(pkt_it->second->getBuffer().getVector());
        }
    } else if (options_.getIpVersion() == 6) {
        if (packet_type == DHCPV6_REQUEST) {
            arg_idx = 1;
        }
        std::map<uint8_t, dhcp::Pkt6Ptr>::const_iterator pkt_it =
            template_packets_v6_.find(packet_type);
        if (pkt_it != template_packets_v6_.end() &&
            pkt_it->second) {
            hex_buf = vector2Hex(pkt_it->second->getBuffer().getVector());
        }
    }
    std::cout << "xid-offset=" << getTransactionIdOffset(arg_idx) << std::endl;
    std::cout << "random-offset=" << getRandomOffset(arg_idx) << std::endl;
    if (arg_idx > 0) {
        std::cout << "srvid-offset=" << getServerIdOffset() << std::endl;
        std::cout << "time-offset=" << getElapsedTimeOffset() << std::endl;
        std::cout << "ip-offset=" << getRequestedIpOffset() << std::endl;
    }

    std::cout << "contents: " << std::endl;
    int line_len = 32;
    int i = 0;
    while (line_len == 32) {
        if (hex_buf.length() - i < 32) {
            line_len = hex_buf.length() - i;
        };
        if (line_len > 0) {
            std::cout << setfill('0') << setw(4) << std::hex << i << std::dec
                      << "   " << hex_buf.substr(i, line_len) << std::endl;
        }
        i += 32;
    }
    std::cout << std::endl;
}

void
TestControl::printTemplates() const {
    if (options_.getIpVersion() == 4) {
        printTemplate(DHCPDISCOVER);
        printTemplate(DHCPREQUEST);
    } else if (options_.getIpVersion() == 6) {
        printTemplate(DHCPV6_SOLICIT);
        printTemplate(DHCPV6_REQUEST);
    }
}

void
TestControl::printRate() const {
    double rate = 0;
    std::string exchange_name = "4-way exchanges";
    ExchangeType xchg_type = ExchangeType::DO;
    if (options_.getIpVersion() == 4) {
        xchg_type =
            options_.getExchangeMode() == CommandOptions::DO_SA ?
            ExchangeType::DO : ExchangeType::RA;
        if (xchg_type == ExchangeType::DO) {
            exchange_name = "DISCOVER-OFFER";
        }
    } else if (options_.getIpVersion() == 6) {
        xchg_type =
            options_.getExchangeMode() == CommandOptions::DO_SA ?
            ExchangeType::SA : ExchangeType::RR;
        if (xchg_type == ExchangeType::SA) {
            exchange_name = options_.isRapidCommit() ? "Solicit-Reply" :
                "Solicit-Advertise";
        }
    }
    double duration =
        stats_mgr_.getTestPeriod().length().total_nanoseconds() / 1e9;
    rate = stats_mgr_.getRcvdPacketsNum(xchg_type) / duration;
    std::ostringstream s;
    s << "***Rate statistics***" << std::endl;
    s << "Rate: " << rate << " " << exchange_name << "/second";
    if (options_.getRate() > 0) {
        s << ", expected rate: " << options_.getRate() << std::endl;
    }

    std::cout << s.str() << std::endl;

    std::cout <<"***Malformed Packets***" << std::endl
              << "Malformed packets: " << ExchangeStats::malformed_pkts_
              << std::endl;
}

void
TestControl::printIntermediateStats() {
    int delay = options_.getReportDelay();
    ptime now = microsec_clock::universal_time();
    time_period time_since_report(last_report_, now);
    if (time_since_report.length().total_seconds() >= delay) {
        stats_mgr_.printIntermediateStats(options_.getCleanReport(),
                                          options_.getCleanReportSeparator());
        last_report_ = now;
    }
}

void
TestControl::printStats() const {
    printRate();
    stats_mgr_.printStats();
    if (options_.testDiags('i')) {
        stats_mgr_.printCustomCounters();
    }
}

std::string
TestControl::vector2Hex(const std::vector<uint8_t>& vec,
                        const std::string& separator /* = "" */) {
    std::ostringstream stream;
    bool first = true;
    for (auto const& it : vec) {
        if (first) {
            stream << byte2Hex(it);
            first = false;
        } else {
            stream << separator << byte2Hex(it);
        }
    }
    return (stream.str());
}

void
TestControl::readPacketTemplate(const std::string& file_name) {
    std::ifstream temp_file;
    temp_file.open(file_name.c_str(), ios::in | ios::binary | ios::ate);
    if (!temp_file.is_open()) {
        isc_throw(BadValue, "unable to open template file " << file_name);
    }
    // Read template file contents.
    std::streampos temp_size = temp_file.tellg();
    if (temp_size == std::streampos(0)) {
        temp_file.close();
        isc_throw(OutOfRange, "the template file " << file_name << " is empty");
    }
    temp_file.seekg(0, ios::beg);
    std::vector<char> file_contents(temp_size);
    temp_file.read(&file_contents[0], temp_size);
    temp_file.close();
    // Spaces are allowed so we have to strip the contents
    // from them. In the same time we want to make sure that
    // apart from spaces the file contains hexadecimal digits
    // only.
    std::vector<char> hex_digits;
    for (size_t i = 0; i < file_contents.size(); ++i) {
        if (isxdigit(file_contents[i])) {<--- Assuming that condition 'isxdigit(file_contents[i])' is not redundant
            hex_digits.push_back(file_contents[i]);
        } else if (!isxdigit(file_contents[i]) &&<--- Condition '!isxdigit(file_contents[i])' is always true
                   !isspace(file_contents[i])) {
            isc_throw(BadValue, "'" << file_contents[i] << "' is not a"
                      " hexadecimal digit");
        }
    }
    // Expect even number of digits.
    if (hex_digits.size() % 2 != 0) {
        isc_throw(OutOfRange, "odd number of digits in template file");
    } else if (hex_digits.empty()) {
        isc_throw(OutOfRange, "template file " << file_name << " is empty");
    }
    std::vector<uint8_t> binary_stream;
    for (size_t i = 0; i < hex_digits.size(); i += 2) {
        stringstream s;
        s << "0x" << hex_digits[i] << hex_digits[i+1];
        int b;
        s >> std::hex >> b;
        binary_stream.push_back(static_cast<uint8_t>(b));
    }
    template_buffers_.push_back(binary_stream);
}

void
TestControl::processReceivedPacket4(const Pkt4Ptr& pkt4) {
    if (pkt4->getType() == DHCPOFFER) {
        PktPtr pkt = stats_mgr_.passRcvdPacket(ExchangeType::DO, pkt4);
        address4Uniqueness(pkt4, ExchangeType::DO);
        Pkt4Ptr discover_pkt4(boost::dynamic_pointer_cast<Pkt4>(pkt));
        CommandOptions::ExchangeMode xchg_mode = options_.getExchangeMode();
        if ((xchg_mode == CommandOptions::DORA_SARR) && discover_pkt4) {
            if (template_buffers_.size() < 2) {
                sendRequest4(discover_pkt4, pkt4);
            } else {
                /// @todo add defines for packet type index that can be
                /// used to access template_buffers_.
                sendRequest4(template_buffers_[1], discover_pkt4, pkt4);
            }
        }
    } else if (pkt4->getType() == DHCPACK) {
        // If received message is DHCPACK, we have to check if this is
        // a response to 4-way exchange. We'll match this packet with
        // a DHCPREQUEST sent as part of the 4-way exchanges.
        if (stats_mgr_.passRcvdPacket(ExchangeType::RA, pkt4)) {
            address4Uniqueness(pkt4, ExchangeType::RA);
            // The DHCPACK belongs to DHCPREQUEST-DHCPACK exchange type.
            // So, we may need to keep this DHCPACK in the storage if renews.
            // Note that, DHCPACK messages hold the information about
            // leases assigned. We use this information to renew.
            if (stats_mgr_.hasExchangeStats(ExchangeType::RNA) ||
                stats_mgr_.hasExchangeStats(ExchangeType::RLA)) {
                // Renew or release messages are sent, because StatsMgr has the
                // specific exchange type specified. Let's append the DHCPACK
                // message to a storage.
                ack_storage_.append(pkt4);
            }
        // The DHCPACK message is not a server's response to the DHCPREQUEST
        // message sent within the 4-way exchange. It may be a response to a
        // renewal. In this case we first check if StatsMgr has exchange type
        // for renew specified, and if it has, if there is a corresponding
        // renew message for the received DHCPACK.
        } else if (stats_mgr_.hasExchangeStats(ExchangeType::RNA)) {
            stats_mgr_.passRcvdPacket(ExchangeType::RNA, pkt4);
        }
    }
}

void
TestControl::address6Uniqueness(const Pkt6Ptr& pkt6, ExchangeType xchg_type) {
    // check if received address is unique
    if (options_.getAddrUnique()) {
        std::set<std::string> current;
        // addresses were already checked in validateIA
        // we can safely assume that those are correct
        for (auto const& opt : pkt6->options_) {
            switch (opt.second->getType()) {
            case D6O_IA_PD: {
                // add address and check if it has not been already assigned
                // addresses should be unique cross options of the packet
                auto ret = current.emplace(boost::dynamic_pointer_cast<
                    Option6IAPrefix>(opt.second->getOption(D6O_IAPREFIX))->getAddress().toText());
                if (!ret.second) {
                    stats_mgr_.updateNonUniqueAddrNum(xchg_type);
                }
                break;
            }
            case D6O_IA_NA: {
                // add address and check if it has not been already assigned
                // addresses should be unique cross options of the packet
                auto ret = current.emplace(boost::dynamic_pointer_cast<
                    Option6IAAddr>(opt.second->getOption(D6O_IAADDR))->getAddress().toText());
                if (!ret.second) {
                    stats_mgr_.updateNonUniqueAddrNum(xchg_type);
                }
                break;
            }
            default:
                break;
            }
        }
        // addresses should be unique cross packets
        addUniqeAddr(current, xchg_type);
    }
}

void
TestControl::address4Uniqueness(const Pkt4Ptr& pkt4, ExchangeType xchg_type) {
    // check if received address is unique
    if (options_.getAddrUnique()) {
        // addresses were already checked in validateIA
        // we can safely assume that those are correct
        std::set<std::string> current;
        current.insert(pkt4->getYiaddr().toText());
        // addresses should be unique cross packets
        addUniqeAddr(current, xchg_type);
    }
}

bool
TestControl::validateIA(const Pkt6Ptr& pkt6) {
    // check if iaaddr exists - if it does, we can continue sending request
    // if not we will update statistics about rejected leases
    // @todo it's checking just one iaaddress option for now it's ok
    // but when perfdhcp will be extended to create message with multiple IA
    // this will have to be iterate on:
    // OptionCollection ias = pkt6->getOptions(D6O_IA_NA);
    Option6IAPrefixPtr iapref;
    Option6IAAddrPtr iaaddr;
    if (pkt6->getOption(D6O_IA_PD)) {
        iapref = boost::dynamic_pointer_cast<
                 Option6IAPrefix>(pkt6->getOption(D6O_IA_PD)->getOption(D6O_IAPREFIX));
    }
    if (pkt6->getOption(D6O_IA_NA)) {
        iaaddr = boost::dynamic_pointer_cast<
                 Option6IAAddr>(pkt6->getOption(D6O_IA_NA)->getOption(D6O_IAADDR));
    }

    bool address_and_prefix = options_.getLeaseType().includes(
                              CommandOptions::LeaseType::ADDRESS_AND_PREFIX);
    bool prefix_only = options_.getLeaseType().includes(
                       CommandOptions::LeaseType::PREFIX);
    bool address_only = options_.getLeaseType().includes(
                        CommandOptions::LeaseType::ADDRESS);
    if ((address_and_prefix && iapref && iaaddr) ||
        (prefix_only && iapref && !address_and_prefix) ||
        (address_only && iaaddr && !address_and_prefix)) {
        return (true);
    } else {
        return (false);
    }
}

void
TestControl::processReceivedPacket6(const Pkt6Ptr& pkt6) {
    uint8_t packet_type = pkt6->getType();
    if (packet_type == DHCPV6_ADVERTISE) {
        PktPtr pkt = stats_mgr_.passRcvdPacket(ExchangeType::SA, pkt6);
        Pkt6Ptr solicit_pkt6(boost::dynamic_pointer_cast<Pkt6>(pkt));
        CommandOptions::ExchangeMode xchg_mode = options_.getExchangeMode();
        if ((xchg_mode == CommandOptions::DORA_SARR) && solicit_pkt6) {
            if (validateIA(pkt6)) {
               // if address is correct - check uniqueness
               address6Uniqueness(pkt6, ExchangeType::SA);
               if (template_buffers_.size() < 2) {
                    sendRequest6(pkt6);
               } else {
                    /// @todo add defines for packet type index that can be
                    /// used to access template_buffers_.
                    sendRequest6(template_buffers_[1], pkt6);
               }
            } else {
                stats_mgr_.updateRejLeases(ExchangeType::SA);
            }
        }
    } else if (packet_type == DHCPV6_REPLY) {
        // If the received message is Reply, we have to find out which exchange
        // type the Reply message belongs to. It is doable by matching the Reply
        // transaction id with the transaction id of the sent Request, Renew
        // or Release. First we start with the Request.
        if (stats_mgr_.passRcvdPacket(ExchangeType::RR, pkt6)) {
            // The Reply belongs to Request-Reply exchange type. So, we may need
            // to keep this Reply in the storage if Renews or/and Releases are
            // being sent. Note that, Reply messages hold the information about
            // leases assigned. We use this information to construct Renew and
            // Release messages.
            if (validateIA(pkt6)) {
                // if address is correct - check uniqueness
                address6Uniqueness(pkt6, ExchangeType::RR);
                // check if there is correct IA to continue with Renew/Release
                if (stats_mgr_.hasExchangeStats(ExchangeType::RN) ||
                    stats_mgr_.hasExchangeStats(ExchangeType::RL)) {
                    // Renew or Release messages are sent, because StatsMgr has the
                    // specific exchange type specified. Let's append the Reply
                    // message to a storage.
                    reply_storage_.append(pkt6);
                }
            } else {
                stats_mgr_.updateRejLeases(ExchangeType::RR);
            }
        // The Reply message is not a server's response to the Request message
        // sent within the 4-way exchange. It may be a response to the Renew
        // or Release message. In the if clause we first check if StatsMgr
        // has exchange type for Renew specified, and if it has, if there is
        // a corresponding Renew message for the received Reply. If not,
        // we check that StatsMgr has exchange type for Release specified,
        // as possibly the Reply has been sent in response to Release.
        } else if (!(stats_mgr_.hasExchangeStats(ExchangeType::RN) &&
                     stats_mgr_.passRcvdPacket(ExchangeType::RN, pkt6)) &&
                   stats_mgr_.hasExchangeStats(ExchangeType::RL)) {
            // At this point, it is only possible that the Reply has been sent
            // in response to a Release. Try to match the Reply with Release.
            stats_mgr_.passRcvdPacket(ExchangeType::RL, pkt6);
        }
    }
}

unsigned int
TestControl::consumeReceivedPackets() {
    unsigned int pkt_count = 0;
    PktPtr pkt;
    while ((pkt = receiver_.getPkt())) {
        pkt_count += 1;
        if (options_.getIpVersion() == 4) {
            Pkt4Ptr pkt4 = boost::dynamic_pointer_cast<Pkt4>(pkt);
            processReceivedPacket4(pkt4);
        } else {
            Pkt6Ptr pkt6 = boost::dynamic_pointer_cast<Pkt6>(pkt);
            processReceivedPacket6(pkt6);
        }
    }
    return pkt_count;
}
void
TestControl::registerOptionFactories4() const {
    static bool factories_registered = false;
    if (!factories_registered) {
        // DHCP_MESSAGE_TYPE option factory.
        LibDHCP::OptionFactoryRegister(Option::V4,
                                       DHO_DHCP_MESSAGE_TYPE,
                                       &TestControl::factoryGeneric);
        // DHCP_SERVER_IDENTIFIER option factory.
        LibDHCP::OptionFactoryRegister(Option::V4,
                                       DHO_DHCP_SERVER_IDENTIFIER,
                                       &TestControl::factoryGeneric);
        // DHCP_PARAMETER_REQUEST_LIST option factory.
        LibDHCP::OptionFactoryRegister(Option::V4,
                                       DHO_DHCP_PARAMETER_REQUEST_LIST,
                                       &TestControl::factoryRequestList4);
    }
    factories_registered = true;
}

void
TestControl::registerOptionFactories6() const {
    static bool factories_registered = false;
    if (!factories_registered) {
        // D6O_ELAPSED_TIME
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_ELAPSED_TIME,
                                       &TestControl::factoryElapsedTime6);
        // D6O_RAPID_COMMIT
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_RAPID_COMMIT,
                                       &TestControl::factoryRapidCommit6);
        // D6O_ORO (option request option) factory.
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_ORO,
                                       &TestControl::factoryOptionRequestOption6);
        // D6O_CLIENTID option factory.
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_CLIENTID,
                                       &TestControl::factoryGeneric);
        // D6O_SERVERID option factory.
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_SERVERID,
                                       &TestControl::factoryGeneric);
        // D6O_IA_NA option factory.
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_IA_NA,
                                       &TestControl::factoryIana6);

        // D6O_IA_PD option factory.
        LibDHCP::OptionFactoryRegister(Option::V6,
                                       D6O_IA_PD,
                                       &TestControl::factoryIapd6);


    }
    factories_registered = true;
}

void
TestControl::registerOptionFactories() const {
    switch(options_.getIpVersion()) {
    case 4:
        registerOptionFactories4();
        break;
    case 6:
        registerOptionFactories6();
        break;
    default:
        isc_throw(InvalidOperation, "command line options have to be parsed "
                  "before DHCP option factories can be registered");
    }
}

void
TestControl::reset() {
    transid_gen_.reset();
    last_report_ = microsec_clock::universal_time();
    // Actual generators will have to be set later on because we need to
    // get command line parameters first.
    setTransidGenerator(NumberGeneratorPtr());
    setMacAddrGenerator(NumberGeneratorPtr());
    first_packet_serverid_.clear();
    interrupted_ = false;
}

TestControl::TestControl(CommandOptions& options, BasePerfSocket &socket) :
    exit_time_(not_a_date_time),
    socket_(socket),
    receiver_(socket, options.isSingleThreaded(), options.getIpVersion()),
    stats_mgr_(options),
    random_generator_(new RandomGenerator(0, options.getMacsFromFile().size())),
    options_(options) {
    // Reset singleton state before test starts.
    reset();

    // Ip version is not set ONLY in case the command options
    // were not parsed. This surely means that parse() function
    // was not called prior to starting the test. This is fatal
    // error.
    if (options_.getIpVersion() == 0) {
        isc_throw(InvalidOperation,
                  "command options must be parsed before running a test");
    } else if (options_.getIpVersion() == 4) {
        // Turn off packet queueing.
        IfaceMgr::instance().configureDHCPPacketQueue(AF_INET, data::ElementPtr());
        setTransidGenerator(NumberGeneratorPtr(new SequentialGenerator()));
    } else {
        // Turn off packet queueing.
        IfaceMgr::instance().configureDHCPPacketQueue(AF_INET6, data::ElementPtr());
        setTransidGenerator(NumberGeneratorPtr(new SequentialGenerator(0x00FFFFFF)));
    }

    uint32_t clients_num = options_.getClientsNum() == 0 ?
        1 : options_.getClientsNum();
    setMacAddrGenerator(NumberGeneratorPtr(new SequentialGenerator(clients_num)));

    // Diagnostics are command line options mainly.
    printDiagnostics();
    // Option factories have to be registered.
    registerOptionFactories();
    // Initialize packet templates.
    initPacketTemplates();
    // Initialize randomization seed.
    if (options_.isSeeded()) {
        srandom(options_.getSeed());
    } else {
        // Seed with current time.
        time_period duration(from_iso_string("20111231T235959"),
                             microsec_clock::universal_time());
        srandom(duration.length().total_seconds()
                + duration.length().fractional_seconds());
    }
    // If user interrupts the program we will exit gracefully.
    signal(SIGINT, TestControl::handleInterrupt);
}

void
TestControl::runWrapped(bool do_stop /*= false */) const {
    if (!options_.getWrapped().empty()) {
        pid_t pid = 0;
        signal(SIGCHLD, handleChild);
        pid = fork();
        if (pid < 0) {
            isc_throw(Unexpected, "unable to fork");
        } else if (pid == 0) {
            execlp(options_.getWrapped().c_str(), do_stop ? "stop" : "start", (void*)0);
        }
    }
}

void
TestControl::saveFirstPacket(const Pkt4Ptr& pkt) {
    if (options_.testDiags('T')) {
        if (template_packets_v4_.find(pkt->getType()) == template_packets_v4_.end()) {
            template_packets_v4_[pkt->getType()] = pkt;
        }
    }
}

void
TestControl::saveFirstPacket(const Pkt6Ptr& pkt) {
    if (options_.testDiags('T')) {
        if (template_packets_v6_.find(pkt->getType()) == template_packets_v6_.end()) {
            template_packets_v6_[pkt->getType()] = pkt;
        }
    }
}

void
TestControl::sendDiscover4(const bool preload /*= false*/) {
    // Generate the MAC address to be passed in the packet.
    uint8_t randomized = 0;
    std::vector<uint8_t> mac_address = generateMacAddress(randomized);
    // Generate transaction id to be set for the new exchange.
    const uint32_t transid = generateTransid();
    Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, transid));
    if (!pkt4) {
        isc_throw(Unexpected, "failed to create DISCOVER packet");
    }

    // Delete the default Message Type option set by Pkt4
    pkt4->delOption(DHO_DHCP_MESSAGE_TYPE);

    // Set options: DHCP_MESSAGE_TYPE and DHCP_PARAMETER_REQUEST_LIST
    OptionBuffer buf_msg_type;
    buf_msg_type.push_back(DHCPDISCOVER);
    pkt4->addOption(Option::factory(Option::V4, DHO_DHCP_MESSAGE_TYPE,
                                    buf_msg_type));
    pkt4->addOption(Option::factory(Option::V4,
                                    DHO_DHCP_PARAMETER_REQUEST_LIST));


    // Set client's and server's ports as well as server's address,
    // and local (relay) address.
    setDefaults4(pkt4);

    // Set hardware address
    pkt4->setHWAddr(HTYPE_ETHER, mac_address.size(), mac_address);

    // Set client identifier
    pkt4->addOption(generateClientId(pkt4->getHWAddr()));

    // Check if we need to simulate HA failures by pretending no responses were received.
    // The DHCP protocol signals that by increasing secs field (seconds since the configuration attempt started).
    if (options_.getIncreaseElapsedTime() &&
        stats_mgr_.getTestPeriod().length().total_seconds() >= options_.getWaitForElapsedTime() &&
        stats_mgr_.getTestPeriod().length().total_seconds() < options_.getWaitForElapsedTime() +
                                     options_.getIncreaseElapsedTime()) {

        // Keep increasing elapsed time. The value should start increasing steadily.
        uint32_t val = stats_mgr_.getTestPeriod().length().total_seconds() - options_.getWaitForElapsedTime() + 1;
        if (val > 65535) {
            val = 65535;
        }
        pkt4->setSecs(static_cast<uint16_t>(val));
    }

    // Add any extra options that user may have specified.
    addExtraOpts(pkt4);

    pkt4->pack();
    socket_.send(pkt4);
    if (!preload) {
        stats_mgr_.passSentPacket(ExchangeType::DO, pkt4);
    }

    saveFirstPacket(pkt4);
}

void
TestControl::sendDiscover4(const std::vector<uint8_t>& template_buf,
                           const bool preload /* = false */) {
    // Get the first argument if multiple the same arguments specified
    // in the command line. First one refers to DISCOVER packets.
    const uint8_t arg_idx = 0;
    // Generate the MAC address to be passed in the packet.
    uint8_t randomized = 0;
    std::vector<uint8_t> mac_address = generateMacAddress(randomized);
    // Generate transaction id to be set for the new exchange.
    const uint32_t transid = generateTransid();
    // Get transaction id offset.
    size_t transid_offset = getTransactionIdOffset(arg_idx);
    // Get randomization offset.
    // We need to go back by HW_ETHER_LEN (MAC address length)
    // because this offset points to last octet of MAC address.
    size_t rand_offset = getRandomOffset(arg_idx) - HW_ETHER_LEN + 1;
    // Create temporary buffer with template contents. We will
    // modify this temporary buffer but we don't want to modify
    // the original template.
    std::vector<uint8_t> in_buf(template_buf.begin(),
                                template_buf.end());
    // Check if we are not going out of bounds.
    if (rand_offset + HW_ETHER_LEN > in_buf.size()) {
        isc_throw(OutOfRange, "randomization offset is out of bounds");
    }
    PerfPkt4Ptr pkt4(new PerfPkt4(&in_buf[0], in_buf.size(),
                                  transid_offset,
                                  transid));

    // Replace MAC address in the template with actual MAC address.
    pkt4->writeAt(rand_offset, mac_address.begin(), mac_address.end());
    // Create a packet from the temporary buffer.
    setDefaults4(boost::static_pointer_cast<Pkt4>(pkt4));
    // Pack the input packet buffer to output buffer so as it can
    // be sent to server.
    pkt4->rawPack();
    socket_.send(boost::static_pointer_cast<Pkt4>(pkt4));
    if (!preload) {
        // Update packet stats.
        stats_mgr_.passSentPacket(ExchangeType::DO,
                                    boost::static_pointer_cast<Pkt4>(pkt4));
    }
    saveFirstPacket(pkt4);
}

bool
TestControl::sendMessageFromAck(const uint16_t msg_type) {
    // We only permit Request or Release messages to be sent using this
    // function.
    if (msg_type != DHCPREQUEST && msg_type != DHCPRELEASE) {
        isc_throw(isc::BadValue,
                  "invalid message type "
                      << msg_type
                      << " to be sent, expected DHCPREQUEST or DHCPRELEASE");
    }

    // Get one of the recorded DHCPACK messages.
    Pkt4Ptr ack = ack_storage_.getRandom();
    if (!ack) {
        return (false);
    }

    // Create message of the specified type.
    Pkt4Ptr msg = createMessageFromAck(msg_type, ack);
    setDefaults4(msg);

    // Override relay address
    msg->setGiaddr(ack->getGiaddr());

    // Add any extra options that user may have specified.
    addExtraOpts(msg);

    // Pack it.
    msg->pack();

    // And send it.
    socket_.send(msg);
    address4Uniqueness(msg, ExchangeType::RLA);
    stats_mgr_.passSentPacket((msg_type == DHCPREQUEST ? ExchangeType::RNA :
                                                         ExchangeType::RLA),
                              msg);
    return (true);
}


bool
TestControl::sendMessageFromReply(const uint16_t msg_type) {
    // We only permit Release or Renew messages to be sent using this function.
    if (msg_type != DHCPV6_RENEW && msg_type != DHCPV6_RELEASE) {
        isc_throw(isc::BadValue, "invalid message type " << msg_type
                  << " to be sent, expected DHCPV6_RENEW or DHCPV6_RELEASE");
    }

    // Get one of the recorded DHCPV6_OFFER messages.
    Pkt6Ptr reply = reply_storage_.getRandom();
    if (!reply) {
        return (false);
    }
    // Prepare the message of the specified type.
    Pkt6Ptr msg = createMessageFromReply(msg_type, reply);
    setDefaults6(msg);

    // Add any extra options that user may have specified.
    addExtraOpts(msg);

    // Pack it.
    msg->pack();

    // And send it.
    socket_.send(msg);
    address6Uniqueness(msg, ExchangeType::RL);
    stats_mgr_.passSentPacket((msg_type == DHCPV6_RENEW ? ExchangeType::RN
                                : ExchangeType::RL), msg);
    return (true);
}

void
TestControl::sendRequest4(const dhcp::Pkt4Ptr& discover_pkt4,
                          const dhcp::Pkt4Ptr& offer_pkt4) {
    // Use the same transaction id as the one used in the discovery packet.
    const uint32_t transid = discover_pkt4->getTransid();
    Pkt4Ptr pkt4(new Pkt4(DHCPREQUEST, transid));

    // Use first flags indicates that we want to use the server
    // id captured in first packet.
    if (options_.isUseFirst() &&
        (first_packet_serverid_.size() > 0)) {
        pkt4->addOption(Option::factory(Option::V4, DHO_DHCP_SERVER_IDENTIFIER,
                                        first_packet_serverid_));
    } else {
        OptionPtr opt_serverid =
            offer_pkt4->getOption(DHO_DHCP_SERVER_IDENTIFIER);
        if (!opt_serverid) {
            isc_throw(BadValue, "there is no SERVER_IDENTIFIER option "
                      << "in OFFER message");
        }
        if (stats_mgr_.getRcvdPacketsNum(ExchangeType::DO) == 1) {
            first_packet_serverid_ = opt_serverid->getData();
        }
        pkt4->addOption(opt_serverid);
    }

    /// Set client address.
    asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
    if (!yiaddr.isV4()) {
        isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
                  " IPv4 address");
    }
    OptionPtr opt_requested_address =
        OptionPtr(new Option(Option::V4, DHO_DHCP_REQUESTED_ADDRESS,
                             OptionBuffer()));
    opt_requested_address->setUint32(yiaddr.toUint32());
    pkt4->addOption(opt_requested_address);
    OptionPtr opt_parameter_list =
        Option::factory(Option::V4, DHO_DHCP_PARAMETER_REQUEST_LIST);
    pkt4->addOption(opt_parameter_list);
    // Set client's and server's ports as well as server's address
    setDefaults4(pkt4);
    // Override relay address
    pkt4->setGiaddr(offer_pkt4->getGiaddr());
    // Add any extra options that user may have specified.
    addExtraOpts(pkt4);

    // Set hardware address
    pkt4->setHWAddr(offer_pkt4->getHWAddr());
    // Set client id.
    pkt4->addOption(generateClientId(pkt4->getHWAddr()));
    // Set elapsed time.
    uint32_t elapsed_time = getElapsedTime<Pkt4Ptr>(discover_pkt4, offer_pkt4);
    pkt4->setSecs(static_cast<uint16_t>(elapsed_time / 1000));
    // Prepare on wire data to send.
    pkt4->pack();
    socket_.send(pkt4);
    stats_mgr_.passSentPacket(ExchangeType::RA, pkt4);
    saveFirstPacket(pkt4);
}

void
TestControl::sendRequest4(const std::vector<uint8_t>& template_buf,
                          const dhcp::Pkt4Ptr& discover_pkt4,
                          const dhcp::Pkt4Ptr& offer_pkt4) {
    // Get the second argument if multiple the same arguments specified
    // in the command line. Second one refers to REQUEST packets.
    const uint8_t arg_idx = 1;
    // Use the same transaction id as the one used in the discovery packet.
    const uint32_t transid = discover_pkt4->getTransid();
    // Get transaction id offset.
    size_t transid_offset = getTransactionIdOffset(arg_idx);
    // Get the offset of MAC's last octet.
    // We need to go back by HW_ETHER_LEN (MAC address length)
    // because this offset points to last octet of MAC address.
    size_t rand_offset = getRandomOffset(arg_idx) - HW_ETHER_LEN + 1;
    // Create temporary buffer from the template.
    std::vector<uint8_t> in_buf(template_buf.begin(),
                                template_buf.end());
    // Check if given randomization offset is not out of bounds.
    if (rand_offset + HW_ETHER_LEN > in_buf.size()) {
        isc_throw(OutOfRange, "randomization offset is out of bounds");
    }

    // Create packet from the temporary buffer.
    PerfPkt4Ptr pkt4(new PerfPkt4(&in_buf[0], in_buf.size(),
                                  transid_offset,
                                  transid));

     // Set hardware address from OFFER packet received.
    HWAddrPtr hwaddr = offer_pkt4->getHWAddr();
    std::vector<uint8_t> mac_address(HW_ETHER_LEN, 0);
    uint8_t hw_len = hwaddr->hwaddr_.size();
    if (hw_len != 0) {
        memcpy(&mac_address[0], &hwaddr->hwaddr_[0],
               hw_len > HW_ETHER_LEN ? HW_ETHER_LEN : hw_len);
    }
    pkt4->writeAt(rand_offset, mac_address.begin(), mac_address.end());

    // Set elapsed time.
    size_t elp_offset = getElapsedTimeOffset();
    uint32_t elapsed_time = getElapsedTime<Pkt4Ptr>(discover_pkt4, offer_pkt4);
    pkt4->writeValueAt<uint16_t>(elp_offset,
                                 static_cast<uint16_t>(elapsed_time / 1000));

    // Get the actual server id offset.
    size_t sid_offset = getServerIdOffset();
    // Use first flags indicates that we want to use the server
    // id captured in first packet.
    if (options_.isUseFirst() &&
        (first_packet_serverid_.size() > 0)) {
        boost::shared_ptr<LocalizedOption>
            opt_serverid(new LocalizedOption(Option::V4,
                                             DHO_DHCP_SERVER_IDENTIFIER,
                                             first_packet_serverid_,
                                             sid_offset));
        pkt4->addOption(opt_serverid);
    } else {
        // Copy the contents of server identifier received in
        // OFFER packet to put this into REQUEST.
        OptionPtr opt_serverid_offer =
            offer_pkt4->getOption(DHO_DHCP_SERVER_IDENTIFIER);
        if (!opt_serverid_offer) {
            isc_throw(BadValue, "there is no SERVER_IDENTIFIER option "
                      << "in OFFER message");
        }
        boost::shared_ptr<LocalizedOption>
            opt_serverid(new LocalizedOption(Option::V4,
                                             DHO_DHCP_SERVER_IDENTIFIER,
                                             opt_serverid_offer->getData(),
                                             sid_offset));
        pkt4->addOption(opt_serverid);
        if (stats_mgr_.getRcvdPacketsNum(ExchangeType::DO) == 1) {
            first_packet_serverid_ = opt_serverid_offer->getData();
        }
    }

    /// Set client address.
    asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
    if (!yiaddr.isV4()) {
        isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
                  " IPv4 address");
    }

    // Get the actual offset of requested ip.
    size_t rip_offset = getRequestedIpOffset();
    // Place requested IP option at specified position (rip_offset).
    boost::shared_ptr<LocalizedOption>
        opt_requested_ip(new LocalizedOption(Option::V4,
                                             DHO_DHCP_REQUESTED_ADDRESS,
                                             OptionBuffer(),
                                             rip_offset));
    // The IOAddress is convertible to uint32_t and returns exactly what we need.
    opt_requested_ip->setUint32(yiaddr.toUint32());
    pkt4->addOption(opt_requested_ip);

    setDefaults4(boost::static_pointer_cast<Pkt4>(pkt4));

    // Add any extra options that user may have specified.
    addExtraOpts(pkt4);

    // Prepare on-wire data.
    pkt4->rawPack();
    socket_.send(boost::static_pointer_cast<Pkt4>(pkt4));
    // Update packet stats.
    stats_mgr_.passSentPacket(ExchangeType::RA,
                               boost::static_pointer_cast<Pkt4>(pkt4));
    saveFirstPacket(pkt4);
}

void
TestControl::sendRequest6(const Pkt6Ptr& advertise_pkt6) {
    const uint32_t transid = generateTransid();
    Pkt6Ptr pkt6(new Pkt6(DHCPV6_REQUEST, transid));
    // Set elapsed time.
    OptionPtr opt_elapsed_time =
        Option::factory(Option::V6, D6O_ELAPSED_TIME);
    pkt6->addOption(opt_elapsed_time);
    // Set client id.
    OptionPtr opt_clientid = advertise_pkt6->getOption(D6O_CLIENTID);
    if (!opt_clientid) {
        isc_throw(Unexpected, "client id not found in received packet");
    }
    pkt6->addOption(opt_clientid);

    // Use first flags indicates that we want to use the server
    // id captured in first packet.
    if (options_.isUseFirst() &&
        (first_packet_serverid_.size() > 0)) {
        pkt6->addOption(Option::factory(Option::V6, D6O_SERVERID,
                                        first_packet_serverid_));
    } else {
        OptionPtr opt_serverid = advertise_pkt6->getOption(D6O_SERVERID);
        if (!opt_serverid) {
            isc_throw(Unexpected, "server id not found in received packet");
        }
        if (stats_mgr_.getRcvdPacketsNum(ExchangeType::SA) == 1) {
            first_packet_serverid_ = opt_serverid->getData();
        }
        pkt6->addOption(opt_serverid);
    }

    // Copy IA_NA or IA_PD option from the Advertise message to the Request
    // message being sent to the server. This will throw exception if the
    // option to be copied is not found. Note that this function will copy
    // one of IA_NA or IA_PD options, depending on the lease-type value
    // specified in the command line.
    copyIaOptions(advertise_pkt6, pkt6);

    // Set default packet data.
    setDefaults6(pkt6);

    // Add any extra options that user may have specified.
    addExtraOpts(pkt6);

    // Prepare on-wire data.
    pkt6->pack();
    socket_.send(pkt6);
    stats_mgr_.passSentPacket(ExchangeType::RR, pkt6);
    saveFirstPacket(pkt6);
}

void
TestControl::sendRequest6(const std::vector<uint8_t>& template_buf,
                          const Pkt6Ptr& advertise_pkt6) {
    // Get the second argument if multiple the same arguments specified
    // in the command line. Second one refers to REQUEST packets.
    const uint8_t arg_idx = 1;
    // Generate transaction id.
    const uint32_t transid = generateTransid();
    // Get transaction id offset.
    size_t transid_offset = getTransactionIdOffset(arg_idx);
    PerfPkt6Ptr pkt6(new PerfPkt6(&template_buf[0], template_buf.size(),
                                  transid_offset, transid));
    // Set elapsed time.
    size_t elp_offset = getElapsedTimeOffset();
    boost::shared_ptr<LocalizedOption>
        opt_elapsed_time(new LocalizedOption(Option::V6, D6O_ELAPSED_TIME,
                                             OptionBuffer(), elp_offset));
    pkt6->addOption(opt_elapsed_time);

    // Get the actual server id offset.
    size_t sid_offset = getServerIdOffset();
    // Use first flags indicates that we want to use the server
    // id captured in first packet.
    if (options_.isUseFirst() &&
        (first_packet_serverid_.size() > 0)) {
        boost::shared_ptr<LocalizedOption>
            opt_serverid(new LocalizedOption(Option::V6,
                                             D6O_SERVERID,
                                             first_packet_serverid_,
                                             sid_offset));
        pkt6->addOption(opt_serverid);

    } else {
        // Copy the contents of server identifier received in
        // ADVERTISE packet to put this into REQUEST.
        OptionPtr opt_serverid_advertise =
            advertise_pkt6->getOption(D6O_SERVERID);
        if (!opt_serverid_advertise) {
            isc_throw(BadValue, "there is no SERVERID option "
                      << "in ADVERTISE message");
        }
        boost::shared_ptr<LocalizedOption>
            opt_serverid(new LocalizedOption(Option::V6,
                                             D6O_SERVERID,
                                             opt_serverid_advertise->getData(),
                                             sid_offset));
        pkt6->addOption(opt_serverid);
        if (stats_mgr_.getRcvdPacketsNum(ExchangeType::SA) == 1) {
            first_packet_serverid_ = opt_serverid_advertise->getData();
        }
    }
    // Set IA_NA
    OptionPtr opt_ia_na_advertise = advertise_pkt6->getOption(D6O_IA_NA);
    if (!opt_ia_na_advertise) {
        isc_throw(Unexpected, "DHCPv6 IA_NA option not found in received "
                  "packet");
    }
    size_t addr_offset = getRequestedIpOffset();
    boost::shared_ptr<LocalizedOption>
        opt_ia_na(new LocalizedOption(Option::V6, D6O_IA_NA, opt_ia_na_advertise->getData(), addr_offset));
    if (!opt_ia_na->valid()) {
        isc_throw(BadValue, "Option IA_NA in advertise packet is invalid");
    }
    pkt6->addOption(opt_ia_na);
    // Set server id.
    OptionPtr opt_serverid_advertise = advertise_pkt6->getOption(D6O_SERVERID);
    if (!opt_serverid_advertise) {
        isc_throw(Unexpected, "DHCPV6 SERVERID option not found in received "
                  "packet");
    }
    size_t srvid_offset = getServerIdOffset();
    boost::shared_ptr<LocalizedOption>
        opt_serverid(new LocalizedOption(Option::V6, D6O_SERVERID,
                                         opt_serverid_advertise->getData(),
                                         srvid_offset));
    pkt6->addOption(opt_serverid);
    // Get randomization offset.
    size_t rand_offset = getRandomOffset(arg_idx);
    OptionPtr opt_clientid_advertise = advertise_pkt6->getOption(D6O_CLIENTID);
    if (!opt_clientid_advertise) {
        isc_throw(Unexpected, "DHCPV6 CLIENTID option not found in received packet");
    }
    rand_offset -= (opt_clientid_advertise->len() - 1);
    // Set client id.
    boost::shared_ptr<LocalizedOption>
        opt_clientid(new LocalizedOption(Option::V6, D6O_CLIENTID,
                                         opt_clientid_advertise->getData(),
                                         rand_offset));
    pkt6->addOption(opt_clientid);
    // Set default packet data.
    setDefaults6(pkt6);

    // Add any extra options that user may have specified.
    addExtraOpts(pkt6);

    // Prepare on wire data.
    pkt6->rawPack();
    // Send packet.
    socket_.send(pkt6);
    // Update packet stats.
    stats_mgr_.passSentPacket(ExchangeType::RR, pkt6);

    // When 'T' diagnostics flag is specified it means that user requested
    // printing packet contents. It will be just one (first) packet which
    // contents will be printed. Here we check if this packet has been already
    // collected. If it hasn't we save this packet so as we can print its
    // contents when test is finished.
    if (options_.testDiags('T') &&
        (template_packets_v6_.find(DHCPV6_REQUEST) == template_packets_v6_.end())) {
        template_packets_v6_[DHCPV6_REQUEST] = pkt6;
    }
}

void
TestControl::sendSolicit6(const bool preload /*= false*/) {
    // Generate DUID to be passed to the packet
    uint8_t randomized = 0;
    std::vector<uint8_t> duid = generateDuid(randomized);
    // Generate transaction id to be set for the new exchange.
    const uint32_t transid = generateTransid();
    Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, transid));
    if (!pkt6) {
        isc_throw(Unexpected, "failed to create SOLICIT packet");
    }

    // Check if we need to simulate HA failures by pretending no responses were received.
    // The DHCPv6 protocol signals that by increasing the elapsed option field. Note it is in 1/100 of a second.
    if (options_.getIncreaseElapsedTime() &&
        stats_mgr_.getTestPeriod().length().total_seconds() >= options_.getWaitForElapsedTime() &&
        stats_mgr_.getTestPeriod().length().total_seconds() < options_.getWaitForElapsedTime() +
                                     options_.getIncreaseElapsedTime()) {


        // Keep increasing elapsed time. The value should start increasing steadily.
        uint32_t val = (stats_mgr_.getTestPeriod().length().total_seconds() - options_.getWaitForElapsedTime() + 1)*100;
        if (val > 65535) {
            val = 65535;
        }
        OptionPtr elapsed(new OptionInt<uint16_t>(Option::V6, D6O_ELAPSED_TIME, val));
        pkt6->addOption(elapsed);
    } else {
        pkt6->addOption(Option::factory(Option::V6, D6O_ELAPSED_TIME));
    }

    if (options_.isRapidCommit()) {
        pkt6->addOption(Option::factory(Option::V6, D6O_RAPID_COMMIT));
    }
    pkt6->addOption(Option::factory(Option::V6, D6O_CLIENTID, duid));
    pkt6->addOption(Option::factory(Option::V6, D6O_ORO));


    // Depending on the lease-type option specified, we should request
    // IPv6 address (with IA_NA) or IPv6 prefix (IA_PD) or both.

    // IA_NA
    if (options_.getLeaseType().includes(CommandOptions::LeaseType::ADDRESS)) {
        pkt6->addOption(Option::factory(Option::V6, D6O_IA_NA));
    }
    // IA_PD
    if (options_.getLeaseType().includes(CommandOptions::LeaseType::PREFIX)) {
        pkt6->addOption(Option::factory(Option::V6, D6O_IA_PD));
    }

    setDefaults6(pkt6);

    // Add any extra options that user may have specified.
    addExtraOpts(pkt6);

    pkt6->pack();
    socket_.send(pkt6);
    if (!preload) {
        stats_mgr_.passSentPacket(ExchangeType::SA, pkt6);
    }

    saveFirstPacket(pkt6);
}

void
TestControl::sendSolicit6(const std::vector<uint8_t>& template_buf,
                          const bool preload /*= false*/) {
    const int arg_idx = 0;
    // Get transaction id offset.
    size_t transid_offset = getTransactionIdOffset(arg_idx);
    // Generate transaction id to be set for the new exchange.
    const uint32_t transid = generateTransid();
    // Create packet.
    PerfPkt6Ptr pkt6(new PerfPkt6(&template_buf[0], template_buf.size(),
                                  transid_offset, transid));
    if (!pkt6) {
        isc_throw(Unexpected, "failed to create SOLICIT packet");
    }
    size_t rand_offset = getRandomOffset(arg_idx);
    // randomized will pick number of bytes randomized so we can
    // just use part of the generated duid and substitute a few bytes
    /// in template.
    uint8_t randomized = 0;
    std::vector<uint8_t> duid = generateDuid(randomized);
    if (rand_offset > template_buf.size()) {
        isc_throw(OutOfRange, "randomization offset is out of bounds");
    }
    // Store random part of the DUID into the packet.
    pkt6->writeAt(rand_offset - randomized + 1,
                  duid.end() - randomized, duid.end());

    // Prepare on-wire data.
    pkt6->rawPack();
    setDefaults6(pkt6);

    // Add any extra options that user may have specified.
    addExtraOpts(pkt6);

    // Send solicit packet.
    socket_.send(pkt6);
    if (!preload) {
        // Update packet stats.
        stats_mgr_.passSentPacket(ExchangeType::SA, pkt6);
    }
    saveFirstPacket(pkt6);
}


void
TestControl::setDefaults4(const Pkt4Ptr& pkt) {
    // Interface name.
    IfacePtr iface = socket_.getIface();
    if (iface == NULL) {
        isc_throw(BadValue, "unable to find interface with given index");
    }
    pkt->setIface(iface->getName());
    // Interface index.
    pkt->setIndex(socket_.ifindex_);
    // Local client's port (68)
    pkt->setLocalPort(DHCP4_CLIENT_PORT);
    // Server's port (67)
    if (options_.getRemotePort()) {
        pkt->setRemotePort(options_.getRemotePort());
    } else {
        pkt->setRemotePort(DHCP4_SERVER_PORT);
    }
    // The remote server's name or IP.
    pkt->setRemoteAddr(IOAddress(options_.getServerName()));
    // Set local address.
    pkt->setLocalAddr(socket_.addr_);
    // Set relay (GIADDR) address to local address if multiple
    // subnet mode is not enabled
    if (!options_.checkMultiSubnet()) {
        pkt->setGiaddr(socket_.addr_);
    } else {
        pkt->setGiaddr(IOAddress(options_.getRandRelayAddr()));
    }
    // Pretend that we have one relay (which is us).
    pkt->setHops(1);
}

void
TestControl::setDefaults6(const Pkt6Ptr& pkt) {
    // Interface name.
    IfacePtr iface = socket_.getIface();
    if (iface == NULL) {
        isc_throw(BadValue, "unable to find interface with given index");
    }
    pkt->setIface(iface->getName());
    // Interface index.
    pkt->setIndex(socket_.ifindex_);
    // Local client's port (547)
    pkt->setLocalPort(DHCP6_CLIENT_PORT);
    // Server's port (548)
    if (options_.getRemotePort()) {
        pkt->setRemotePort(options_.getRemotePort());
    } else {
        pkt->setRemotePort(DHCP6_SERVER_PORT);
    }
    // Set local address.
    pkt->setLocalAddr(socket_.addr_);
    // The remote server's name or IP.
    pkt->setRemoteAddr(IOAddress(options_.getServerName()));

    // only act as a relay agent when told so.
    /// @todo: support more level of encapsulation, at the moment we only support
    /// one, via -A1 option.
    if (options_.isUseRelayedV6()) {
      Pkt6::RelayInfo relay_info;
      relay_info.msg_type_ = DHCPV6_RELAY_FORW;
      relay_info.hop_count_ = 0;
      if (options_.checkMultiSubnet()) {
          relay_info.linkaddr_ = IOAddress(options_.getRandRelayAddr());
      } else {
          relay_info.linkaddr_ = socket_.addr_;
      }
      relay_info.peeraddr_ = socket_.addr_;
      relay_info.options_.insert(options_.getRelayOpts().begin(), options_.getRelayOpts().end());
      pkt->addRelayInfo(relay_info);
    }
}

namespace {

static OptionBuffer const concatenateBuffers(OptionBuffer const& a,
                                             OptionBuffer const& b) {
    OptionBuffer result;
    result.insert(result.end(), a.begin(), a.end());
    result.insert(result.end(), b.begin(), b.end());
    return (result);
}

static void mergeOptionIntoPacket(Pkt4Ptr const& packet,
                                  OptionPtr const& extra_option) {
    uint16_t const code(extra_option->getType());
    // If option already exists...
    OptionPtr const& option(packet->getOption(code));
    if (option) {
        switch (code) {
        // List here all the options for which we want to concatenate buffers.
        case DHO_DHCP_PARAMETER_REQUEST_LIST:
            packet->delOption(code);
            packet->addOption(boost::make_shared<Option>(
                Option::V4, code,
                concatenateBuffers(option->getData(),
                                   extra_option->getData())));
            return;
        default:
            // For all others, add option as usual, it will result in "Option
            // already present in this message" error.
            break;
        }
    }
    packet->addOption(extra_option);
}

}  // namespace

void
TestControl::addExtraOpts(const Pkt4Ptr& pkt) {
    // Add all extra options that the user may have specified.
    const dhcp::OptionCollection& extra_opts = options_.getExtraOpts();
    for (auto const& entry : extra_opts) {
        mergeOptionIntoPacket(pkt, entry.second);
    }
}

void
TestControl::addExtraOpts(const Pkt6Ptr& pkt) {
    // Add all extra options that the user may have specified.
    const dhcp::OptionCollection& extra_opts = options_.getExtraOpts();
    for (auto const& entry : extra_opts) {
        pkt->addOption(entry.second);
    }
}

}  // namespace perfdhcp
}  // namespace isc