Skip to content

Instantly share code, notes, and snippets.

View boxofrad's full-sized avatar

Dan Upton boxofrad

View GitHub Profile
package main
import (
"fmt"
"time"
)
type Video struct {
Name string
}
require 'socket'
BUFFER_SIZE = 1024
socket = UDPSocket.new
socket.bind('192.168.33.10', 4321)
loop do
message, sender = socket.recvfrom(BUFFER_SIZE)
socket = Socket.new(:INET, :DGRAM)
require 'socket'
# Size in bytes of a C `ifreq` structure on a 64-bit system
# http://man7.org/linux/man-pages/man7/netdevice.7.html
#
# struct ifreq {
# char ifr_name[IFNAMSIZ]; /* Interface name */
# union {
# struct sockaddr ifr_addr;
# struct sockaddr ifr_dstaddr;
# Receive every packet
ETH_P_ALL = 0x0300
# Size in bytes of a C `sockaddr_ll` structure on a 64-bit system
#
# struct sockaddr_ll {
# unsigned short sll_family; /* Always AF_PACKET */
# unsigned short sll_protocol; /* Physical-layer protocol */
# int sll_ifindex; /* Interface number */
# unsigned short sll_hatype; /* ARP hardware type */
require 'hexdump'
BUFFER_SIZE = 1024
loop do
data = socket.recv(BUFFER_SIZE)
Hexdump.dump(data)
end
class EthernetFrame
attr_reader :bytes
def initialize(bytes)
@bytes = bytes
end
def destination_mac
format_mac(bytes[0, 6])
end
def data
# Drop the first 14 bytes (MAC Header) and last 4 bytes (CRC Checksum)
IPPacket.new(bytes[14...-4])
end
class IPPacket
attr_reader :bytes
def initialize(bytes)
@bytes = bytes
end
def version
bytes[0] >> 4
end
def ihl
bytes[0] & 0xF
end