Skip to content

Instantly share code, notes, and snippets.

View iximiuz's full-sized avatar
🪲

Ivan Velichko iximiuz

🪲
View GitHub Profile
@iximiuz
iximiuz / serv_async.py
Last active February 13, 2024 11:00
Python Web Servers
# 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 / ethsend.py
Last active January 13, 2024 22:18
Send Ethernet frames from Python.
#!/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 January 13, 2024 22:17
A bunch of helper functions to create Linux bridges, network namespaces, and interconnect everything using veth pairs.
#!/usr/bin/env bash
set -xeuo pipefail
create_bridge() {
local nsname="$1"
local ifname="$2"
echo "Creating bridge ${nsname}/${ifname}"
{"time": "2023-12-11T00:00:01.123Z", "method": "GET", "path": "/foo/bar", "status_code": 200, "content_length": 423}
{"time": "2023-12-11T00:01:01.123Z", "method": "POST", "path": "/foo/bar", "status_code": 200, "content_length": 553}
{"time": "2023-12-11T00:02:01.123Z", "method": "GET", "path": "/foo/baz", "status_code": 200, "content_length": 345}
{"time": "2023-12-11T00:03:01.123Z", "method": "GET", "path": "/foo/baz", "status_code": 401, "content_length": 235}
@iximiuz
iximiuz / README.md
Last active July 6, 2023 11:52
execve() file descriptors sharing example

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).

# syntax=docker/dockerfile:1
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC
RUN <<EOF
set -eu
apt-get update
@iximiuz
iximiuz / net_lab_simple_vlan.sh
Last active November 20, 2022 23:33
Configure VLAN tagging on a Linux bridge.
#!/usr/bin/env bash
set -euo pipefail
create_bridge() {
local nsname="$1"
local ifname="$2"
echo "Creating bridge ${nsname}/${ifname}"
@iximiuz
iximiuz / keep-me-alive.js
Last active February 3, 2022 01:11
Node.js http server keep-alive behaviour test
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');
@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.
#!/usr/bin/env bash
set -euo pipefail
create_bridge() {
local nsname="$1"
local ifname="$2"
echo "Creating bridge ${nsname}/${ifname}"
#!/usr/bin/env python3
import json
import sys
for line in sys.stdin:
ds = eval(line)
print(json.dumps(ds))