Skip to content

Instantly share code, notes, and snippets.

@MathisMARION
MathisMARION / sysctl.c
Last active May 16, 2025 12:33
A minimalist version of the `sysctl` command to get and set system configuration parameters.
#include <fcntl.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
int main(int argc, char *argv[])
@MathisMARION
MathisMARION / sha1.c
Last active February 7, 2024 20:56
SHA-1 implementation (http://dx.doi.org/10.6028/NIST.FIPS.180-4). Written for learning purposes, do not use for production.
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#define SHA1_LEN (160 / 8)
#define SHA1_BLOCK_LEN (512 / 8)
struct sha1_ctx {
uint32_t H[SHA1_LEN / sizeof(uint32_t)];
@MathisMARION
MathisMARION / sha256.c
Created February 7, 2024 20:59
SHA-256 implementation (http://dx.doi.org/10.6028/NIST.FIPS.180-4). Written for learning purposes, do not use for production.
#include <assert.h>
#include <stdint.h>
#include <string.h>
#define SHA256_LEN (256 / 8)
#define SHA256_BLOCK_LEN (512 / 8)
struct sha256_ctx {
uint32_t H[SHA256_LEN / sizeof(uint32_t)];
uint64_t l;
@MathisMARION
MathisMARION / ip6-cksum.h
Last active March 14, 2025 16:29
IPv6 checksum
#ifndef IP6_CKSUM_H
#define IP6_CKSUM_H
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdint.h>
// RFC 8200 8.1. Upper-Layer Checksums
// RFC 4443 2.3. Message Checksum Calculation
@MathisMARION
MathisMARION / which.c
Last active September 24, 2025 12:50
A minimalist version of the `which` command to expand a program path using the `PATH` environment variable.
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
int which(const char *filename, char out[PATH_MAX])
@MathisMARION
MathisMARION / dhcp6-relay.c
Created March 14, 2025 16:30
Simple DHCPv6 Relay Agent implementation for Linux (https://datatracker.ietf.org/doc/html/rfc8415#section-19)
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <err.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
int main()
{