Skip to content

Instantly share code, notes, and snippets.

View bew's full-sized avatar
😎
https://nohello.net

Benoit de Chezelles bew

😎
https://nohello.net
View GitHub Profile
// References:
//
// Wikipedia IPv4 / IPv6:
// - https://en.wikipedia.org/wiki/IPv4
// - https://en.wikipedia.org/wiki/IPv6_packet
//
// Working implem:
// - https://www.binarytides.com/packet-sniffer-code-in-c-using-linux-sockets-bsd-part-2/
#include <unistd.h> // close
@bew
bew / scapy_script.py
Created October 6, 2018 10:20
ARP sniffer & force reply a pre-defined MAC address to every ARP request - using Scapy
#!/usr/bin/env python
from scapy.all import ARP, Ether, sniff, sendp
def arp_do_stuff(pkt):
if ARP not in pkt:
return
arp_display(pkt)
@bew
bew / class pre allocation.cr
Created September 22, 2018 22:03
Class pre-allocation in Crystal
class Object
alias TypeIdType = Int32
end
#
# This is unsafe to store any class created through the ObjectPool, as as
# soon the ObjectPool is GC-ed, all pointers to classes inside that pool
# will have unknown behavior if used.
class ObjectPool(T)
@buffer : Pointer(UInt8)
@bew
bew / manual_class.cr
Created September 21, 2018 20:07
Manually create a class in Crystal, from existing memory
#macro data(name, &block)
#end
#
# #### Ultimately I want to do this:
# and be able to make a pre-allocated buffer of Person,
# and be able to get a reference to a single Person (but pre-allocated
# in that buffer)
#
#data Person do
# property name : String
@bew
bew / literal_expander_in_macros.cr
Last active May 25, 2018 20:50
ArrayLiteral expansion using macros only
# Based on https://github.com/crystal-lang/crystal/blob/26635f1052b99ab5b52a8b051020a25435dba751/src/compiler/crystal/semantic/literal_expander.cr
macro expand_literal_ArrayLiteral_empty(node)
{% puts "--- expansion for: #{node}" %}
Array({{ node.of }}).new
{% debug %}
end
@bew
bew / concurrent_rpc.cr
Last active March 9, 2024 17:57
Simple implementation of a concurrent server handling requests on STDIN
# How to use / how it works:
#
# NOTE: The server reads on STDIN, and writes on STDOUT
#
# Start the program. Now you can write something then press enter.
# The server will do something 'useful' with the input, and gives you the result.
#
# The requests/responses are handled asynchronously, and can handle multiple requests at the same time.
#
# So you can spam the input (write a lot of lines) then close the input (Ctrl-d on a terminal), and
@bew
bew / 5_combinators.cr
Created February 15, 2018 01:32
Functional programming in Crystal to implement parser combinators
# Following: https://fsharpforfunandprofit.com/posts/understanding-parser-combinators/
require "spec"
# Result types
record Success(T), value : T, remaining : String do
def_equals_and_hash value, remaining
end
// Super Fast Blur v1.1
// by Mario Klingemann <http://incubator.quasimondo.com>
//
// Tip: Multiple invovations of this filter with a small
// radius will approximate a gaussian blur quite well.
//
BImage a;
BImage b;
void setup()
@bew
bew / noprelude_exception.cr
Last active March 9, 2024 17:59
Minimal noprelude exception example in Crystal, with the generated LLVM IR code
# Compile it with: crystal build --prelude=empty --emit llvm-ir noprelude_exception.cr
lib LibC
alias SizeT = UInt64
fun printf(format : UInt8*, ...) : Int32
fun exit(exit_code : Int32) : NoReturn
end
require "callstack/lib_unwind"
module Contract
# We put everything in `included` macro so the constants don't clashes
# when used in multiple classes
macro included
CONTRACTS_FOR_DEF = {} of _ => _
macro __add_contract(contract)
\{% if CONTRACTS_FOR_DEF[:next_def] == nil %}
\{% CONTRACTS_FOR_DEF[:next_def] = [contract] %}
\{% else %}
\{% CONTRACTS_FOR_DEF[:next_def] << contract %}