2024/04/18

[C] chek cpu endian

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


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

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

  1. Create a class 1:1 under 1: of br-lan and set the speed limit to 1000bps.
  2. For class 1:1 set its netem to delay 100ms and jitter 10ms.
  3. 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

  1. Create a class 1:2 under 1: of br-lan and set the speed limit to 1000bps.
  2. For class 1:2 set its netem to loss 0%, duplicate 0%, delay 30ms and jitter 20ms.
  3. 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--) {
if (flag || src[srcsize]) {
dest[pos++] = HEX_CHAR[src[srcsize]>>4];
dest[pos++] = HEX_CHAR[src[srcsize]&0xf];
} } } void hex2bytes(char *src, uint8_t* dest, uint8_t destsize) { uint8_t len; uint8_t pos; uint8_t hilo; uint8_t byte; uint8_t lohi[2]; char *cptr; len = strlen(src); if (len > (destsize* 2)) {
return; } cptr = src + len; pos = 0; ++len; while (len) { memset(lohi, 0, 2); hilo = 2; while (hilo-- && --len) { lohi[hilo] = HEX_VALUE[*--cptr]; } dest[pos++] = lohi[1] | (lohi[0] << 4); } return; }




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 --help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --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 quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.