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
|
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Mike Schiffman">
<meta name="GENERATOR" content="Mozilla/4.6 [en] (Win98; I) [Netscape]">
<title>Introduction</title>
</head>
<body text="#000000" bgcolor="#CCCCCC" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="7.html">Next</a> <a href="5.html">Previous</a> <a href="lrm.html">Top</a>
<br>
<hr ALIGN=LEFT WIDTH="97%">
<br><b><font size=+2>6. Sample applications</font></b>
<br>
<hr ALIGN=LEFT WIDTH="97%">
<br><font size=-1>The following are some simple examples that highlight
different areas of functionality offered by libnet. Each one of these
programs is autonomous and can be compiled into standalone executable.
We'll go through the first example step by step, to fully understand what's
going on, then, as we progress to subsequent examples, we will cover only
new or salient code. Comments are clearly visible in <font color="#FF0000">red</font><font color="#000000">.</font></font>
<p><font size=-1>To compile any of these examples (you can download the
C files from the corresponding links) simply:</font>
<p><tt><font size=-1>shattered:~> gcc -Wall `libnet-config --defines` libnet-example-x.c
-o libnet-example-x `libnet-config --libs`</font></tt>
<p>
<hr ALIGN=LEFT WIDTH="97%">
<br><a NAME="s6.1"></a><b><font size=+2>6.1 Example 1 [raw socket api -
TCP packet]</font></b>
<br><font size=-1>Download <a href="examples/libnet-example-1.c">libnet-example-1.c</a></font>
<p><tt><font size=-2>#include <libnet.h></font></tt>
<p><tt><font size=-2>void usage(char *);</font></tt>
<p><tt><font size=-2>int</font></tt>
<br><tt><font size=-2>main(int argc, char **argv)</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> int network,
/* our network interface */</font></tt>
<br><tt><font size=-2> packet_size,
/* packet size */</font></tt>
<br><tt><font size=-2> c;
/* misc */</font></tt>
<br><tt><font size=-2> u_long src_ip, dst_ip;
/* ip addresses */</font></tt>
<br><tt><font size=-2> u_short src_prt, dst_prt;
/* ports */</font></tt>
<br><tt><font size=-2> u_char *cp, *packet;
/* misc / packet */</font></tt>
<p><tt><font size=-2> printf("libnet example code:\tmodule
1\n\n");</font></tt>
<br><tt><font size=-2> printf("packet injection interface:\traw
socket\n");</font></tt>
<br><tt><font size=-2> printf("packet type:\t\t\tTCP
[no payload]\n");</font></tt>
<p><tt><font size=-2> src_ip = 0;</font></tt>
<br><tt><font size=-2> dst_ip = 0;</font></tt>
<br><tt><font size=-2> src_prt = 0;</font></tt>
<br><tt><font size=-2> dst_prt = 0;</font></tt>
<p><font color="#FF0000">Standard command line argument parsing.
We require a source IP and a destination IP, both in the form "ip.ip.ip.ip.port".</font>
<p><tt><font size=-2> while((c = getopt(argc, argv, "d:s:"))
!= EOF)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> switch
(c)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
/*</font></tt>
<br><tt><font size=-2>
* We expect the input to be of the form `ip.ip.ip.ip.port`.
We</font></tt>
<br><tt><font size=-2>
* point cp to the last dot of the IP address/port string and</font></tt>
<br><tt><font size=-2>
* then seperate them with a NULL byte. The optarg now points
to</font></tt>
<br><tt><font size=-2>
* just the IP address, and cp points to the port.</font></tt>
<br><tt><font size=-2>
*/</font></tt>
<br><tt><font size=-2>
case 'd':</font></tt>
<br><tt><font size=-2>
if (!(cp = strrchr(optarg, '.')))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
usage(argv[0]);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
*cp++ = 0;</font></tt>
<br><tt><font size=-2>
dst_prt = (u_short)atoi(cp);</font></tt>
<br><tt><font size=-2>
if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 's':</font></tt>
<br><tt><font size=-2>
if (!(cp = strrchr(optarg, '.')))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
usage(argv[0]);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
*cp++ = 0;</font></tt>
<br><tt><font size=-2>
src_prt = (u_short)atoi(cp);</font></tt>
<br><tt><font size=-2>
if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> if (!src_ip || !src_prt || !dst_ip
|| !dst_prt)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> usage(argv[0]);</font></tt>
<br><tt><font size=-2> exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">Our packet is a TCP/IP packet with no payload,
so we only need LIBNET_IP_H + LIBNET_TCP_H (40 bytes) of memory.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * We're just going
to build a TCP packet with no payload using the</font></tt>
<br><tt><font size=-2> * raw sockets API,
so we only need memory for a TCP header and an IP</font></tt>
<br><tt><font size=-2> * header.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> packet_size = LIBNET_IP_H + LIBNET_TCP_H;</font></tt>
<p><font color="#FF0000">We allocate the heap memory for our packet.
If it failed, this would be a fatal error that we couldn't recover from
so we would be done. When libnet_error is called with the LIBNET_ERR_FATAL
severity, the program exits. This is <i>only </i>defined exit point
inside of libnet.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 1: Memory initialization
(interchangable with step 2).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_init_packet(packet_size,
&packet);</font></tt>
<br><tt><font size=-2> if (packet == NULL)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_init_packet failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">Next we open up our network interface. Again
failure is fatal.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 2: Network
initialization (interchangable with step 1).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> network = libnet_open_raw_sock(IPPROTO_RAW);</font></tt>
<br><tt><font size=-2> if (network == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"Can't open network.\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">Now the fun begins. Like so many tinker
toys, or so much capsula, we build our packets. Note that no consideration
is given to data-types or byte-ordering (well almost no consideration for
data-types). This is all taken care of inside of libnet. All
you have to worry about is what values to stuff in there. Notice
that the order we call the modular packet constructors is unimportant.
The major thing to note is that we pass in the packet at the correct memory
location.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(IP header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_ip(LIBNET_TCP_H,
/* size of the packet sans IP header */</font></tt>
<br><tt><font size=-2>
IPTOS_LOWDELAY, /* IP tos
*/</font></tt>
<br><tt><font size=-2>
242,
/* IP ID */</font></tt>
<br><tt><font size=-2>
0,
/* frag stuff */</font></tt>
<br><tt><font size=-2>
48,
/* TTL */</font></tt>
<br><tt><font size=-2>
IPPROTO_TCP,
/* transport protocol */</font></tt>
<br><tt><font size=-2>
src_ip,
/* source IP */</font></tt>
<br><tt><font size=-2>
dst_ip,
/* destination IP */</font></tt>
<br><tt><font size=-2>
NULL,
/* payload (none) */</font></tt>
<br><tt><font size=-2>
0,
/* payload length */</font></tt>
<br><tt><font size=-2>
packet);
/* packet header memory */</font></tt>
<p><font color="#FF0000">Notice the TCP builder gets the packet memory
buffer at LIBNET_IP_H (20) bytes.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(TCP header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_tcp(src_prt,
/* source TCP port */</font></tt>
<br><tt><font size=-2>
dst_prt,
/* destination TCP port */</font></tt>
<br><tt><font size=-2>
0xa1d95,
/* sequence number */</font></tt>
<br><tt><font size=-2>
0x53,
/* acknowledgement number */</font></tt>
<br><tt><font size=-2>
TH_SYN,
/* control flags */</font></tt>
<br><tt><font size=-2>
1024,
/* window size */</font></tt>
<br><tt><font size=-2>
0,
/* urgent pointer */</font></tt>
<br><tt><font size=-2>
NULL,
/* payload (none) */</font></tt>
<br><tt><font size=-2>
0,
/* payload length */</font></tt>
<br><tt><font size=-2>
packet + LIBNET_IP_H); /* packet header memory */</font></tt>
<p><font color="#FF0000">Perform our checksum on the TCP header only.
Since we are using the raw sockets api, the kernel will handle our IP header
checksum for us. How thoughtful!</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 4: Packet checksums
(TCP header only).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_do_checksum(packet,
IPPROTO_TCP, LIBNET_TCP_H) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_do_checksum failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">In you go! The packet is sent on it's merry
little way!</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 5: Packet injection.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> c = libnet_write_ip(network, packet,
packet_size);</font></tt>
<br><tt><font size=-2> if (c < packet_size)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LN_ERR_WARNING,
"libnet_write_ip only wrote %d bytes\n", c);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> else</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> printf("construction
and injection completed, wrote all %d bytes\n", c);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">Since we're good programmers, we always shut things
down when we are done.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Shut down the interface.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_close_raw_sock(network)
== -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LN_ERR_WARNING,
"libnet_close_raw_sock couldn't close the interface");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Free packet memory.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_destroy_packet(&packet);</font></tt>
<p><tt><font size=-2> return (c == -1 ? EXIT_FAILURE
: EXIT_SUCCESS);</font></tt>
<br><tt><font size=-2>}</font></tt>
<br>
<p><tt><font size=-2>void</font></tt>
<br><tt><font size=-2>usage(char *name)</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> fprintf(stderr, "usage: %s -s
s_ip.s_port -d d_ip.d_port\n", name);</font></tt>
<br><tt><font size=-2>}</font></tt>
<br>
<p><tt><font size=-2>/* EOF */</font></tt>
<br>
<p>
<hr ALIGN=LEFT WIDTH="97%">
<p>
<hr ALIGN=LEFT WIDTH="97%">
<br><a NAME="s6.2"></a><b><font size=+2>6.2 Example 2 [link layer api -
ICMP_MASK]</font></b>
<br><font size=-1>Download <a href="examples/libnet-example-2.c">libnet-example-1.c</a></font>
<br>
<p><tt><font size=-2>#include <libnet.h></font></tt>
<p><tt><font size=-2>void usage(char *);</font></tt>
<p><tt><font size=-2>u_char enet_src[6] = {0x0d, 0x0e, 0x0a, 0x0d, 0x00,
0x00};</font></tt>
<br><tt><font size=-2>u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff,
0xff};</font></tt>
<p><tt><font size=-2>int</font></tt>
<br><tt><font size=-2>main(int argc, char *argv[])</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> int packet_size,
/* size of our packet */</font></tt>
<br><tt><font size=-2> c;
/* misc */</font></tt>
<br><tt><font size=-2> u_long src_ip, dst_ip;
/* source ip, dest ip */</font></tt>
<br><tt><font size=-2> u_char *packet;
/* pointer to our packet buffer */</font></tt>
<br><tt><font size=-2> char err_buf[LIBNET_ERRBUF_SIZE];
/* error buffer */</font></tt>
<br><tt><font size=-2> u_char *device;
/* pointer to the device to use */</font></tt>
<br><tt><font size=-2> struct libnet_link_int *network;
/* pointer to link interface struct */</font></tt>
<p><tt><font size=-2> printf("libnet example code:\tmodule
2\n\n");</font></tt>
<br><tt><font size=-2> printf("packet injection interface:\tlink
layer\n");</font></tt>
<br><tt><font size=-2> printf("packet type:\t\t\tICMP
net mask [no payload]\n");</font></tt>
<p><tt><font size=-2> device = NULL;</font></tt>
<br><tt><font size=-2> src_ip = 0;</font></tt>
<br><tt><font size=-2> dst_ip = 0;</font></tt>
<p><font color="#FF0000">This time, we have an additional `device` to specify
the link layer device to use.</font>
<p><tt><font size=-2> while ((c = getopt(argc, argv,
"i:d:s:")) != EOF)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> switch
(c)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
case 'd':</font></tt>
<br><tt><font size=-2>
if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);</font></tt>
<p><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 'i':</font></tt>
<br><tt><font size=-2>
device = optarg;</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 's':</font></tt>
<br><tt><font size=-2>
if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
default:</font></tt>
<br><tt><font size=-2>
exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> if (!src_ip || !dst_ip)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> usage(argv[0]);</font></tt>
<br><tt><font size=-2> exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">If a device isn't specified, libnet will
attempt to choose one for you. libnet_select_device will return
the first `up` non-loopback device it finds.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 1: Network
Initialization (interchangable with step 2).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (device == NULL)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> struct
sockaddr_in sin;</font></tt>
<br><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2>
* Try to locate a device.</font></tt>
<br><tt><font size=-2>
*/</font></tt>
<br><tt><font size=-2> if (libnet_select_device(&sin,
&device, err_buf) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "libnet_select_device failed: %s\n", err_buf);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> printf("device:\t\t\t\t%s\n",
device);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">Instead of opening a raw socket, we're opening
up a link layer interface.</font>
<p><tt><font size=-2> if ((network = libnet_open_link_interface(device,
err_buf)) == NULL)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_open_link_interface: %s\n", err_buf);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">The ethernet header needs to be specified explicitly
in the packet size.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * We're going to build
an ICMP packet with no payload using the</font></tt>
<br><tt><font size=-2> * link-layer API,
so this time we need memory for a ethernet header</font></tt>
<br><tt><font size=-2> * as well as memory
for the ICMP and IP headers.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> packet_size = LIBNET_IP_H + LIBNET_ETH_H
+ LIBNET_ICMP_MASK_H;</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 2: Memory Initialization
(interchangable with step 1).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_init_packet(packet_size,
&packet) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_init_packet failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(ethernet header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_ethernet(enet_dst,</font></tt>
<br><tt><font size=-2>
enet_src,</font></tt>
<br><tt><font size=-2>
ETHERTYPE_IP,</font></tt>
<br><tt><font size=-2>
NULL,</font></tt>
<br><tt><font size=-2>
0,</font></tt>
<br><tt><font size=-2>
packet);</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(ICMP header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_icmp_mask(ICMP_MASKREPLY,
/* type */</font></tt>
<br><tt><font size=-2>
0,
/* code */</font></tt>
<br><tt><font size=-2>
242,
/* id */</font></tt>
<br><tt><font size=-2>
0,
/* seq */</font></tt>
<br><tt><font size=-2>
0xffffffff,
/* mask */</font></tt>
<br><tt><font size=-2>
NULL,
/* payload */</font></tt>
<br><tt><font size=-2>
0,
/* payload_s */</font></tt>
<br><tt><font size=-2>
packet + LIBNET_ETH_H + LIBNET_IP_H);</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(IP header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_ip(ICMP_MASK_H,</font></tt>
<br><tt><font size=-2>
0,
/* IP tos */</font></tt>
<br><tt><font size=-2>
242,
/* IP ID */</font></tt>
<br><tt><font size=-2>
0,
/* Frag */</font></tt>
<br><tt><font size=-2>
64,
/* TTL */</font></tt>
<br><tt><font size=-2>
IPPROTO_ICMP,
/* Transport protocol */</font></tt>
<br><tt><font size=-2>
src_ip,
/* Source IP */</font></tt>
<br><tt><font size=-2>
dst_ip,
/* Destination IP */</font></tt>
<br><tt><font size=-2>
NULL,
/* Pointer to payload (none) */</font></tt>
<br><tt><font size=-2>
0,</font></tt>
<br><tt><font size=-2>
packet + LIBNET_ETH_H); /* Packet header memory */</font></tt>
<p><font color="#FF0000">The IP header checksum must be explicitly computed
for the link layer interface.</font>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 4: Packet checksums
(ICMP header *AND* IP header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_do_checksum(packet
+ ETH_H, IPPROTO_ICMP, LIBNET_ICMP_MASK_H) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_do_checksum failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> if (libnet_do_checksum(packet
+ ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_do_checksum failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 5: Packet injection.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> c = libnet_write_link_layer(network,
device, packet, packet_size);</font></tt>
<br><tt><font size=-2> if (c < packet_size)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LN_ERR_WARNING,
"libnet_write_link_layer only wrote %d bytes\n", c);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> else</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> printf("construction
and injection completed, wrote all %d bytes\n", c);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Shut down the interface.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_close_link_interface(network)
== -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LN_ERR_WARNING,
"libnet_close_link_interface couldn't close the interface");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Free packet memory.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_destroy_packet(&packet);</font></tt>
<p><tt><font size=-2> return (c == -1 ? EXIT_FAILURE
: EXIT_SUCCESS);</font></tt>
<br><tt><font size=-2>}</font></tt>
<br>
<p><tt><font size=-2>void</font></tt>
<br><tt><font size=-2>usage(char *name)</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> fprintf(stderr, "usage: %s [-i
interface] -s s_ip -d d_ip\n", name);</font></tt>
<br><tt><font size=-2>}</font></tt>
<p><tt><font size=-2>/* EOF */</font></tt>
<p>
<hr ALIGN=LEFT WIDTH="97%">
<p>
<hr ALIGN=LEFT WIDTH="97%">
<br><a NAME="s6.3"></a><b><font size=+2>6.3 Example 3 [raw socket api -
ICMP_ECHO using an arena]</font></b>
<br><font size=-1>Download <a href="examples/libnet-example-3.c">libnet-example-1.c</a></font>
<p><tt><font size=-2>#include <libnet.h></font></tt>
<p><tt><font size=-2>void usage(char *);</font></tt>
<br>
<p><tt><font size=-2>int</font></tt>
<br><tt><font size=-2>main(int argc, char **argv)</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> int network, n, c, number_of_packets,
packet_size;</font></tt>
<br><tt><font size=-2> struct libnet_arena arena, *arena_p;</font></tt>
<br><tt><font size=-2> u_char *packets[10];</font></tt>
<br><tt><font size=-2> u_long src_ip, dst_ip;</font></tt>
<p><tt><font size=-2> printf("libnet example code:\tmodule
3\n\n");</font></tt>
<br><tt><font size=-2> printf("packet injection interface:\tlink
layer\n");</font></tt>
<br><tt><font size=-2> printf("packet type:\t\t\tICMP_ECHO
[no payload] using an arena\n");</font></tt>
<p><tt><font size=-2> src_ip = 0;</font></tt>
<br><tt><font size=-2> dst_ip = 0;</font></tt>
<br><tt><font size=-2> while((c = getopt(argc, argv,
"d:s:")) != EOF)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> switch
(c)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
case 'd':</font></tt>
<br><tt><font size=-2>
if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 's':</font></tt>
<br><tt><font size=-2>
if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> if (!src_ip || !dst_ip)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> usage(argv[0]);</font></tt>
<br><tt><font size=-2> exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * We're just going
to build an ICMP packet with no payload using the</font></tt>
<br><tt><font size=-2> * raw sockets API,
so we only need memory for a ICMP header and an IP</font></tt>
<br><tt><font size=-2> * header.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> packet_size = LIBNET_IP_H + LIBNET_ICMP_ECHO_H;</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Let's just build
say, 10 packets.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> number_of_packets = 10;</font></tt>
<p><font color="#FF0000">The arena can be thought of as a pool of memory
that can be called upon to yield usable memory until it dries up.
In this case, we'll have enough memory for 10 packets of the above packet
size.</font>
<p><tt><font size=-2> arena_p = &arena;</font></tt>
<br><tt><font size=-2> if (libnet_init_packet_arena(&arena_p,
number_of_packets, packet_size) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_init_packet_arena failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> else</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> printf("Allocated
an arena of %ld bytes..\n", LIBNET_GET_ARENA_SIZE(arena));</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> network = libnet_open_raw_sock(IPPROTO_RAW);</font></tt>
<br><tt><font size=-2> if (network == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"Can't open the network.\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">We'll keep grabbing chunks of memory from the
arena to build packets until it runs dry. This isn't the most useful
implementation of an arena as all of the packets are the same (a for loop
around in the injection call would accomplish the same thing without an
arena). Arenas really become useful when you're injecting packets
of different types (the arena is used to preload packets).</font>
<p><tt><font size=-2> for (n = 0; n < number_of_packets;
n++)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> printf("%ld
bytes remaining in arena\n", LIBNET_GET_ARENA_REMAINING_BYTES(arena));</font></tt>
<br><tt><font size=-2> packets[n]
= libnet_next_packet_from_arena(&arena_p, packet_size);</font></tt>
<br><tt><font size=-2> if (!packets[n])</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_WARNING, "Arena is empty\n");</font></tt>
<br><tt><font size=-2>
continue;</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> libnet_build_ip(ICMP_ECHO_H,
/* Size of the payload */</font></tt>
<br><tt><font size=-2>
IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* IP tos */</font></tt>
<br><tt><font size=-2>
242,
/* IP ID */</font></tt>
<br><tt><font size=-2>
0,
/* frag stuff */</font></tt>
<br><tt><font size=-2>
48,
/* TTL */</font></tt>
<br><tt><font size=-2>
IPPROTO_ICMP,
/* transport protocol */</font></tt>
<br><tt><font size=-2>
src_ip,
/* source IP */</font></tt>
<br><tt><font size=-2>
dst_ip,
/* destination IP */</font></tt>
<br><tt><font size=-2>
NULL,
/* pointer to payload */</font></tt>
<br><tt><font size=-2>
0,
/* payload length */</font></tt>
<br><tt><font size=-2>
packets[n]);
/* packet header memory */</font></tt>
<p><tt><font size=-2> libnet_build_icmp_echo(ICMP_ECHO,
/* type */</font></tt>
<br><tt><font size=-2>
0,
/* code */</font></tt>
<br><tt><font size=-2>
242,
/* id */</font></tt>
<br><tt><font size=-2>
5,
/* seq */</font></tt>
<br><tt><font size=-2>
NULL,
/* pointer to payload */</font></tt>
<br><tt><font size=-2>
0,
/* payload length */</font></tt>
<br><tt><font size=-2>
packets[n] + LIBNET_IP_H);
/* packet header memory */</font></tt>
<p><tt><font size=-2> if (libnet_do_checksum(packets[n],
IPPROTO_ICMP, LIBNET_ICMP_ECHO_H) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> c = libnet_write_ip(network,
packets[n], packet_size);</font></tt>
<br><tt><font size=-2> if (c
< packet_size)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
libnet_error(LN_ERR_WARNING, "libnet_write_ip only wrote %d bytes\n", c);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> else</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
printf("construction and injection of packet %d of %d completed, wrote
all %d bytes\n", n + 1, number_of_packets, c);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> libnet_destroy_packet_arena(&arena_p);</font></tt>
<br><tt><font size=-2> return (c == -1 ? EXIT_FAILURE
: EXIT_SUCCESS);</font></tt>
<br><tt><font size=-2>}</font></tt>
<br>
<p><tt><font size=-2>void</font></tt>
<br><tt><font size=-2>usage(char *name)</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> fprintf(stderr, "usage: %s -s
source_ip -d destination_ip\n ", name);</font></tt>
<br><tt><font size=-2>}</font></tt>
<p><tt><font size=-2>/* EOF */</font></tt>
<p>
<hr ALIGN=LEFT WIDTH="97%">
<p>
<hr ALIGN=LEFT WIDTH="97%">
<br><a NAME="s6.4"></a><b><font size=+2>6.4 Example 4 [link-layer api -
UDP packet using port list chaining]</font></b>
<br><font size=-1>Download <a href="examples/libnet-example-4.c">libnet-example-1.c</a></font>
<p><tt><font size=-2>#include <libnet.h></font></tt>
<p><tt><font size=-2>#define MAX_PAYLOAD_SIZE 1024</font></tt>
<p><tt><font size=-2>void usage(char *);</font></tt>
<p><tt><font size=-2>u_char enet_src[6] = {0x0d, 0x0e, 0x0a, 0x0d, 0x00,
0x00};</font></tt>
<br><tt><font size=-2>u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff,
0xff};</font></tt>
<p><tt><font size=-2>int</font></tt>
<br><tt><font size=-2>main(int argc, char *argv[])</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> int packet_size,
/* size of our packet */</font></tt>
<br><tt><font size=-2> payload_size,
/* size of our packet */</font></tt>
<br><tt><font size=-2> c;
/* misc */</font></tt>
<br><tt><font size=-2> u_long src_ip, dst_ip;
/* source ip, dest ip */</font></tt>
<br><tt><font size=-2> u_short bport, eport;
/* beginning and end ports */</font></tt>
<br><tt><font size=-2> u_short cport;
/* current port */</font></tt>
<br><tt><font size=-2> u_char payload[MAX_PAYLOAD_SIZE];
/* packet payload */</font></tt>
<br><tt><font size=-2> u_char *packet;
/* pointer to our packet buffer */</font></tt>
<br><tt><font size=-2> char err_buf[LIBNET_ERRBUF_SIZE];
/* error buffer */</font></tt>
<br><tt><font size=-2> u_char *device;
/* pointer to the device to use */</font></tt>
<br><tt><font size=-2> struct libnet_link_int *network;
/* pointer to link interface struct */</font></tt>
<br><tt><font size=-2> struct libnet_plist_chain plist;
/* plist chain */</font></tt>
<br><tt><font size=-2> struct libnet_plist_chain *plist_p;
/* plist chain pointer */</font></tt>
<p><tt><font size=-2> printf("libnet example code:\tmodule
4\n\n");</font></tt>
<br><tt><font size=-2> printf("packet injection interface:\tlink
layer\n");</font></tt>
<br><tt><font size=-2> printf("packet type:\t\t\tUDP
[with payload] using port list chaining\n");</font></tt>
<p><tt><font size=-2> plist_p = NULL;</font></tt>
<br><tt><font size=-2> device = NULL;</font></tt>
<br><tt><font size=-2> src_ip = 0;</font></tt>
<br><tt><font size=-2> dst_ip = 0;</font></tt>
<p><font color="#FF0000">HEY LOOK! We have another argument for the
port list. Port lists must be specified as ranges delimited by dashes,
with multiple ranges delimited by commas. See <a href="5.html#s5.5">section
5</a> (support functions) for more information on port list chain syntax.</font>
<p><tt><font size=-2> while ((c = getopt(argc, argv,
"i:d:s:p:")) != EOF)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> switch
(c)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
case 'd':</font></tt>
<br><tt><font size=-2>
if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL,</font></tt>
<br><tt><font size=-2>
"Bad destination IP address: %s\n", optarg);</font></tt>
<p><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 'i':</font></tt>
<br><tt><font size=-2>
device = optarg;</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 's':</font></tt>
<br><tt><font size=-2>
if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL,</font></tt>
<br><tt><font size=-2>
"Bad source IP address: %s\n", optarg);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
case 'p':</font></tt>
<br><tt><font size=-2>
plist_p = &plist;</font></tt>
<br><tt><font size=-2>
if (libnet_plist_chain_new(&plist_p, optarg) == -1)</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "Could not build port list\n");</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
break;</font></tt>
<br><tt><font size=-2>
default:</font></tt>
<br><tt><font size=-2>
usage(argv[0]);</font></tt>
<br><tt><font size=-2>
exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> if (!src_ip || !dst_ip || !plist_p)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> usage(argv[0]);</font></tt>
<br><tt><font size=-2> exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><font color="#FF0000">We also require an argument for the packet payload.
This code is probably not proof from overflows so take heed.</font><font color="#FF0000"></font>
<p><tt><font size=-2> c = argc - optind;</font></tt>
<br><tt><font size=-2> if (c != 1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> usage(argv[0]);</font></tt>
<br><tt><font size=-2> exit(EXIT_FAILURE);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> memset(payload, 0, sizeof(payload));</font></tt>
<br><tt><font size=-2> strncpy(payload, argv[optind],
strlen(argv[optind]));</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 1: Network
Initialization (interchangable with step 2).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (device == NULL)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> struct
sockaddr_in sin;</font></tt>
<br><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2>
* Try to locate a device.</font></tt>
<br><tt><font size=-2>
*/</font></tt>
<br><tt><font size=-2> if (libnet_select_device(&sin,
&device, err_buf) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "libnet_select_device failed: %s\n", err_buf);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> printf("device:\t\t\t\t%s\n",
device);</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> if ((network = libnet_open_link_interface(device,
err_buf)) == NULL)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_open_link_interface: %s\n", err_buf);</font></tt>
<br><tt><font size=-2> }</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Get the payload
from the user. Hrm. This might fail on a Sparc</font></tt>
<br><tt><font size=-2> * if byte alignment
is off...</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> payload_size = strlen(payload);</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * We're going to build
a UDP packet with a payload using the</font></tt>
<br><tt><font size=-2> * link-layer API,
so this time we need memory for a ethernet header</font></tt>
<br><tt><font size=-2> * as well as memory
for the ICMP and IP headers and our payload.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> packet_size = LIBNET_IP_H + LIBNET_ETH_H
+ LIBNET_UDP_H + payload_size;</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 2: Memory Initialization
(interchangable with step 1).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_init_packet(packet_size,
&packet) == -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LIBNET_ERR_FATAL,
"libnet_init_packet failed\n");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(ethernet header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_ethernet(enet_dst,</font></tt>
<br><tt><font size=-2>
enet_src,</font></tt>
<br><tt><font size=-2>
ETHERTYPE_IP,</font></tt>
<br><tt><font size=-2>
NULL,</font></tt>
<br><tt><font size=-2>
0,</font></tt>
<br><tt><font size=-2>
packet);</font></tt>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Step 3: Packet construction
(IP header).</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_build_ip(LIBNET_UDP_H +
payload_size,</font></tt>
<br><tt><font size=-2>
0,
/* IP tos */</font></tt>
<br><tt><font size=-2>
242,
/* IP ID */</font></tt>
<br><tt><font size=-2>
0,
/* Frag */</font></tt>
<br><tt><font size=-2>
64,
/* TTL */</font></tt>
<br><tt><font size=-2>
IPPROTO_UDP,
/* Transport protocol */</font></tt>
<br><tt><font size=-2>
src_ip,
/* Source IP */</font></tt>
<br><tt><font size=-2>
dst_ip,
/* Destination IP */</font></tt>
<br><tt><font size=-2>
NULL,
/* Pointer to payload (none) */</font></tt>
<br><tt><font size=-2>
0,</font></tt>
<br><tt><font size=-2>
packet + LIBNET_ETH_H); /* Packet header memory */</font></tt>
<p><font color="#FF0000">We run in a loop, grabbing port list ranges as
we go. We only really need to update the UDP port and checksum portions
of the packet, but for simplicity's sake, we'll lump it all together thusly...</font><font color="#FF0000"></font>
<p><tt><font size=-2> while (libnet_plist_chain_next_pair(plist_p,
&bport, &eport))</font></tt>
<br><tt><font size=-2> {</font></tt><font color="#FF0000"></font>
<p><font color="#FF0000">Check to see if we've exhausted our cache of port
list ranges.</font><font color="#FF0000"></font>
<p><tt><font size=-2> while (!(bport
> eport) && bport != 0)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2>
cport = bport++;</font></tt>
<br><tt><font size=-2>
/*</font></tt>
<br><tt><font size=-2>
* Step 3: Packet construction (UDP header).</font></tt>
<br><tt><font size=-2>
*/</font></tt>
<br><tt><font size=-2>
libnet_build_udp(242,
/* source port */</font></tt>
<br><tt><font size=-2>
cport,
/* dest. port */</font></tt>
<br><tt><font size=-2>
payload,
/* payload */</font></tt>
<br><tt><font size=-2>
payload_size,
/* payload length */</font></tt>
<br><tt><font size=-2>
packet + LIBNET_ETH_H + LIBNET_IP_H);</font></tt>
<p><tt><font size=-2>
/*</font></tt>
<br><tt><font size=-2>
* Step 4: Packet checksums (ICMP header *AND* IP header).</font></tt>
<br><tt><font size=-2>
*/</font></tt>
<br><tt><font size=-2>
if (libnet_do_checksum(packet + ETH_H, IPPROTO_UDP, LIBNET_UDP_H + payload_size)
== -1)</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
if (libnet_do_checksum(packet + ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1)</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");</font></tt>
<br><tt><font size=-2>
}</font></tt>
<p><tt><font size=-2>
/*</font></tt>
<br><tt><font size=-2>
* Step 5: Packet injection.</font></tt>
<br><tt><font size=-2>
*/</font></tt>
<br><tt><font size=-2>
c = libnet_write_link_layer(network, device, packet, packet_size);</font></tt>
<br><tt><font size=-2>
if (c < packet_size)</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
libnet_error(LN_ERR_WARNING, "libnet_write_link_layer only wrote %d bytes\n",
c);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2>
else</font></tt>
<br><tt><font size=-2>
{</font></tt>
<br><tt><font size=-2>
printf("construction and injection completed, wrote all %d bytes, port
%d\n", c, cport);</font></tt>
<br><tt><font size=-2>
}</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> }</font></tt>
<br><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Shut down the interface.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> if (libnet_close_link_interface(network)
== -1)</font></tt>
<br><tt><font size=-2> {</font></tt>
<br><tt><font size=-2> libnet_error(LN_ERR_WARNING,
"libnet_close_link_interface couldn't close the interface");</font></tt>
<br><tt><font size=-2> }</font></tt>
<br>
<p><tt><font size=-2> /*</font></tt>
<br><tt><font size=-2> * Free packet memory.</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2> libnet_destroy_packet(&packet);</font></tt>
<p><tt><font size=-2> return (c == -1 ? EXIT_FAILURE
: EXIT_SUCCESS);</font></tt>
<br><tt><font size=-2>}</font></tt>
<br>
<p><tt><font size=-2>void</font></tt>
<br><tt><font size=-2>usage(char *name)</font></tt>
<br><tt><font size=-2>{</font></tt>
<br><tt><font size=-2> fprintf(stderr, "usage: %s [-i
interface] -s s_ip -d d_ip -p port list payload\n", name);</font></tt>
<br><tt><font size=-2>}</font></tt>
<p><tt><font size=-2>/* EOF */</font></tt>
<p>
<hr ALIGN=LEFT WIDTH="97%">
<p>
<hr ALIGN=LEFT WIDTH="97%">
<p><a href="7.html">Next</a> <a href="5.html">Previous</a> <a href="lrm.html">Top</a>
</body>
</html>
|