Skip to content

Instantly share code, notes, and snippets.

Avatar
🪲

Ivan Velichko iximiuz

🪲
View GitHub Profile
@iximiuz
iximiuz / net_lab_l3_to_l2_mapping.sh
Created March 18, 2021 08:46
Sets up IP subnets over a single Ethernet broadcast domain formed by a Linux bridge.
View net_lab_l3_to_l2_mapping.sh
#!/usr/bin/env bash
set -euo pipefail
create_bridge() {
local nsname="$1"
local ifname="$2"
echo "Creating bridge ${nsname}/${ifname}"
@iximiuz
iximiuz / net_lab_simple_vlan.sh
Last active November 20, 2022 23:33
Configure VLAN tagging on a Linux bridge.
View net_lab_simple_vlan.sh
#!/usr/bin/env bash
set -euo pipefail
create_bridge() {
local nsname="$1"
local ifname="$2"
echo "Creating bridge ${nsname}/${ifname}"
@iximiuz
iximiuz / ethsend.py
Last active March 3, 2023 12:14
Send Ethernet frames from Python.
View ethsend.py
#!/usr/bin/env python3
# Usage: ethsend.py eth0 ff:ff:ff:ff:ff:ff 'Hello everybody!'
# ethsend.py eth0 06:e5:f0:20:af:7a 'Hello 06:e5:f0:20:af:7a!'
#
# Note: CAP_NET_RAW capability is required to use SOCK_RAW
import fcntl
import socket
import struct
@iximiuz
iximiuz / net_lab_broadcast_domains.sh
Last active February 28, 2023 04:36
A bunch of helper functions to create Linux bridges, network namespaces, and interconnect everything using veth pairs.
View net_lab_broadcast_domains.sh
#!/usr/bin/env bash
set -xeuo pipefail
create_bridge() {
local nsname="$1"
local ifname="$2"
echo "Creating bridge ${nsname}/${ifname}"
View pprint2json.py
#!/usr/bin/env python3
import json
import sys
for line in sys.stdin:
ds = eval(line)
print(json.dumps(ds))
@iximiuz
iximiuz / serv_async.py
Last active May 7, 2021 10:22
Python Web Servers
View serv_async.py
# python3
import asyncio
import sys
counter = 0
async def run_server(host, port):
server = await asyncio.start_server(serve_client, host, port)
await server.serve_forever()
@iximiuz
iximiuz / walkdir.js
Created March 13, 2019 13:43
Tiny recursive walkdir (aka readdir) in JavaScript
View walkdir.js
const fs = require('fs');
const path = require('path');
async function *walkdir(dir) {
const stack = [dir];
while (stack.length) {
const filename = stack.pop();
const stat = await fs.promises.stat(filename);
if (stat.isDirectory()) {
const files = (await fs.promises.readdir(filename))
@iximiuz
iximiuz / keep-me-alive.js
Last active February 3, 2022 01:11
Node.js http server keep-alive behaviour test
View keep-me-alive.js
const http = require('http');
const log = console.log;
console.log = (...args) => {
log.apply(console, [new Date().toISOString()].concat(args));
};
const port = process.argv[2];
const server = http.createServer((req, res) => {
console.log('Incoming request');
View indices.md
i => j1 = 2*i           0 => 0     1 => 2    2 => 4
     j2 = 2*i+1         0 => 1     1 => 3    2 => 5

i = j//2                0 => 0     2 => 1    4 => 2
                        1 => 0     3 => 1    5 => 2


i => j1 = 2*i + 1       0 => 1    1 => 3    2 => 5
 j2 = 2*i + 2 0 => 2 1 => 4 2 => 6
@iximiuz
iximiuz / README.md
Last active August 2, 2022 00:37
execve() file descriptors sharing example
View README.md

Description

As everybody knows, execve() system call replaces a calling process image with a new process image. At the same time the file descriptors table remains the same.

The idea of this example is to show how launched via execve() processes can access file descriptors from their parent.

The parent process creates 4 pipes (unidirectional data flows) for stdin/stdout streams of its two children. Then the parent forks itself twice to spawn its children. Each child process binds corresponding pipes to its stdin & stdout and then runs child executable via execl(). To simplify the example, child processes also explicitly shares their stdin file descriptors between each other via command line argument. In the end parent is able to read from children stdouts and write to their stdins, while children are able to write to sibling's stdins (see the schema below).