File: cli_send.c

package info (click to toggle)
packeth 3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,116 kB
  • sloc: ansic: 11,823; makefile: 111; xml: 29; sh: 1
file content (2123 lines) | stat: -rw-r--r-- 85,584 bytes parent folder | download
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
/*
 * packETH, packETHcli - ethernet packet generator
 * By Miha Jemec <jemcek@gmail.com>
 * Copyright 2018 Miha Jemec
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include <assert.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

#include <net/if.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define PCAP_MAGIC   0xa1b2c3d4
#ifndef MAX_MTU
    #define MAX_MTU 9000
    #define MAX_MTU_STR "9000"
#endif

#define MY_PATTERN "a9b8c7d6"
//char my_pattern[]="a9b8c7d6";

/* "libpcap" file header (minus magic number). */
struct pcap_hdr {
    uint32_t     magic;          /* magic */
    uint16_t     version_major;  /* major version number */
    uint16_t     version_minor;  /* minor version number */
    uint32_t     thiszone;       /* GMT to local correction */
    uint32_t     sigfigs;        /* accuracy of timestamps */
    uint32_t     snaplen;        /* max length of captured packets, in octets */
    uint32_t     network;        /* data link type */
};

/* "libpcap" record header. */
struct pcaprec_hdr {
    int32_t      ts_sec;         /* timestamp seconds */
    uint32_t     ts_usec;        /* timestamp microseconds */
    uint32_t     incl_len;       /* number of octets of packet saved in file */
    uint32_t     orig_len;       /* actual length of packet */
};

struct params {
    int mode;
    struct sockaddr_ll sa;
    struct ifreq ifr;
    struct ifreq ifopts;    /* set promiscuous mode */
    struct pcap_hdr fh;
    struct pcaprec_hdr ph;
    char iftext[20];
    int fd;
    char *ptr; 
    char pkt_temp[10000];
    char filename[200];
    long long delay;
    int bw;
    int BW;
    long long number;
    long duration;
    int period;
    int attack;
    char sizeramp[50];
    char rateramp[50];
    char rateRAMP[50];
    char burstargs[50];
    int packetsize;
    int seqnum;
    int offset_counter;
    int offset_pattern;
    char pattern[20];
    int my_pattern;
    int delay_mode;
    int paramnum;
    int display_interval;
    int rate;
    int size;
    int startrate;
    int stoprate;
    int steprate;
    int startsize;
    int stopsize;
    int stepsize;
    int ConstantRate;
    int num_rules;
    int burst_size;
    int burst_packets_in_burst;
    int burst_delay_between_packets;
    int burst_delay_to_next_burst;
    int burst_total;


} params1;

int STOP=0;

/* Link-layer type; */
//static unsigned long pcap_link_type = 1;   /* Default is DLT-EN10MB */

/* for mode 4, please change them accordingly */
#define MAC_DST_ADDR    {0x00, 0x04, 0x23, 0xB7, 0x29, 0xC4}
#define MAC_SRC_ADDR    {0x00, 0x04, 0x23, 0xB7, 0x21, 0xD8}
#define IP_SRC_ADDR     0x0A0A0A0A
#define IP_DST_ADDR     0x0B0B0B0B
#define TCP_SRC_PORT    htons(80)
#define TCP_DST_PORT    (MyRandom(seed)>>16)

extern char **g_content;
extern char *null_payload;
uint16_t TCPChecksum(uint16_t* buf1, int buf1len, uint16_t* buf2, int buf2len);
uint32_t MyRandom(uint64_t *seed);
__sum16 ip_fast_csum(const void *iph, unsigned int ihl);
char *build_packet(char *buffer, int pktsize, int tot_rules, int *rule_idx, uint64_t *seed, int attack);
int readSnortRules(const char *filename);
void cleanupRules(int);
int send_single_packet(void);
int send_constant_stream(void);
int send_variable_rate(void);
int send_variable_size(void);
int send_burst_constant_mode(void);
int send_ids_mode(void);
int receiver_mode(void);
int two(char *interface, long delay, long pkt2send, char *filename, char *sizetmp, int period, char *ratetmp);
int four(char *interface, long delay, long pkt2send, char *filename, char *sizetmp, int period, int attack);
void usage(void);
void usage_1(void);
void usage_2(void);
void usage_3(void);
void usage_4(void);
void usage_5(void);
void usage_6(void);
void usage_9(void);
void examples(void);
void print_final(struct timeval first, long packets_sent, char *interface_name);
void print_intermidiate(long packets_sent, long packets_last_sent, int packet_size, int print_interval);
void onexit(int);
int interface_setup(void);
int read_packet_from_file(char *filename);
int function_send(void);
int function_send_burst(void);



int main(int argc, char *argv[])
{
    
    int c;
    char *p;

    /* set default values */
    params1.mode=1;
    params1.delay = -2;
    params1.bw = -2;
    params1.BW = -2;
    params1.number = -2;
    params1.duration = -2;
    params1.sizeramp[0]='\0';
    params1.rateramp[0]='\0';
    params1.rateRAMP[0]='\0';
    params1.burstargs[0]='\0';
    params1.iftext[0]='\0';
    params1.period = -2;
    params1.attack = -2;
    params1.packetsize = -2;
    params1.seqnum = -2;
    params1.pattern[0]='\0';
    params1.offset_counter = 0;
    params1.offset_pattern = 0;
    params1.my_pattern = 0;
    params1.delay_mode = 0;
    params1.paramnum = 0;
    params1.display_interval = 1;
    params1.rate = 0;
    params1.size = 0;
    params1.startrate = 0;
    params1.stoprate = 0;
    params1.steprate = 0;
    params1.startsize = 0;
    params1.stopsize = 0;
    params1.stepsize = 0 ;
    params1.ConstantRate = 0;
    params1.num_rules = 0;
    params1.burst_size = 0;
    params1.burst_packets_in_burst = 0;
    params1.burst_delay_between_packets = 0;
    params1.burst_delay_to_next_burst = 0;
    params1.burst_total = 0;

    setlinebuf(stdout);

    /* Scan CLI parameters */
    while ((c = getopt(argc, argv, "heI:i:m:d:D:t:b:B:n:s:S:L:p:f:z:Z:a:c:o:q:w:x")) != -1) {
        switch(c) {
            case 'a': {
                params1.attack = strtol(optarg, &p, 10);
                if ((params1.attack < 1) || (params1.attack > 4)) {
                   printf("\n Selected amount of attack traffic (-a <value>) should be between 1-4!\n\n");
                   exit(0);
                }
                params1.attack = (params1.attack < 0 || params1.attack > 4) ? 4 : params1.attack;
                break;
            }
            case 'h': {
                usage(); 
                usage_1(); 
                usage_2(); 
                usage_3(); 
                usage_4(); 
                usage_5(); 
                usage_6(); 
                usage_9();
                exit(0); 
                break;
            }
            case 'e': {
                examples();
                break;
            }
            case 'i': {
                strcpy(params1.iftext, optarg); 
                // waht values are allowed
                break;
            }
            case 'I': {
                params1.display_interval = strtol(optarg, &p, 10); 
                if ((params1.display_interval < 1) || (params1.display_interval > 600)) {
                   printf("\n Diplay interval (-I <value>) should be between 1s (default) and 600s!\n\n");
                   exit(0);
                }
                break;
            }
            case 'm': {
                params1.mode = strtol(optarg, &p, 10);
                if ( (params1.mode!=1) && (params1.mode!=2) && (params1.mode!=3) && (params1.mode!=4) && (params1.mode!=5) && (params1.mode!=6) && (params1.mode!=9)) {
                    printf("\n Wrong mode option (-m mode). Allowed 1,2,3,4,5,6 or 9.\n\n");
                    exit (7);
                }
                break;
            }
            case 'd': {
                params1.delay = strtoll(optarg, &p, 10);
                params1.delay_mode = params1.delay_mode + 1;
                if ( (params1.delay < -1) || (params1.delay > 100000000)) {
                    printf("\n Delay between packets (-d <value>) should be between 0 and 100000000ms (100s).\n\n");
                    exit (7);
                }
                break;
            }
            case 'D': {
                params1.delay = strtoll(optarg, &p, 10);
                params1.delay_mode = params1.delay_mode + 2;
                if ( (params1.delay < 1) || (params1.delay > 1000000000)) {
                    printf("\n Delay between packets (-D <value>) should be between 1 and 1000000000ns (1s).\n\n");
                    exit (7);
                }
                break;
            }
            case 'b': {
                params1.bw = strtol(optarg, &p, 10);
                params1.delay_mode = params1.delay_mode + 4;
                if ((params1.bw < 1) || (params1.bw > 100000000)) {
                    printf("\n Desired bandwidth  (-b <value>) should be between 1kbit/s and 100Gbit/s!\n\n");
                    exit(0);
                }
                break;
            }
            case 'B': {
                params1.BW = strtol(optarg, &p, 10);
                params1.delay_mode = params1.delay_mode + 8;
                if ((params1.BW < 1) || (params1.BW > 100000000)) {
                    printf("\n Desired bandwidth  (-B <value>) should be between 1Mbit/s and 100Gbit/s!\n\n");
                    exit(0);
                }
                break;
            }
            case 'c': {
                params1.seqnum = strtol(optarg, &p, 10);
                break;
            }
            case 'n': {
                params1.number = strtoll(optarg, &p, 10);
                if ((params1.number < 0) || (params1.number > 10000000000000000)) {
                    printf("\n Number of packets to send (-n <value>) out of range!\n\n");
                    exit(0);
                }
                break;
            }
            case 't': {
                params1.duration = strtol(optarg, &p, 10);
                if ((params1.duration < 1) || (params1.duration > 360000000)) {
                    printf("\n Duration (-t <value>) out of range!\n\n");
                    exit(0);
                }
                break;
            }
            case 'S': {
                params1.packetsize = strtol(optarg, &p, 10);
                if ((params1.packetsize < 60) || (params1.packetsize > MAX_MTU)) {
                    printf("\n Packetsize (-S <value>) out of range!\n\n");
                    exit(0);
                }
                break;
            }
            case 'L': {
                memcpy(params1.burstargs, optarg, 50);
                break;
            }
            case 's': {
                memcpy(params1.sizeramp, optarg, 20);
                break;
            }
            case 'z': {
                memcpy(params1.rateramp, optarg, 50);
                break;
            }
            case 'Z': {
                memcpy(params1.rateRAMP, optarg, 50);
                break;
            }
            case 'p': {
                params1.period = strtol(optarg, &p, 10);
                if ((params1.period < 1) || (params1.period > 360000)) {
                    printf("\n Period (-p <value>) out of range!\n\n");
                    exit(0);
                }
                break;
            }
            case 'o': {
                params1.offset_counter = strtol(optarg, &p, 10);
                params1.my_pattern = params1.my_pattern + 2;
                if ((params1.offset_counter < 1) || (params1.offset_counter > MAX_MTU)) {
                    printf("\n Offset counter (-o <value>) out of range!\n\n");
                    exit(0);
                }
                break;
            }
            case 'q': {
                params1.offset_pattern = strtol(optarg, &p, 10);
                params1.my_pattern = params1.my_pattern + 4;
                if ((params1.offset_pattern < 1) || (params1.offset_pattern > MAX_MTU)) {
                    printf("\n Offset pattern (-q <value>) out of range!\n\n");
                    exit(0);
                }
                break;
            }
            case 'w': {
                memcpy(params1.pattern, optarg, 20);
                params1.my_pattern = params1.my_pattern + 8;
                break;
            }
            case 'x': {
                params1.my_pattern = params1.my_pattern + 1;
                break;
            }
            case 'f': {
                memcpy(params1.filename, optarg, 99);
                break;
            }
            default: {
                usage();
                exit(0);
            }
        }
    }
   
    
    if (argc == 1) {
        usage();
        printf("FOR COMPLETE HELP: ./packETHcli -h\n");
        printf("\n");   
        exit(0);      
    }

    
    if (argc == 3) {
        //just the help
        params1.paramnum = 1;
    }
    else {
        /* set up the selected interface */
        interface_setup();
        /* read packet from file in modes 1-4 */
        if ((params1.mode < 5) || (params1.mode == 6))
            read_packet_from_file(params1.filename);
    }

    switch (params1.mode) {
        case 1: {
            send_single_packet();
            break;
        }
        case 2: {
            send_constant_stream();
            break;
        }
        case 3: {
            send_variable_rate();
            break;
        }
        case 4: {
            send_variable_size();
            break;
        }
        case 5: {
            send_ids_mode();
            break;
        }
        case 6: {
            send_burst_constant_mode();
            break;
        }
        case 9: {
            receiver_mode();
            break;
        }
    }
    return 0;
}


/*------------------------------------------------------------------------------*/
int receiver_mode(void) {

    char buf[10000];
    ssize_t recv_size = -1, size = 0;

    int firstround=0, first_packet=0;
    long packets=0, packets_total=0, my_packets=0, my_total_packets=0;
    long seconds=0, gap=0, errors=0; 
    //long gapns=0;
    unsigned int last_value=0, current_value=0;
    long mbps;
    float Mbps;

    //struct sockaddr_ll socket_address;
    //struct ifreq ifr;
    //char iftext[30];

    struct timeval nowstr, first;
    //struct timespec first_ns, now_ns;

    //char pattern[30];   
    //int offset_counter=0;
    //int offset_pattern=0;
    //int my_pattern=0;

    // print help for this mode
    if (params1.paramnum == 1) {  
        usage_9();
        exit(0);
    }

    if ((params1.my_pattern > 1) && (params1.my_pattern != 14)) {
        printf("\n Wrong pattern parameters. Choose one option:\n\n");
        printf("   Predifined pattern:  -x \n");
        printf("   Custom pattern:      -o <offset_counter> -q <offset_pattern> -w <pattern> \n\n");
        exit(7);
    }
    else if (( strlen(params1.pattern) > 0) && ((params1.offset_counter == 0) || (params1.offset_pattern == 0))) {
        printf("\n Option -w requires options -o and -q!\n\n");
        exit(7);
    }
    else if (( params1.offset_counter != 0) && ((strlen(params1.pattern) == 0) || (params1.offset_pattern == 0))) {
        printf("\n Option -o requires options -q and -w!\n\n");
        exit(7);
    }
    else if (( params1.offset_pattern != 0) && ((strlen(params1.pattern) == 0) || (params1.offset_counter == 0))) {
        printf("\n Option -q requires options -o and -w!\n\n");
        exit(7);
    }
    else if (strlen(params1.pattern) > 16) {
        printf("\n Pattern should not be longer than 16 chars!\n\n");
        exit(7);   
    }
    else if ((params1.offset_pattern < 0) || (params1.offset_pattern > 9900)) {
        printf("\n Offset of the pattern should be between 0 and 9900!\n\n");
        exit(7);   
    }
    else if ((params1.offset_counter < 0) || (params1.offset_counter > 9900)) {
        printf("\n Offset of the counter should be between 0 and 9900!\n\n");
        exit(7);   
    }
    else if ((strlen(params1.pattern) > 0) && (params1.offset_counter >= params1.offset_pattern) && (params1.offset_counter <= params1.offset_pattern + strlen(params1.pattern))) {
        printf("\n Counter position offset and pattern position offset should not overlap!\n\n");
        exit(7);   
    }
    else if (params1.attack != -2) {
        printf("\n -a option not allowed in this mode!\n\n");
        exit(7);      
    }
     if (params1.packetsize != -2) {
        printf("\n Option -S not allowed in this mode!\n\n");
        exit(7);    
    }
    if (params1.delay_mode != 0) {
        printf("\n Delay (-d, -D) and bandwidth (-b, -B) options not allowed in this mode!\n\n");
        exit(7);
    }
    if (params1.number != -2) {
        printf("\n Option (-n) not allowed in this mode!\n\n");
        exit(7);
    }
    if (params1.duration != -2) {
        printf("\n Option (-t) not allowed in this mode!\n\n");
        exit(7);
    }
    if (params1.display_interval != 1) {
        printf("\n Option (-I) ignored in this mode!\n\n");
        exit(7);
    }
    if ((strlen(params1.sizeramp) > 0 ) || (strlen(params1.rateramp) > 0 ) || (strlen(params1.rateRAMP) > 0 ) || (params1.period != -2)) {
        printf("\n Ramp options not allowed in this mode!\n\n");
        exit(7);
    }
    if (strlen(params1.filename) > 0) {
        printf("\n Option -f not allowed in this mode!\n\n");
        exit(7);
    }
    if (params1.seqnum != -2) {
        printf("\n Option -c not allowed in this mode!\n\n");
        exit(7);
    }

    signal(SIGINT, onexit);

    gettimeofday(&first, NULL);
    gettimeofday(&nowstr, NULL);

    while (1) {
        memset(&buf, 0, sizeof(buf));

        recv_size = recv(params1.fd, &buf, sizeof(buf), 0);

        //we received a packet
        if (recv_size > 0) {
            
            // is -x options enabled?
            if (params1.my_pattern == 1) {
                // do the last 10 bytes match
                if (strncmp(&buf[recv_size-10], MY_PATTERN, 8) == 0) {
                    //now check if the sequence number matches (this is the last byte in payload)
                    current_value = (unsigned int)buf[recv_size-1];
                    //printf("2 %02x %02x \n", current_value, last_value);        
                    
                    //if this is (re)start ignore the first packet value and don't count as error
                    if (first_packet == 0) {
                        first_packet = 1;
                    }
                    else if ((current_value != last_value + 1) && (current_value != last_value - 255)) {
                        //it doesn't match, so increase the error counter
                        errors++;
                    }

                    last_value = current_value; 
 
                    // ok they match, so this is my packet. We can increase the counter
                    my_packets++;
                    my_total_packets++;
                    size = recv_size;

                }

                // count all packets also not ours
                packets++;
                packets_total++;
                
            }
            // it seems that custom option was choosed
            else if (params1.offset_pattern > 0) {  
                // does the pattern matches?
                if (strncmp(&buf[params1.offset_pattern], params1.pattern, strlen(params1.pattern)) == 0) {
            
                    //now check if the sequence number matches (this is the last byte in payload)
                    current_value = (unsigned int)buf[params1.offset_counter-1];
                    //if this is (re)start ignore the first packet value and don't count as error
                    if (first_packet == 0) {
                        first_packet = 1;
                    }
                    else if ((current_value != last_value + 1) && (current_value != last_value - 255)) {
                        //it doesn't match, so increase the error counter
                        errors++;
                    }

                    last_value = current_value; 
 
                    // ok they match, so this is my packet. We can increase the counter
                    my_packets++;
                    my_total_packets++;
                    size = recv_size;
                   
                }

                packets++;
                packets_total++;
                
            }

            // we match all packets, so let's count them in case there is no filter
            else {
                packets++;
                packets_total++;
            }
            
            //printf("new packet\n");
            //for(i=0; i < recv_size; i++)
            //{
            //printf("%02x ", buf[i]);
            //}
            
            if (firstround == 0) {
                firstround = 1;
            }
 
        }


        gettimeofday(&nowstr, NULL);
        gap = nowstr.tv_sec - first.tv_sec;

        //clock_gettime(CLOCK_MONOTONIC, &now_ns);
        //gapns = now_ns.tv_sec - first_ns.tv_sec;
        
        //if (gapns > seconds) {
        if (gap > seconds) {
            if (firstround == 1) {
                firstround = 2;
                errors = 0;
            }
            else {
                mbps = my_packets * size / 125; // 8 bits per byte / 1024 for kbit
                Mbps = (float)mbps/1000;
                printf("Elapsed %lds; Interface %s; Matched packets: %ld pps, %.3f Mbit/s, total %ld packets, %ld sequence errors; All packets: %ld pps, total %ld \n", seconds, params1.iftext, my_packets, Mbps, my_total_packets, errors, packets, packets_total);
                
                // in case sender stops trasmitting and later restarts, we don't want to see this as an error
                if (my_packets == 0)
                    first_packet = 0;

                //some counters update
                seconds++;
                packets=0;
                my_packets=0;
            }

            if (STOP == 1)
                break;
        }
        
    }

    printf("----\n");
    printf("Received %ld my packets and %ld all packets on inteface %s\n", my_total_packets, packets_total, params1.iftext);
    printf("----\n");

    return 0;
}

/*------------------------------------------------------------------------------*/

int send_single_packet(void)
{
    int c;

    if (params1.paramnum == 1) {  
        usage_1();
        exit(0);
    }

    if ((params1.delay != -2 ) || (params1.bw != -2) || (params1.BW != -2) || (params1.number != -2) || (params1.duration != -2) || (params1.packetsize != -2) || (params1.attack != -2)) {
        printf("\n No special options allowed in this mode! You can only select interface (-i), filename (-f) and packet number (-c)!\n\n");
        return 1;
    }

    if ((strlen(params1.sizeramp) > 0) || (strlen(params1.rateramp) > 0 ) || (strlen(params1.rateRAMP) > 0 ) || (params1.period != -2) || (params1.my_pattern > 0)) {
        printf("\n No special options allowed in this mode! You can only select interface (-i), filename (-f) and packet number (-c)\n\n");
        return 1;
    }

    c = sendto(params1.fd, params1.ptr, params1.ph.incl_len, 0, (struct sockaddr *)&params1.sa, sizeof (params1.sa));

    printf("\nSent 1 packet (%d bytes) on interface %s\n\n", c, params1.iftext);
    fflush(stdout);

    exit(1);
}

/*------------------------------------------------------------------------------*/
/* send one packet more than once */
int send_constant_stream() {

    // print help for this mode
    if (params1.paramnum == 1) {  
        usage_2();
        exit(0);
    }

    //check if the options are ok
    if ((params1.number == -2) && (params1.duration == -2)) {
        printf("\n Missing number of packets to send or time in seconds to transmit.\n Specify -n <number of packets> or -t <seconds to transmit>.\n");
        printf(" Set -n 0 to send infinite number of packets\n\n");
        exit(7);
    }
    else if ((params1.number != -2) && (params1.duration != -2)) {
        printf("\n Only one option allowed at a time (-n or -t). \n Specify -n <number of packets> or -t <seconds to tramsmit>!\n\n");
        exit(7);
    }
    
    if ((params1.delay_mode != 1) && (params1.delay_mode != 2) && (params1.delay_mode != 4) && (params1.delay_mode != 8)) {
        printf("\n Wrong or missing delay between packets or bandwidth parameter.\n\n Specify one of the following options:\n");
        printf("   -D <nanoseconds>    - delay between packets in nanoseconds\n");
        printf("   -d <microseconds>   - delay between packets in microseconds\n");
        printf("   -d -1               - maximum speed without counters\n");
        printf("   -d 0                - maximum speed with counters\n");
        printf("   -b <bandwidth>      - desired bandwidth in kbit/s\n");
        printf("   -B <bandwidth>      - desired bandwidth in Mbit/s\n\n");
        exit(7);    
    }

    if (params1.delay_mode == 1)
        params1.delay = params1.delay * 1000;
    else if (params1.delay_mode == 2) 
        params1.delay = params1.delay;
    else if (params1.delay_mode == 4)
        params1.delay = (long long)(1000000 * (long long)params1.ph.incl_len * 8 / params1.bw);
    else if (params1.delay_mode == 8) 
        params1.delay = (long long)(1000 * (long long)params1.ph.incl_len * 8 / params1.BW);


    if ((params1.delay == -1000) && (params1.number != 0)) {
        printf("\n Option -d -1 also requires option -n 0 (infinite numbers of packest to send)\n\n");
        exit(7);
    }

    if ((params1.number == -2) && (params1.duration > 0)) {
        params1.number = 0;
    }
    
    if (params1.packetsize != -2) {
        printf("\n Option -S not allowed in this mode\n\n");
        exit(7);    
    }

    if ((strlen(params1.sizeramp) > 0 ) || (strlen(params1.rateramp) > 0 ) || (strlen(params1.rateRAMP) > 0 ) || (params1.period != -2)) {
        printf("\n Ramp options not allowed in this mode\n\n");
        exit(7);
    }
    if (params1.delay > 999000000) {
            printf ("\n Warning! Rate is below 1pps, statistics will be displayed only when a packet will be sent.\n\n"); 
    }
    if ((params1.my_pattern > 1) && (params1.my_pattern != 14)) {
        printf("\n Wrong pattern parameters. Choose one option:\n\n");
        printf("   Predifined pattern:  -x \n");
        printf("   Custom pattern:      -o <offset_counter> -q <offset_pattern> -w <pattern> \n\n");
        exit(7);
    }
    else if (strlen(params1.pattern) > 16) {
        printf("\n Pattern should not be longer than 16 chars!\n\n");
        exit(7);   
    }
    else if ((params1.offset_pattern < 0) || (params1.offset_pattern+strlen(params1.pattern) > params1.ph.incl_len)) {
        printf("\n Offset of the pattern is outside the packet size!\n\n");
        exit(7);   
    }
    else if ((params1.offset_counter < 0) || (params1.offset_counter > params1.ph.incl_len)) {
        printf("\n Offset of the counter is outside the packet size!\n\n");
        exit(7);   
    }
    else if ((params1.my_pattern > 1) && (params1.offset_counter >= params1.offset_pattern) && (params1.offset_counter <= params1.offset_pattern + strlen(params1.pattern))) {
        printf("\n Counter position and pattern position should not overlap!\n\n");
        exit(7);   
    }
    else if ((params1.delay == -1) && ((params1.offset_counter !=0) || (params1.offset_pattern != 0) || (strlen(params1.pattern) >0 ))) {
        printf("\n Option -x OR -o -q -w are not compatible with high speed -d -1 mode!\n\n");
        exit(7);   
    }
    if (params1.attack != -2) {
        printf("\n -a option not allowed in this mode!\n\n");
        exit(7);      
    }


    // if we insert my_pattern, this will be inserted from last 10 to last 2 bytes. Last 2 bytes themselves are reserved for counter 
    if (params1.my_pattern == 1) {
        memcpy(params1.ptr+params1.ph.incl_len-10, MY_PATTERN, 8);
        memset(params1.ptr+params1.ph.incl_len-2, 0, 1);
        memset(params1.ptr+params1.ph.incl_len-1, 1, 1);
    }

    // in case we use custom pattern and offset
    if(params1.my_pattern > 1) {
        memcpy(params1.ptr+params1.offset_pattern, params1.pattern, strlen(params1.pattern));
    }

    params1.size = params1.ph.incl_len; 

    //everything is set up, lets start sending
    function_send();

    return 1;
}

/*------------------------------------------------------------------------------*/
int send_variable_rate() {

    int count, flag = 0;
    int Mega = 0;

    int wordcount = 0;
    
    //char *ptr; 
    char *p;
   
    char tmp8[50];   
    char tmp7[20];
    char ch;

    if (params1.paramnum == 1) {  
        usage_3();
        exit(0);
    }

    //check if the options are ok
    if (params1.delay_mode != 0) {
        printf("\n Delay (-d, -D) and bandwidth (-b, -B) options not allowed in this mode. Rate is specified with -z or -Zoption!\n\n");
        exit(7);
    }

    if ((params1.number == -2) && (params1.duration == -2)) {
        printf("\n Missing number of packets to send or time in seconds to transmit.\n Specify -n <number of packets> or -t <seconds to transmit>.\n");
        printf(" Set -n 0 to send until the ramp finishes. \n\n");
        exit(7);
    }
    else if ((params1.number != -2) && (params1.duration != -2)) {
        printf("\n Only one option allowed at a time (-n or -t). \n Specify -n <number of packets> or -t <seconds to tramsmit>!\n\n");
        printf(" Set -n 0 to send until the ramp finishes. \n\n");
        exit(7);
    }
    if ((params1.number == -2) && (params1.duration > 0)) {
        params1.number = 0;
    }

    if (params1.packetsize != -2) {
        printf("\n Option -S not allowed in this mode\n\n");
        exit(7);    
    }
    if (params1.attack != -2) {
        printf("\n -a option not allowed in this mode!\n\n");
        exit(7);      
    }

    if (strlen(params1.sizeramp) > 0 ) {
        printf("\n Option -s not allowed in this mode. Packet size can not be changed.\n\n");
        exit(7);
    }
    if (( strlen(params1.rateramp) == 0 ) && (strlen(params1.rateRAMP) == 0 )) {
        printf("\n Did you specify rate with -z option (in kbit/s) or -Z (in Mbit/s)? \n And don't forget the quotation marks! (for example: -z \"100 1000 200\")\n\n");
        exit(7);
    }
    if (( strlen(params1.rateramp) > 0 ) && (strlen(params1.rateRAMP) > 0 )) {
        printf("\n Only one option allowed at a time: -z (kbit/s) or -Z (Mbit/s)!\"\n\n");
        exit(7);
    }
    if (params1.period == -2) {
        printf("\n Did you specify duration of one step (in seconds) with -p option?\n\n");
        exit(7);
    }

    if ((params1.my_pattern > 1) && (params1.my_pattern != 14)) {
        printf("\n Wrong pattern parameters. Choose one option:\n\n");
        printf("   Predifined pattern:  -x \n");
        printf("   Custom pattern:      -o <offset_counter> -q <offset_pattern> -w <pattern> \n\n");
        exit(7);
    }
    else if (strlen(params1.pattern) > 16) {
        printf("\n Pattern should not be longer than 16 chars!\n\n");
        exit(7);   
    }
    else if ((params1.offset_pattern < 0) || (params1.offset_pattern+strlen(params1.pattern) > params1.ph.incl_len)) {
        printf("\n Offset of the pattern is outside the packet size!\n\n");
        exit(7);   
    }
    else if ((params1.offset_counter < 0) || (params1.offset_counter > params1.ph.incl_len)) {
        printf("\n Offset of the counter is outside the packet size!\n\n");
        exit(7);   
    }
    else if ((params1.my_pattern > 1) && (params1.offset_counter >= params1.offset_pattern) && (params1.offset_counter <= params1.offset_pattern + strlen(params1.pattern))) {
        printf("\n Counter position and pattern position should not overlap!\n\n");
        exit(7);   
    }

    params1.size = params1.ph.incl_len;

    if (strlen(params1.rateramp) > 0 ) {
        memcpy(tmp8, params1.rateramp, 50);
        Mega = 0;
    }
    else if (strlen(params1.rateRAMP) > 0) {
        memcpy(tmp8, params1.rateRAMP, 50);
        Mega = 1;
    }
    else {
        printf("\n Shouldn't be here...\n\n");
        exit(7);
    }


    for (count = 0; count <= strlen(tmp8); count ++){
        ch = tmp8[count];
        if((isblank(ch)) || (tmp8[count] == '\0')){ 
            memcpy(tmp7, &tmp8[flag],count-flag); 
            tmp7[count-flag]='\0';
            if (wordcount==0) 
                params1.startrate = strtol(tmp7, &p, 10);
            else if (wordcount ==1)                     
                params1.stoprate = strtol(tmp7, &p, 10);
            else if (wordcount ==2)                     
                params1.steprate = strtol(tmp7, &p, 10);

                    wordcount += 1;
            flag = count;
        }
        
    }

    if (Mega == 1) {
        params1.startrate = params1.startrate * 1000;
        params1.stoprate = params1.stoprate * 1000;
        params1.steprate = params1.steprate * 1000;
    }

    //we allow also the decreasing ramp
    if (params1.startrate > params1.stoprate) {
        //printf("\nstartrate is greater than stoprate (or did you forget the quotation marks?)\n\n");
        //exit(7);
        params1.steprate = 0 - params1.steprate;
    }
    if ((params1.startrate < 1) || (params1.stoprate < 1)) {
        printf("\nstartrate and stoprate must be >= 1kbit/s\n\n");
        exit(7);
    }
    if ((params1.stoprate > 100000000) || (params1.stoprate > 100000000)) {
        printf("\nstartrate and stoprate must be <= 100Gbit/s\n\n");
        exit(7);
    }

    if (1000 * params1.size * 8 / params1.startrate > 999000) {
        printf ("startrate is to low (less than 1pps)\n\n");
        exit(7); 
    }
    if (1000 * params1.size * 8 / params1.stoprate > 999000) {
        printf ("stoprate is to low (less than 1pps)\n\n");
        exit(7); 
    }

    params1.delay = (long long)(1000000 * (long long)params1.size * 8 / params1.startrate);
    params1.rate = params1.startrate;
    
    
    // if we inser my_pattern, this will be inserted from last 10 to last 2 bytes. Last 2 bytes themselves are reserved for counter 
    if (params1.my_pattern == 1) {
        memcpy(params1.ptr+params1.ph.incl_len-10, MY_PATTERN, 8);
        memset(params1.ptr+params1.ph.incl_len-2, 0, 1);
        memset(params1.ptr+params1.ph.incl_len-1, 1, 1);
    }

    // in case we use custom pattern and offset
    if(params1.my_pattern > 1) {
        memcpy(params1.ptr+params1.offset_pattern, params1.pattern, strlen(params1.pattern));
    }
   
    function_send();
    return 1;
}

/*------------------------------------------------------------------------------*/
int send_variable_size() {

    int count, flag = 0;
    int wordcount = 0;
    char *p;
    char tmp7[20];
    char ch;

    if (params1.paramnum == 1) {  
        usage_4();
        exit(0);
    }

    //check if the options are ok
    if ((params1.delay_mode != 1) && (params1.delay_mode != 2) && (params1.delay_mode != 4) && (params1.delay_mode != 8)) {
        printf("\n Wrong or missing delay between packets or bandwidth parameter.\n\n Specify one of the following options:\n");
        printf("   -D <nanoseconds>    - delay between packets in nanoseconds\n");
        printf("   -d <microseconds>   - delay between packets in microseconds\n");
        printf("   -d 0                - maximum speed with counters\n");
        printf("   -b <bandwidth>      - desired bandwidth in kbit/s\n");
        printf("   -B <bandwidth>      - desired bandwidth in Mbit/s\n\n");
        exit(7);    
    }
    else if ((params1.delay_mode == 1) && (params1.delay == -1)) {
        printf("\n Option -d -1 not allowed with this mode\n\n");
        exit(7);
    }
    
    if (params1.delay_mode == 1)
        params1.delay = params1.delay * 1000;
    else if (params1.delay_mode == 2) 
        params1.delay = params1.delay;
    else if (params1.delay_mode == 4)
        params1.delay = (long long)(1000000 * (long long)params1.ph.incl_len * 8 / params1.bw);
    else if (params1.delay_mode == 8) 
        params1.delay = (long long)(1000 * (long long)params1.ph.incl_len * 8 / params1.BW);

    if (params1.delay > 999000000) {
            printf ("\n Warning! Rate is below 1pps, statistics will be displayed only when a packet will be sent.\n\n"); 
    }
    
    if ((params1.number == -2) && (params1.duration == -2)) {
        printf("\n Missing number of packets to send or time in seconds to transmit.\n Specify -n <number of packets> or -t <seconds to transmit>.\n");
        printf(" Set -n 0 to send until the ramp finishes. \n\n");
        exit(7);
    }
    else if ((params1.number != -2) && (params1.duration != -2)) {
        printf("\n Only one option allowed at a time (-n or -t). \n Specify -n <number of packets> or -t <seconds to tramsmit>!\n\n");
        printf(" Set -n 0 to send until the ramp finishes. \n\n");
        exit(7);
    } 
    
    if ((params1.number == -2) && (params1.duration > 0)) {
        params1.number = 0;
    }

    if (params1.packetsize != -2) {
        printf("\n Option -S not allowed in this mode\n\n");
        exit(7);    
    }
    if (params1.attack != -2) {
        printf("\n -a option not allowed in this mode!\n\n");
        exit(7);      
    }
    if (strlen(params1.rateramp) > 0 ) {
        printf("\n Options -z and -Z are not allowed in this mode.\n\n");
        exit(7);
    }

    if (strlen(params1.sizeramp) ==0 ) {
        printf("\n Did you specify size ramp values with -s option (in bytes)? \n And don't forget the quotation marks! (for example: -s \"100 1000 200\")\n\n");
        exit(7);
    }

    if (params1.period == -2) {
        printf("\n Did you specify duration of one step (in seconds) with -p option?\n\n");
        exit(7);
    }

    for (count = 0; count <= strlen(params1.sizeramp); count ++){
        ch = params1.sizeramp[count];
        if((isblank(ch)) || (params1.sizeramp[count] == '\0')){ 
            memcpy(tmp7, &params1.sizeramp[flag],count-flag); 
            tmp7[count-flag]='\0';
            if (wordcount==0) 
                params1.startsize = strtol(tmp7, &p, 10);
            else if (wordcount ==1)                     
                params1.stopsize = strtol(tmp7, &p, 10);
            else if (wordcount ==2)                     
                params1.stepsize = strtol(tmp7, &p, 10);

            wordcount += 1;
            flag = count;
        }
        
        }
    if (params1.startsize > params1.stopsize) {
        printf("\nstartsize is greater than stopzize (or did you forget the quotation marks?)\n\n");
        return 1;
    }
    if (params1.startsize < 60) {
        printf("\nstartsize must be >60\n\n");
        return 1;
    }
    if (params1.stopsize > MAX_MTU) {
        printf("\nstopsize must be <" MAX_MTU_STR "\n\n");
        return 1;
    }
    if (params1.ph.incl_len < params1.stopsize) {
        printf("\nPacket loaded from pcap file is shorter than stopsize!\n\n");
        return 1;   
    }

    if ((params1.my_pattern > 1) && (params1.my_pattern != 14)) {
        printf("\n Wrong pattern parameters. Choose one option:\n\n");
        printf("   Predifined pattern:  -x \n");
        printf("   Custom pattern:      -o <offset_counter> -q <offset_pattern> -w <pattern> \n\n");
        exit(7);
    }
    else if (strlen(params1.pattern) > 16) {
        printf("\n Pattern should not be longer than 16 chars!\n\n");
        exit(7);   
    }
    else if ((params1.offset_pattern < 0) || (params1.offset_pattern+strlen(params1.pattern) > params1.startsize)) {
        printf("\n Offset of the pattern is outside of the start packet size!\n\n");
        exit(7);   
    }
    else if ((params1.offset_counter < 0) || (params1.offset_counter > params1.startsize)) {
        printf("\n Offset of the counter is outside the start packet size!\n\n");
        exit(7);   
    }
    else if ((params1.my_pattern > 1) && (params1.offset_counter >= params1.offset_pattern) && (params1.offset_counter <= params1.offset_pattern + strlen(params1.pattern))) {
        printf("\n Counter position and pattern position should not overlap!\n\n");
        exit(7);   
    }

    params1.size = params1.startsize;

    if (params1.delay_mode == 4) {
        params1.delay = (long long)(1000000 * (long long)params1.size * 8 / params1.bw);
        params1.ConstantRate = 1;
        params1.rate = params1.bw;
    }
    else if (params1.delay_mode == 8) {
        params1.delay = (long long)(1000 * (long long)params1.size * 8 / params1.BW);
        params1.ConstantRate = 1;
        params1.rate = params1.BW*1000;
    }
    else
        params1.ConstantRate = 0;

    // if we inser my_pattern, this will be inserted from last 10 to last 2 bytes. Last 2 bytes themselves are reserved for counter 
    if (params1.my_pattern == 1) {
        memcpy(params1.ptr+params1.size-10, MY_PATTERN, 8);
        memset(params1.ptr+params1.size-2, 0, 1);
        memset(params1.ptr+params1.size-1, 1, 1);
    }

    // in case we use custom pattern and offset
    if(params1.my_pattern > 1) {
        memcpy(params1.ptr+params1.offset_pattern, params1.pattern, strlen(params1.pattern));
    }
    function_send();
    return 1;
}
 
/*------------------------------------------------------------------------------*/
int send_burst_constant_mode() {

    int count, flag = 0;
    int wordcount = 0;
    char *p;
    char tmp7[20];
    char ch;

    if (params1.paramnum == 1) {  
        usage_6();
        exit(0);
    }

    //check if the options are ok
    if (params1.delay_mode != 0) {
        printf("\n Option -d not allowed with this mode\n\n");
        exit(7);    
    }
    
    if ((params1.number == -2) && (params1.duration == -2)) {
        printf("\n Missing number of packets to send or time in seconds to transmit.\n Specify -n <number of packets> or -t <seconds to transmit>.\n");
        printf(" Set -n 0 to send infinite number of packets. \n\n");
        exit(7);
    }
    else if ((params1.number != -2) && (params1.duration != -2)) {
        printf("\n Only one option allowed at a time (-n or -t). \n Specify -n <number of packets> or -t <seconds to tramsmit>!\n\n");
        printf(" Set -n 0 to send infinite number of packets. \n\n");
        exit(7);
    } 
    
    if ((params1.number == -2) && (params1.duration > 0)) {
        params1.number = 0;
    }

    if (params1.packetsize != -2) {
        printf("\n Option -S not allowed in this mode\n\n");
        exit(7);    
    }
    if (params1.attack != -2) {
        printf("\n -a option not allowed in this mode!\n\n");
        exit(7);      
    }
    if ((strlen(params1.sizeramp) > 0 ) || (strlen(params1.rateramp) > 0 ) || (strlen(params1.rateRAMP) > 0 ) || (params1.period != -2)) {
        printf("\n Ramp options not allowed in this mode\n\n");
        exit(7);
    }

    if (strlen(params1.burstargs) ==0 ) {
        printf("\n Did you specify burst arguments with -L option? \n And don't forget the quotation marks! (for example: -L \"100 1000 200\")\n\n");
        exit(7);
    }

    //extract the number of packets in burst, delay between packets and delay between bursts
    //last 2 are multiplied by 1000 because we input the values in ms not ns
    for (count = 0; count <= strlen(params1.burstargs); count ++){
        ch = params1.burstargs[count];
        if((isblank(ch)) || (params1.burstargs[count] == '\0')){ 
            memcpy(tmp7, &params1.burstargs[flag],count-flag); 
            tmp7[count-flag]='\0';
            if (wordcount==0) 
                params1.burst_packets_in_burst = strtol(tmp7, &p, 10) ;
            else if (wordcount ==1)                     
                params1.burst_delay_between_packets = strtol(tmp7, &p, 10) * 1000;
            else if (wordcount ==2)                     
                params1.burst_delay_to_next_burst = strtol(tmp7, &p, 10) * 1000;

            wordcount += 1;
            flag = count;
        }
        
    }

    if (params1.burst_delay_between_packets > 999000000) {
            printf ("\n Warning! Rate is below 1pps, statistics will be displayed only when a packet will be sent.\n\n"); 
    }
    if (params1.burst_delay_to_next_burst > 999000000) {
            printf ("\n Warning! Rate is below 1pps, statistics will be displayed only when a packet will be sent.\n\n"); 
    }

    /*if (params1.startsize > params1.stopsize) {
        printf("\nstartsize is greater than stopzize (or did you forget the quotation marks?)\n\n");
        return 1;
    }
    if (params1.startsize < 60) {
        printf("\nstartsize must be >60\n\n");
        return 1;
    }
    if (params1.stopsize > MAX_MTU) {
        printf("\nstopsize must be <" MAX_MTU_STR "\n\n");
        return 1;
    }
    if (params1.ph.incl_len < params1.stopsize) {
        printf("\nPacket loaded from pcap file is shorter than stopsize!\n\n");
        return 1;   
    }*/

    if ((params1.my_pattern > 1) && (params1.my_pattern != 14)) {
        printf("\n Wrong pattern parameters. Choose one option:\n\n");
        printf("   Predifined pattern:  -x \n");
        printf("   Custom pattern:      -o <offset_counter> -q <offset_pattern> -w <pattern> \n\n");
        exit(7);
    }
    else if (strlen(params1.pattern) > 16) {
        printf("\n Pattern should not be longer than 16 chars!\n\n");
        exit(7);   
    }
    else if ((params1.offset_pattern < 0) || (params1.offset_pattern+strlen(params1.pattern) > params1.ph.incl_len)) {
        printf("\n Offset of the pattern is outside the packet size!\n\n");
        exit(7);   
    }
    else if ((params1.offset_counter < 0) || (params1.offset_counter > params1.ph.incl_len)) {
        printf("\n Offset of the counter is outside the packet size!\n\n");
        exit(7);   
    }
    else if ((params1.my_pattern > 1) && (params1.offset_counter >= params1.offset_pattern) && (params1.offset_counter <= params1.offset_pattern + strlen(params1.pattern))) {
        printf("\n Counter position and pattern position should not overlap!\n\n");
        exit(7);   
    }


    // if we insert my_pattern, this will be inserted from last 10 to last 2 bytes. Last 2 bytes themselves are reserved for counter 
    if (params1.my_pattern == 1) {
        memcpy(params1.ptr+params1.ph.incl_len-10, MY_PATTERN, 8);
        memset(params1.ptr+params1.ph.incl_len-2, 0, 1);
        memset(params1.ptr+params1.ph.incl_len-1, 1, 1);
    }

    // in case we use custom pattern and offset
    if(params1.my_pattern > 1) {
        memcpy(params1.ptr+params1.offset_pattern, params1.pattern, strlen(params1.pattern));
    }


    params1.size = params1.ph.incl_len;

    function_send_burst();

    return 1;
}
 
/*------------------------------------------------------------------------------*/
int function_send_burst() {

    int c;
    
    long  sentnumber = 0, lastnumber = 0;
    long long gap=0, gap2=0, gap2s=0, gap3s=0;
    struct timeval first;
    struct timespec first_ns, now_ns, last_ns, last2s_ns, last3s_ns;
    long burst_sent = 0;
   
    /* this is the time we started */
    gettimeofday(&first, NULL);

    clock_gettime(CLOCK_MONOTONIC, &first_ns);
    clock_gettime(CLOCK_MONOTONIC, &now_ns);
    clock_gettime(CLOCK_MONOTONIC, &last_ns);
    clock_gettime(CLOCK_MONOTONIC, &last2s_ns);
    clock_gettime(CLOCK_MONOTONIC, &last3s_ns);

    /* to send first packet immedialtelly */
    gap = 0;

    /*-----------------------------------------------------------------------------------------------*/
    for(; params1.number == 0 ? 1 : sentnumber < params1.number; ) {
    
            clock_gettime(CLOCK_MONOTONIC, &now_ns);
            gap = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (last_ns.tv_sec*1000000000 + last_ns.tv_nsec);
            gap2 = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (first_ns.tv_sec*1000000000 + first_ns.tv_nsec);
            gap2s = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (last2s_ns.tv_sec*1000000000 + last2s_ns.tv_nsec);
            gap3s = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (last3s_ns.tv_sec*1000000000 + last3s_ns.tv_nsec);
    
            if (burst_sent < params1.burst_packets_in_burst) {
                if (gap < params1.burst_delay_between_packets)
                    continue;
            }
            else {
                if (gap < params1.burst_delay_to_next_burst)
                    continue;
                else
                    burst_sent = 0;
                   
            }

            //send!
            c = sendto(params1.fd, params1.ptr, params1.size, 0, (struct sockaddr *)&params1.sa, sizeof (params1.sa));

            last_ns.tv_sec = now_ns.tv_sec;
            last_ns.tv_nsec = now_ns.tv_nsec;
            gap = 0;

            if (c > 0) {
                sentnumber++;
                burst_sent++;
                if (params1.my_pattern == 1) 
                    (*(params1.ptr+params1.ph.incl_len-1))++;
                else if (params1.my_pattern > 1)
                    (*(params1.ptr+params1.offset_counter-1))++;
            }
            /* every display interval we print some output */
            if (gap2s > ((long long)params1.display_interval*1000000000)) {
                print_intermidiate(sentnumber, lastnumber, params1.size, params1.display_interval);
                lastnumber = sentnumber;
                last2s_ns.tv_sec = now_ns.tv_sec;
                last2s_ns.tv_nsec = now_ns.tv_nsec;
            }
                //exit if time has elapsed we exit
                if ((params1.duration > 0) && (gap2 >= (long long)(params1.duration*1000000000)))
                    break; 
            //}

            // every second we check if we need to adjust size, rate or both
            if (gap3s > 1000000000) {
                //reset timer
                last3s_ns.tv_sec = now_ns.tv_sec;
                last3s_ns.tv_nsec = now_ns.tv_nsec;

                //if we need do the rate (bandwidth) ramp mode
                /*if (params1.steprate != 0) { 
                    if ( (period2 > (params1.period-2)) && (params1.period>0) ) {
                        params1.rate = params1.rate + params1.steprate;
                        if ((params1.steprate > 0) && (params1.rate > params1.stoprate)) {
                            break;
                        }
                        else if ((params1.steprate < 0) && (params1.rate < params1.stoprate)) {
                            break;
                        }
                        params1.delay = (long long)(params1.rate*1000) / (params1.size*8);
                        params1.delay = 1000000000 / params1.delay;
                        period2 = 0;
                        
                    }
                    else
                    period2++;
                }*/
                
            }
    }
    print_final(first, sentnumber, params1.iftext);
    return 1;
    
}


/*------------------------------------------------------------------------------*/
int function_send() {

    int c;
    int period2 = 0;
    


    long  sentnumber = 0, lastnumber = 0;
    long long gap=0, gap2=0, gap2s=0, gap3s=0;
    struct timeval first;
    struct timespec first_ns, now_ns, last_ns, last2s_ns, last3s_ns;
   
    /* this is the time we started */
    gettimeofday(&first, NULL);

    clock_gettime(CLOCK_MONOTONIC, &first_ns);
    clock_gettime(CLOCK_MONOTONIC, &now_ns);
    clock_gettime(CLOCK_MONOTONIC, &last_ns);
    clock_gettime(CLOCK_MONOTONIC, &last2s_ns);
    clock_gettime(CLOCK_MONOTONIC, &last3s_ns);

    /* to send first packet immedialtelly */
    gap = 0;


    /*-----------------------------------------------------------------------------------------------*/
    //if the -1 for delay was choosed, just send as fast as possible, no output, no counters, no pattern, nothing
    if ((params1.delay==-1000) && (params1.number==0)) {
        for(;;)
            c = sendto(params1.fd, params1.ptr, params1.size, 0, (struct sockaddr *)&params1.sa, sizeof (params1.sa));
    }
    /* with counters and delay between packets set */
    else {
        for(; params1.number == 0 ? 1 : sentnumber < params1.number; ) {
    
            clock_gettime(CLOCK_MONOTONIC, &now_ns);
            gap = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (last_ns.tv_sec*1000000000 + last_ns.tv_nsec);
            gap2 = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (first_ns.tv_sec*1000000000 + first_ns.tv_nsec);
            gap2s = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (last2s_ns.tv_sec*1000000000 + last2s_ns.tv_nsec);
            gap3s = (now_ns.tv_sec*1000000000 + now_ns.tv_nsec) - (last3s_ns.tv_sec*1000000000 + last3s_ns.tv_nsec);
    
            if (gap < params1.delay)
                continue;

            //send!
            c = sendto(params1.fd, params1.ptr, params1.size, 0, (struct sockaddr *)&params1.sa, sizeof (params1.sa));

            last_ns.tv_sec = now_ns.tv_sec;
            last_ns.tv_nsec = now_ns.tv_nsec;
            gap = 0;

            if (c > 0) {
                sentnumber++;
                if (params1.my_pattern == 1) 
                    (*(params1.ptr+params1.ph.incl_len-1))++;
                else if (params1.my_pattern > 1)
                    (*(params1.ptr+params1.offset_counter-1))++;
            }
            /* every display interval we print some output */
            if (gap2s > ((long long)params1.display_interval*1000000000)) {
                print_intermidiate(sentnumber, lastnumber, params1.size, params1.display_interval);
                lastnumber = sentnumber;
                last2s_ns.tv_sec = now_ns.tv_sec;
                last2s_ns.tv_nsec = now_ns.tv_nsec;
            }
                //exit if time has elapsed we exit
                if ((params1.duration > 0) && (gap2 >= (long long)(params1.duration*1000000000)))
                    break; 
            //}

            // every second we check if we need to adjust size, rate or both
            if (gap3s > 1000000000) {
                //reset timer
                last3s_ns.tv_sec = now_ns.tv_sec;
                last3s_ns.tv_nsec = now_ns.tv_nsec;

                //if we need do the rate (bandwidth) ramp mode
                if (params1.steprate != 0) { 
                    if ( (period2 > (params1.period-2)) && (params1.period>0) ) {
                        params1.rate = params1.rate + params1.steprate;
                        if ((params1.steprate > 0) && (params1.rate > params1.stoprate)) {
                            break;
                        }
                        else if ((params1.steprate < 0) && (params1.rate < params1.stoprate)) {
                            break;
                        }
                        params1.delay = (long long)(params1.rate*1000) / (params1.size*8);
                        params1.delay = 1000000000 / params1.delay;
                        period2 = 0;
                        
                    }
                    else
                    period2++;
                }
                //if we do the size ramp mode
                else if (params1.stepsize > 0) {
                    if ( (period2 > (params1.period-2)) && (params1.period>0) ) {
                        params1.size = params1.size + params1.stepsize;
                        if (params1.size > params1.stopsize) {
                            break;
                        }
                        period2 = 0;
                        if (params1.my_pattern == 1) {
                            memcpy(params1.ptr+params1.size-1, params1.ptr+params1.size-params1.stepsize-1, 1);
                            memcpy(params1.ptr+params1.size-10, MY_PATTERN, 8);
                        }
                    }
                    else
                        period2++;
                }
                //if we want to keep the rate the same, we need to change the delay
                if (params1.ConstantRate == 1) {
                    params1.delay = (long long)(1000 * params1.rate) / (params1.size*8);
                    params1.delay = 1000000000  / params1.delay;    
                }
            }
        }
        print_final(first, sentnumber, params1.iftext);
        return 1;
    }
    return 1;
}

/*------------------------------------------------------------------------------*/
int interface_setup()
{    
    int i=0;

    if (strlen(params1.iftext) == 0 ) {
        printf("\n You need to specify output interface (-i interface_name)\n\n");
        exit (7);
    }

    /* do we have the rights to do that? */
    if (getuid() && geteuid()) {
        //printf("Sorry but need the su rights!\n");
        printf("\nSorry but need the su rights!\n\n");
        exit (7);
    }

    /* open socket in raw mode */
    params1.fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (params1.fd == -1) {
        //printf("Error: Could not open socket!\n");
        printf("\nError: Could not open socket!\n\n");
        exit(7);
    }

    // form mode 9 (receiver) - put the socket in non-blocking mode:
    if (params1.mode == 9) {
        if(fcntl(params1.fd, F_SETFL, fcntl(params1.fd, F_GETFL) | O_NONBLOCK) < 0) {
            printf("socket non-blocking failed\n");
            exit (7);
        }
    }

    /* which interface would you like to use? */
    memset(&params1.ifr, 0, sizeof(params1.ifr));
    memcpy (params1.ifr.ifr_name, params1.iftext, sizeof(params1.ifr.ifr_name) - 1);
    params1.ifr.ifr_name[sizeof(params1.ifr.ifr_name)-1] = '\0';

    /* does the interface exists? */
    if (ioctl(params1.fd, SIOCGIFINDEX, &params1.ifr) == -1) {
        printf("\nNo such interface: %s\n\n", params1.iftext);
        close(params1.fd);
        exit(7);
    }

    /* is the interface up? */
    ioctl(params1.fd, SIOCGIFFLAGS, &params1.ifr);
    if ( (params1.ifr.ifr_flags & IFF_UP) == 0) {
        printf("\nInterface %s is down\n\n", params1.iftext);
        close(params1.fd);
        exit(7);
    }

    if (params1.mode == 9) {
        /* Set interface to promiscuous mode - do we need to do this every time? */
        memcpy(params1.ifopts.ifr_name, params1.iftext, sizeof(params1.ifopts.ifr_name)-1);
        ioctl(params1.fd, SIOCGIFFLAGS, &params1.ifopts);
        params1.ifopts.ifr_flags |= IFF_PROMISC;
        ioctl(params1.fd, SIOCSIFFLAGS, &params1.ifopts);
    }

    /* just write in the structure again */
    ioctl(params1.fd, SIOCGIFINDEX, &params1.ifr);

    /* well we need this to work, don't ask me what is it about */
    memset(&params1.sa, 0, sizeof (params1.sa));
    params1.sa.sll_family    = AF_PACKET;
    params1.sa.sll_ifindex   = params1.ifr.ifr_ifindex;
    params1.sa.sll_protocol  = htons(ETH_P_ALL);

    /* for mode 9 (receiver) - you need this to receive from the right interface */
    if (params1.mode == 9) {
        i = bind(params1.fd, (struct sockaddr*)&params1.sa, sizeof(params1.sa));
        if (i == -1)
        {
            perror("Interface bind error");
            exit (0);
        }
    }    

    return 1;   
}

/*------------------------------------------------------------------------------*/
int read_packet_from_file(char *filename) {

    FILE *file_p;
    int freads;
    //int last=0
    int i=0;
    //char *ptr2;

    if((file_p = fopen(filename, "r")) == NULL) {
        printf("\nCan not open file for reading. Did you specify pcap file with option -f ?\n\n");
        exit(7);
    }

    /* first we read the pcap file header */
    freads = fread(params1.pkt_temp, sizeof(params1.fh), 1, file_p);
    /* if EOF, exit */
    if (freads == 0) {
        printf("\nPcap file not correct?\n\n");
        exit(7);
    }

    memcpy(&params1.fh, params1.pkt_temp, 24);

    /* if magic number in NOK, exit */
    if (params1.fh.magic != PCAP_MAGIC) {
        printf("\nWrong pcap file format?\n\n");
        exit(7);
    }

    // we can select which packet we want to send
    if (params1.seqnum == -2)
        params1.seqnum = 1;
    for (i=0; i < params1.seqnum; i++) {
        /* next the  pcap packet header */
        freads = fread(params1.pkt_temp, sizeof(params1.ph), 1, file_p);
    
            /* if EOF, exit */
            if (freads == 0) {
                printf("\nWrong sequence number? Or wrong pcap file format?\n\n");
                exit(7);
            }
    
            /* copy the 16 bytes into ph structure */
            memcpy(&params1.ph, params1.pkt_temp, 16);    
            params1.ptr = params1.pkt_temp + sizeof(params1.ph);
    
            /* and the packet itself, but only up to the capture length */
            freads = fread(params1.ptr, params1.ph.incl_len, 1, file_p);
    
            /* if EOF, exit */
            if (freads == 0) {
                printf("\nWrong sequence number? Or wrong pcap file format?\n\n");
                exit(7);
        }
    }
    fclose(file_p);

    return 1;
}

/*------------------------------------------------------------------------------*/
void print_intermidiate(long packets_sent, long packets_last_sent, int packet_size, int print_interval) {
    
    long  mbps, packets_pps, link;
    float Mbps, Link;

    packets_pps = (packets_sent - packets_last_sent) / print_interval;
    mbps = packets_pps * packet_size / 125; // 8 bits per byte / 1024 for kbit
    Mbps = (float)mbps/1000;
    link = packets_pps * (packet_size + 24) / 125; /* +12 bytes for interframe gap time and 12 for preamble, sfd and checksum */
    Link = (float)link/1000;
    
    printf("  Sent %ld packets on %s; %d bytes packet length; %ld packets/s; %.3f Mbit/s data rate; %.3f Mbit/s link utilization\n", packets_sent, params1.iftext, packet_size, packets_pps, Mbps, Link);
    fflush(stdout);
}

/*------------------------------------------------------------------------------*/
void print_final(struct timeval first, long packets_sent, char *interface_name)
{
    struct timeval now;
    long duration;
    float duration_s;

    gettimeofday(&now, NULL);
    duration = (now.tv_sec*1000000 + now.tv_usec) - (first.tv_sec*1000000 + first.tv_usec);
    duration_s = (float)duration/1000000;
    printf("------------------------------------------------\n");
    printf("  Sent %ld packets on %s in %f second(s). \n", packets_sent, interface_name, duration_s);
    printf("------------------------------------------------\n");
    fflush(stdout);
    close(params1.fd);
    if (params1.mode == 5)
        cleanupRules(params1.num_rules);  
    exit(1);  
}

/*------------------------------------------------------------------------------*/
void onexit(int signum)
{
    (void)signum;
    //printf(" ... Exiting\n");
    STOP = 1;
    close(params1.fd);

}

/*------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------*/
uint16_t
TCPChecksum(uint16_t* buf1, int buf1len, uint16_t* buf2, int buf2len)
{
    uint32_t sum = 0;
    uint16_t tmp = 0;
    
    assert(buf2 == NULL || buf1len % 2 == 0);
    
    while (buf1len > 1) {
        sum += *buf1++;
        buf1len -= 2;
    }
    
    /*  Add left-over byte, if any */
    if (buf1len > 0) {
        tmp = 0;
        *(uint8_t *) &tmp = *(uint8_t *) buf1;
        sum += tmp;
    }
    
    if (buf2) {
        while (buf2len > 1) {
            sum += *buf2++;
            buf2len -= 2;
        }
        
        /*  Add left-over byte, if any */
        if (buf2len > 0) {
            tmp = 0;
            *(uint8_t *) &tmp = *(uint8_t *) buf2;
            sum += tmp;
        }
    }
    
    /* fold 32-bit sum to 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    
    return (uint16_t)~sum;
}
/*------------------------------------------------------------------------------*/
inline uint32_t
MyRandom(uint64_t *seed)
{
    *seed = *seed * 1103515245 + 12345;
    return (uint32_t)(*seed >> 32); 
}
/*------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------*/
/* IDS mode */

//int four(char *iftext, long delay, long pkt2send, char* filename, char *sizetmp, int period, int attack)
int send_ids_mode() 
{
    int count, flag=0;
    
    int wordcount = 0;
    char *p;
    char tmp7[10];
    char ch;
    
    if (params1.paramnum == 1) {  
        usage_5();
        exit(0);
    }

    //check if the options are ok
    if ((params1.attack < 1) || (params1.attack > 4)) {
        printf("\n Missing amount of attack traffic. Select: -a <1-4>");
        printf("\n 0 for innocent traffic, 1 for 25%% attack, 2 for 50%% attack, 3 for 75%% attack, 4 for 100%% attack\n\n");
        exit(7);
    }

    //check if the options are ok
    if ((params1.delay_mode != 1) && (params1.delay_mode != 2) && (params1.delay_mode != 4) && (params1.delay_mode != 8)) {
        printf("\n Wrong or missing delay between packets or bandwidth parameter.\n\n Specify one of the following options:\n");
        printf("   -D <nanoseconds>    - delay between packets in nanoseconds\n");
        printf("   -d <microseconds>   - delay between packets in microseconds\n");
        printf("   -d 0                - maximum speed with counters\n");
        printf("   -b <bandwidth>      - desired bandwidth in kbit/s\n");
        printf("   -B <bandwidth>      - desired bandwidth in Mbit/s\n\n");
        exit(7);    
    }
    else if ((params1.delay_mode == 1) && (params1.delay == -1)) {
        printf("\n Option -d -1 not allowed with this mode\n\n");
        exit(7);
    }
    
    if (params1.delay_mode == 1)
        params1.delay = params1.delay * 1000;
    else if (params1.delay_mode == 2) 
        params1.delay = params1.delay;
    else if (params1.delay_mode == 4)
        params1.delay = (long long)(1000000 * (long long)params1.ph.incl_len * 8 / params1.bw);
    else if (params1.delay_mode == 8) 
        params1.delay = (long long)(1000 * (long long)params1.ph.incl_len * 8 / params1.BW);

    if (params1.delay > 999000000) {
            printf ("\n Warning! Rate is below 1pps, statistics will be displayed only when a packet will be sent.\n\n"); 
    }
    
    if ((params1.number == -2) && (params1.duration == -2)) {
        printf("\n Missing number of packets to send or time in seconds to transmit.\n Specify -n <number of packets> or -t <seconds to transmit>.\n");
        printf(" Set -n 0 to send infinite number of packets. \n\n");
        exit(7);
    }
    else if ((params1.number != -2) && (params1.duration != -2)) {
        printf("\n Only one option allowed at a time (-n or -t). \n Specify -n <number of packets> or -t <seconds to tramsmit>!\n\n");
        printf(" Set -n 0 to send until the ramp finishes. \n\n");
        exit(7);
    }
    
    if ((params1.number == -2) && (params1.duration > 0)) {
        params1.number = 0;
    }

    if (strlen(params1.rateramp) > 0 ) {
        printf("\n Options -z and -Z are not allowed in this mode.\n\n");
        exit(7);
    }

    if (params1.seqnum != -2 ) {
        printf("\n Option -c not allowed in this mode.\n\n");
        exit(7);
    }

    if ((strlen(params1.sizeramp) ==0 ) && (params1.packetsize == -2)) {
        printf("\n Did you specify packet size with -S or size ramp values with -s option (in bytes)? \n And don't forget the quotation marks! (for example: -s \"100 1000 200\")\n\n");
        exit(7);
    }

    if ((strlen(params1.sizeramp) > 0) && (params1.period == -2)) {
        printf("\n Did you specify duration of one step (in seconds) with -p option?\n\n");
        exit(7);
    }

    if (params1.my_pattern > 0) {
        printf("\n Pattern options not allowed in this mode!\n\n");
        exit(7);
    }

    /* read snort rule file */
    params1.num_rules = readSnortRules(params1.filename);
    if (params1.num_rules == 0) {
        /* if there are no rules, then die! */
        fprintf(stderr, "Rules file is empty!\n");
        exit(EXIT_FAILURE);
    }
    
    if (strlen(params1.sizeramp) > 0 ) {
        for (count = 0; count <= strlen(params1.sizeramp); count ++){
            ch = params1.sizeramp[count];
            if((isblank(ch)) || (params1.sizeramp[count] == '\0')){ 
                memcpy(tmp7, &params1.sizeramp[flag],count-flag); 
                tmp7[count-flag]='\0';
                if (wordcount==0) 
                    params1.startsize = strtol(tmp7, &p, 10);
                else if (wordcount == 1)
                    params1.stopsize = strtol(tmp7, &p, 10);
                else if (wordcount == 2)
                    params1.stepsize = strtol(tmp7, &p, 10);
                
                wordcount += 1;
                flag = count;
            }
            
        }
        if (params1.startsize > params1.stopsize) {
            printf("\nstartsize is greater than stopzize\n\n");
            close(params1.fd);
            cleanupRules(params1.num_rules);
            return 1;
        }
        if (params1.startsize < 60) {
            printf("\nstartsize must be >60\n\n");
            close(params1.fd);
            cleanupRules(params1.num_rules);            
            return 1;
        }
        if (params1.stopsize > MAX_MTU) {
            printf("\nstopsize must be <%d\n\n", MAX_MTU);
            close(params1.fd);
            cleanupRules(params1.num_rules);            
            return 1;
        }
        params1.size = params1.startsize;
    }
    else
        params1.size = params1.packetsize;

    function_send();
              
    return 1;
}

void usage(void) {
    printf("\nUsage: ./packETHcli -m <mode > -i <interface> -f <file> [options]\n");
    printf(" \n");
    printf(" There are diffent modes, use ./packETHcli -m <mode> to get detailed help for particular mode\n");
    printf(" \n");
    printf("   -m 1   - SEND PACKET ONCE (default mode)\n");
    printf("   -m 2   - SEND PACKETS CONTINUOUSLY WITH CONSTANT RATE:\n");
    printf("   -m 3   - SEND PACKETS CONTINUOUSLY WITH VARIABLE RATE (SPEED RAMP)\n");
    printf("   -m 4   - SEND PACKETS CONTINUOUSLY WITH VARIABLE SIZE (SIZE RAMP)\n");
    printf("   -m 5   - SEND SEQUENCE OF PACKETS (IDS TEST MODE)\n");
    printf("   -m 6   - SEND PACKETS IN BURST MODE (CONSTANT BURST)\n");
    printf("   -m 9   - RECEIVER MODE (count packets sent by packETHcli or packETH\n");
    printf("\n");
    //printf(" -f <file> - file name where packet is stored in pcap format (or attack definitions file in Snort rule format in mode 5) \n");
    //printf(" -I <seconds> - time interval to display results (default 1s) \n");
    printf("\n");
    printf("FOR EXAMPLES SEE: ./packETHcli -e \n\n");
}

void usage_1(void) {
    printf(" -m 1   - SEND PACKET ONCE (default mode): send packet from the pcap file once \n");
    printf("          Usage: ./packETHcli -m 1 -i <interface> -f <pacp file> [-c]\n");  
    printf("          Optional parameter:\n");
    printf("               -c <number>  - sequence number of packet stored in pcap file (by default first packet will be sent)\n");
    printf("                              to see sequence numbers of packets inside pcap file: tcpdump -# -r filename\n");
    printf("          Example: ./packETHcli -i eth0 -f packet.pcap\n\n");
}

void usage_2(void) {
    printf(" -m 2   - SEND PACKETS CONTINUOUSLY WITH CONSTANT RATE: send (first) packet from pcap file at constant rate\n");
    printf("          Usage: ./packETHcli -m 2 -i <interface> -f <pcap file> [options]\n");  
    printf("          Required parameters:\n");
    printf("              Number of packets to send or duration in seconds (only one option possible)\n");
    printf("               -n <number, 0> - number of packets to send or 0 for infinite\n");
    printf("               -t <seconds> - seconds to transmit\n");
    printf("              Delay between packets or sendrate (only one option possible)\n");
    printf("               -D <ns>        - delay between packets in nano seconds;\n");
    printf("               -d <us>        - delay between packets in micro seconds;\n");
    printf("               -d 0           - maximum speed with counters\n");
    printf("               -d -1          - maximum speed without counters\n");
    printf("               -b <bandwidth> - desired sending rate in kbit/s\n");
    printf("               -B <bandwidth> - desired sending rate in Mbit/s\n");
    printf("          Optional parameters:\n");
    printf("               -c <number>  - sequence number of packet stored in pcap file (by default first packet will be sent)\n");
    printf("               -I <seconds> - time interval to display results (default 1s) \n");
    printf("              Insert predifined pattern into packet: \n");
    printf("               -x             - insert pattern \"a9b8c7d6\" and counter inside last 10 bytes of packet\n");
    printf("              Insert custom pattern at custom positon and counter at custom position\n");
    printf("               -q <offset>    - where should the pattern be (bytes offset)\n");
    printf("               -w <pattern>   - what should be the pattern to match\n");
    printf("               -o <offset>    - where should the inceremented counter be (bytes offset)\n");
    printf("               \n");
    printf("          Example: ./packETHcli -i eth0 -m 2 -B 100 -n 10000 -f p1.pcap \n\n");
 }

 void usage_3(void) {
    printf(" -m 3   - SEND PACKETS CONTINUOUSLY WITH VARIABLE RATE (SPEED RAMP)\n");
    printf("          Usage: ./packETHcli -m 3 -i <interface> -f <pcap file> [options]\n");  
    printf("          Required parameters:\n");
    printf("              Number of packets to send or duration in seconds (only one option possible)\n");
    printf("               -n <number, 0> - number of packets to send or 0 for infinite\n");
    printf("               -t <seconds> - seconds to transmit\n");
    printf("              Startrate, Stoprate, Steprate and Step duration (only one option possible):\n");
    printf("               -z \"<startrate stoprate steprate)\" in kbit/s \n");
    printf("               -Z \"<startrate stoprate steprate)\" in Mbit/s \n");
    printf("              Step duration:\n" );
    printf("               -p <seconds> - period between steps in seconds \n");
    printf("          Optional parameters:\n");
    printf("               -c <number>  - sequence number of packet stored in pcap file (by default first packet will be sent)\n");
    printf("               -I <seconds> - time interval to display results (default 1s) \n");
    printf("              Insert predifined pattern into packet: \n");
    printf("               -x             - insert pattern \"a9b8c7d6\" and counter inside last 10 bytes of packet\n");
    printf("              Insert custom pattern at custom positon and counter at custom position\n");
    printf("               -q <offset>    - where should the pattern be (bytes offset)\n");
    printf("               -w <pattern>   - what should be the pattern to match\n");
    printf("               -o <offset>    - where should the inceremented counter be (bytes offset)\n");
    printf("               \n");
    printf("          Example: ./packETHcli -i eth1 -m 3 -t 3600 -Z \"500 100 1\" -p 5 -f p1.pcap \n\n");
}

void usage_4(void) {
    printf(" -m 4   - SEND PACKETS CONTINUOUSLY WITH VARIABLE SIZE (SIZE RAMP)\n");
    printf("          Usage: ./packETHcli -m 4 -i <interface> -f <pcap file> [options]\n");  
    printf("          Required parameters:\n");
    printf("              Number of packets to send or duration in seconds (only one option possible)\n");
    printf("               -n <number, 0> - number of packets to send or 0 for infinite\n");
    printf("               -t <seconds> - seconds to transmit\n");
    printf("              Delay between packets or sendrate (only one option possible). Choose first option for constant pps and second one for constant bandwidth\n");
    printf("               -d <us, 0> - delay between packets in micro seconds; select 0 for maximum speed\n");
    printf("               -D <ns, 0, -1> - delay between packets in nano seconds; select 0 for maximum speed with counters; select -1 for max speed without counters)\n");
    printf("               -b <bandwidth> - desired sending rate in kbit/s\n");
    printf("               -B <bandwidth> - desired sending rate in Mbit/s\n");
    printf("              Startsize, Stopsize, Stepsize and Step duration number\n");
    printf("               -s \"<startsize stopsize stepsize>\" in bytes (please note that TCP&UDP checksums are not (yet :) ) recalculated!!!) \n");
    printf("               -p <seconds> - period between steps in seconds\n");
    printf("          Optional parameters:\n");
    printf("               -c <number>  - sequence number of packet stored in pcap file (by default first packet will be sent)\n");
    printf("               -I <seconds> - time interval to display results (default 1s) \n");
    printf("              Insert predifined pattern into packet: \n");
    printf("               -x             - insert pattern \"a9b8c7d6\" and counter inside last 10 bytes of packet\n");
    printf("          Example: ./packETHcli -i eth1 -m 4 -d 2000 -n 0 -s \"100 1500 100\" -p 5 -f p1.pcap\n\n");
}

void usage_5(void) {
    printf(" -m 5   - SEND SEQUENCE OF PACKETS (IDS TEST MODE)\n");
    printf("          Usage: ./packETHcli -m 5 -i <interface> -f <attack definitions file> [options]\n");
    printf("          Required parameters\n");
    printf("            -f <attack definitions file in Snort rule format> \n"); 
    printf("            -a <numbers from 0 to 4> - innocent traffic for 0, 25%% attack for 1, 50%% attack for 2, 75%% attack for 3, 100%% attack for 4> \n");
    printf("            -S <packet size in bytes OR -s \"<startsize stopsize stepsize>\" -p <step period>\n");
    printf("            -d <us, 0, -1> - delay between packets OR -b <bandwidth in kbit/s>  OR -B <bandwidth in Mbit/s\n");
    printf("            -n <number, 0> - number of packets to send (0 for infinite) OR -t <duration in seconds>\n");
    printf("           Example: ./packETHcli -i lo -f sample_snort_rules.txt -B 10 -m 5 -t 60 -S 1000 -a 2\n\n");
    printf("\n");
}

void usage_6(void) {
    printf(" -m 6   - SEND PACKETS IN BURST MODE (CONSTANT BURST)\n");
    printf("          Usage: ./packETHcli -m 4 -i <interface> -f <pcap file> [options]\n");  
    printf("          Required parameters:\n");
    printf("              Number of packets to send or duration in seconds (only one option possible)\n");
    printf("               -n <number, 0> - number of packets to send or 0 for infinite\n");
    printf("               -t <seconds> - seconds to transmit\n");
    printf("              Number of packets in burst, delay between packets in burst (us), delay till next burst (us)\n");
    printf("               -L \"<packets_in_burst  delay_between_packets_in_burst_us  delay_between_bursts_us>\" \n");
    printf("          Optional parameters:\n");
    printf("               -c <number>  - sequence number of packet stored in pcap file (by default first packet will be sent)\n");
    printf("               -I <seconds> - time interval to display results (default 1s) \n");
    printf("              Insert predifined pattern into packet: \n");
    printf("               -x             - insert pattern \"a9b8c7d6\" and counter inside last 10 bytes of packet\n");
    printf("          Example: ./packETHcli -i eth1 -m 6 -n 0 -L \"100 1 100\" -f p1.pcap\n\n");
}

void usage_9(void)
{
    printf(" -m 9   - RECEIVER MODE: COUNT PACKETS (FROM packETHcli)\n");
    printf("          Usage: ./packETHcli -m 9 -i <interface> [-x OR -o <offset counter> -q <offset pattern> -w <pattern>]\n");
    printf("          Optional parameter:\n");
    printf("          To count packets with predifined pattern sent by packETHcli use -x option on both sides (sender and receiver)  :\n");
    printf("            -x   - Last 10 bytes in received packets will be checked for pattern \"a8b7c7d6\" and counter\n");
    printf("          To count packets with custom pattern at custom positon and counter at custom position:\n");
    printf("            -q <offset>   - where should the pattern be (bytes offset)\n");
    printf("            -w <pattern>  - what should be the pattern to match\n");
    printf("            -o <offset>   - where should the inceremented counter be (bytes offset)\n");
    printf("          Examples:\n");
    printf("          ./packETHcli -m 9 -i eth0\n");
    printf("          ./packETHcli -m 9 -i eth0 -x\n");
    printf("          ./packETHcli -m 9 -i eth0 -o 60 -q 70 -w 12345678\n");
    printf("\n");
}

void examples(void) {
    printf("\n");
    printf("Examples:  \n");
    printf("\n");
    printf("All examples assume that we send on interface eth0 and that the packet is stored in file p1.pcap\n");
    printf("\n");
    printf("  mode 1 - send one packet and exit:\n");
    printf("   ./packETHcli -i eth0 -f p1.pcap                                               - send packet p1.pcap once on interface eth0\n");
    printf("   ./packETHcli -i eth0 -f p10.pcap -c 5                                         - send 5th packet from file p10.pcap\n");
    printf("\n");
    printf("  mode 2 - send packets at constant rate:\n");
    printf("   ./packETHcli -i eth0 -m 2 -d 0 -n 0 -f p1.pcap                                - send at max speed, infinite times, display counters every seconf\n");
    printf("   ./packETHcli -i eth0 -m 2 -d -1 -n 0 -f p1.pcap                               - send at max speed, infinite times, no counters\n");
    printf("   ./packETHcli -i eth0 -m 2 -d 1000 -n 300 -f p1.pcap                           - send 300 packets with 1000 us (1ms) between them\n");
    printf("   ./packETHcli -i eth0 -m 2 -b 1500 -t 30 -f p1.pcap -I 5                       - send packets with rate 1500 kbit/s for 30s, display results every 5s\n");        
    printf("   ./packETHcli -i eth0 -m 2 -B 100 -n 10000 -f p1.pcap -c 7                     - send 7th packet 10000 times, with rate 100 Mbit/s\n");        
    printf("   ./packETHcli -i eth0 -m 2 -B 100 -n 0 -f p1.pcap -x                           - send infinite times with rate 100 Mbit/s, add predifined pattern and counter\n");        
    printf("   ./packETHcli -i eth0 -m 2 -B 100 -n 0 -f p1.pcap -o 60 -q 70 -w 12345         - send infinite times with rate 100 Mbit/s, add counter at byte 60 and pattern 12345 at byte 70\n");        
    printf("\n"); 
    printf("  mode 3 - send packets with different rates (speed ramp):\n");
    printf("   ./packETHcli -i eth1 -m   -n 0 -z \"100 1500 100\" -p 10 -f p1.pcap             - start sendind at 100kbit/s for 10s, then increase rate by 100kbit/s each 10s up to 1500 kbit/s\n");
    printf("   ./packETHcli -i eth1 -m 3 -t 3600 -Z \"500 100 1\" -p 5 -f p1.pcap              - send with 500Mbit/s for 5s, then decrease rate by 1Mbit/s each 5s. Stop after 3600s if not finished\n");
    printf("\n");
    printf("  mode 4 - send packets with variable size (size ramp):\n");
    printf("   ./packETHcli -i eth1 -m 4 -d 0 -n 0 -s \"100 1500 100\" -p 10 -f p1.pcap        - send at max speed, start with packet size of 100 bytes for 10s then increase by 100 bytes up to 1500 bytes\n");
    printf("   ./packETHcli -i eth1 -m 4 -d 2000 -n 0 -s \"100 1500 100\" -p 5 -f p1.pcap      - send with constant rate 500pps (bandwidth changes), increase length by 100 bytes every 5s from 100 to 1500 \n");
    printf("   ./packETHcli -i eth1 -m 4 -B 10 -t 300 -s \"1000 1500 100\" -p 10 -f p1.pcap    - send with constant bandwidth 10Mbit/s (pps changes), increase the length by 100 bytes every 10s from 1000 to 1500\n");
    printf("\n");
    printf("  mode 5 - send packets for IDS testing:\n");
    printf("   ./packETHcli -i eth1 -m 5 -f sample_snort_rules.txt -B 10 -t 60 -S1000 -a 2    - send 50%% IDS traffic (-a 2) at 10Mbit/s for 60 seconds, packet size 1000 bytes\n");
    printf("   ./packETHcli -i eth1 -m 5 -f sample_snort_rules.txt -d 1000 -t 60 -s \"100 1000 100\" -a 4 -p 10\n");
    printf("                                                                                  - send 100%% IDS traffic, 1000pps for 60 seconds, increase packet size from 100 to 1000 bytes\n");
    printf("\n");
    printf("  mode 6 - send packets in burst mode:\n");
    printf("   ./packETHcli -i eth1 -m 6 -n 0 -L \"100 1000 200000\"  -f p1.pcap              - send a burst of 100 packets with 1ms between them then wait for 200ms and send next burst again\n");
    printf("   ./packETHcli -i eth1 -m 6 -n 0 -L \"100 0 100000\"  -f p1.pcap                 - send a burst of 100 packets as fast as possible then then wait for 100ms and send next burst again\n");
    printf("\n");
    printf("  mode 9 - receive and count packets sent by packETHcli:\n");
    printf("   ./packETHcli -i eth1 -m 9 -x                                                  - receive and count packets sent by packETHcli with -x option\n");
    printf("   ./packETHcli -i eth1 -m 9 -o 60 -q 70 -w 12345                                - receive and count packets that have counter at byte 60 and the pattern is 12345 at byte 70\n");
    printf("\n");
    printf("\n\n");
    exit (8);    
}