File: ec_send.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (1490 lines) | stat: -rw-r--r-- 54,789 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
/*
    ettercap -- send the the wire functions

    Copyright (C) ALoR & NaGA

    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 2 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 <ec.h>

#if defined(OS_DARWIN) || defined(OS_BSD)
   #include <sys/ioctl.h>
#endif

#include <ec_packet.h>
#include <ec_send.h>
#include <ec_network.h>

#include <pthread.h>
#include <pcap.h>

#include <libnet.h>

#define PCAP_TIMEOUT 10


/* globals */

u_int8 MEDIA_BROADCAST[MEDIA_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u_int8 ARP_BROADCAST[MEDIA_ADDR_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

static SLIST_HEAD (, build_entry) builders_table;

struct build_entry {
   u_int8 dlt;
   FUNC_BUILDER_PTR(builder);
   SLIST_ENTRY (build_entry) next;
};

/* protos */

libnet_ptag_t ec_build_link_layer(u_int8 dlt, u_int8 *dst, u_int16 proto, libnet_t* l);

static pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
#define SEND_LOCK     do{ pthread_mutex_lock(&send_mutex); } while(0)
#define SEND_UNLOCK   do{ pthread_mutex_unlock(&send_mutex); } while(0)


/*******************************************/

/*
 * send the packet at layer 3
 * the eth header will be handled by the kernel
 */
int send_to_L3(struct packet_object *po)
{
   libnet_ptag_t t;
   libnet_t *l;
   char tmp[MAX_ASCII_ADDR_LEN];
   int c;

   switch(ntohs(po->L3.src.addr_type)) {
      case AF_INET:  l = GBL_LNET->lnet_IP4;
                     break;
#ifdef WITH_IPV6
      case AF_INET6: l = GBL_LNET->lnet_IP6;
                     break;
#endif
      default:       l = NULL;
                     break;
   }

   /* Do not send the packet if corresponding
    * libnet handler is not initialized
    */
   if(l == NULL)
      return -E_NOTHANDLED;
   
   SEND_LOCK;

   
   t = libnet_build_data(po->fwd_packet, po->fwd_len, l, 0);
   ON_ERROR(t, -1, "libnet_build_data: %s", libnet_geterror(l));
   c = libnet_write(l);
   //ON_ERROR(c, -1, "libnet_write %d (%d): %s", po->fwd_len, c, libnet_geterror(l));
   if (c == -1)
      USER_MSG("SEND L3 ERROR: %d byte packet (%04x:%02x) destined to %s was not forwarded (%s)\n", 
            po->fwd_len, ntohs(po->L3.proto), po->L4.proto, ip_addr_ntoa(&po->L3.dst, tmp), 
            libnet_geterror(l));
   
   /* clear the pblock */
   libnet_clear_packet(l);
   
   SEND_UNLOCK;
   
   return c;
}


/*
 * send the packet at layer 2
 * this can be used to send ARP messages
 */

int send_to_L2(struct packet_object *po)
{
   return send_to_iface(po, GBL_IFACE); 
}

/*
 * send the packet to the bridge
 */

int send_to_bridge(struct packet_object *po)
{
   return send_to_iface(po, GBL_BRIDGE);
}

int send_to_iface(struct packet_object *po, struct iface_env *iface)
{
   libnet_ptag_t t;
   int c;
   libnet_t *l;

   if(iface->unoffensive)
      return -E_INVALID;

   /* if not lnet warn the developer ;) */
   BUG_IF(iface->lnet == NULL);
   l = iface->lnet;   
   SEND_LOCK;

   t = libnet_build_data(po->packet, po->len, l, 0);

   ON_ERROR(t, -1, "libnet_build_data: %s", libnet_geterror(l));
   
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write %d (%d): %s", po->len, c, libnet_geterror(l));
   
   /* clear the pblock */
   libnet_clear_packet(l);
   
   SEND_UNLOCK;
   
   return c;
}

/*
 * we MUST not sniff packets sent by us at link layer.
 * expecially useful in bridged sniffing.
 *
 * so we have to find a solution...
 */
void capture_only_incoming(pcap_t *p, libnet_t *l)
{
   (void)p;
   (void)l;
#ifdef OS_LINUX   
   /*
    * a dirty hack to use the same socket for pcap and libnet.
    * both the structures contains a "int fd" field representing the socket.
    * we can close the fd opened by libnet and use the one already in use by pcap.
   */
   
   DEBUG_MSG("hack_pcap_lnet (before) pcap %d | lnet %d", pcap_fileno(p), l->fd);

   /* needed to avoid double execution (for portstealing) */
   if (pcap_fileno(p) == l->fd)
      return;
      
   /* close the lnet socket */
   close(libnet_getfd(l));
   /* use the socket opened by pcap */
   l->fd = pcap_fileno(p);
   
   DEBUG_MSG("hack_pcap_lnet  (after) pcap %d | lnet %d", pcap_fileno(p), l->fd);
#endif

#ifdef OS_BSD
   /*
    * under BSD we cannot hack the fd as in linux... 
    * pcap opens the /dev/bpf in O_RDONLY and lnet needs O_RDWR
    * 
    * so (if supported: only FreeBSD) we can set the BIOCSSEESENT to 0 to 
    * see only incoming packets
    * but this is unconfortable, because we will not able to sniff ourself.
    */
   #ifdef OS_BSD_FREE
      int val = 0;
   
      DEBUG_MSG("hack_pcap_lnet: setting BIOCSSEESENT on pcap handler");
     
      /* set it to 0 to capture only incoming traffic */
      ioctl(pcap_fileno(p), BIOCSSEESENT, &val);
   #else
      DEBUG_MSG("hack_pcap_lnet: not applicable on this OS");
   #endif
#endif
      
#ifdef OS_DARWIN
   int val = 0;
   
   DEBUG_MSG("hack_pcap_lnet: setting BIOCSSEESENT on pcap handler");
   
   /* not all darwin versions support BIOCSSEESENT */
   #ifdef BIOCSSEESENT
   ioctl(pcap_fileno(p), BIOCSSEESENT, &val);
   #endif
#endif

#ifdef OS_SOLARIS
   DEBUG_MSG("hack_pcap_lnet: not applicable on this OS");
#endif
   
#ifdef OS_CYGWIN
   DEBUG_MSG("hack_pcap_lnet: not applicable on this OS");
#endif
   
}

/*
 * register a builder in the table
 * a builder is a function to create a link layer header.
 */
void add_builder(u_int8 dlt, FUNC_BUILDER_PTR(builder))
{
   struct build_entry *e;

   SAFE_CALLOC(e, 1, sizeof(struct build_entry));
   
   e->dlt = dlt;
   e->builder = builder;

   SLIST_INSERT_HEAD(&builders_table, e, next); 
   
   return;
   
}

/*
 * build the header calling the registered
 * function for the current media
 */
libnet_ptag_t ec_build_link_layer(u_int8 dlt, u_int8 *dst, u_int16 proto, libnet_t* l)
{
   struct build_entry *e;

   SLIST_FOREACH (e, &builders_table, next) {
      if (e->dlt == dlt) {
         return e->builder(dst, proto, l);
      }
   }

   /* on error return -1 */
   return -1;
}


/*
 * helper function to send out an ARP packet
 */
int send_arp(u_char type, struct ip_addr *sip, u_int8 *smac, struct ip_addr *tip, u_int8 *tmac)
{
   libnet_ptag_t t;
   libnet_t *l;
   int c;

   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == NULL);
   l = GBL_IFACE->lnet;

   SEND_LOCK;

   /* ARP uses 00:00:00:00:00:00 broadcast */
   if (type == ARPOP_REQUEST && tmac == MEDIA_BROADCAST)
      tmac = ARP_BROADCAST;
   
   /* create the ARP header */
   t = libnet_build_arp(
           ARPHRD_ETHER,            /* hardware addr */
           ETHERTYPE_IP,            /* protocol addr */
           MEDIA_ADDR_LEN,          /* hardware addr size */
           IP_ADDR_LEN,             /* protocol addr size */
           type,                    /* operation type */
           smac,                    /* sender hardware addr */
           (u_char *)&(sip->addr),  /* sender protocol addr */
           tmac,                    /* target hardware addr */
           (u_char *)&(tip->addr),  /* target protocol addr */
           NULL,                    /* payload */
           0,                       /* payload size */
           l,                       /* libnet handle */
           0);                      /* pblock id */
   ON_ERROR(t, -1, "libnet_build_arp: %s", libnet_geterror(l));
   
   /* MEDIA uses ff:ff:ff:ff:ff:ff broadcast */
   if (type == ARPOP_REQUEST && tmac == ARP_BROADCAST)
      tmac = MEDIA_BROADCAST;
   
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, tmac, ETHERTYPE_ARP, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");
   
   /* send the packet */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));
   
   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}

/*
 * helper function to send out an ICMP PORT UNREACH packet at layer 3
 */
int send_L3_icmp_unreach(struct packet_object *po)
{
   libnet_ptag_t t;
   int c;
   libnet_t *l;

   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_LNET->lnet_IP4 == 0);
   l = GBL_LNET->lnet_IP4;
   SEND_LOCK;


   /* create the ICMP header */
   t = libnet_build_icmpv4_echo(
           3,                       /* type */
           3,                       /* code */
           0,                       /* checksum */
           htons(EC_MAGIC_16),      /* identification number */
           htons(EC_MAGIC_16),      /* sequence number */
           po->L3.header,           /* payload */
           po->L3.len + 8,          /* payload size */
           l,                       /* libnet handle */
           0);                      /* pblock id */
   ON_ERROR(t, -1, "libnet_build_icmpv4_echo: %s", libnet_geterror(l));
  
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   t = libnet_build_ipv4(
           LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H,       /* length */
           0,                                          /* TOS */
           htons(EC_MAGIC_16),                         /* IP ID */
           0,                                          /* IP Frag */
           64,                                         /* TTL */
           IPPROTO_ICMP,                               /* protocol */
           0,                                          /* checksum */
           *po->L3.dst.addr32,                         /* source IP */
           *po->L3.src.addr32,                         /* destination IP */
           NULL,                                       /* payload */
           0,                                          /* payload size */
           l,                                          /* libnet handle */
           0);
   ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
  
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
 
   /* send the packet to Layer 3 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}


/*
 * helper function to send out an ICMP ECHO packet at layer 3
 */
int send_L3_icmp(u_char type, struct ip_addr *sip, struct ip_addr *tip)
{
   libnet_ptag_t t;
   int c;
   libnet_t *l;

   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_LNET->lnet_IP4 == 0);
   l = GBL_LNET->lnet_IP4;

   SEND_LOCK;


   /* create the ICMP header */
   t = libnet_build_icmpv4_echo(
           type,                    /* type */
           0,                       /* code */
           0,                       /* checksum */
           htons(EC_MAGIC_16),      /* identification number */
           htons(EC_MAGIC_16),      /* sequence number */
           NULL,                    /* payload */
           0,                       /* payload size */
           l,                       /* libnet handle */
           0);                      /* pblock id */
   ON_ERROR(t, -1, "libnet_build_icmpv4_echo: %s", libnet_geterror(l));
  
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   t = libnet_build_ipv4(
           LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H,       /* length */
           0,                                          /* TOS */
           htons(EC_MAGIC_16),                         /* IP ID */
           0,                                          /* IP Frag */
           64,                                         /* TTL */
           IPPROTO_ICMP,                               /* protocol */
           0,                                          /* checksum */
           *sip->addr32,                               /* source IP */
           *tip->addr32,                               /* destination IP */
           NULL,                                       /* payload */
           0,                                          /* payload size */
           l,                                          /* libnet handle */
           0);
   ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
 
   /* send the packet to Layer 3 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}

int send_L3_icmp_echo(struct ip_addr *src, struct ip_addr *tgt)
{
   return send_L3_icmp(ICMP_ECHO, src, tgt);
}

/*
 * helper function to send out an ICMP ECHO packet at layer 2
 */
int send_L2_icmp_echo(u_char type, struct ip_addr *sip, struct ip_addr *tip, u_int8 *tmac)
{
   libnet_ptag_t t;
   int c;
   libnet_t *l;
   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == 0);
   l = GBL_IFACE->lnet;
   
   SEND_LOCK;

   /* create the ICMP header */
   t = libnet_build_icmpv4_echo(
           type,                    /* type */
           0,                       /* code */
           0,                       /* checksum */
           htons(EC_MAGIC_16),      /* identification number */
           htons(EC_MAGIC_16),      /* sequence number */
           NULL,                    /* payload */
           0,                       /* payload size */
           l,                       /* libnet handle */
           0);                      /* pblock id */
   ON_ERROR(t, -1, "libnet_build_icmpv4_echo: %s", libnet_geterror(l));
  
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   t = libnet_build_ipv4(
           LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H,       /* length */
           0,                                          /* TOS */
           htons(EC_MAGIC_16),                         /* IP ID */
           0,                                          /* IP Frag */
           64,                                         /* TTL */
           IPPROTO_ICMP,                               /* protocol */
           0,                                          /* checksum */
           *sip->addr32,                               /* source IP */
           *tip->addr32,                               /* destination IP */
           NULL,                                       /* payload */
           0,                                          /* payload size */
           l,                                          /* libnet handle */
           0);
   ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
  
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
   
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, tmac, ETHERTYPE_IP, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");

   /* send the packet to Layer 2 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));
 
   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}


/*
 * helper function to send out an ICMP REDIRECT packet
 * the header are created on the source packet PO.
 */
int send_icmp_redir(u_char type, struct ip_addr *sip, struct ip_addr *gw, struct packet_object *po)
{
   libnet_ptag_t t;
   libnet_t *l;
   struct libnet_ipv4_hdr *ip;
   int c;
 
   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == 0);
   l = GBL_IFACE->lnet;
  
   /* retrieve the old ip header */
   ip = (struct libnet_ipv4_hdr *)po->L3.header;
   
   SEND_LOCK;

   /* create the fake ip header for the icmp payload */
   t = libnet_build_ipv4(
            LIBNET_IPV4_H + 8,
            //ntohs(ip->ip_len),                   /* original len */
            ip->ip_tos,                          /* original tos */
            ntohs(ip->ip_id),                    /* original id */
            ntohs(ip->ip_off),                   /* original fragments */
            ip->ip_ttl,                          /* original ttl */
            ip->ip_p,                            /* original proto */
            ip->ip_sum,                          /* original checksum */
            ip->ip_src.s_addr,                   /* original source */
            ip->ip_dst.s_addr,                   /* original dest */
            po->L4.header,                       /* the 64 bit of the original datagram */
            8,                                   /* payload size */
            l,
            0);
   ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
   /* create the ICMP header */
   t = libnet_build_icmpv4_redirect(
           ICMP_REDIRECT,                       /* type */
           type,                                /* code */
           0,                                   /* checksum */
           *gw->addr32,                         /* gateway ip */
           NULL,                                /* payload */
           0,                                   /* payload len */
           l,                                   /* libnet handle */
           0);                                  /* pblock id */
   ON_ERROR(t, -1, "libnet_build_icmpv4_redirect: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   t = libnet_build_ipv4(                                                                          
           LIBNET_IPV4_H + LIBNET_ICMPV4_REDIRECT_H + 
           LIBNET_IPV4_H + 8,                            /* length */
           0,                                            /* TOS */
           htons(EC_MAGIC_16),                           /* IP ID */
           0,                                            /* IP Frag */
           64,                                           /* TTL */
           IPPROTO_ICMP,                                 /* protocol */
           0,                                            /* checksum */
           *sip->addr32,                                 /* source IP */
           *po->L3.src.addr32,                           /* destination IP */
           NULL,                                         /* payload */
           0,                                            /* payload size */
           l,                                            /* libnet handle */ 
           0);
   ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
 
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, po->L2.src, ETHERTYPE_IP, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");
  
   /* 
    * send the packet to Layer 2
    * (sending icmp redirect is not permitted at layer 3)
    */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}

#ifdef WITH_IPV6
int send_icmp6_echo(struct ip_addr *sip, struct ip_addr *tip)
{
   libnet_ptag_t t;
   struct libnet_in6_addr src, dst;
   int c;
   libnet_t *l;

   BUG_IF(GBL_LNET->lnet_IP6 == NULL);
   l = GBL_LNET->lnet_IP6;

   SEND_LOCK;


   memcpy(&src, sip->addr, sizeof(src));
   memcpy(&dst, tip->addr, sizeof(dst));

   t = libnet_build_icmpv6_echo(ICMP6_ECHO_REQUEST,   /* type */
                                0,                    /* code */
                                0,                    /* checksum */
                                EC_MAGIC_16,          /* id */
                                0,                    /* sequence number */
                                NULL,                 /* data */
                                0,                    /* its size */
                                l,                    /* handle */
                                0);
   ON_ERROR(t, -1, "libnet_build_icmpv6_echo: %s", libnet_geterror(l));
   libnet_toggle_checksum(l, t, LIBNET_ON);

   t = libnet_build_ipv6(0,                           /* tc */
                         0,                           /* flow label */
                         LIBNET_ICMPV6_H,             /* next header size */
                         IPPROTO_ICMPV6,              /* next header */
                         255,                         /* hop limit */
                         src,                         /* source */
                         dst,                         /* destination */
                         NULL,                        /* payload and size */
                         0,
                         l,                           /* handle */
                         0);                          /* ptag */
   ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write: %s", libnet_geterror(l));

   libnet_clear_packet(l);

   SEND_UNLOCK;

   return c;
}

/*
 * send IP packet with an unknown header option
 * RFC2460 conforming hosts, respond with a ICMPv6 parameter problem 
 * message even those not intended to respond to ICMP echos
 */
int send_icmp6_echo_opt(struct ip_addr *sip, struct ip_addr *tip, u_int8* o_data, u_int32 o_len)
{
    libnet_ptag_t t;
    struct libnet_in6_addr src, dst;
    int c, h = 0;
    libnet_t *l;

    BUG_IF(GBL_LNET->lnet_IP6 == NULL);

    l = GBL_LNET->lnet_IP6;

    SEND_LOCK;

    memcpy(&src, sip->addr, sizeof(src));
    memcpy(&dst, tip->addr, sizeof(dst));

    t = libnet_build_icmpv6_echo(ICMP6_ECHO_REQUEST,   /* type */
                                 0,                    /* code */
                                 0,                    /* checksum */
                                 EC_MAGIC_16,          /* id */
                                 0,                    /* sequence number */
                                 NULL,                 /* data */
                                 0,                    /* its size */
                                 l,                    /* handle */
                                 0);
    ON_ERROR(t, -1, "libnet_build_icmpv6_echo: %s", libnet_geterror(l));
    libnet_toggle_checksum(l, t, LIBNET_ON);

    t = libnet_build_ipv6_destopts(IPPROTO_ICMPV6,             /* next header */
                                   LIBNET_IPV6_DESTOPTS_H / 8, /* lenth */
                                   o_data,                     /* payload */
                                   o_len,                      /* payload length */
                                   l,                          /* handle */
                                   0);
    ON_ERROR(t, -1, "libnet_build_ipv6_destopts: %s", libnet_geterror(l));

    h = LIBNET_IPV6_DESTOPTS_H + o_len + LIBNET_ICMPV6_H;
    t = libnet_build_ipv6(0,                /* tc */
                          0,                /* flow label */
                          h,                /* next header size */
                          IPPROTO_DSTOPTS,  /* next header */
                          255,              /* hop limit */
                          src,              /* source */
                          dst,              /* destination */
                          NULL,             /* payload and size */
                          0,
                          l,                /* handle */
                          0);               /* ptag */
    ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));

    c = libnet_write(l);
    ON_ERROR(c, -1, "libnet_write: %s", libnet_geterror(l));

    libnet_clear_packet(l);


    SEND_UNLOCK;

    return c;
}

/* 
 * Sends neighbor solicitation request (like arp request with ipv4)
 * macaddr parameter allows to add sender's mac address. This is an option for unicast requests.
 * See RFC4861 for more information.
 */
int send_icmp6_nsol(struct ip_addr *sip, struct ip_addr *tip, struct ip_addr *req, u_int8 *macaddr)
{  
   libnet_ptag_t t;
   int c, h = 0;
   struct libnet_in6_addr src, dst, r;
   libnet_t *l;

   BUG_IF(GBL_LNET->lnet_IP6 == NULL);
   l = GBL_LNET->lnet_IP6;

   SEND_LOCK;


   memcpy(&src, sip->addr, sizeof(src));
   memcpy(&dst, tip->addr, sizeof(dst));
   memcpy(&r, req->addr, sizeof(r));

   if(macaddr != NULL) {
      t = libnet_build_icmpv6_ndp_opt(ND_OPT_SOURCE_LINKADDR,   /* Address type */
                                      macaddr,                  /* MAC address */
                                      MEDIA_ADDR_LEN,           /* Address length */
                                      l,                        /* libnet handle */
                                      0);                       /* ptag */
      ON_ERROR(t, -1, "libnet_build_icmpv6_ndp_opt: %s", libnet_geterror(l));
      /* base header size + MAC address size */
      h += LIBNET_ICMPV6_NDP_OPT_H + 6;
   }

   t = libnet_build_icmpv6_ndp_nsol(ND_NEIGHBOR_SOLICIT,        /* type */
                                    0,                          /* code */
                                    0,                          /* checksum */
                                    r,                          /* target address */
                                    NULL,                       /* payload */
                                    0,                          /* its size */
                                    l,                          /* libnet handler */
                                    0);                         /* ptag */
   ON_ERROR(t, -1, "libnet_build_icmpv6_ndp_nsol: %s", libnet_geterror(l));
   libnet_toggle_checksum(l, t, LIBNET_ON);
   h += LIBNET_ICMPV6_NDP_NSOL_H;
   
   t = libnet_build_ipv6(0,                                     /* tc */
                         0,                                     /* flow label */
                         h,                                     /* length */
                         IPPROTO_ICMP6,                         /* proto */
                         255,                                   /* hop limit */
                         src,                                   /* source address */
                         dst,                                   /* target address */
                         NULL,                                  /* payload */
                         0,                                     /* its size */
                         l,                                     /* handle */
                         0);                                    /* ptag */
   ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write: %s", libnet_geterror(l));

   libnet_clear_packet(l);

   SEND_UNLOCK;

   return c;
}

int send_icmp6_nadv(struct ip_addr *sip, struct ip_addr *tip, u_int8 *macaddr, int router)
{
   libnet_ptag_t t;
   int c, h = 0;
   struct libnet_in6_addr src, dst;
   int flags;
   libnet_t *l;
   
   BUG_IF(GBL_LNET->lnet_IP6 == NULL);
   l = GBL_LNET->lnet_IP6;

   SEND_LOCK;


   memcpy(&src, sip->addr, sizeof(src));
   memcpy(&dst, tip->addr, sizeof(dst));

   t = libnet_build_icmpv6_ndp_opt(ND_OPT_TARGET_LINKADDR,   /* Address type */
                                   macaddr,                  /* MAC address */
                                   MEDIA_ADDR_LEN,           /* Address length */
                                   l,                        /* libnet handle */
                                   0);                       /* ptag */
   ON_ERROR(t, -1, "libnet_build_icmpv6_ndp_lla: %s", libnet_geterror(l));
   h += LIBNET_ICMPV6_NDP_OPT_H + 6;

   flags = ND_NA_FLAG_SOLICITED|ND_NA_FLAG_OVERRIDE;
   if(router)
      flags |= ND_NA_FLAG_ROUTER;
   t = libnet_build_icmpv6_ndp_nadv(ND_NEIGHBOR_ADVERT,  /* type */
                                    0,                   /* code */
                                    0,                   /* checksum */
                                    flags,               /* flags */
                                    src,                 /* address */
                                    NULL,                /* payload */
                                    0,                   /* payload size */
                                    l,                   /* libnet handle */
                                    0);                  /* ptag */
   ON_ERROR(t, -1, "libnet_build_icmpv6_ndp_nadv: %s", libnet_geterror(l));
   libnet_toggle_checksum(l, t, LIBNET_ON);
   h += LIBNET_ICMPV6_NDP_NADV_H;
   
   t = libnet_build_ipv6(0,                  /* tc */
                         0,                  /* flow label */
                         h,                  /* length */
                         IPPROTO_ICMP6,      /* proto */
                         255,                /* hop limit */
                         src,                /* source address */
                         dst,                /* target address */
                         NULL,               /* payload */
                         0,                  /* its size */
                         l,                  /* handle */
                         0);                 /* ptag */
   ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));
   
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write: %s", libnet_geterror(l));

   libnet_clear_packet(l);

   SEND_UNLOCK;

   return c;
}

#endif /* WITH_IPV6 */

/*
 * send a dhcp reply to tmac/tip
 */
int send_dhcp_reply(struct ip_addr *sip, struct ip_addr *tip, u_int8 *tmac, u_int8 *dhcp_hdr, u_int8 *options, size_t optlen)
{
   libnet_ptag_t t;
   int c;
   libnet_t *l;
   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == 0);
   l = GBL_IFACE->lnet;
  
   SEND_LOCK;
   
   /* add the dhcp options */
   t = libnet_build_data(
            options,                /* the options */
            optlen,                 /* options len */
            l,                      /* libnet handle */
            0);                     /* libnet ptag */
   ON_ERROR(t, -1, "libnet_build_data: %s", libnet_geterror(l));
   /* create the dhcp header */
   t = libnet_build_data(
            dhcp_hdr,               /* the header */
            LIBNET_DHCPV4_H,        /* dhcp len */
            l,                      /* libnet handle */
            0);                     /* libnet ptag */
   ON_ERROR(t, -1, "libnet_build_data: %s", libnet_geterror(l));
   /* create the udp header */
   t = libnet_build_udp(
            67,                                             /* source port */
            68,                                             /* destination port */
            LIBNET_UDP_H + LIBNET_DHCPV4_H + optlen,        /* packet size */
            0,                                              /* checksum */
            NULL,                                           /* payload */
            0,                                              /* payload size */
            l,                                              /* libnet handle */
            0);                                             /* libnet id */
   ON_ERROR(t, -1, "libnet_build_udp: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   t = libnet_build_ipv4(                                                                          
           LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_DHCPV4_H + optlen,  /* length */
           0,                                                        /* TOS */
           htons(EC_MAGIC_16),                                       /* IP ID */
           0,                                                        /* IP Frag */
           64,                                                       /* TTL */
           IPPROTO_UDP,                                              /* protocol */
           0,                                                        /* checksum */
           *sip->addr32,                                             /* source IP */
           *tip->addr32,                                             /* destination IP */
           NULL,                                                     /* payload */
           0,                                                        /* payload size */
           l,                                                        /* libnet handle */ 
           0);
   ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
 
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, tmac, ETHERTYPE_IP, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");
   
   /* 
    * send the packet to Layer 2
    * (sending icmp redirect is not permitted at layer 3)
    */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}

/*
 * send a mdns reply
 */
int send_mdns_reply(u_int16 dport, struct ip_addr *sip, struct ip_addr *tip, u_int8 *tmac, u_int16 id, u_int8 *data, size_t datalen, u_int16 anws_rr, u_int16 auth_rr, u_int16 addi_rr)
{
   libnet_ptag_t t;
   int c, proto, ethertype;
   libnet_t *l;
   proto = ntohs(sip->addr_type);

   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == 0);
   l = GBL_IFACE->lnet;
  
   SEND_LOCK;

   /* create the dns packet */
    t = libnet_build_dnsv4(
             LIBNET_UDP_DNSV4_H,    /* TCP or UDP */
             id,                    /* id */
             0x8400,                /* standard reply, no error */
             0,                     /* num_q */
             anws_rr,               /* num_anws_rr */
             auth_rr,               /* num_auth_rr */
             addi_rr,               /* num_addi_rr */
             data,
             datalen,
             l,                     /* libnet handle */
             0);                    /* libnet id */
   ON_ERROR(t, -1, "libnet_build_dns: %s", libnet_geterror(l));
  
   /* create the udp header */
   t = libnet_build_udp(
            5353,                                           /* source port */
            htons(dport),                                   /* destination port */
            LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + datalen,    /* packet size */
            0,                                              /* checksum */
            NULL,                                           /* payload */
            0,                                              /* payload size */
            l,                                              /* libnet handle */
            0);                                             /* libnet id */
   ON_ERROR(t, -1, "libnet_build_udp: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   switch (proto) {
      case AF_INET: {
         t = libnet_build_ipv4(                                                                          
                 LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + datalen, /* length */
                 0,                                                           /* TOS */
                 htons(EC_MAGIC_16),                                          /* IP ID */
                 0,                                                           /* IP Frag */
                 255,                                                         /* TTL */
                 IPPROTO_UDP,                                                 /* protocol */
                 0,                                                           /* checksum */
                 *sip->addr32,                                                /* source IP */
                 *tip->addr32,                                                /* destination IP */
                 NULL,                                                        /* payload */
                 0,                                                           /* payload size */
                 l,                                                           /* libnet handle */ 
                 0);
         ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
         /* auto calculate the checksum */
         libnet_toggle_checksum(l, t, LIBNET_ON);
   
         /* set Ethertype to IPv4 */
         ethertype = ETHERTYPE_IP;

         break;
      }
#ifdef WITH_IPV6
      case AF_INET6: {
         struct libnet_in6_addr src, dst;
         memcpy(&src, sip->addr, sizeof(src));
         memcpy(&dst, tip->addr, sizeof(dst));
         t = libnet_build_ipv6(
               0,                                             /* traffic class */
               0,                                             /* flow label */
               LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + datalen,   /* payload length */
               IPPROTO_UDP,                                   /* next header */
               255,                                           /* hop limit */
               src,                                           /* source IP */
               dst,                                           /* destination IP */
               NULL,                                          /* payload */
               0,                                             /* payload size */
               l,                                             /* libnet handle */
               0);
         ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));
   
         /* set Ethertype to IPv6 */
         ethertype = ETHERTYPE_IPV6;

         break;
      }
#endif
   };
  
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, tmac, ethertype, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");
   
   /* send the packet to Layer 2 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);
   
   SEND_UNLOCK;
   
   return c;
}

/*
 * send a dns reply
 */
int send_dns_reply(u_int16 dport, struct ip_addr *sip, struct ip_addr *tip, u_int8 *tmac, u_int16 id, u_int8 *data, size_t datalen, u_int16 anws_rr, u_int16 auth_rr, u_int16 addi_rr)
{
   libnet_ptag_t t;
   int c, proto, ethertype;
   libnet_t *l;

   proto = ntohs(sip->addr_type);
 
   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == 0);
   l = GBL_IFACE->lnet;
  
   SEND_LOCK;

   /* create the dns packet */
    t = libnet_build_dnsv4(
             LIBNET_UDP_DNSV4_H,    /* TCP or UDP */
             id,                    /* id */
             0x8400,                /* standard reply, no error */
             1,                     /* num_q */
             anws_rr,               /* num_anws_rr */
             auth_rr,               /* num_auth_rr */
             addi_rr,               /* num_addi_rr */
             data,
             datalen,
             l,                     /* libnet handle */
             0);                    /* libnet id */
   ON_ERROR(t, -1, "libnet_build_dns: %s", libnet_geterror(l));
   /* create the udp header */
   t = libnet_build_udp(
            53,                                             /* source port */
            htons(dport),                                   /* destination port */
            LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + datalen,    /* packet size */
            0,                                              /* checksum */
            NULL,                                           /* payload */
            0,                                              /* payload size */
            l,                                              /* libnet handle */
            0);                                             /* libnet id */
   ON_ERROR(t, -1, "libnet_build_udp: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   switch (proto) {
      case AF_INET: {
         t = libnet_build_ipv4(
                 LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + datalen, /* length */
                 0,                                                           /* TOS */
                 htons(EC_MAGIC_16),                                          /* IP ID */
                 0,                                                           /* IP Frag */
                 64,                                                          /* TTL */
                 IPPROTO_UDP,                                                 /* protocol */
                 0,                                                           /* checksum */
                 *sip->addr32,                                                /* source IP */
                 *tip->addr32,                                                /* destination IP */
                 NULL,                                                        /* payload */
                 0,                                                           /* payload size */
                 l,                                                           /* libnet handle */ 
                 0);
         ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
         /* auto calculate the checksum */
         libnet_toggle_checksum(l, t, LIBNET_ON);

         /* set Ethertype to IPv4 */
         ethertype = ETHERTYPE_IP;
   
         break;
      }
#ifdef WITH_IPV6
      case AF_INET6: {
         struct libnet_in6_addr src, dst;
         memcpy(&src, sip->addr, sizeof(src));
         memcpy(&dst, tip->addr, sizeof(dst));
         t = libnet_build_ipv6(
               0,                                             /* traffic class */
               0,                                             /* flow label */
               LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + datalen,   /* payload length */
               IPPROTO_UDP,                                   /* next header */
               255,                                           /* hop limit */
               src,                                           /* source IP */
               dst,                                           /* destination IP */
               NULL,                                          /* payload */
               0,                                             /* payload size */
               l,                                             /* libnet handle */
               0);
         ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));

         /* set Ethertype to IPv6 */
         ethertype = ETHERTYPE_IPV6;
   
         break;
      }
#endif
   };
  
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, tmac, ethertype, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");
   
   /* send the packet to Layer 2 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);
   
   SEND_UNLOCK;
   
   return c;
}

/*
 * send an udp packet
 */
int send_udp(struct ip_addr *sip, struct ip_addr *tip, u_int8 *tmac, u_int16 sport, u_int16 dport, u_int8 *payload, size_t length)
{
   libnet_ptag_t t;
   libnet_t *l;

   int c, proto, ethertype;

   proto = ntohs(sip->addr_type);

   l = GBL_IFACE->lnet;
   BUG_IF(GBL_IFACE->lnet == 0);

   SEND_LOCK;


/*
 * libnet_build_udp(uint16_t sp, uint16_t dp, uint16_t len, uint16_t sum,
const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
*/
   t = libnet_build_udp(
      htons(sport),
      htons(dport),
      LIBNET_UDP_H +  length,
      0,
      payload,
      length,
      l,
      0);
   ON_ERROR(t, -1, "libnet_build_udp: %s", libnet_geterror(l));

   /* auto calculate checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);

   /* create IP header */
   switch(proto) {
      case AF_INET: {
         t = libnet_build_ipv4(
            LIBNET_IPV4_H + LIBNET_UDP_H + length, /* length */
            0,                                     /* TOS */
            htons(EC_MAGIC_16),                    /* IP ID */
            0,                                     /* IP FRAG */
            64,                                    /* TTL */
            IPPROTO_UDP,                           /* protocol */
            0,                                     /* checksum */
            *sip->addr32,                          /* source IP */
            *tip->addr32,                          /* destination IP */
            NULL,            
            0,                                     /* payload size */
            l,
            0);

         libnet_toggle_checksum(l, t, LIBNET_ON);

         /* set Ethertype to IPv4 */
         ethertype = ETHERTYPE_IP;

         break;
      }   

#ifdef WITH_IPV6
      case AF_INET6: {
         struct libnet_in6_addr src, dst;
         memcpy(&src, sip->addr, sizeof(src));
         memcpy(&dst, tip->addr, sizeof(dst));
         t = libnet_build_ipv6(
            0,                                     /* tc */
            0,                                     /* flow label */
            LIBNET_UDP_H + length,                 /* payload length */
            IPPROTO_UDP,                           /* protocol */
            255,                                   /* hop limit */
            src,                                   /* source */
            dst,                                   /* destination */
            NULL,                                  /* payload */
            0,                                     /* its length */
            l,                                     /* handle */
            0);                                    /* ptag */

         /* set Ethertype to IPv6 */
         ethertype = ETHERTYPE_IPV6;

         break;
      }
#endif
   };

   ON_ERROR(t, -1, "libnet_build_ipvX: %s", libnet_geterror(l));

      /* add the media header */
      t = ec_build_link_layer(GBL_PCAP->dlt, tmac, ethertype, l);
      if (t == -1)
            FATAL_ERROR("Interface not suitable for layer2 sending");

   /* send the packet to Layer 3 */

   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the block */
   libnet_clear_packet(l);
   SEND_UNLOCK;

   return c;
}
/*
 * send a tcp packet
 */
int send_tcp(struct ip_addr *sip, struct ip_addr *tip, u_int16 sport, u_int16 dport, u_int32 seq, u_int32 ack, u_int8 flags, u_int8 *payload, size_t length)
{
   libnet_ptag_t t;
   libnet_t *l;
   int proto;
   int c;
 
   proto = ntohs(sip->addr_type);
   l = (proto == AF_INET) ? GBL_LNET->lnet_IP4 : GBL_LNET->lnet_IP6;   
   /* if not lnet warn the developer ;) */
   BUG_IF(l == NULL);
  
   SEND_LOCK;

   
    t = libnet_build_tcp(
        ntohs(sport),            /* source port */
        ntohs(dport),            /* destination port */
        ntohl(seq),              /* sequence number */
        ntohl(ack),              /* acknowledgement num */
        flags,                   /* control flags */
        32767,                   /* window size */
        0,                       /* checksum */
        0,                       /* urgent pointer */
        LIBNET_TCP_H + length,   /* TCP packet size */
        payload,                 /* payload */
        length,                  /* payload size */
        l,                       /* libnet handle */
        0);                      /* libnet id */
   ON_ERROR(t, -1, "libnet_build_tcp: %s", libnet_geterror(l));
   
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   /* create the IP header */
   switch(proto) {
      case AF_INET: {
         t = libnet_build_ipv4(                                                                          
                 LIBNET_IPV4_H + LIBNET_TCP_H,       /* length */
                 0,                                  /* TOS */
                 htons(EC_MAGIC_16),                 /* IP ID */
                 0,                                  /* IP Frag */
                 64,                                 /* TTL */
                 IPPROTO_TCP,                        /* protocol */
                 0,                                  /* checksum */
                 *sip->addr32,                       /* source IP */
                 *tip->addr32,                       /* destination IP */
                 NULL,                               /* payload */
                 0,                                  /* payload size */
                 l,                                  /* libnet handle */ 
                 0);
         libnet_toggle_checksum(l, t, LIBNET_ON);
         break;
      }
#ifdef WITH_IPV6
      case AF_INET6: {
         struct libnet_in6_addr src, dst;
         memcpy(&src, sip->addr, sizeof(src));
         memcpy(&dst, tip->addr, sizeof(dst));
         t = libnet_build_ipv6(
                  0,                                 /* tc */
                  0,                                 /* flow label */
                  LIBNET_TCP_H,                      /* payload length */
                  IPPROTO_TCP,                       /* protocol */
                  255,                               /* hop limit */
                  src,                               /* source address */
                  dst,                               /* destination address */
                  NULL,                              /* payload */
                  0,                                 /* its length */
                  l,                                 /* handle */
                  0);                                /* ptag */
         break;
      }
#endif
   };
   ON_ERROR(t, -1, "libnet_build_ipvX: %s", libnet_geterror(l));
  
   /* send the packet to Layer 3 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}

/*
 * similar to send_tcp but the user can specify the destination mac addresses
 */
int send_tcp_ether(u_int8 *dmac, struct ip_addr *sip, struct ip_addr *tip, u_int16 sport, u_int16 dport, u_int32 seq, u_int32 ack, u_int8 flags)
{
   libnet_ptag_t t;
   int c, proto, ethertype;
   libnet_t *l;

   proto = ntohs(sip->addr_type);

   /* if not lnet warn the developer ;) */
   BUG_IF(GBL_IFACE->lnet == 0);
   l = GBL_IFACE->lnet;
  
   SEND_LOCK;
   
    t = libnet_build_tcp(
        ntohs(sport),            /* source port */
        ntohs(dport),            /* destination port */
        ntohl(seq),              /* sequence number */
        ntohl(ack),              /* acknowledgement num */
        flags,                   /* control flags */
        32767,                   /* window size */
        0,                       /* checksum */
        0,                       /* urgent pointer */
        LIBNET_TCP_H,            /* TCP packet size */
        NULL,                    /* payload */
        0,                       /* payload size */
        l,                       /* libnet handle */
        0);                      /* libnet id */
   ON_ERROR(t, -1, "libnet_build_tcp: %s", libnet_geterror(l));
   /* auto calculate the checksum */
   libnet_toggle_checksum(l, t, LIBNET_ON);
  
   switch (proto) {
      case AF_INET: {
         /* create the IP header */
         t = libnet_build_ipv4(                                                                          
                 LIBNET_IPV4_H + LIBNET_TCP_H,       /* length */
                 0,                                  /* TOS */
                 htons(EC_MAGIC_16),                 /* IP ID */
                 0,                                  /* IP Frag */
                 64,                                 /* TTL */
                 IPPROTO_TCP,                        /* protocol */
                 0,                                  /* checksum */
                 *sip->addr32,                       /* source IP */
                 *tip->addr32,                       /* destination IP */
                 NULL,                               /* payload */
                 0,                                  /* payload size */
                 l,                                  /* libnet handle */ 
                 0);
         ON_ERROR(t, -1, "libnet_build_ipv4: %s", libnet_geterror(l));
  
         /* auto calculate the checksum */
         libnet_toggle_checksum(l, t, LIBNET_ON);

         /* set Ethertype to IPv4 */
         ethertype = ETHERTYPE_IP;

         break;
      }
#ifdef WITH_IPV6
      case AF_INET6: {
         struct libnet_in6_addr src, dst;
         memcpy(&src, sip->addr, sizeof(src));
         memcpy(&dst, tip->addr, sizeof(dst));
         t = libnet_build_ipv6(
               0,                                     /* traffic class */
               0,                                     /* flow label */
               LIBNET_TCP_H,                          /* payload length */
               IPPROTO_TCP,                           /* next header */
               255,                                   /* hop limit */
               src,                                   /* source IP */
               dst,                                   /* destination IP */
               NULL,                                  /* payload */
               0,                                     /* payload size */
               l,                                     /* libnet handle */
               0);
         ON_ERROR(t, -1, "libnet_build_ipv6: %s", libnet_geterror(l));

         /* set Ethertype to IPv6 */
         ethertype = ETHERTYPE_IPV6;
   
         break;
      }
#endif
   
   }
   
   /* add the media header */
   t = ec_build_link_layer(GBL_PCAP->dlt, dmac, ethertype, l);
   if (t == -1)
      FATAL_ERROR("Interface not suitable for layer2 sending");
 
   /* send the packet to Layer 3 */
   c = libnet_write(l);
   ON_ERROR(c, -1, "libnet_write (%d): %s", c, libnet_geterror(l));

   /* clear the pblock */
   libnet_clear_packet(l);

   SEND_UNLOCK;
   
   return c;
}


/* EOF */

// vim:ts=3:expandtab