libnet4r 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +15 -0
- data/LICENSE +28 -0
- data/README +187 -0
- data/Rakefile +110 -0
- data/ext/extconf.rb +60 -0
- data/ext/libnet.c +705 -0
- data/ext/libnet_arp.c +152 -0
- data/ext/libnet_ethernet.c +115 -0
- data/ext/libnet_ipv4.c +140 -0
- data/ext/libnet_ipv6.c +138 -0
- data/ext/libnet_udp.c +112 -0
- data/ext/libnet_vlan.c +131 -0
- data/lib/libnet4r/header.rb +609 -0
- data/lib/libnet4r/helpers.rb +19 -0
- data/lib/libnet4r/libnet.rb +39 -0
- data/libnet/CVS/Entries +24 -0
- data/libnet/CVS/Repository +1 -0
- data/libnet/CVS/Root +1 -0
- data/libnet/Makefile.am +15 -0
- data/libnet/Makefile.am.common +10 -0
- data/libnet/Makefile.in +443 -0
- data/libnet/README +21 -0
- data/libnet/VERSION +1 -0
- data/libnet/acconfig.h +36 -0
- data/libnet/acinclude.m4 +463 -0
- data/libnet/aclocal.m4 +1337 -0
- data/libnet/autom4te.cache/CVS/Entries +1 -0
- data/libnet/autom4te.cache/CVS/Repository +1 -0
- data/libnet/autom4te.cache/CVS/Root +1 -0
- data/libnet/autom4te.cache/output.0 +6225 -0
- data/libnet/autom4te.cache/requests +111 -0
- data/libnet/autom4te.cache/traces.0 +272 -0
- data/libnet/config.guess +1314 -0
- data/libnet/config.sub +1410 -0
- data/libnet/configure +6225 -0
- data/libnet/configure.in +235 -0
- data/libnet/doc/BUGS +19 -0
- data/libnet/doc/CHANGELOG +527 -0
- data/libnet/doc/CONTRIB +53 -0
- data/libnet/doc/COPYING +31 -0
- data/libnet/doc/CVS/Entries +12 -0
- data/libnet/doc/CVS/Repository +1 -0
- data/libnet/doc/CVS/Root +1 -0
- data/libnet/doc/DESIGN_NOTES +134 -0
- data/libnet/doc/MIGRATION +172 -0
- data/libnet/doc/PACKET_BUILDING +161 -0
- data/libnet/doc/PORTED +45 -0
- data/libnet/doc/RAWSOCKET_NON_SEQUITUR +41 -0
- data/libnet/doc/TODO +96 -0
- data/libnet/doc/html/acconfig_8h-source.html +49 -0
- data/libnet/doc/html/annotated.html +17 -0
- data/libnet/doc/html/bpf_8h-source.html +277 -0
- data/libnet/doc/html/config_8h-source.html +125 -0
- data/libnet/doc/html/doxygen.css +169 -0
- data/libnet/doc/html/doxygen.png +0 -0
- data/libnet/doc/html/files.html +29 -0
- data/libnet/doc/html/functions.html +26 -0
- data/libnet/doc/html/functions_vars.html +26 -0
- data/libnet/doc/html/getopt_8h-source.html +97 -0
- data/libnet/doc/html/globals.html +219 -0
- data/libnet/doc/html/globals_defs.html +104 -0
- data/libnet/doc/html/globals_func.html +136 -0
- data/libnet/doc/html/gnuc_8h-source.html +56 -0
- data/libnet/doc/html/graph_legend.dot +22 -0
- data/libnet/doc/html/graph_legend.html +75 -0
- data/libnet/doc/html/ifaddrlist_8h-source.html +65 -0
- data/libnet/doc/html/in__systm_8h-source.html +90 -0
- data/libnet/doc/html/index.html +17 -0
- data/libnet/doc/html/libnet-asn1_8h-source.html +268 -0
- data/libnet/doc/html/libnet-functions_8h-source.html +742 -0
- data/libnet/doc/html/libnet-functions_8h.html +8844 -0
- data/libnet/doc/html/libnet-headers_8h-source.html +1655 -0
- data/libnet/doc/html/libnet-headers_8h.html +3053 -0
- data/libnet/doc/html/libnet-macros_8h-source.html +161 -0
- data/libnet/doc/html/libnet-macros_8h.html +358 -0
- data/libnet/doc/html/libnet-structures_8h-source.html +233 -0
- data/libnet/doc/html/libnet-types_8h-source.html +58 -0
- data/libnet/doc/html/libnet_8h-source.html +119 -0
- data/libnet/doc/html/libnet_8h.html +24 -0
- data/libnet/doc/html/structlibnet__802__1q__hdr.html +145 -0
- data/libnet/doc/html/structlibnet__802__1x__hdr.html +97 -0
- data/libnet/doc/libnet.doxygen.conf +1102 -0
- data/libnet/doc/man/CVS/Entries +1 -0
- data/libnet/doc/man/CVS/Repository +1 -0
- data/libnet/doc/man/CVS/Root +1 -0
- data/libnet/doc/man/man3/libnet-functions.h.3 +3136 -0
- data/libnet/doc/man/man3/libnet-headers.h.3 +1872 -0
- data/libnet/doc/man/man3/libnet-macros.h.3 +172 -0
- data/libnet/doc/man/man3/libnet.h.3 +17 -0
- data/libnet/doc/man/man3/libnet_802_1q_hdr.3 +53 -0
- data/libnet/doc/man/man3/libnet_802_1x_hdr.3 +41 -0
- data/libnet/include/CVS/Entries +10 -0
- data/libnet/include/CVS/Repository +1 -0
- data/libnet/include/CVS/Root +1 -0
- data/libnet/include/Makefile.am +7 -0
- data/libnet/include/Makefile.in +395 -0
- data/libnet/include/bpf.h +264 -0
- data/libnet/include/config.h.in +118 -0
- data/libnet/include/gnuc.h +43 -0
- data/libnet/include/ifaddrlist.h +52 -0
- data/libnet/include/libnet/CVS/Entries +9 -0
- data/libnet/include/libnet/CVS/Repository +1 -0
- data/libnet/include/libnet/CVS/Root +1 -0
- data/libnet/include/libnet/Makefile.am +12 -0
- data/libnet/include/libnet/Makefile.in +294 -0
- data/libnet/include/libnet/libnet-asn1.h +255 -0
- data/libnet/include/libnet/libnet-functions.h +2157 -0
- data/libnet/include/libnet/libnet-headers.h +1662 -0
- data/libnet/include/libnet/libnet-macros.h +186 -0
- data/libnet/include/libnet/libnet-structures.h +222 -0
- data/libnet/include/libnet/libnet-types.h +45 -0
- data/libnet/include/libnet.h.in +132 -0
- data/libnet/include/stamp-h.in +1 -0
- data/libnet/include/win32/CVS/Entries +5 -0
- data/libnet/include/win32/CVS/Repository +1 -0
- data/libnet/include/win32/CVS/Root +1 -0
- data/libnet/include/win32/config.h +112 -0
- data/libnet/include/win32/getopt.h +84 -0
- data/libnet/include/win32/in_systm.h +77 -0
- data/libnet/include/win32/libnet.h +106 -0
- data/libnet/install-sh +250 -0
- data/libnet/libnet-config.in +62 -0
- data/libnet/libnet.doxygen.conf +1102 -0
- data/libnet/man/CVS/Entries +1 -0
- data/libnet/man/CVS/Repository +1 -0
- data/libnet/man/CVS/Root +1 -0
- data/libnet/missing +283 -0
- data/libnet/mkinstalldirs +40 -0
- data/libnet/sample/CVS/Entries +47 -0
- data/libnet/sample/CVS/Repository +1 -0
- data/libnet/sample/CVS/Root +1 -0
- data/libnet/sample/Makefile.am +63 -0
- data/libnet/sample/Makefile.in +729 -0
- data/libnet/sample/arp-new.c +144 -0
- data/libnet/sample/arp.c +151 -0
- data/libnet/sample/bgp4_hdr.c +225 -0
- data/libnet/sample/bgp4_notification.c +242 -0
- data/libnet/sample/bgp4_open.c +266 -0
- data/libnet/sample/bgp4_update.c +337 -0
- data/libnet/sample/cdp.c +187 -0
- data/libnet/sample/dhcp_discover.c +257 -0
- data/libnet/sample/dns.c +260 -0
- data/libnet/sample/dot1x.c +113 -0
- data/libnet/sample/fddi_tcp1.c +213 -0
- data/libnet/sample/fddi_tcp2.c +209 -0
- data/libnet/sample/get_addr.c +112 -0
- data/libnet/sample/gre.c +410 -0
- data/libnet/sample/icmp6_echoreq.c +184 -0
- data/libnet/sample/icmp_echo_cq.c +201 -0
- data/libnet/sample/icmp_redirect.c +200 -0
- data/libnet/sample/icmp_timeexceed.c +190 -0
- data/libnet/sample/icmp_timestamp.c +157 -0
- data/libnet/sample/icmp_unreach.c +204 -0
- data/libnet/sample/ieee.c +177 -0
- data/libnet/sample/ip_link.c +201 -0
- data/libnet/sample/ip_raw.c +180 -0
- data/libnet/sample/isl.c +167 -0
- data/libnet/sample/libnet_test.h +60 -0
- data/libnet/sample/mpls.c +251 -0
- data/libnet/sample/ntp.c +193 -0
- data/libnet/sample/ospf_hello.c +179 -0
- data/libnet/sample/ospf_lsa.c +190 -0
- data/libnet/sample/ping_of_death.c +171 -0
- data/libnet/sample/rpc_tcp.c +214 -0
- data/libnet/sample/rpc_udp.c +213 -0
- data/libnet/sample/sebek.c +299 -0
- data/libnet/sample/smurf.c +194 -0
- data/libnet/sample/stp.c +227 -0
- data/libnet/sample/synflood.c +200 -0
- data/libnet/sample/synflood6.c +209 -0
- data/libnet/sample/synflood6_frag.c +234 -0
- data/libnet/sample/tcp1.c +227 -0
- data/libnet/sample/tcp2.c +192 -0
- data/libnet/sample/tftp.c +207 -0
- data/libnet/sample/tring_tcp1.c +214 -0
- data/libnet/sample/tring_tcp2.c +210 -0
- data/libnet/sample/udp1.c +223 -0
- data/libnet/sample/udp2.c +232 -0
- data/libnet/sample/win32/CVS/Entries +9 -0
- data/libnet/sample/win32/CVS/Repository +1 -0
- data/libnet/sample/win32/CVS/Root +1 -0
- data/libnet/sample/win32/arp/CVS/Entries +2 -0
- data/libnet/sample/win32/arp/CVS/Repository +1 -0
- data/libnet/sample/win32/arp/CVS/Root +1 -0
- data/libnet/sample/win32/arp/arp.vcproj +136 -0
- data/libnet/sample/win32/cdp/CVS/Entries +2 -0
- data/libnet/sample/win32/cdp/CVS/Repository +1 -0
- data/libnet/sample/win32/cdp/CVS/Root +1 -0
- data/libnet/sample/win32/cdp/cdp.vcproj +136 -0
- data/libnet/sample/win32/dhcp_discover/CVS/Entries +2 -0
- data/libnet/sample/win32/dhcp_discover/CVS/Repository +1 -0
- data/libnet/sample/win32/dhcp_discover/CVS/Root +1 -0
- data/libnet/sample/win32/dhcp_discover/dhcp_discover.vcproj +139 -0
- data/libnet/sample/win32/dns/CVS/Entries +2 -0
- data/libnet/sample/win32/dns/CVS/Repository +1 -0
- data/libnet/sample/win32/dns/CVS/Root +1 -0
- data/libnet/sample/win32/dns/dns.vcproj +139 -0
- data/libnet/sample/win32/get_addr/CVS/Entries +2 -0
- data/libnet/sample/win32/get_addr/CVS/Repository +1 -0
- data/libnet/sample/win32/get_addr/CVS/Root +1 -0
- data/libnet/sample/win32/get_addr/get_addr.vcproj +139 -0
- data/libnet/sample/win32/getopt.c +121 -0
- data/libnet/sample/win32/icmp_echo_cq/CVS/Entries +2 -0
- data/libnet/sample/win32/icmp_echo_cq/CVS/Repository +1 -0
- data/libnet/sample/win32/icmp_echo_cq/CVS/Root +1 -0
- data/libnet/sample/win32/icmp_echo_cq/icmp_echo_cq.vcproj +139 -0
- data/libnet/sample/win32/tcp1/CVS/Entries +2 -0
- data/libnet/sample/win32/tcp1/CVS/Repository +1 -0
- data/libnet/sample/win32/tcp1/CVS/Root +1 -0
- data/libnet/sample/win32/tcp1/tcp1.vcproj +142 -0
- data/libnet/sample/win32/udp1/CVS/Entries +3 -0
- data/libnet/sample/win32/udp1/CVS/Repository +1 -0
- data/libnet/sample/win32/udp1/CVS/Root +1 -0
- data/libnet/sample/win32/udp1/dns.vcproj +125 -0
- data/libnet/sample/win32/udp1/udp1.vcproj +139 -0
- data/libnet/src/CVS/Entries +60 -0
- data/libnet/src/CVS/Repository +1 -0
- data/libnet/src/CVS/Root +1 -0
- data/libnet/src/Makefile.am +71 -0
- data/libnet/src/Makefile.in +428 -0
- data/libnet/src/libnet_advanced.c +136 -0
- data/libnet/src/libnet_asn1.c +436 -0
- data/libnet/src/libnet_build_802.1q.c +115 -0
- data/libnet/src/libnet_build_802.1x.c +103 -0
- data/libnet/src/libnet_build_802.2.c +167 -0
- data/libnet/src/libnet_build_802.3.c +101 -0
- data/libnet/src/libnet_build_arp.c +169 -0
- data/libnet/src/libnet_build_bgp.c +350 -0
- data/libnet/src/libnet_build_cdp.c +191 -0
- data/libnet/src/libnet_build_data.c +90 -0
- data/libnet/src/libnet_build_dhcp.c +156 -0
- data/libnet/src/libnet_build_dns.c +129 -0
- data/libnet/src/libnet_build_ethernet.c +173 -0
- data/libnet/src/libnet_build_fddi.c +191 -0
- data/libnet/src/libnet_build_gre.c +427 -0
- data/libnet/src/libnet_build_icmp.c +437 -0
- data/libnet/src/libnet_build_igmp.c +112 -0
- data/libnet/src/libnet_build_ip.c +892 -0
- data/libnet/src/libnet_build_ipsec.c +232 -0
- data/libnet/src/libnet_build_isl.c +113 -0
- data/libnet/src/libnet_build_link.c +90 -0
- data/libnet/src/libnet_build_mpls.c +112 -0
- data/libnet/src/libnet_build_ntp.c +121 -0
- data/libnet/src/libnet_build_ospf.c +699 -0
- data/libnet/src/libnet_build_rip.c +108 -0
- data/libnet/src/libnet_build_rpc.c +138 -0
- data/libnet/src/libnet_build_sebek.c +108 -0
- data/libnet/src/libnet_build_snmp.c +52 -0
- data/libnet/src/libnet_build_stp.c +222 -0
- data/libnet/src/libnet_build_tcp.c +365 -0
- data/libnet/src/libnet_build_token_ring.c +193 -0
- data/libnet/src/libnet_build_udp.c +111 -0
- data/libnet/src/libnet_build_vrrp.c +117 -0
- data/libnet/src/libnet_checksum.c +376 -0
- data/libnet/src/libnet_cq.c +395 -0
- data/libnet/src/libnet_crc.c +113 -0
- data/libnet/src/libnet_dll.c +47 -0
- data/libnet/src/libnet_error.c +55 -0
- data/libnet/src/libnet_if_addr.c +415 -0
- data/libnet/src/libnet_init.c +234 -0
- data/libnet/src/libnet_internal.c +310 -0
- data/libnet/src/libnet_link_bpf.c +348 -0
- data/libnet/src/libnet_link_dlpi.c +785 -0
- data/libnet/src/libnet_link_linux.c +310 -0
- data/libnet/src/libnet_link_nit.c +125 -0
- data/libnet/src/libnet_link_none.c +66 -0
- data/libnet/src/libnet_link_pf.c +179 -0
- data/libnet/src/libnet_link_snit.c +149 -0
- data/libnet/src/libnet_link_snoop.c +166 -0
- data/libnet/src/libnet_link_win32.c +346 -0
- data/libnet/src/libnet_pblock.c +511 -0
- data/libnet/src/libnet_port_list.c +295 -0
- data/libnet/src/libnet_prand.c +106 -0
- data/libnet/src/libnet_raw.c +257 -0
- data/libnet/src/libnet_resolve.c +414 -0
- data/libnet/src/libnet_version.c +54 -0
- data/libnet/src/libnet_write.c +502 -0
- data/libnet/version.h.in +6 -0
- data/libnet/win32/CVS/Entries +15 -0
- data/libnet/win32/CVS/Repository +1 -0
- data/libnet/win32/CVS/Root +1 -0
- data/libnet/win32/Libnet-1.1.1-2002.sln +77 -0
- data/libnet/win32/Libnet-1.1.1-2003.ncb +0 -0
- data/libnet/win32/Libnet-1.1.1-2003.sln +84 -0
- data/libnet/win32/Libnet-1.1.1-2003.suo +0 -0
- data/libnet/win32/Libnet-1.1.1.vcproj +311 -0
- data/libnet/win32/Libnet-latest.ncb +0 -0
- data/libnet/win32/Libnet-latest.opt +0 -0
- data/libnet/win32/Libnet-latest.sln +77 -0
- data/libnet/win32/Libnet-latest.suo +0 -0
- data/libnet/win32/Libnet-latest.vcproj +311 -0
- data/libnet/win32/Libnet.dsp +337 -0
- data/libnet/win32/Libnet.dsw +32 -0
- data/libnet/win32/README.txt +57 -0
- data/libnet/win32/libnet_dll.def +164 -0
- data/test/tc_arp.rb +169 -0
- data/test/tc_class.rb +219 -0
- data/test/tc_ethernet.rb +97 -0
- data/test/tc_header.rb +163 -0
- data/test/tc_init.rb +25 -0
- data/test/tc_ipv4.rb +219 -0
- data/test/tc_ipv6.rb +235 -0
- data/test/tc_udp.rb +229 -0
- data/test/tc_vlan.rb +118 -0
- data/test/ts_all.rb +15 -0
- metadata +401 -0
@@ -0,0 +1,365 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: libnet_build_tcp.c,v 1.11 2004/01/28 19:45:00 mike Exp $
|
3
|
+
*
|
4
|
+
* libnet
|
5
|
+
* libnet_build_tcp.c - TCP packet assembler
|
6
|
+
*
|
7
|
+
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
8
|
+
* All rights reserved.
|
9
|
+
*
|
10
|
+
* Redistribution and use in source and binary forms, with or without
|
11
|
+
* modification, are permitted provided that the following conditions
|
12
|
+
* are met:
|
13
|
+
* 1. Redistributions of source code must retain the above copyright
|
14
|
+
* notice, this list of conditions and the following disclaimer.
|
15
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
* notice, this list of conditions and the following disclaimer in the
|
17
|
+
* documentation and/or other materials provided with the distribution.
|
18
|
+
*
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
20
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
22
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
23
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
25
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
26
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
28
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
29
|
+
* SUCH DAMAGE.
|
30
|
+
*
|
31
|
+
*/
|
32
|
+
|
33
|
+
#if (HAVE_CONFIG_H)
|
34
|
+
#include "../include/config.h"
|
35
|
+
#endif
|
36
|
+
#if (!(_WIN32) || (__CYGWIN__))
|
37
|
+
#include "../include/libnet.h"
|
38
|
+
#else
|
39
|
+
#include "../include/win32/libnet.h"
|
40
|
+
#endif
|
41
|
+
|
42
|
+
libnet_ptag_t
|
43
|
+
libnet_build_tcp(u_int16_t sp, u_int16_t dp, u_int32_t seq, u_int32_t ack,
|
44
|
+
u_int8_t control, u_int16_t win, u_int16_t sum, u_int16_t urg, u_int16_t len,
|
45
|
+
u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
46
|
+
{
|
47
|
+
int n, offset;
|
48
|
+
u_int32_t i, j;
|
49
|
+
libnet_pblock_t *p, *p_data, *p_temp;
|
50
|
+
libnet_ptag_t ptag_hold, ptag_data;
|
51
|
+
struct libnet_tcp_hdr tcp_hdr;
|
52
|
+
struct libnet_ipv4_hdr *ip_hdr;
|
53
|
+
|
54
|
+
if (l == NULL)
|
55
|
+
{
|
56
|
+
return (-1);
|
57
|
+
}
|
58
|
+
|
59
|
+
ptag_data = 0; /* for possible options */
|
60
|
+
|
61
|
+
/*
|
62
|
+
* Find the existing protocol block if a ptag is specified, or create
|
63
|
+
* a new one.
|
64
|
+
*/
|
65
|
+
p = libnet_pblock_probe(l, ptag, LIBNET_TCP_H, LIBNET_PBLOCK_TCP_H);
|
66
|
+
if (p == NULL)
|
67
|
+
{
|
68
|
+
return (-1);
|
69
|
+
}
|
70
|
+
|
71
|
+
memset(&tcp_hdr, 0, sizeof(tcp_hdr));
|
72
|
+
tcp_hdr.th_sport = htons(sp); /* source port */
|
73
|
+
tcp_hdr.th_dport = htons(dp); /* destination port */
|
74
|
+
tcp_hdr.th_seq = htonl(seq); /* sequence number */
|
75
|
+
tcp_hdr.th_ack = htonl(ack); /* acknowledgement number */
|
76
|
+
tcp_hdr.th_flags = control; /* control flags */
|
77
|
+
tcp_hdr.th_x2 = 0; /* UNUSED */
|
78
|
+
tcp_hdr.th_off = 5; /* 20 byte header */
|
79
|
+
|
80
|
+
/* check to see if there are TCP options to include */
|
81
|
+
if (p->prev)
|
82
|
+
{
|
83
|
+
p_temp = p->prev;
|
84
|
+
while ((p_temp->prev) && (p_temp->type != LIBNET_PBLOCK_TCPO_H))
|
85
|
+
{
|
86
|
+
p_temp = p_temp->prev;
|
87
|
+
}
|
88
|
+
if (p_temp->type == LIBNET_PBLOCK_TCPO_H)
|
89
|
+
{
|
90
|
+
/*
|
91
|
+
* Count up number of 32-bit words in options list, padding if
|
92
|
+
* neccessary.
|
93
|
+
*/
|
94
|
+
for (i = 0, j = 0; i < p_temp->b_len; i++)
|
95
|
+
{
|
96
|
+
(i % 4) ? j : j++;
|
97
|
+
}
|
98
|
+
tcp_hdr.th_off += j;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
tcp_hdr.th_win = htons(win); /* window size */
|
103
|
+
tcp_hdr.th_sum = (sum ? htons(sum) : 0); /* checksum */
|
104
|
+
tcp_hdr.th_urp = htons(urg); /* urgent pointer */
|
105
|
+
|
106
|
+
n = libnet_pblock_append(l, p, (u_int8_t *)&tcp_hdr, LIBNET_TCP_H);
|
107
|
+
if (n == -1)
|
108
|
+
{
|
109
|
+
goto bad;
|
110
|
+
}
|
111
|
+
|
112
|
+
ptag_hold = ptag;
|
113
|
+
if (ptag == LIBNET_PTAG_INITIALIZER)
|
114
|
+
{
|
115
|
+
ptag = libnet_pblock_update(l, p, len, LIBNET_PBLOCK_TCP_H);
|
116
|
+
}
|
117
|
+
|
118
|
+
/* find and set the appropriate ptag, or else use the default of 0 */
|
119
|
+
offset = payload_s;
|
120
|
+
if (ptag_hold && p->prev)
|
121
|
+
{
|
122
|
+
p_temp = p->prev;
|
123
|
+
while (p_temp->prev &&
|
124
|
+
(p_temp->type != LIBNET_PBLOCK_TCPDATA) &&
|
125
|
+
(p_temp->type != LIBNET_PBLOCK_TCP_H))
|
126
|
+
{
|
127
|
+
p_temp = p_temp->prev;
|
128
|
+
}
|
129
|
+
|
130
|
+
if (p_temp->type == LIBNET_PBLOCK_TCPDATA)
|
131
|
+
{
|
132
|
+
ptag_data = p_temp->ptag;
|
133
|
+
offset -= p_temp->b_len;
|
134
|
+
p->h_len += offset;
|
135
|
+
}
|
136
|
+
else
|
137
|
+
{
|
138
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
139
|
+
"%s(): TCP data pblock not found\n", __func__);
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
/* update ip_len if present */
|
144
|
+
if (ptag_hold && p->next)
|
145
|
+
{
|
146
|
+
p_temp = p->next;
|
147
|
+
while (p_temp->next && (p_temp->type != LIBNET_PBLOCK_IPV4_H))
|
148
|
+
{
|
149
|
+
p_temp = p_temp->next;
|
150
|
+
}
|
151
|
+
if (p_temp->type == LIBNET_PBLOCK_IPV4_H)
|
152
|
+
{
|
153
|
+
ip_hdr = (struct libnet_ipv4_hdr *)p_temp->buf;
|
154
|
+
n = ntohs(ip_hdr->ip_len) + offset;
|
155
|
+
ip_hdr->ip_len = htons(n);
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
if ((payload && !payload_s) || (!payload && payload_s))
|
160
|
+
{
|
161
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
162
|
+
"%s(): payload inconsistency\n", __func__);
|
163
|
+
goto bad;
|
164
|
+
}
|
165
|
+
|
166
|
+
/* if there is a payload, add it in the context */
|
167
|
+
if (payload && payload_s)
|
168
|
+
{
|
169
|
+
/* update ptag_data with the new payload */
|
170
|
+
p_data = libnet_pblock_probe(l, ptag_data, payload_s,
|
171
|
+
LIBNET_PBLOCK_TCPDATA);
|
172
|
+
if (p_data == NULL)
|
173
|
+
{
|
174
|
+
return (-1);
|
175
|
+
}
|
176
|
+
|
177
|
+
if (libnet_pblock_append(l, p_data, payload, payload_s) == -1)
|
178
|
+
{
|
179
|
+
goto bad;
|
180
|
+
}
|
181
|
+
|
182
|
+
if (ptag_data == LIBNET_PTAG_INITIALIZER)
|
183
|
+
{
|
184
|
+
if (p_data->prev->type == LIBNET_PBLOCK_TCP_H)
|
185
|
+
{
|
186
|
+
libnet_pblock_update(l, p_data, payload_s,
|
187
|
+
LIBNET_PBLOCK_TCPDATA);
|
188
|
+
/* swap pblocks to correct the protocol order */
|
189
|
+
libnet_pblock_swap(l, p->ptag, p_data->ptag);
|
190
|
+
}
|
191
|
+
else
|
192
|
+
{
|
193
|
+
/* update without setting this as the final pblock */
|
194
|
+
p_data->type = LIBNET_PBLOCK_TCPDATA;
|
195
|
+
p_data->ptag = ++(l->ptag_state);
|
196
|
+
p_data->h_len = payload_s;
|
197
|
+
|
198
|
+
/* Adjust h_len for checksum. */
|
199
|
+
p->h_len += payload_s;
|
200
|
+
|
201
|
+
/* data was added after the initial construction */
|
202
|
+
for (p_temp = l->protocol_blocks;
|
203
|
+
p_temp->type == LIBNET_PBLOCK_TCP_H ||
|
204
|
+
p_temp->type == LIBNET_PBLOCK_TCPO_H;
|
205
|
+
p_temp = p_temp->next)
|
206
|
+
{
|
207
|
+
libnet_pblock_insert_before(l, p_temp->ptag, p_data->ptag);
|
208
|
+
break;
|
209
|
+
}
|
210
|
+
/* The end block needs to have its next pointer cleared. */
|
211
|
+
l->pblock_end->next = NULL;
|
212
|
+
}
|
213
|
+
|
214
|
+
if (p_data->prev && p_data->prev->type == LIBNET_PBLOCK_TCPO_H)
|
215
|
+
{
|
216
|
+
libnet_pblock_swap(l, p_data->prev->ptag, p_data->ptag);
|
217
|
+
}
|
218
|
+
}
|
219
|
+
}
|
220
|
+
else
|
221
|
+
{
|
222
|
+
p_data = libnet_pblock_find(l, ptag_data);
|
223
|
+
if (p_data)
|
224
|
+
{
|
225
|
+
libnet_pblock_delete(l, p_data);
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
if (sum == 0)
|
230
|
+
{
|
231
|
+
/*
|
232
|
+
* If checksum is zero, by default libnet will compute a checksum
|
233
|
+
* for the user. The programmer can override this by calling
|
234
|
+
* libnet_toggle_checksum(l, ptag, 1);
|
235
|
+
*/
|
236
|
+
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
|
237
|
+
}
|
238
|
+
return (ptag);
|
239
|
+
bad:
|
240
|
+
libnet_pblock_delete(l, p);
|
241
|
+
return (-1);
|
242
|
+
}
|
243
|
+
|
244
|
+
|
245
|
+
libnet_ptag_t
|
246
|
+
libnet_build_tcp_options(u_int8_t *options, u_int32_t options_s, libnet_t *l,
|
247
|
+
libnet_ptag_t ptag)
|
248
|
+
{
|
249
|
+
int offset, underflow;
|
250
|
+
u_int32_t i, j, n, adj_size;
|
251
|
+
libnet_pblock_t *p, *p_temp;
|
252
|
+
struct libnet_ipv4_hdr *ip_hdr;
|
253
|
+
struct libnet_tcp_hdr *tcp_hdr;
|
254
|
+
|
255
|
+
if (l == NULL)
|
256
|
+
{
|
257
|
+
return (-1);
|
258
|
+
}
|
259
|
+
|
260
|
+
underflow = 0;
|
261
|
+
offset = 0;
|
262
|
+
|
263
|
+
/* check options list size */
|
264
|
+
if (options_s > LIBNET_MAXOPTION_SIZE)
|
265
|
+
{
|
266
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
267
|
+
"%s(): options list is too large %d\n", __func__, options_s);
|
268
|
+
return (-1);
|
269
|
+
}
|
270
|
+
|
271
|
+
adj_size = options_s;
|
272
|
+
if (adj_size % 4)
|
273
|
+
{
|
274
|
+
/* size of memory block with padding */
|
275
|
+
adj_size += 4 - (options_s % 4);
|
276
|
+
}
|
277
|
+
|
278
|
+
/* if this pblock already exists, determine if there is a size diff */
|
279
|
+
if (ptag)
|
280
|
+
{
|
281
|
+
p_temp = libnet_pblock_find(l, ptag);
|
282
|
+
if (p_temp)
|
283
|
+
{
|
284
|
+
if (adj_size >= p_temp->b_len)
|
285
|
+
{
|
286
|
+
offset = adj_size - p_temp->b_len;
|
287
|
+
}
|
288
|
+
else
|
289
|
+
{
|
290
|
+
offset = p_temp->b_len - adj_size;
|
291
|
+
underflow = 1;
|
292
|
+
}
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
/*
|
297
|
+
* Find the existing protocol block if a ptag is specified, or create
|
298
|
+
* a new one.
|
299
|
+
*/
|
300
|
+
p = libnet_pblock_probe(l, ptag, adj_size, LIBNET_PBLOCK_TCPO_H);
|
301
|
+
if (p == NULL)
|
302
|
+
{
|
303
|
+
return (-1);
|
304
|
+
}
|
305
|
+
|
306
|
+
n = libnet_pblock_append(l, p, options, adj_size);
|
307
|
+
if (n == -1)
|
308
|
+
{
|
309
|
+
goto bad;
|
310
|
+
}
|
311
|
+
|
312
|
+
if (ptag && p->next)
|
313
|
+
{
|
314
|
+
p_temp = p->next;
|
315
|
+
while ((p_temp->next) && (p_temp->type != LIBNET_PBLOCK_TCP_H))
|
316
|
+
{
|
317
|
+
p_temp = p_temp->next;
|
318
|
+
}
|
319
|
+
if (p_temp->type == LIBNET_PBLOCK_TCP_H)
|
320
|
+
{
|
321
|
+
/*
|
322
|
+
* Count up number of 32-bit words in options list, padding if
|
323
|
+
* neccessary.
|
324
|
+
*/
|
325
|
+
for (i = 0, j = 0; i < p->b_len; i++)
|
326
|
+
{
|
327
|
+
(i % 4) ? j : j++;
|
328
|
+
}
|
329
|
+
tcp_hdr = (struct libnet_tcp_hdr *)p_temp->buf;
|
330
|
+
tcp_hdr->th_off = j + 5;
|
331
|
+
if (!underflow)
|
332
|
+
{
|
333
|
+
p_temp->h_len += offset;
|
334
|
+
}
|
335
|
+
else
|
336
|
+
{
|
337
|
+
p_temp->h_len -= offset;
|
338
|
+
}
|
339
|
+
}
|
340
|
+
while ((p_temp->next) && (p_temp->type != LIBNET_PBLOCK_IPV4_H))
|
341
|
+
{
|
342
|
+
p_temp = p_temp->next;
|
343
|
+
}
|
344
|
+
if (p_temp->type == LIBNET_PBLOCK_IPV4_H)
|
345
|
+
{
|
346
|
+
ip_hdr = (struct libnet_ipv4_hdr *)p_temp->buf;
|
347
|
+
if (!underflow)
|
348
|
+
{
|
349
|
+
ip_hdr->ip_len += htons(offset);
|
350
|
+
}
|
351
|
+
else
|
352
|
+
{
|
353
|
+
ip_hdr->ip_len -= htons(offset);
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}
|
357
|
+
|
358
|
+
return (ptag ? ptag : libnet_pblock_update(l, p, adj_size,
|
359
|
+
LIBNET_PBLOCK_TCPO_H));
|
360
|
+
bad:
|
361
|
+
libnet_pblock_delete(l, p);
|
362
|
+
return (-1);
|
363
|
+
}
|
364
|
+
|
365
|
+
/* EOF */
|
@@ -0,0 +1,193 @@
|
|
1
|
+
/*
|
2
|
+
* libnet
|
3
|
+
* libnet_build_token_ring.c - Token Ring (802.5) packet assembler
|
4
|
+
*
|
5
|
+
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
6
|
+
* Jason Damron <jsdamron@hushmail.com>
|
7
|
+
* All rights reserved.
|
8
|
+
*
|
9
|
+
* Redistribution and use in source and binary forms, with or without
|
10
|
+
* modification, are permitted provided that the following conditions
|
11
|
+
* are met:
|
12
|
+
* 1. Redistributions of source code must retain the above copyright
|
13
|
+
* notice, this list of conditions and the following disclaimer.
|
14
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
15
|
+
* notice, this list of conditions and the following disclaimer in the
|
16
|
+
* documentation and/or other materials provided with the distribution.
|
17
|
+
*
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
19
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
21
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
22
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
24
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
25
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
27
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
28
|
+
* SUCH DAMAGE.
|
29
|
+
*
|
30
|
+
*/
|
31
|
+
|
32
|
+
#if (HAVE_CONFIG_H)
|
33
|
+
#include "../include/config.h"
|
34
|
+
#endif
|
35
|
+
#if (!(_WIN32) || (__CYGWIN__))
|
36
|
+
#include "../include/libnet.h"
|
37
|
+
#else
|
38
|
+
#include "../include/win32/libnet.h"
|
39
|
+
#endif
|
40
|
+
|
41
|
+
libnet_ptag_t
|
42
|
+
libnet_build_token_ring(u_int8_t ac, u_int8_t fc, u_int8_t *dst, u_int8_t *src,
|
43
|
+
u_int8_t dsap, u_int8_t ssap, u_int8_t cf, u_int8_t *org, u_int16_t type,
|
44
|
+
u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
45
|
+
{
|
46
|
+
u_int32_t n, h;
|
47
|
+
libnet_pblock_t *p;
|
48
|
+
struct libnet_token_ring_hdr token_ring_hdr;
|
49
|
+
|
50
|
+
if (l == NULL)
|
51
|
+
{
|
52
|
+
return (-1);
|
53
|
+
}
|
54
|
+
|
55
|
+
/* sanity check injection type if we're not in advanced mode */
|
56
|
+
if (l->injection_type != LIBNET_LINK &&
|
57
|
+
!(((l->injection_type) & LIBNET_ADV_MASK)))
|
58
|
+
{
|
59
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
60
|
+
"%s(): called with non-link layer wire injection primitive\n",
|
61
|
+
__func__);
|
62
|
+
p = NULL;
|
63
|
+
goto bad;
|
64
|
+
}
|
65
|
+
|
66
|
+
n = LIBNET_TOKEN_RING_H + payload_s;
|
67
|
+
h = 0;
|
68
|
+
|
69
|
+
/*
|
70
|
+
* Find the existing protocol block if a ptag is specified, or create
|
71
|
+
* a new one.
|
72
|
+
*/
|
73
|
+
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_TOKEN_RING_H);
|
74
|
+
if (p == NULL)
|
75
|
+
{
|
76
|
+
return (-1);
|
77
|
+
}
|
78
|
+
|
79
|
+
memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
|
80
|
+
token_ring_hdr.token_ring_access_control = ac;
|
81
|
+
token_ring_hdr.token_ring_frame_control = fc;
|
82
|
+
memcpy(token_ring_hdr.token_ring_dhost, dst, TOKEN_RING_ADDR_LEN);
|
83
|
+
memcpy(token_ring_hdr.token_ring_shost, src, TOKEN_RING_ADDR_LEN);
|
84
|
+
token_ring_hdr.token_ring_llc_dsap = dsap;
|
85
|
+
token_ring_hdr.token_ring_llc_ssap = ssap;
|
86
|
+
token_ring_hdr.token_ring_llc_control_field = cf;
|
87
|
+
memcpy(&token_ring_hdr.token_ring_llc_org_code, org, LIBNET_ORG_CODE_SIZE);
|
88
|
+
token_ring_hdr.token_ring_type = htons(type);
|
89
|
+
|
90
|
+
n = libnet_pblock_append(l, p, (u_int8_t *)&token_ring_hdr,
|
91
|
+
LIBNET_TOKEN_RING_H);
|
92
|
+
if (n == -1)
|
93
|
+
{
|
94
|
+
goto bad;
|
95
|
+
}
|
96
|
+
|
97
|
+
if ((payload && !payload_s) || (!payload && payload_s))
|
98
|
+
{
|
99
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
100
|
+
"%s(): payload inconsistency\n", __func__);
|
101
|
+
goto bad;
|
102
|
+
}
|
103
|
+
|
104
|
+
if (payload && payload_s)
|
105
|
+
{
|
106
|
+
n = libnet_pblock_append(l, p, payload, payload_s);
|
107
|
+
if (n == -1)
|
108
|
+
{
|
109
|
+
goto bad;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
114
|
+
LIBNET_PBLOCK_TOKEN_RING_H));
|
115
|
+
bad:
|
116
|
+
libnet_pblock_delete(l, p);
|
117
|
+
return (-1);
|
118
|
+
}
|
119
|
+
|
120
|
+
|
121
|
+
libnet_ptag_t
|
122
|
+
libnet_autobuild_token_ring(u_int8_t ac, u_int8_t fc, u_int8_t *dst,
|
123
|
+
u_int8_t dsap, u_int8_t ssap, u_int8_t cf, u_int8_t *org, u_int16_t type,
|
124
|
+
libnet_t *l)
|
125
|
+
{
|
126
|
+
u_int32_t n, h;
|
127
|
+
struct libnet_token_ring_addr *src;
|
128
|
+
libnet_pblock_t *p;
|
129
|
+
libnet_ptag_t ptag;
|
130
|
+
struct libnet_token_ring_hdr token_ring_hdr;
|
131
|
+
|
132
|
+
if (l == NULL)
|
133
|
+
{
|
134
|
+
return (-1);
|
135
|
+
}
|
136
|
+
|
137
|
+
/* sanity check injection type if we're not in advanced mode */
|
138
|
+
if (l->injection_type != LIBNET_LINK &&
|
139
|
+
!(((l->injection_type) & LIBNET_ADV_MASK)))
|
140
|
+
{
|
141
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
142
|
+
"%s(): called with non-link layer wire injection primitive\n",
|
143
|
+
__func__);
|
144
|
+
p = NULL;
|
145
|
+
goto bad;
|
146
|
+
}
|
147
|
+
|
148
|
+
n = LIBNET_TOKEN_RING_H;
|
149
|
+
h = 0;
|
150
|
+
ptag = LIBNET_PTAG_INITIALIZER;
|
151
|
+
|
152
|
+
/* Token Ring and Ethernet have the same address size - so just typecast */
|
153
|
+
src = (struct libnet_token_ring_addr *) libnet_get_hwaddr(l);
|
154
|
+
if (src == NULL)
|
155
|
+
{
|
156
|
+
/* err msg set in libnet_get_hwaddr() */
|
157
|
+
return (-1);
|
158
|
+
}
|
159
|
+
|
160
|
+
/*
|
161
|
+
* Create a new pblock.
|
162
|
+
*/
|
163
|
+
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_TOKEN_RING_H);
|
164
|
+
if (p == NULL)
|
165
|
+
{
|
166
|
+
return (-1);
|
167
|
+
}
|
168
|
+
|
169
|
+
memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
|
170
|
+
token_ring_hdr.token_ring_access_control = ac;
|
171
|
+
token_ring_hdr.token_ring_frame_control = fc;
|
172
|
+
memcpy(token_ring_hdr.token_ring_dhost, dst, TOKEN_RING_ADDR_LEN);
|
173
|
+
memcpy(token_ring_hdr.token_ring_shost, src, TOKEN_RING_ADDR_LEN);
|
174
|
+
token_ring_hdr.token_ring_llc_dsap = dsap;
|
175
|
+
token_ring_hdr.token_ring_llc_ssap = ssap;
|
176
|
+
token_ring_hdr.token_ring_llc_control_field = cf;
|
177
|
+
memcpy(&token_ring_hdr.token_ring_llc_org_code, org, LIBNET_ORG_CODE_SIZE);
|
178
|
+
token_ring_hdr.token_ring_type = htons(type);
|
179
|
+
|
180
|
+
|
181
|
+
n = libnet_pblock_append(l, p, (u_int8_t *)&token_ring_hdr,
|
182
|
+
LIBNET_TOKEN_RING_H);
|
183
|
+
if (n == -1)
|
184
|
+
{
|
185
|
+
goto bad;
|
186
|
+
}
|
187
|
+
|
188
|
+
return (libnet_pblock_update(l, p, h, LIBNET_PBLOCK_TOKEN_RING_H));
|
189
|
+
bad:
|
190
|
+
libnet_pblock_delete(l, p);
|
191
|
+
return (-1);
|
192
|
+
}
|
193
|
+
/* EOF */
|
@@ -0,0 +1,111 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: libnet_build_udp.c,v 1.10 2004/01/21 19:01:29 mike Exp $
|
3
|
+
*
|
4
|
+
* libnet
|
5
|
+
* libnet_build_udp.c - UDP packet assembler
|
6
|
+
*
|
7
|
+
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
8
|
+
* All rights reserved.
|
9
|
+
*
|
10
|
+
* Redistribution and use in source and binary forms, with or without
|
11
|
+
* modification, are permitted provided that the following conditions
|
12
|
+
* are met:
|
13
|
+
* 1. Redistributions of source code must retain the above copyright
|
14
|
+
* notice, this list of conditions and the following disclaimer.
|
15
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
* notice, this list of conditions and the following disclaimer in the
|
17
|
+
* documentation and/or other materials provided with the distribution.
|
18
|
+
*
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
20
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
22
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
23
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
25
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
26
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
28
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
29
|
+
* SUCH DAMAGE.
|
30
|
+
*
|
31
|
+
*/
|
32
|
+
|
33
|
+
#if (HAVE_CONFIG_H)
|
34
|
+
#include "../include/config.h"
|
35
|
+
#endif
|
36
|
+
#if (!(_WIN32) || (__CYGWIN__))
|
37
|
+
#include "../include/libnet.h"
|
38
|
+
#else
|
39
|
+
#include "../include/win32/libnet.h"
|
40
|
+
#endif
|
41
|
+
|
42
|
+
libnet_ptag_t
|
43
|
+
libnet_build_udp(u_int16_t sp, u_int16_t dp, u_int16_t len, u_int16_t sum,
|
44
|
+
u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
45
|
+
{
|
46
|
+
u_int32_t n, h;
|
47
|
+
libnet_pblock_t *p;
|
48
|
+
struct libnet_udp_hdr udp_hdr;
|
49
|
+
|
50
|
+
if (l == NULL)
|
51
|
+
{
|
52
|
+
return (-1);
|
53
|
+
}
|
54
|
+
|
55
|
+
n = LIBNET_UDP_H + payload_s; /* size of memory block */
|
56
|
+
h = len; /* header length (for cksum) */
|
57
|
+
|
58
|
+
/*
|
59
|
+
* Find the existing protocol block if a ptag is specified, or create
|
60
|
+
* a new one.
|
61
|
+
*/
|
62
|
+
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_UDP_H);
|
63
|
+
if (p == NULL)
|
64
|
+
{
|
65
|
+
return (-1);
|
66
|
+
}
|
67
|
+
|
68
|
+
memset(&udp_hdr, 0, sizeof(udp_hdr));
|
69
|
+
udp_hdr.uh_sport = htons(sp); /* source port */
|
70
|
+
udp_hdr.uh_dport = htons(dp); /* destination port */
|
71
|
+
udp_hdr.uh_ulen = htons(h); /* total length of UDP packet*/
|
72
|
+
udp_hdr.uh_sum = (sum ? htons(sum) : 0);/* checksum */
|
73
|
+
|
74
|
+
n = libnet_pblock_append(l, p, (u_int8_t *)&udp_hdr, LIBNET_UDP_H);
|
75
|
+
if (n == -1)
|
76
|
+
{
|
77
|
+
goto bad;
|
78
|
+
}
|
79
|
+
|
80
|
+
if ((payload && !payload_s) || (!payload && payload_s))
|
81
|
+
{
|
82
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
83
|
+
"%s(): payload inconsistency\n", __func__);
|
84
|
+
goto bad;
|
85
|
+
}
|
86
|
+
|
87
|
+
if (payload && payload_s)
|
88
|
+
{
|
89
|
+
n = libnet_pblock_append(l, p, payload, payload_s);
|
90
|
+
if (n == -1)
|
91
|
+
{
|
92
|
+
goto bad;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
if (sum == 0)
|
97
|
+
{
|
98
|
+
/*
|
99
|
+
* If checksum is zero, by default libnet will compute a checksum
|
100
|
+
* for the user. The programmer can override this by calling
|
101
|
+
* libnet_toggle_checksum(l, ptag, 1);
|
102
|
+
*/
|
103
|
+
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
|
104
|
+
}
|
105
|
+
return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_UDP_H));
|
106
|
+
bad:
|
107
|
+
libnet_pblock_delete(l, p);
|
108
|
+
return (-1);
|
109
|
+
}
|
110
|
+
|
111
|
+
/* EOF */
|