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
data/ext/libnet_arp.c
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
#include <arpa/inet.h>
|
2
|
+
#include <ruby.h>
|
3
|
+
#include <libnet.h>
|
4
|
+
|
5
|
+
extern VALUE cLibnet;
|
6
|
+
static VALUE cARP;
|
7
|
+
|
8
|
+
static VALUE net_s_decode_arp(VALUE self, VALUE bytes)
|
9
|
+
{
|
10
|
+
VALUE arp_obj;
|
11
|
+
VALUE str;
|
12
|
+
VALUE payload;
|
13
|
+
struct libnet_arp_hdr *arp;
|
14
|
+
u_int16_t ar_hrd;
|
15
|
+
u_int16_t ar_pro;
|
16
|
+
u_int8_t ar_hln;
|
17
|
+
u_int8_t ar_pln;
|
18
|
+
u_int16_t ar_op;
|
19
|
+
int msg_len;
|
20
|
+
int offset;
|
21
|
+
|
22
|
+
arp_obj = rb_funcall(self, rb_intern("new"), 0);
|
23
|
+
|
24
|
+
str = StringValue(bytes);
|
25
|
+
|
26
|
+
if (RSTRING(str)->len < LIBNET_ARP_H) {
|
27
|
+
rb_raise(rb_eArgError, "string is too small to contain an ARP header");
|
28
|
+
}
|
29
|
+
|
30
|
+
arp = (struct libnet_arp_hdr *)RSTRING(str)->ptr;
|
31
|
+
|
32
|
+
ar_hrd = ntohs(arp->ar_hrd);
|
33
|
+
ar_pro = ntohs(arp->ar_pro);
|
34
|
+
ar_hln = arp->ar_hln;
|
35
|
+
ar_pln = arp->ar_pln;
|
36
|
+
ar_op = ntohs(arp->ar_op);
|
37
|
+
|
38
|
+
rb_iv_set(arp_obj, "@htype", UINT2NUM(ar_hrd));
|
39
|
+
rb_iv_set(arp_obj, "@protocol", UINT2NUM(ar_pro));
|
40
|
+
rb_iv_set(arp_obj, "@hlen", UINT2NUM(ar_hln));
|
41
|
+
rb_iv_set(arp_obj, "@plen", UINT2NUM(ar_pln));
|
42
|
+
rb_iv_set(arp_obj, "@operation", UINT2NUM(ar_op));
|
43
|
+
|
44
|
+
msg_len = LIBNET_ARP_H + (2 * ar_hln) + (2 * ar_pln);
|
45
|
+
|
46
|
+
if (RSTRING(str)->len < msg_len) {
|
47
|
+
rb_raise(rb_eArgError, "string is too small to contain an ARP message");
|
48
|
+
}
|
49
|
+
|
50
|
+
/* unpack the addresses */
|
51
|
+
offset = LIBNET_ARP_H;
|
52
|
+
rb_iv_set(arp_obj, "@sender_haddr", rb_str_new(RSTRING(str)->ptr + offset, ar_hln));
|
53
|
+
offset += ar_hln;
|
54
|
+
rb_iv_set(arp_obj, "@sender_paddr", rb_str_new(RSTRING(str)->ptr + offset, ar_pln));
|
55
|
+
offset += ar_pln;
|
56
|
+
rb_iv_set(arp_obj, "@target_haddr", rb_str_new(RSTRING(str)->ptr + offset, ar_hln));
|
57
|
+
offset += ar_hln;
|
58
|
+
rb_iv_set(arp_obj, "@target_paddr", rb_str_new(RSTRING(str)->ptr + offset, ar_pln));
|
59
|
+
offset += ar_pln;
|
60
|
+
|
61
|
+
/* unpack the payload */
|
62
|
+
if (RSTRING(str)->len > msg_len) {
|
63
|
+
payload = rb_str_new(RSTRING(str)->ptr + msg_len,
|
64
|
+
RSTRING(str)->len - msg_len);
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
payload = Qnil;
|
68
|
+
}
|
69
|
+
|
70
|
+
rb_iv_set(arp_obj, "@payload", payload);
|
71
|
+
|
72
|
+
return arp_obj;
|
73
|
+
}
|
74
|
+
|
75
|
+
static VALUE net_build_arp(int argc, VALUE *argv, VALUE self)
|
76
|
+
{
|
77
|
+
libnet_t *l;
|
78
|
+
VALUE arp_obj, v;
|
79
|
+
u_int16_t hrd, pro, op;
|
80
|
+
u_int8_t hln, pln;
|
81
|
+
VALUE sha, spa, tha, tpa;
|
82
|
+
char *payload = NULL;
|
83
|
+
u_int32_t payload_s = 0;
|
84
|
+
VALUE ptag_obj;
|
85
|
+
libnet_ptag_t ptag = 0;
|
86
|
+
|
87
|
+
rb_scan_args(argc, argv, "01", &arp_obj);
|
88
|
+
|
89
|
+
Data_Get_Struct(self, libnet_t, l);
|
90
|
+
|
91
|
+
if (NIL_P(arp_obj)) {
|
92
|
+
arp_obj = rb_class_new_instance(0, NULL, cARP);
|
93
|
+
rb_yield(arp_obj);
|
94
|
+
}
|
95
|
+
|
96
|
+
/* check that all required fields are set */
|
97
|
+
rb_funcall(arp_obj, rb_intern("check_packable"), 0);
|
98
|
+
|
99
|
+
v = rb_funcall(arp_obj, rb_intern("htype"), 0);
|
100
|
+
hrd = NUM2UINT(v);
|
101
|
+
v = rb_funcall(arp_obj, rb_intern("protocol"), 0);
|
102
|
+
pro = NUM2UINT(v);
|
103
|
+
v = rb_funcall(arp_obj, rb_intern("hlen"), 0);
|
104
|
+
hln = NUM2UINT(v);
|
105
|
+
v = rb_funcall(arp_obj, rb_intern("plen"), 0);
|
106
|
+
pln = NUM2UINT(v);
|
107
|
+
v = rb_funcall(arp_obj, rb_intern("operation"), 0);
|
108
|
+
op = NUM2UINT(v);
|
109
|
+
v = rb_funcall(arp_obj, rb_intern("sender_haddr"), 0);
|
110
|
+
sha = StringValue(v);
|
111
|
+
v = rb_funcall(arp_obj, rb_intern("sender_paddr"), 0);
|
112
|
+
spa = StringValue(v);
|
113
|
+
v = rb_funcall(arp_obj, rb_intern("target_haddr"), 0);
|
114
|
+
tha = StringValue(v);
|
115
|
+
v = rb_funcall(arp_obj, rb_intern("target_paddr"), 0);
|
116
|
+
tpa = StringValue(v);
|
117
|
+
|
118
|
+
/* get the payload */
|
119
|
+
if ((v = rb_funcall(arp_obj, rb_intern("payload"), 0)) != Qnil) {
|
120
|
+
v = StringValue(v);
|
121
|
+
payload = RSTRING(v)->ptr;
|
122
|
+
payload_s = RSTRING(v)->len;
|
123
|
+
}
|
124
|
+
|
125
|
+
ptag_obj = rb_iv_get(arp_obj, "@ptag");
|
126
|
+
|
127
|
+
if (!NIL_P(ptag_obj)) {
|
128
|
+
ptag = NUM2LONG(ptag_obj);
|
129
|
+
}
|
130
|
+
|
131
|
+
ptag = libnet_build_arp(hrd, pro, hln, pln, op, RSTRING(sha)->ptr,
|
132
|
+
RSTRING(spa)->ptr, RSTRING(tha)->ptr, RSTRING(tpa)->ptr,
|
133
|
+
payload, payload_s, l, ptag);
|
134
|
+
|
135
|
+
if (ptag == -1) {
|
136
|
+
rb_raise(rb_eRuntimeError, libnet_geterror(l));
|
137
|
+
}
|
138
|
+
|
139
|
+
rb_iv_set(arp_obj, "@ptag", LONG2NUM(ptag));
|
140
|
+
|
141
|
+
return arp_obj;
|
142
|
+
}
|
143
|
+
|
144
|
+
void define_arp_methods()
|
145
|
+
{
|
146
|
+
cARP = rb_const_get(cLibnet, rb_intern("ARP"));
|
147
|
+
|
148
|
+
rb_define_singleton_method(cARP, "decode", net_s_decode_arp, 1);
|
149
|
+
|
150
|
+
rb_define_method(cLibnet, "build_arp", net_build_arp, -1);
|
151
|
+
}
|
152
|
+
|
@@ -0,0 +1,115 @@
|
|
1
|
+
#include <arpa/inet.h>
|
2
|
+
#include <ruby.h>
|
3
|
+
#include <libnet.h>
|
4
|
+
|
5
|
+
extern VALUE cLibnet;
|
6
|
+
|
7
|
+
static VALUE net_s_decode_ethernet(VALUE self, VALUE bytes)
|
8
|
+
{
|
9
|
+
VALUE eth_obj;
|
10
|
+
VALUE str;
|
11
|
+
VALUE payload;
|
12
|
+
struct libnet_ethernet_hdr *eth;
|
13
|
+
u_int16_t ether_type;
|
14
|
+
|
15
|
+
eth_obj = rb_funcall(self, rb_intern("new"), 0);
|
16
|
+
|
17
|
+
str = StringValue(bytes);
|
18
|
+
|
19
|
+
if (RSTRING(str)->len < LIBNET_ETH_H) {
|
20
|
+
rb_raise(rb_eArgError, "string is too small to contain an Ethernet header");
|
21
|
+
}
|
22
|
+
|
23
|
+
eth = (struct libnet_ethernet_hdr *)RSTRING(str)->ptr;
|
24
|
+
|
25
|
+
ether_type = ntohs(eth->ether_type);
|
26
|
+
|
27
|
+
/*
|
28
|
+
rb_iv_set(eth_obj, "@dst", rb_str_new(eth->ether_dhost, ETHER_ADDR_LEN));
|
29
|
+
rb_iv_set(eth_obj, "@src", rb_str_new(eth->ether_shost, ETHER_ADDR_LEN));
|
30
|
+
rb_iv_set(eth_obj, "@type", UINT2NUM(ether_type));
|
31
|
+
*/
|
32
|
+
rb_funcall(eth_obj, rb_intern("dst="), 1, rb_str_new((char *)eth->ether_dhost, ETHER_ADDR_LEN));
|
33
|
+
rb_funcall(eth_obj, rb_intern("src="), 1, rb_str_new((char *)eth->ether_shost, ETHER_ADDR_LEN));
|
34
|
+
rb_funcall(eth_obj, rb_intern("type="), 1, UINT2NUM(ether_type));
|
35
|
+
|
36
|
+
if (RSTRING(str)->len > LIBNET_ETH_H) {
|
37
|
+
payload = rb_str_new(RSTRING(str)->ptr + LIBNET_ETH_H,
|
38
|
+
RSTRING(str)->len - LIBNET_ETH_H);
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
payload = Qnil;
|
42
|
+
}
|
43
|
+
|
44
|
+
//rb_iv_set(eth_obj, "@payload", payload);
|
45
|
+
rb_funcall(eth_obj, rb_intern("payload="), 1, payload);
|
46
|
+
|
47
|
+
return eth_obj;
|
48
|
+
}
|
49
|
+
|
50
|
+
static VALUE net_build_ethernet(int argc, VALUE *argv, VALUE self)
|
51
|
+
{
|
52
|
+
libnet_t *l;
|
53
|
+
VALUE eth, v;
|
54
|
+
u_int8_t *dst = NULL;
|
55
|
+
u_int8_t *src = NULL;
|
56
|
+
u_int16_t type;
|
57
|
+
char *payload = NULL;
|
58
|
+
u_int32_t payload_s = 0;
|
59
|
+
VALUE ptag_obj;
|
60
|
+
libnet_ptag_t ptag = 0;
|
61
|
+
|
62
|
+
rb_scan_args(argc, argv, "01", ð);
|
63
|
+
|
64
|
+
Data_Get_Struct(self, libnet_t, l);
|
65
|
+
|
66
|
+
if (NIL_P(eth)) {
|
67
|
+
eth = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("Ethernet")));
|
68
|
+
rb_yield(eth);
|
69
|
+
}
|
70
|
+
|
71
|
+
/* check that all required fields are set */
|
72
|
+
rb_funcall(eth, rb_intern("check_packable"), 0);
|
73
|
+
|
74
|
+
/* required parameters */
|
75
|
+
v = rb_funcall(eth, rb_intern("dst"), 0);
|
76
|
+
dst = (u_int8_t *)StringValuePtr(v);
|
77
|
+
v = rb_funcall(eth, rb_intern("type"), 0);
|
78
|
+
type = NUM2UINT(v);
|
79
|
+
|
80
|
+
/* optional parameters */
|
81
|
+
v = rb_funcall(eth, rb_intern("src"), 0);
|
82
|
+
src = (u_int8_t *)StringValuePtr(v);
|
83
|
+
|
84
|
+
if ((v = rb_funcall(eth, rb_intern("payload"), 0)) != Qnil) {
|
85
|
+
v = StringValue(v);
|
86
|
+
payload = RSTRING(v)->ptr;
|
87
|
+
payload_s = RSTRING(v)->len;
|
88
|
+
}
|
89
|
+
|
90
|
+
ptag_obj = rb_iv_get(eth, "@ptag");
|
91
|
+
|
92
|
+
if (!NIL_P(ptag_obj)) {
|
93
|
+
ptag = NUM2LONG(ptag_obj);
|
94
|
+
}
|
95
|
+
|
96
|
+
ptag = libnet_build_ethernet(dst, src, type, (u_int8_t *)payload,
|
97
|
+
payload_s, l, ptag);
|
98
|
+
|
99
|
+
if (ptag == -1) {
|
100
|
+
rb_raise(rb_eRuntimeError, libnet_geterror(l));
|
101
|
+
}
|
102
|
+
|
103
|
+
rb_iv_set(eth, "@ptag", LONG2NUM(ptag));
|
104
|
+
|
105
|
+
return eth;
|
106
|
+
}
|
107
|
+
|
108
|
+
void define_ethernet_methods()
|
109
|
+
{
|
110
|
+
VALUE cEthernet = rb_const_get(cLibnet, rb_intern("Ethernet"));
|
111
|
+
|
112
|
+
rb_define_singleton_method(cEthernet, "decode", net_s_decode_ethernet, 1);
|
113
|
+
|
114
|
+
rb_define_method(cLibnet, "build_ethernet", net_build_ethernet, -1);
|
115
|
+
}
|
data/ext/libnet_ipv4.c
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
#include <arpa/inet.h>
|
2
|
+
#include <ruby.h>
|
3
|
+
#include <libnet.h>
|
4
|
+
|
5
|
+
extern VALUE cLibnet;
|
6
|
+
|
7
|
+
static VALUE net_s_decode_ipv4(VALUE self, VALUE bytes)
|
8
|
+
{
|
9
|
+
VALUE ip_obj;
|
10
|
+
VALUE str;
|
11
|
+
VALUE payload;
|
12
|
+
struct libnet_ipv4_hdr *ip;
|
13
|
+
u_int8_t ip_hl, ip_v, ip_tos, ip_ttl, ip_p, ip_sum;
|
14
|
+
u_int16_t ip_len, ip_id, ip_off;
|
15
|
+
u_int32_t ip_src, ip_dst;
|
16
|
+
|
17
|
+
ip_obj = rb_funcall(self, rb_intern("new"), 0);
|
18
|
+
|
19
|
+
str = StringValue(bytes);
|
20
|
+
|
21
|
+
if (RSTRING(str)->len < LIBNET_IPV4_H) {
|
22
|
+
rb_raise(rb_eArgError, "string is too small to contain an IPv4 header");
|
23
|
+
}
|
24
|
+
|
25
|
+
ip = (struct libnet_ipv4_hdr *)RSTRING(str)->ptr;
|
26
|
+
|
27
|
+
ip_hl = ip->ip_hl;
|
28
|
+
ip_v = ip->ip_v;
|
29
|
+
ip_tos = ip->ip_tos;
|
30
|
+
ip_len = ntohs(ip->ip_len);
|
31
|
+
ip_id = ntohs(ip->ip_id);
|
32
|
+
ip_off = ntohs(ip->ip_off);
|
33
|
+
ip_ttl = ip->ip_ttl;
|
34
|
+
ip_p = ip->ip_p;
|
35
|
+
ip_sum = ntohs(ip->ip_sum);
|
36
|
+
ip_src = ntohl(ip->ip_src.s_addr);
|
37
|
+
ip_dst = ntohl(ip->ip_dst.s_addr);
|
38
|
+
|
39
|
+
rb_iv_set(ip_obj, "@version", UINT2NUM(ip_v));
|
40
|
+
rb_iv_set(ip_obj, "@ihl", UINT2NUM(ip_hl));
|
41
|
+
rb_iv_set(ip_obj, "@tos", UINT2NUM(ip_tos));
|
42
|
+
rb_iv_set(ip_obj, "@length", UINT2NUM(ip_len));
|
43
|
+
rb_iv_set(ip_obj, "@id", UINT2NUM(ip_id));
|
44
|
+
rb_iv_set(ip_obj, "@frag_off", UINT2NUM(ip_off));
|
45
|
+
rb_iv_set(ip_obj, "@ttl", UINT2NUM(ip_ttl));
|
46
|
+
rb_iv_set(ip_obj, "@protocol", UINT2NUM(ip_p));
|
47
|
+
rb_iv_set(ip_obj, "@checksum", UINT2NUM(ip_sum));
|
48
|
+
rb_iv_set(ip_obj, "@src_ip", ULONG2NUM(ip_src));
|
49
|
+
rb_iv_set(ip_obj, "@dst_ip", ULONG2NUM(ip_dst));
|
50
|
+
|
51
|
+
if (RSTRING(str)->len >= ip_len) {
|
52
|
+
payload = rb_str_new(RSTRING(str)->ptr + LIBNET_IPV4_H,
|
53
|
+
ip_len - LIBNET_IPV4_H);
|
54
|
+
}
|
55
|
+
else {
|
56
|
+
payload = Qnil;
|
57
|
+
}
|
58
|
+
|
59
|
+
rb_iv_set(ip_obj, "@payload", payload);
|
60
|
+
|
61
|
+
return ip_obj;
|
62
|
+
}
|
63
|
+
|
64
|
+
static VALUE net_build_ipv4(int argc, VALUE *argv, VALUE self)
|
65
|
+
{
|
66
|
+
libnet_t *l;
|
67
|
+
VALUE ipv4, v;
|
68
|
+
u_int8_t tos = 0, ttl = 0, prot = 0;
|
69
|
+
u_int16_t len = 0, id = 0, frag = 0, sum = 0;
|
70
|
+
u_int32_t src = 0, dst = 0;
|
71
|
+
char *payload = NULL;
|
72
|
+
u_int32_t payload_s = 0;
|
73
|
+
VALUE ptag_obj;
|
74
|
+
libnet_ptag_t ptag = 0;
|
75
|
+
|
76
|
+
rb_scan_args(argc, argv, "01", &ipv4);
|
77
|
+
|
78
|
+
Data_Get_Struct(self, libnet_t, l);
|
79
|
+
|
80
|
+
if (NIL_P(ipv4)) {
|
81
|
+
ipv4 = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("IPv4")));
|
82
|
+
rb_yield(ipv4);
|
83
|
+
}
|
84
|
+
|
85
|
+
/* check that all required fields are set */
|
86
|
+
rb_funcall(ipv4, rb_intern("check_packable"), 0);
|
87
|
+
|
88
|
+
v = rb_funcall(ipv4, rb_intern("length"), 0);
|
89
|
+
len = NUM2UINT(v);
|
90
|
+
v = rb_funcall(ipv4, rb_intern("protocol"), 0);
|
91
|
+
prot = NUM2UINT(v);
|
92
|
+
v = rb_funcall(ipv4, rb_intern("tos"), 0);
|
93
|
+
tos = NUM2UINT(v);
|
94
|
+
v = rb_funcall(ipv4, rb_intern("id"), 0);
|
95
|
+
id = NUM2UINT(v);
|
96
|
+
v = rb_funcall(ipv4, rb_intern("frag_off"), 0);
|
97
|
+
frag = NUM2UINT(v);
|
98
|
+
v = rb_funcall(ipv4, rb_intern("ttl"), 0);
|
99
|
+
ttl = NUM2UINT(v);
|
100
|
+
v = rb_funcall(ipv4, rb_intern("checksum"), 0);
|
101
|
+
sum = NUM2UINT(v);
|
102
|
+
v = rb_funcall(ipv4, rb_intern("src_ip"), 0);
|
103
|
+
src = htonl(NUM2ULONG(v));
|
104
|
+
v = rb_funcall(ipv4, rb_intern("dst_ip"), 0);
|
105
|
+
dst = htonl(NUM2ULONG(v));
|
106
|
+
|
107
|
+
/* payload is optional */
|
108
|
+
if ((v = rb_funcall(ipv4, rb_intern("payload"), 0)) != Qnil) {
|
109
|
+
v = StringValue(v);
|
110
|
+
payload = RSTRING(v)->ptr;
|
111
|
+
payload_s = RSTRING(v)->len;
|
112
|
+
}
|
113
|
+
|
114
|
+
ptag_obj = rb_iv_get(ipv4, "@ptag");
|
115
|
+
|
116
|
+
if (!NIL_P(ptag_obj)) {
|
117
|
+
ptag = NUM2LONG(ptag_obj);
|
118
|
+
}
|
119
|
+
|
120
|
+
ptag = libnet_build_ipv4(len, tos, id, frag, ttl, prot, sum, src, dst,
|
121
|
+
(u_int8_t *)payload, payload_s, l, ptag);
|
122
|
+
|
123
|
+
if (ptag == -1) {
|
124
|
+
rb_raise(rb_eRuntimeError, libnet_geterror(l));
|
125
|
+
}
|
126
|
+
|
127
|
+
rb_iv_set(ipv4, "@ptag", LONG2NUM(ptag));
|
128
|
+
|
129
|
+
return ipv4;
|
130
|
+
}
|
131
|
+
|
132
|
+
void define_ipv4_methods()
|
133
|
+
{
|
134
|
+
VALUE cIPv4 = rb_const_get(cLibnet, rb_intern("IPv4"));
|
135
|
+
|
136
|
+
rb_define_singleton_method(cIPv4, "decode", net_s_decode_ipv4, 1);
|
137
|
+
|
138
|
+
rb_define_method(cLibnet, "build_ipv4", net_build_ipv4, -1);
|
139
|
+
//rb_define_method(cLibnet, "autobuild_ipv4", net_autobuild_ipv4, 0);
|
140
|
+
}
|
data/ext/libnet_ipv6.c
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
#include <arpa/inet.h>
|
2
|
+
#include <ruby.h>
|
3
|
+
#include <libnet.h>
|
4
|
+
|
5
|
+
extern VALUE cLibnet;
|
6
|
+
|
7
|
+
static VALUE net_s_decode_ipv6(VALUE self, VALUE bytes)
|
8
|
+
{
|
9
|
+
VALUE ip_obj;
|
10
|
+
VALUE str;
|
11
|
+
VALUE payload;
|
12
|
+
struct libnet_ipv6_hdr *ip;
|
13
|
+
u_int8_t ip_v, ip_tc;
|
14
|
+
u_int32_t ip_fl;
|
15
|
+
u_int16_t ip_len;
|
16
|
+
u_int8_t ip_nh;
|
17
|
+
u_int8_t ip_hl;
|
18
|
+
|
19
|
+
ip_obj = rb_funcall(self, rb_intern("new"), 0);
|
20
|
+
|
21
|
+
str = StringValue(bytes);
|
22
|
+
|
23
|
+
if (RSTRING(str)->len < LIBNET_IPV6_H) {
|
24
|
+
rb_raise(rb_eArgError, "string is too small to contain an IPv6 header");
|
25
|
+
}
|
26
|
+
|
27
|
+
ip = (struct libnet_ipv6_hdr *)RSTRING(str)->ptr;
|
28
|
+
|
29
|
+
ip_v = (ip->ip_flags[0] & 0xf0) >> 4;
|
30
|
+
ip_tc = (ip->ip_flags[0] & 0x0f) << 4;
|
31
|
+
ip_tc |= (ip->ip_flags[1] & 0xf0) >> 4;
|
32
|
+
ip_fl = (ip->ip_flags[1] & 0x0f) << 16;
|
33
|
+
ip_fl |= (ip->ip_flags[2]) << 8;
|
34
|
+
ip_fl |= (ip->ip_flags[3]);
|
35
|
+
ip_len = ntohs(ip->ip_len);
|
36
|
+
ip_nh = ip->ip_nh;
|
37
|
+
ip_hl = ip->ip_hl;
|
38
|
+
|
39
|
+
rb_iv_set(ip_obj, "@version", UINT2NUM(ip_v));
|
40
|
+
rb_iv_set(ip_obj, "@traffic_class", UINT2NUM(ip_tc));
|
41
|
+
rb_iv_set(ip_obj, "@flow_label", ULONG2NUM(ip_fl));
|
42
|
+
rb_iv_set(ip_obj, "@length", UINT2NUM(ip_len));
|
43
|
+
rb_iv_set(ip_obj, "@next_header", UINT2NUM(ip_nh));
|
44
|
+
rb_iv_set(ip_obj, "@hop_limit", UINT2NUM(ip_hl));
|
45
|
+
rb_iv_set(ip_obj, "@src_ip", rb_str_new((u_int8_t *)&ip->ip_src,
|
46
|
+
sizeof(ip->ip_src)));
|
47
|
+
rb_iv_set(ip_obj, "@dst_ip", rb_str_new((u_int8_t *)&ip->ip_dst,
|
48
|
+
sizeof(ip->ip_dst)));
|
49
|
+
|
50
|
+
if (RSTRING(str)->len >= LIBNET_IPV6_H + ip_len) {
|
51
|
+
payload = rb_str_new(RSTRING(str)->ptr + LIBNET_IPV6_H,
|
52
|
+
ip_len);
|
53
|
+
}
|
54
|
+
else {
|
55
|
+
payload = Qnil;
|
56
|
+
}
|
57
|
+
|
58
|
+
rb_iv_set(ip_obj, "@payload", payload);
|
59
|
+
|
60
|
+
return ip_obj;
|
61
|
+
}
|
62
|
+
|
63
|
+
static VALUE net_build_ipv6(int argc, VALUE *argv, VALUE self)
|
64
|
+
{
|
65
|
+
libnet_t *l;
|
66
|
+
VALUE ipv6, v;
|
67
|
+
u_int8_t tc, nh, hl;
|
68
|
+
u_int16_t len;
|
69
|
+
u_int32_t fl;
|
70
|
+
struct libnet_in6_addr src, dst;
|
71
|
+
u_int8_t *payload = NULL;
|
72
|
+
u_int32_t payload_s = 0;
|
73
|
+
VALUE ptag_obj;
|
74
|
+
libnet_ptag_t ptag = 0;
|
75
|
+
|
76
|
+
rb_scan_args(argc, argv, "01", &ipv6);
|
77
|
+
|
78
|
+
Data_Get_Struct(self, libnet_t, l);
|
79
|
+
|
80
|
+
if (NIL_P(ipv6)) {
|
81
|
+
ipv6 = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("IPv6")));
|
82
|
+
rb_yield(ipv6);
|
83
|
+
}
|
84
|
+
|
85
|
+
/* check that all required fields are set */
|
86
|
+
rb_funcall(ipv6, rb_intern("check_packable"), 0);
|
87
|
+
|
88
|
+
v = rb_funcall(ipv6, rb_intern("traffic_class"), 0);
|
89
|
+
tc = NUM2UINT(v);
|
90
|
+
v = rb_funcall(ipv6, rb_intern("flow_label"), 0);
|
91
|
+
fl = NUM2ULONG(v);
|
92
|
+
v = rb_funcall(ipv6, rb_intern("length"), 0);
|
93
|
+
len = NUM2ULONG(v);
|
94
|
+
v = rb_funcall(ipv6, rb_intern("next_header"), 0);
|
95
|
+
nh = NUM2ULONG(v);
|
96
|
+
v = rb_funcall(ipv6, rb_intern("hop_limit"), 0);
|
97
|
+
hl = NUM2ULONG(v);
|
98
|
+
v = rb_funcall(ipv6, rb_intern("src_ip"), 0);
|
99
|
+
v = StringValue(v);
|
100
|
+
memcpy(&src, RSTRING(v)->ptr, sizeof(src));
|
101
|
+
v = rb_funcall(ipv6, rb_intern("dst_ip"), 0);
|
102
|
+
v = StringValue(v);
|
103
|
+
memcpy(&dst, RSTRING(v)->ptr, sizeof(dst));
|
104
|
+
|
105
|
+
/* payload is optional */
|
106
|
+
if ((v = rb_funcall(ipv6, rb_intern("payload"), 0)) != Qnil) {
|
107
|
+
v = StringValue(v);
|
108
|
+
payload = RSTRING(v)->ptr;
|
109
|
+
payload_s = RSTRING(v)->len;
|
110
|
+
}
|
111
|
+
|
112
|
+
ptag_obj = rb_iv_get(ipv6, "@ptag");
|
113
|
+
|
114
|
+
if (!NIL_P(ptag_obj)) {
|
115
|
+
ptag = NUM2LONG(ptag_obj);
|
116
|
+
}
|
117
|
+
|
118
|
+
ptag = libnet_build_ipv6(tc, fl, len, nh, hl, src, dst, (u_int8_t *)payload,
|
119
|
+
payload_s, l, ptag);
|
120
|
+
|
121
|
+
if (ptag == -1) {
|
122
|
+
rb_raise(rb_eRuntimeError, libnet_geterror(l));
|
123
|
+
}
|
124
|
+
|
125
|
+
rb_iv_set(ipv6, "@ptag", LONG2NUM(ptag));
|
126
|
+
|
127
|
+
return ipv6;
|
128
|
+
}
|
129
|
+
|
130
|
+
void define_ipv6_methods()
|
131
|
+
{
|
132
|
+
VALUE cIPv6 = rb_const_get(cLibnet, rb_intern("IPv6"));
|
133
|
+
|
134
|
+
rb_define_singleton_method(cIPv6, "decode", net_s_decode_ipv6, 1);
|
135
|
+
|
136
|
+
rb_define_method(cLibnet, "build_ipv6", net_build_ipv6, -1);
|
137
|
+
}
|
138
|
+
|
data/ext/libnet_udp.c
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
#include <arpa/inet.h>
|
2
|
+
#include <ruby.h>
|
3
|
+
#include <libnet.h>
|
4
|
+
|
5
|
+
extern VALUE cLibnet;
|
6
|
+
|
7
|
+
static VALUE net_s_decode_udp(VALUE self, VALUE bytes)
|
8
|
+
{
|
9
|
+
VALUE udp_obj;
|
10
|
+
VALUE str;
|
11
|
+
VALUE payload;
|
12
|
+
struct libnet_udp_hdr *udp;
|
13
|
+
u_int16_t uh_sport, uh_dport, uh_ulen, uh_sum;
|
14
|
+
|
15
|
+
udp_obj = rb_funcall(self, rb_intern("new"), 0);
|
16
|
+
|
17
|
+
str = StringValue(bytes);
|
18
|
+
|
19
|
+
if (RSTRING(str)->len < LIBNET_UDP_H) {
|
20
|
+
rb_raise(rb_eArgError, "string is too small to contain an UDP header");
|
21
|
+
}
|
22
|
+
|
23
|
+
udp = (struct libnet_udp_hdr *)RSTRING(str)->ptr;
|
24
|
+
|
25
|
+
uh_sport = ntohs(udp->uh_sport);
|
26
|
+
uh_dport = ntohs(udp->uh_dport);
|
27
|
+
uh_ulen = ntohs(udp->uh_ulen);
|
28
|
+
uh_sum = ntohs(udp->uh_sum);
|
29
|
+
|
30
|
+
rb_iv_set(udp_obj, "@src_port", UINT2NUM(uh_sport));
|
31
|
+
rb_iv_set(udp_obj, "@dst_port", UINT2NUM(uh_dport));
|
32
|
+
rb_iv_set(udp_obj, "@length", UINT2NUM(uh_ulen));
|
33
|
+
rb_iv_set(udp_obj, "@checksum", UINT2NUM(uh_sum));
|
34
|
+
|
35
|
+
if (RSTRING(str)->len >= uh_ulen) {
|
36
|
+
payload = rb_str_new(RSTRING(str)->ptr + LIBNET_UDP_H,
|
37
|
+
uh_ulen - LIBNET_UDP_H);
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
payload = Qnil;
|
41
|
+
}
|
42
|
+
|
43
|
+
rb_iv_set(udp_obj, "@payload", payload);
|
44
|
+
|
45
|
+
return udp_obj;
|
46
|
+
}
|
47
|
+
|
48
|
+
static VALUE net_build_udp(int argc, VALUE *argv, VALUE self)
|
49
|
+
{
|
50
|
+
libnet_t *l;
|
51
|
+
VALUE udp, v;
|
52
|
+
u_int16_t sp, dp, len, sum;
|
53
|
+
char *payload = NULL;
|
54
|
+
u_int32_t payload_s = 0;
|
55
|
+
VALUE ptag_obj;
|
56
|
+
libnet_ptag_t ptag = 0;
|
57
|
+
|
58
|
+
rb_scan_args(argc, argv, "01", &udp);
|
59
|
+
|
60
|
+
Data_Get_Struct(self, libnet_t, l);
|
61
|
+
|
62
|
+
if (NIL_P(udp)) {
|
63
|
+
udp = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("UDP")));
|
64
|
+
rb_yield(udp);
|
65
|
+
}
|
66
|
+
|
67
|
+
/* check that all required fields are set */
|
68
|
+
rb_funcall(udp, rb_intern("check_packable"), 0);
|
69
|
+
|
70
|
+
v = rb_funcall(udp, rb_intern("src_port"), 0);
|
71
|
+
sp = NUM2UINT(v);
|
72
|
+
v = rb_funcall(udp, rb_intern("dst_port"), 0);
|
73
|
+
dp = NUM2UINT(v);
|
74
|
+
v = rb_funcall(udp, rb_intern("length"), 0);
|
75
|
+
len = NUM2UINT(v);
|
76
|
+
v = rb_funcall(udp, rb_intern("checksum"), 0);
|
77
|
+
sum = NUM2UINT(v);
|
78
|
+
|
79
|
+
/* get the payload */
|
80
|
+
if ((v = rb_funcall(udp, rb_intern("payload"), 0)) != Qnil) {
|
81
|
+
v = StringValue(v);
|
82
|
+
payload = RSTRING(v)->ptr;
|
83
|
+
payload_s = RSTRING(v)->len;
|
84
|
+
}
|
85
|
+
|
86
|
+
ptag_obj = rb_iv_get(udp, "@ptag");
|
87
|
+
|
88
|
+
if (!NIL_P(ptag_obj)) {
|
89
|
+
ptag = NUM2LONG(ptag_obj);
|
90
|
+
}
|
91
|
+
|
92
|
+
ptag = libnet_build_udp(sp, dp, len, sum, (u_int8_t *)payload,
|
93
|
+
payload_s, l, ptag);
|
94
|
+
|
95
|
+
if (ptag == -1) {
|
96
|
+
rb_raise(rb_eRuntimeError, libnet_geterror(l));
|
97
|
+
}
|
98
|
+
|
99
|
+
rb_iv_set(udp, "@ptag", LONG2NUM(ptag));
|
100
|
+
|
101
|
+
return udp;
|
102
|
+
}
|
103
|
+
|
104
|
+
void define_udp_methods()
|
105
|
+
{
|
106
|
+
VALUE cUDP = rb_const_get(cLibnet, rb_intern("UDP"));
|
107
|
+
|
108
|
+
rb_define_singleton_method(cUDP, "decode", net_s_decode_udp, 1);
|
109
|
+
|
110
|
+
rb_define_method(cLibnet, "build_udp", net_build_udp, -1);
|
111
|
+
}
|
112
|
+
|