1 2 3 4 5 6 7 8 | #include <stdint.h> static uint8_t is_little_endian(void) { uint16_t value = 0x0001; uint8_t *byte_ptr = (uint8_t *)&value; return (*byte_ptr == 0x01); // If the system is little endian, the least significant byte is 0x01. } |
Aim Wang
https://github.com/aimwang https://gitlab.com/aimwang
2024/04/18
[C] chek cpu endian
2024/04/09
[C] Create a folder even if the parent folder does not exist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | static int my_dirname(char *path) { char *p = strrchr(path, '/'); if (p == NULL || p == path) { return -1; } *p = '\0'; return 0; } static int my_mkdir(char *path) { struct stat st; char p[128]; char l[128]; mkdir(path, 0775); while ((stat(path, &st) != 0)) { memset(p, 0, 128); memcpy(p, path, strlen(path)); if (0 != my_dirname(p)) { return (-1); } while ((stat(p, &st) != 0)) { memset(l, 0, 128); memcpy(l, p, strlen(p)); // save the last not exist parent path. if (0 != my_dirname(p)) { return (-1); } } mkdir(l, 0775); if (stat(l, &st) != 0) { return (-1); } mkdir(path, 0775); } return (0); } |
2023/06/20
Use tc and netem to simulate network delay, jitter and packet loss.
netem relies on kernel module:
CONFIG_IFB
CONFIG_NET_SCHED
CONFIG_NET_SCH_NETEM.
tc qdisc add dev br-lan root handle 1: htb
- This will create a root qdisc on the br-lan interface for htb
tc class add dev br-lan parent 1: classid 1:1 htb rate 1000mbit
tc qdisc add dev br-lan parent 1:1 netem delay 100ms 10ms
tc filter add dev br-lan parent 1:0 protocol ip u32 match ip sport 5101 0xffff flowid 1:1
- Create a class 1:1 under 1: of br-lan and set the speed limit to 1000bps.
- For class 1:1 set its netem to delay 100ms and jitter 10ms.
- Direct the packet with src port 5101 of the br-lan interface to class 1:1.
tc class add dev br-lan parent 1: classid 1:2 htb rate 1000mbit
tc qdisc add dev br-lan parent 1:2 netem loss 0% duplicate 0% delay 30ms 20ms
tc filter add dev br-lan parent 1:0 protocol ip u32 match ip sport 5001 0xffff flowid 1:2
- Create a class 1:2 under 1: of br-lan and set the speed limit to 1000bps.
- For class 1:2 set its netem to loss 0%, duplicate 0%, delay 30ms and jitter 20ms.
- Direct the packet with src port 5001 of the br-lan interface to class 1:2.
2023/04/21
[C] 16進制字串與 byte 陣列戶轉
Convert between hex string and byte array.
以空間換取時間, 盡量減少計算跟比較.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | static uint8_t HEX_VALUE[256] = { // x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // 3x 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5x 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // Fx }; static uint8_t HEX_CHAR[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; void bytes2hex(uint8_t* src, char* dest, uint8_t srcsize) { uint8_t flag; uint8_t pos; flag = 0; pos = 0; while (srcsize--) { |
2023/03/16
Docker 筆記
列出所有 container
docker ps -a
啟動即停止 container
docker start container_id/name
docker stop container_id/name
刪除 container
docker rm container_id/name
在目前路徑執行 container 指令 (通常用於編譯)
docker --workdir $PWD container_id/name command
--------------------------------------------------------------------------------
docker --helpUsage: docker [OPTIONS] COMMANDA self-sufficient runtime for containersOptions:--config string Location of client config files (default "/home/jwang/.docker")-D, --debug Enable debug mode-H, --host list Daemon socket(s) to connect to-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")--tls Use TLS; implied by --tlsverify--tlscacert string Trust certs signed only by this CA (default "/home/jwang/.docker/ca.pem")--tlscert string Path to TLS certificate file (default "/home/jwang/.docker/cert.pem")--tlskey string Path to TLS key file (default "/home/jwang/.docker/key.pem")--tlsverify Use TLS and verify the remote-v, --version Print version information and quitManagement Commands:builder Manage buildsconfig Manage Docker configscontainer Manage containersengine Manage the docker engineimage Manage imagesnetwork Manage networksnode Manage Swarm nodesplugin Manage pluginssecret Manage Docker secretsservice Manage servicesstack Manage Docker stacksswarm Manage Swarmsystem Manage Dockertrust Manage trust on Docker imagesvolume Manage volumesCommands:attach Attach local standard input, output, and error streams to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogin Log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerps List containerspull Pull an image or a repository from a registrypush Push an image or a repository to a registryrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagesrun Run a command in a new containersave Save one or more images to a tar archive (streamed to STDOUT by default)search Search the Docker Hub for imagesstart Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containersversion Show the Docker version informationwait Block until one or more containers stop, then print their exit codesRun 'docker COMMAND --help' for more information on a command.