Class: Libnet
- Inherits:
-
Object
- Object
- Libnet
- Defined in:
- lib/libnet4r/header.rb,
lib/libnet4r/libnet.rb,
lib/libnet4r/helpers.rb,
ext/libnet.c
Defined Under Namespace
Modules: Helpers Classes: ARP, Ethernet, Header, IPv4, IPv6, TCP, UDP, VLAN
Constant Summary collapse
- HL_802_1Q =
libnet header length constants
INT2FIX(LIBNET_802_1Q_H)
- HL_802_1X =
INT2FIX(LIBNET_802_1X_H)
- HL_802_2 =
INT2FIX(LIBNET_802_2_H)
- HL_802_2SNAP =
INT2FIX(LIBNET_802_2SNAP_H)
- HL_802_3 =
INT2FIX(LIBNET_802_3_H)
- HL_ARP =
INT2FIX(LIBNET_ARP_H)
- HL_ARP_ETH_IP =
INT2FIX(LIBNET_ARP_ETH_IP_H)
- HL_BGP4_HEADER =
INT2FIX(LIBNET_BGP4_HEADER_H)
- HL_BGP4_OPEN =
INT2FIX(LIBNET_BGP4_OPEN_H)
- HL_BGP4_UPDATE =
INT2FIX(LIBNET_BGP4_UPDATE_H)
- HL_BGP4_NOTIFICATION =
INT2FIX(LIBNET_BGP4_NOTIFICATION_H)
- HL_CDP =
INT2FIX(LIBNET_CDP_H)
- HL_DHCPV4 =
INT2FIX(LIBNET_DHCPV4_H)
- HL_UDP_DNSV4 =
INT2FIX(LIBNET_UDP_DNSV4_H)
- HL_TCP_DNSV4 =
INT2FIX(LIBNET_TCP_DNSV4_H)
- HL_ETH =
INT2FIX(LIBNET_ETH_H)
- HL_FDDI =
INT2FIX(LIBNET_FDDI_H)
- HL_ICMPV4 =
INT2FIX(LIBNET_ICMPV4_H)
- HL_ICMPV4_ECHO =
INT2FIX(LIBNET_ICMPV4_ECHO_H)
- HL_ICMPV4_MASK =
INT2FIX(LIBNET_ICMPV4_MASK_H)
- HL_ICMPV4_UNREACH =
INT2FIX(LIBNET_ICMPV4_UNREACH_H)
- HL_ICMPV4_TIMXCEED =
INT2FIX(LIBNET_ICMPV4_TIMXCEED_H)
- HL_ICMPV4_REDIRECT =
INT2FIX(LIBNET_ICMPV4_REDIRECT_H)
- HL_ICMPV4_TS =
INT2FIX(LIBNET_ICMPV4_TS_H)
- HL_ICMPV6 =
INT2FIX(LIBNET_ICMPV6_H)
- HL_IGMP =
INT2FIX(LIBNET_IGMP_H)
- HL_IPV4 =
INT2FIX(LIBNET_IPV4_H)
- HL_IPV6 =
INT2FIX(LIBNET_IPV6_H)
- HL_IPV6_FRAG =
INT2FIX(LIBNET_IPV6_FRAG_H)
- HL_IPV6_ROUTING =
INT2FIX(LIBNET_IPV6_ROUTING_H)
- HL_IPV6_DESTOPTS =
INT2FIX(LIBNET_IPV6_DESTOPTS_H)
- HL_IPV6_HBHOPTS =
INT2FIX(LIBNET_IPV6_HBHOPTS_H)
- HL_IPSEC_ESP_HDR =
INT2FIX(LIBNET_IPSEC_ESP_HDR_H)
- HL_IPSEC_ESP_FTR =
INT2FIX(LIBNET_IPSEC_ESP_FTR_H)
- HL_IPSEC_AH =
INT2FIX(LIBNET_IPSEC_AH_H)
- HL_ISL =
INT2FIX(LIBNET_ISL_H)
- HL_GRE =
INT2FIX(LIBNET_GRE_H)
- HL_GRE_SRE =
INT2FIX(LIBNET_GRE_SRE_H)
- HL_MPLS =
INT2FIX(LIBNET_MPLS_H)
- HL_OSPF =
INT2FIX(LIBNET_OSPF_H)
- HL_OSPF_HELLO =
INT2FIX(LIBNET_OSPF_HELLO_H)
- HL_OSPF_DBD =
INT2FIX(LIBNET_OSPF_DBD_H)
- HL_OSPF_LSR =
INT2FIX(LIBNET_OSPF_LSR_H)
- HL_OSPF_LSU =
INT2FIX(LIBNET_OSPF_LSU_H)
- HL_OSPF_LSA =
INT2FIX(LIBNET_OSPF_LSA_H)
- HL_OSPF_AUTH =
INT2FIX(LIBNET_OSPF_AUTH_H)
- HL_OSPF_CKSUM =
INT2FIX(LIBNET_OSPF_CKSUM)
- HL_OSPF_LS_RTR =
INT2FIX(LIBNET_OSPF_LS_RTR_H)
- HL_OSPF_LS_NET =
INT2FIX(LIBNET_OSPF_LS_NET_H)
- HL_OSPF_LS_SUM =
INT2FIX(LIBNET_OSPF_LS_SUM_H)
- HL_OSPF_LS_AS_EXT =
INT2FIX(LIBNET_OSPF_LS_AS_EXT_H)
- HL_NTP =
INT2FIX(LIBNET_NTP_H)
- HL_RIP =
INT2FIX(LIBNET_RIP_H)
- HL_RPC_CALL =
INT2FIX(LIBNET_RPC_CALL_H)
- HL_RPC_CALL_TCP =
INT2FIX(LIBNET_RPC_CALL_TCP_H)
- HL_SEBEK =
INT2FIX(LIBNET_SEBEK_H)
- HL_STP_CONF =
INT2FIX(LIBNET_STP_CONF_H)
- HL_STP_TCN =
INT2FIX(LIBNET_STP_TCN_H)
- HL_TOKEN_RING =
INT2FIX(LIBNET_TOKEN_RING_H)
- HL_TCP =
INT2FIX(LIBNET_TCP_H)
- HL_UDP =
INT2FIX(LIBNET_UDP_H)
- HL_VRRP =
INT2FIX(LIBNET_VRRP_H)
- IPPROTO_IP =
INT2FIX(IPPROTO_IP)
- IPPROTO_HOPOPTS =
INT2FIX(IPPROTO_HOPOPTS)
- IPPROTO_ICMP =
INT2FIX(IPPROTO_ICMP)
- IPPROTO_IGMP =
INT2FIX(IPPROTO_IGMP)
- IPPROTO_GGP =
INT2FIX(IPPROTO_GGP)
- IPPROTO_ENCAP =
INT2FIX(IPPROTO_ENCAP)
- IPPROTO_TCP =
INT2FIX(IPPROTO_TCP)
- IPPROTO_EGP =
INT2FIX(IPPROTO_EGP)
- IPPROTO_PUP =
INT2FIX(IPPROTO_PUP)
- IPPROTO_UDP =
INT2FIX(IPPROTO_UDP)
- IPPROTO_IDP =
INT2FIX(IPPROTO_IDP)
- IPPROTO_IPV6 =
INT2FIX(IPPROTO_IPV6)
- IPPROTO_ROUTING =
INT2FIX(IPPROTO_ROUTING)
- IPPROTO_FRAGMENT =
INT2FIX(IPPROTO_FRAGMENT)
- IPPROTO_RSVP =
INT2FIX(IPPROTO_RSVP)
- IPPROTO_ESP =
INT2FIX(IPPROTO_ESP)
- IPPROTO_AH =
INT2FIX(IPPROTO_AH)
- IPPROTO_ICMPV6 =
INT2FIX(IPPROTO_ICMPV6)
- IPPROTO_NONE =
INT2FIX(IPPROTO_NONE)
- IPPROTO_DSTOPTS =
INT2FIX(IPPROTO_DSTOPTS)
- IPPROTO_HELLO =
INT2FIX(IPPROTO_HELLO)
- IPPROTO_ND =
INT2FIX(IPPROTO_ND)
- IPPROTO_EON =
INT2FIX(IPPROTO_EON)
- IPPROTO_PIM =
INT2FIX(IPPROTO_PIM)
- IPPROTO_SCTP =
INT2FIX(IPPROTO_SCTP)
- IPPROTO_RAW =
INT2FIX(IPPROTO_RAW)
- IPPROTO_MAX =
INT2FIX(IPPROTO_MAX)
Instance Attribute Summary collapse
-
#injection_type ⇒ Object
readonly
Returns the value of attribute injection_type.
Class Method Summary collapse
-
.addr2name4(addr, do_dns_lookup = true) ⇒ Object
Takes a network byte ordered IPv4 address and returns a string containing either the canonical DNS name (if it has one) or a dotted decimal respresentation of the IP address.
-
.addr2name6(addr, do_dns_lookup = true) ⇒ printable IPv6 string
Takes a 16 byte IPv6 address and returns either the canonical DNS name (if it has one) or a printable representation of the address.
-
.hex_aton(string) ⇒ String
Takes a colon separated hexidecimal address and returns a byte string suitable for use in a protocol builder method.
-
.hexdump(bytes) ⇒ Object
:call-seq: Libnet.hexdump(bytes) # -> string.
-
.ipv4_aton(string) ⇒ Integer
Takes a dotted-decimal representation of an IPv4 address and returns a 4 byte integer suitable for use in a header builder method.
-
.ipv4_ntoa(integer) ⇒ String
Takes a network formated IPv4 address and converts it to its dotted-decimal string representation.
-
.ipv6_aton(string) ⇒ Integer
Takes a string representation of an IPv6 address and returns a 16 byte string suitable for use in a header builder method.
-
.ipv6_ntoa(string) ⇒ String
Takes a network formated IPv6 address and converts it to its printable format.
-
.new(injection_type = :raw4, device = nil) ⇒ a new Libnet object
Creates a new Libnet object.
Instance Method Summary collapse
- #build_arp(*args) ⇒ Object
- #build_ethernet(*args) ⇒ Object
- #build_ipv4(*args) ⇒ Object
- #build_ipv6(*args) ⇒ Object
- #build_tcp(*args) ⇒ Object
- #build_udp(*args) ⇒ Object
- #build_vlan(*args) ⇒ Object
-
#clear ⇒ Object
Clears the current packet inside the Libnet object.
-
#cull_packet ⇒ Object
Yanks a prebuilt, wire-ready packet from the Libnet object.
-
#device ⇒ Object
Returns the canonical name of the device used for packet injection.
-
#dump ⇒ Object
Prints the contents of the Libnet object to STDOUT.
-
#hwaddr ⇒ Object
Returns the MAC address for the device the Libnet object was initialized with.
-
#pack ⇒ Object
Yanks a prebuilt, wire-ready packet from the Libnet object.
-
#write ⇒ Object
Writes a prebuilt packet to the network.
-
#write_bytes(string) ⇒ Object
Writes a packet to the wire.
Instance Attribute Details
#injection_type ⇒ Object (readonly)
Returns the value of attribute injection_type.
3 4 5 |
# File 'lib/libnet4r/libnet.rb', line 3 def injection_type @injection_type end |
Class Method Details
.addr2name4(addr, do_dns_lookup = true) ⇒ Object
Takes a network byte ordered IPv4 address and returns a string containing
either the canonical DNS name (if it has one) or a dotted decimal
respresentation of the IP address. This may incur a DNS lookup if the
+do_dns_lookup+ parameter is set to true. If the +do_dns_lookup+
parameter is set to false, no DNS lookup will be performed and the dotted
decimal representation of the IP address will be returned.
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 |
# File 'ext/libnet.c', line 123
static VALUE net_s_addr2name4(int argc, VALUE *argv, VALUE self)
{
VALUE addr_v, use_name_v, name_v;
u_int32_t addr;
u_int8_t use_name = LIBNET_RESOLVE;
char *name = NULL;
rb_scan_args(argc, argv, "11", &addr_v, &use_name_v);
addr = NUM2ULONG(addr_v);
addr = htonl(addr);
if (!NIL_P(use_name_v) && use_name_v == Qfalse) {
use_name = LIBNET_DONT_RESOLVE;
}
name = libnet_addr2name4(addr, use_name);
name_v = rb_str_new2(name);
/* don't free the returned name here as its stored in a static buffer by
* libnet */
return name_v;
}
|
.addr2name6(addr, do_dns_lookup = true) ⇒ printable IPv6 string
Takes a 16 byte IPv6 address and returns either the canonical DNS name (if it has one) or a printable representation of the address.
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 |
# File 'ext/libnet.c', line 156
static VALUE net_s_addr2name6(int argc, VALUE *argv, VALUE self)
{
VALUE addr_v, use_name_v, name_v;
struct libnet_in6_addr addr;
u_int8_t use_name = LIBNET_RESOLVE;
char name[100];
rb_scan_args(argc, argv, "11", &addr_v, &use_name_v);
if (RSTRING(addr_v)->len != 16) {
rb_raise(rb_eArgError, "invalid IPv6 address");
}
memcpy(addr.libnet_s6_addr, RSTRING(addr_v)->ptr, sizeof(addr.libnet_s6_addr));
if (!NIL_P(use_name_v) && use_name_v == Qfalse) {
use_name = LIBNET_DONT_RESOLVE;
}
libnet_addr2name6_r(addr, use_name, name, sizeof(name));
name_v = rb_str_new2(name);
return name_v;
}
|
.hex_aton(string) ⇒ String
Takes a colon separated hexidecimal address and returns a byte string suitable for use in a protocol builder method.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'ext/libnet.c', line 188
static VALUE net_s_hex_aton(VALUE self, VALUE str)
{
u_int8_t *rv;
int len;
VALUE s;
rv = libnet_hex_aton((int8_t *)StringValuePtr(str), &len);
if (rv == NULL) {
rb_raise(rb_eArgError, "could not convert input string");
}
s = rb_str_new((char *)rv, len);
free(rv);
return s;
}
|
.hexdump(bytes) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/libnet4r/libnet.rb', line 16 def self.hexdump(bytes) offset = 0 i = 0 dump = "0x00000000: " bytes.each_byte do |b| if i != 0 if i % 16 == 0 offset += 16 dump += sprintf("\n0x%08x: ", offset) elsif i % 4 == 0 dump += " " end end dump += sprintf("%02x", b) i += 1 end dump += "\n" end |
.ipv4_aton(string) ⇒ Integer
Takes a dotted-decimal representation of an IPv4 address and returns a 4 byte integer suitable for use in a header builder method.
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 |
# File 'ext/libnet.c', line 214
static VALUE net_s_ipv4_aton(VALUE self, VALUE str)
{
char *ip_s;
int len = RSTRING(str)->len;
unsigned long ipv4;
VALUE r;
ip_s = malloc(len + 1);
memcpy(ip_s, RSTRING(str)->ptr, len);
ip_s[len] = '\0';
if (inet_pton(AF_INET, ip_s, &ipv4) == 1) {
ipv4 = htonl(ipv4);
r = ULONG2NUM(ipv4);
}
else {
free(ip_s);
rb_raise(rb_eArgError, "invalid IPv4 address: %s\n", ip_s);
}
free(ip_s);
return r;
}
|
.ipv4_ntoa(integer) ⇒ String
Takes a network formated IPv4 address and converts it to its dotted-decimal string representation.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'ext/libnet.c', line 277
static VALUE net_s_ipv4_ntoa(VALUE self, VALUE integer)
{
unsigned long ipv4 = NUM2ULONG(integer);
char pbuf[100];
VALUE r;
ipv4 = htonl(ipv4);
if (inet_ntop(AF_INET, &ipv4, pbuf, sizeof(pbuf))) {
r = rb_str_new2(pbuf);
}
else {
rb_raise(rb_eArgError,
"could not convert integer value %#x to a printable IPv4 string",
ipv4);
}
return r;
}
|
.ipv6_aton(string) ⇒ Integer
Takes a string representation of an IPv6 address and returns a 16 byte string suitable for use in a header builder method.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'ext/libnet.c', line 246
static VALUE net_s_ipv6_aton(VALUE self, VALUE str)
{
char *ip_s;
int len = RSTRING(str)->len;
unsigned char ipv6[16];
VALUE r;
ip_s = malloc(len + 1);
memcpy(ip_s, RSTRING(str)->ptr, len);
ip_s[len] = '\0';
if (inet_pton(AF_INET6, ip_s, ipv6) == 1) {
r = rb_str_new(ipv6, sizeof(ipv6));
}
else {
free(ip_s);
rb_raise(rb_eArgError, "invalid IPv6 address: %s\n", ip_s);
}
free(ip_s);
return r;
}
|
.ipv6_ntoa(string) ⇒ String
Takes a network formated IPv6 address and converts it to its printable format.
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 |
# File 'ext/libnet.c', line 303
static VALUE net_s_ipv6_ntoa(VALUE self, VALUE str)
{
char *ip_s;
int len = RSTRING(str)->len;
char pbuf[100];
VALUE r;
if (len != 16) {
rb_raise(rb_eArgError, "invalid IPv6 address");
}
ip_s = malloc(len + 1);
memcpy(ip_s, RSTRING(str)->ptr, len);
ip_s[len] = '\0';
if (inet_ntop(AF_INET6, ip_s, pbuf, sizeof(pbuf))) {
r = rb_str_new2(pbuf);
}
else {
rb_raise(rb_eArgError,
"could not convert IPv6 address to a printable string");
}
return r;
}
|
.new(injection_type = :raw4, device = nil) ⇒ a new Libnet object
Creates a new Libnet object. The injection_type
parameter defines
how the constructed packet get injected into the network.
If +injection_type+ is <tt>:link</tt>, the packet gets injected at the data
link layer (which provides more granular control over the IP layer). If
+injection_type+ is <tt>:link</tt> and +device+ is not +nil+, Libnet
attempts to use the specified network device for packet injection. The
+device+ parameter is either a canonical string that references the device
(such as "eth0" for a 100MB Ethernet card on Linux or "fxp0" for a 100MB
Ethernet card on OpenBSD) or the dotted decimal representation of the
device's IP address (e.g. 192.168.0.1). If +device+ is +nil+, Libnet
attempts to find a suitable device to use.
If +injection_type+ is <tt>:raw4</tt>, the packet gets injected using a raw
socket interface.
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 |
# File 'ext/libnet.c', line 37
static VALUE net_s_new(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE obj;
char errbuf[LIBNET_ERRBUF_SIZE];
int inj_type = LIBNET_RAW4_ADV;
char *inj_str = NULL;
size_t inj_str_len = 0;
char *dev_str = NULL;
/* get the injection type */
if (argc > 0) {
switch (TYPE(argv[0])) {
case T_STRING:
inj_str = RSTRING(argv[0])->ptr;
inj_str_len = RSTRING(argv[0])->len;
break;
case T_SYMBOL:
inj_str = rb_id2name(SYM2ID(argv[0]));
inj_str_len = strlen(inj_str);
break;
default:
rb_raise(rb_eTypeError,
"invalid type, expecting a String or Symbol");
}
if (strncasecmp("raw", inj_str, inj_str_len) == 0 ||
strncasecmp("raw4", inj_str, inj_str_len) == 0) {
inj_type = LIBNET_RAW4_ADV;
}
else if (strncasecmp("raw6", inj_str, inj_str_len) == 0) {
inj_type = LIBNET_RAW6_ADV;
}
else if (strncasecmp("link", inj_str, inj_str_len) == 0) {
inj_type = LIBNET_LINK_ADV;
}
else {
rb_raise(rb_eArgError,
"%s is not a valid initialization type, should be one of: 'raw4', 'raw6', or 'link'",
inj_str);
}
}
/* get the device string */
if (argc > 1) {
dev_str = StringValuePtr(argv[1]);
}
l = libnet_init(inj_type, dev_str, errbuf);
if (l == NULL) {
rb_raise(rb_eRuntimeError, errbuf);
}
obj = Data_Wrap_Struct(self, NULL, libnet_destroy, l);
/* set the injection type attribute */
switch (inj_type) {
case LIBNET_RAW4_ADV:
rb_iv_set(obj, "@injection_type", ID2SYM(rb_intern("raw4")));
break;
case LIBNET_RAW6_ADV:
rb_iv_set(obj, "@injection_type", ID2SYM(rb_intern("raw6")));
break;
case LIBNET_LINK_ADV:
rb_iv_set(obj, "@injection_type", ID2SYM(rb_intern("link")));
break;
default:
rb_bug("BUG: %d is an invalid injection type (this should not happen)",
inj_type);
}
return obj;
}
|
Instance Method Details
#build_arp(*args) ⇒ Object
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 |
# File 'ext/libnet_arp.c', line 75
static VALUE net_build_arp(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE arp_obj, v;
u_int16_t hrd, pro, op;
u_int8_t hln, pln;
VALUE sha, spa, tha, tpa;
char *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", &arp_obj);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(arp_obj)) {
arp_obj = rb_class_new_instance(0, NULL, cARP);
rb_yield(arp_obj);
}
/* check that all required fields are set */
rb_funcall(arp_obj, rb_intern("check_packable"), 0);
v = rb_funcall(arp_obj, rb_intern("htype"), 0);
hrd = NUM2UINT(v);
v = rb_funcall(arp_obj, rb_intern("protocol"), 0);
pro = NUM2UINT(v);
v = rb_funcall(arp_obj, rb_intern("hlen"), 0);
hln = NUM2UINT(v);
v = rb_funcall(arp_obj, rb_intern("plen"), 0);
pln = NUM2UINT(v);
v = rb_funcall(arp_obj, rb_intern("operation"), 0);
op = NUM2UINT(v);
v = rb_funcall(arp_obj, rb_intern("sender_haddr"), 0);
sha = StringValue(v);
v = rb_funcall(arp_obj, rb_intern("sender_paddr"), 0);
spa = StringValue(v);
v = rb_funcall(arp_obj, rb_intern("target_haddr"), 0);
tha = StringValue(v);
v = rb_funcall(arp_obj, rb_intern("target_paddr"), 0);
tpa = StringValue(v);
/* get the payload */
if ((v = rb_funcall(arp_obj, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(arp_obj, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_arp(hrd, pro, hln, pln, op, RSTRING(sha)->ptr,
RSTRING(spa)->ptr, RSTRING(tha)->ptr, RSTRING(tpa)->ptr,
payload, payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(arp_obj, "@ptag", LONG2NUM(ptag));
return arp_obj;
}
|
#build_ethernet(*args) ⇒ Object
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 |
# File 'ext/libnet_ethernet.c', line 50
static VALUE net_build_ethernet(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE eth, v;
u_int8_t *dst = NULL;
u_int8_t *src = NULL;
u_int16_t type;
char *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", ð);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(eth)) {
eth = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("Ethernet")));
rb_yield(eth);
}
/* check that all required fields are set */
rb_funcall(eth, rb_intern("check_packable"), 0);
/* required parameters */
v = rb_funcall(eth, rb_intern("dst"), 0);
dst = (u_int8_t *)StringValuePtr(v);
v = rb_funcall(eth, rb_intern("type"), 0);
type = NUM2UINT(v);
/* optional parameters */
v = rb_funcall(eth, rb_intern("src"), 0);
src = (u_int8_t *)StringValuePtr(v);
if ((v = rb_funcall(eth, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(eth, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_ethernet(dst, src, type, (u_int8_t *)payload,
payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(eth, "@ptag", LONG2NUM(ptag));
return eth;
}
|
#build_ipv4(*args) ⇒ Object
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 |
# File 'ext/libnet_ipv4.c', line 64
static VALUE net_build_ipv4(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE ipv4, v;
u_int8_t tos = 0, ttl = 0, prot = 0;
u_int16_t len = 0, id = 0, frag = 0, sum = 0;
u_int32_t src = 0, dst = 0;
char *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", &ipv4);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(ipv4)) {
ipv4 = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("IPv4")));
rb_yield(ipv4);
}
/* check that all required fields are set */
rb_funcall(ipv4, rb_intern("check_packable"), 0);
v = rb_funcall(ipv4, rb_intern("length"), 0);
len = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("protocol"), 0);
prot = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("tos"), 0);
tos = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("id"), 0);
id = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("frag_off"), 0);
frag = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("ttl"), 0);
ttl = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("checksum"), 0);
sum = NUM2UINT(v);
v = rb_funcall(ipv4, rb_intern("src_ip"), 0);
src = htonl(NUM2ULONG(v));
v = rb_funcall(ipv4, rb_intern("dst_ip"), 0);
dst = htonl(NUM2ULONG(v));
/* payload is optional */
if ((v = rb_funcall(ipv4, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(ipv4, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_ipv4(len, tos, id, frag, ttl, prot, sum, src, dst,
(u_int8_t *)payload, payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(ipv4, "@ptag", LONG2NUM(ptag));
return ipv4;
}
|
#build_ipv6(*args) ⇒ Object
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 |
# File 'ext/libnet_ipv6.c', line 63
static VALUE net_build_ipv6(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE ipv6, v;
u_int8_t tc, nh, hl;
u_int16_t len;
u_int32_t fl;
struct libnet_in6_addr src, dst;
u_int8_t *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", &ipv6);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(ipv6)) {
ipv6 = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("IPv6")));
rb_yield(ipv6);
}
/* check that all required fields are set */
rb_funcall(ipv6, rb_intern("check_packable"), 0);
v = rb_funcall(ipv6, rb_intern("traffic_class"), 0);
tc = NUM2UINT(v);
v = rb_funcall(ipv6, rb_intern("flow_label"), 0);
fl = NUM2ULONG(v);
v = rb_funcall(ipv6, rb_intern("length"), 0);
len = NUM2ULONG(v);
v = rb_funcall(ipv6, rb_intern("next_header"), 0);
nh = NUM2ULONG(v);
v = rb_funcall(ipv6, rb_intern("hop_limit"), 0);
hl = NUM2ULONG(v);
v = rb_funcall(ipv6, rb_intern("src_ip"), 0);
v = StringValue(v);
memcpy(&src, RSTRING(v)->ptr, sizeof(src));
v = rb_funcall(ipv6, rb_intern("dst_ip"), 0);
v = StringValue(v);
memcpy(&dst, RSTRING(v)->ptr, sizeof(dst));
/* payload is optional */
if ((v = rb_funcall(ipv6, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(ipv6, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_ipv6(tc, fl, len, nh, hl, src, dst, (u_int8_t *)payload,
payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(ipv6, "@ptag", LONG2NUM(ptag));
return ipv6;
}
|
#build_tcp(*args) ⇒ Object
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 |
# File 'ext/libnet_tcp.c', line 63
static VALUE net_build_tcp(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE tcp, v;
u_int16_t sp, dp;
u_int32_t seq, ack;
u_int8_t control;
u_int16_t win, sum, urg;
char *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", &tcp);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(tcp)) {
tcp = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("TCP")));
rb_yield(tcp);
}
/* check that all required fields are set */
rb_funcall(tcp, rb_intern("check_packable"), 0);
v = rb_funcall(tcp, rb_intern("src_port"), 0);
sp = NUM2UINT(v);
v = rb_funcall(tcp, rb_intern("dst_port"), 0);
dp = NUM2UINT(v);
v = rb_funcall(tcp, rb_intern("seq"), 0);
seq = NUM2ULONG(v);
v = rb_funcall(tcp, rb_intern("ack"), 0);
ack = NUM2ULONG(v);
v = rb_funcall(tcp, rb_intern("control"), 0);
control = NUM2UINT(v);
v = rb_funcall(tcp, rb_intern("window"), 0);
win = NUM2UINT(v);
v = rb_funcall(tcp, rb_intern("checksum"), 0);
sum = NUM2UINT(v);
v = rb_funcall(tcp, rb_intern("urgent"), 0);
urg = NUM2UINT(v);
/* get the payload */
if ((v = rb_funcall(tcp, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(tcp, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_tcp(sp, dp, seq, ack, control, win, sum, urg,
LIBNET_TCP_H + payload_s, (u_int8_t *)payload, payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(tcp, "@ptag", LONG2NUM(ptag));
return tcp;
}
|
#build_udp(*args) ⇒ Object
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 |
# File 'ext/libnet_udp.c', line 48
static VALUE net_build_udp(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE udp, v;
u_int16_t sp, dp, len, sum;
char *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", &udp);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(udp)) {
udp = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("UDP")));
rb_yield(udp);
}
/* check that all required fields are set */
rb_funcall(udp, rb_intern("check_packable"), 0);
v = rb_funcall(udp, rb_intern("src_port"), 0);
sp = NUM2UINT(v);
v = rb_funcall(udp, rb_intern("dst_port"), 0);
dp = NUM2UINT(v);
v = rb_funcall(udp, rb_intern("length"), 0);
len = NUM2UINT(v);
v = rb_funcall(udp, rb_intern("checksum"), 0);
sum = NUM2UINT(v);
/* get the payload */
if ((v = rb_funcall(udp, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(udp, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_udp(sp, dp, len, sum, (u_int8_t *)payload,
payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(udp, "@ptag", LONG2NUM(ptag));
return udp;
}
|
#build_vlan(*args) ⇒ Object
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 |
# File 'ext/libnet_vlan.c', line 55
static VALUE net_build_vlan(int argc, VALUE *argv, VALUE self)
{
libnet_t *l;
VALUE vlan_obj, v;
u_int8_t *dst = NULL;
u_int8_t *src = NULL;
u_int16_t tpi;
u_int8_t priority;
u_int8_t cfi;
u_int16_t vlan_id;
u_int16_t len_proto;
char *payload = NULL;
u_int32_t payload_s = 0;
VALUE ptag_obj;
libnet_ptag_t ptag = 0;
rb_scan_args(argc, argv, "01", &vlan_obj);
Data_Get_Struct(self, libnet_t, l);
if (NIL_P(vlan_obj)) {
vlan_obj = rb_class_new_instance(0, NULL, rb_const_get(cLibnet, rb_intern("VLAN")));
rb_yield(vlan_obj);
}
/* check that all required fields are set */
rb_funcall(vlan_obj, rb_intern("check_packable"), 0);
/* required parameters */
v = rb_funcall(vlan_obj, rb_intern("dst"), 0);
dst = (u_int8_t *)StringValuePtr(v);
v = rb_funcall(vlan_obj, rb_intern("src"), 0);
src = (u_int8_t *)StringValuePtr(v);
v = rb_funcall(vlan_obj, rb_intern("tpi"), 0);
tpi = NUM2UINT(v);
v = rb_funcall(vlan_obj, rb_intern("priority"), 0);
priority = NUM2UINT(v);
v = rb_funcall(vlan_obj, rb_intern("cfi"), 0);
cfi = NUM2UINT(v);
v = rb_funcall(vlan_obj, rb_intern("id"), 0);
vlan_id = NUM2UINT(v);
v = rb_funcall(vlan_obj, rb_intern("len"), 0);
len_proto = NUM2UINT(v);
/* optional parameters */
if ((v = rb_funcall(vlan_obj, rb_intern("payload"), 0)) != Qnil) {
v = StringValue(v);
payload = RSTRING(v)->ptr;
payload_s = RSTRING(v)->len;
}
ptag_obj = rb_iv_get(vlan_obj, "@ptag");
if (!NIL_P(ptag_obj)) {
ptag = NUM2LONG(ptag_obj);
}
ptag = libnet_build_802_1q(dst, src, tpi, priority, cfi, vlan_id, len_proto,
(u_int8_t *)payload, payload_s, l, ptag);
if (ptag == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
rb_iv_set(vlan_obj, "@ptag", LONG2NUM(ptag));
return vlan_obj;
}
|
#clear ⇒ Object
Clears the current packet inside the Libnet object. Should be called when the programmer wants to send a completely new packet of a different type using the same Libnet object.
470 471 472 473 474 475 476 477 478 479 |
# File 'ext/libnet.c', line 470
static VALUE net_clear(VALUE self)
{
libnet_t *l;
Data_Get_Struct(self, libnet_t, l);
libnet_clear_packet(l);
return Qnil;
}
|
#cull_packet ⇒ Object
Yanks a prebuilt, wire-ready packet from the Libnet object.
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'ext/libnet.c', line 427
static VALUE net_pack(VALUE self)
{
libnet_t *l;
u_int8_t *packet;
u_int32_t packet_s;
VALUE str;
Data_Get_Struct(self, libnet_t, l);
if (libnet_pblock_coalesce(l, &packet, &packet_s) == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
str = rb_str_new((char *)packet, packet_s);
libnet_adv_free_packet(l, packet);
return str;
}
|
#device ⇒ Object
Returns the canonical name of the device used for packet injection.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'ext/libnet.c', line 335
static VALUE net_get_device(VALUE self)
{
libnet_t *l;
char *dev;
Data_Get_Struct(self, libnet_t, l);
dev = (char *)libnet_getdevice(l);
if (dev == NULL) {
dev = "";
}
return rb_str_new2(dev);
}
|
#dump ⇒ Object
Prints the contents of the Libnet object to STDOUT.
452 453 454 455 456 457 458 459 460 461 |
# File 'ext/libnet.c', line 452
static VALUE net_dump(VALUE self)
{
libnet_t *l;
Data_Get_Struct(self, libnet_t, l);
libnet_diag_dump_context(l);
return Qnil;
}
|
#hwaddr ⇒ Object
Returns the MAC address for the device the Libnet object was initialized with. If the Libnet object was initialized without a device, the method will attempt to find one. If the method fails to find a device, a RuntimeError will be raised.
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 |
# File 'ext/libnet.c', line 489
static VALUE net_hwaddr(VALUE self)
{
libnet_t *l;
struct libnet_ether_addr *eth;
int i;
char eth_s[18];
char byte_s[3];
Data_Get_Struct(self, libnet_t, l);
eth = libnet_get_hwaddr(l);
if (eth == NULL) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
memset(eth_s, 0, sizeof(eth_s));
for (i = 0; i < 6; i++) {
snprintf(byte_s, sizeof(byte_s), "%02x", eth->ether_addr_octet[i]);
strcat(eth_s, byte_s);
if (i != 5) {
strcat(eth_s, ":");
}
}
return rb_str_new2(eth_s);
}
|
#pack ⇒ Object
Yanks a prebuilt, wire-ready packet from the Libnet object.
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'ext/libnet.c', line 427
static VALUE net_pack(VALUE self)
{
libnet_t *l;
u_int8_t *packet;
u_int32_t packet_s;
VALUE str;
Data_Get_Struct(self, libnet_t, l);
if (libnet_pblock_coalesce(l, &packet, &packet_s) == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
str = rb_str_new((char *)packet, packet_s);
libnet_adv_free_packet(l, packet);
return str;
}
|
#write ⇒ Object
Writes a prebuilt packet to the network. The method assumes that a packet has been constructed inside the Libnet object via one or more Libnet#build_* methods and is ready to go. Depending on how this Libnet object was initialized, the method will write the packet to the wire either via the raw or link layer interface.
Note: It is necessary for the UID or EUID of the process running this method to be 0.
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'ext/libnet.c', line 363
static VALUE net_write(VALUE self)
{
libnet_t *l;
int num_bytes;
Data_Get_Struct(self, libnet_t, l);
num_bytes = libnet_write(l);
if (num_bytes == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
return INT2NUM(num_bytes);
}
|
#write_bytes(string) ⇒ Object
Writes a packet to the wire. This method is usedful to write a packet that has been constructed by hand or to write a packet that has been returned from Libnet#pack.
Note: It is necessary for the UID or EUID of the process running this method to be 0.
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 |
# File 'ext/libnet.c', line 389
static VALUE net_write_bytes(VALUE self, VALUE bytes)
{
libnet_t *l;
int num_bytes;
VALUE str;
VALUE inj_type;
str = StringValue(bytes);
Data_Get_Struct(self, libnet_t, l);
inj_type = rb_iv_get(self, "@injection_type");
if (ID2SYM(rb_intern("raw4")) == inj_type) {
num_bytes = libnet_write_raw_ipv4(l, RSTRING(str)->ptr, RSTRING(str)->len);
}
else if (ID2SYM(rb_intern("raw6")) == inj_type) {
num_bytes = libnet_write_raw_ipv6(l, RSTRING(str)->ptr, RSTRING(str)->len);
}
else if (ID2SYM(rb_intern("link")) == inj_type) {
num_bytes = libnet_write_link(l, RSTRING(str)->ptr, RSTRING(str)->len);
}
else {
rb_bug("BUG: invalid injection type found (this should not happen)");
}
if (num_bytes == -1) {
rb_raise(rb_eRuntimeError, libnet_geterror(l));
}
return INT2NUM(num_bytes);
}
|