http3 - nginx with quictls

nginx 从1.25开始支持QUIC和http3 # quictls 编译安装 ./Configure --prefix=$HOME/quictls --openssldir=$HOME/openssl-quic -fPIC no-shared make && make install # nginx 编译安装 auto/configure --with-debug --with-http_v3_module --with-http_v2_module --with-cc-opt="-I $HOME/quictls/include" --with-ld-opt="-L $HOME/quictls/lib64" --prefix=$HOME/nginx-quic --with-cc-opt="-DNGX_QUIC_DEBUG_PACKETS -DNGX_QUIC_DEBUG_CRYPTO" --with-openssl=$HOME/install/openssl make && make install nginx.conf user root; worker_processes 1; error_log logs/error.log debug; #pid logs/nginx.pid; events { worker_connections 1024; } http { log_format quic '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$http3"'; access_log logs/access.log quic; server { # for better compatibility it's recommended # to use the same port for quic and https listen 443 quic reuseport; listen 443 ssl ; http3 on; http2 on; quic_retry on; ssl_early_data on; #server_name server_name; ssl_certificate certs/cert.pem; ssl_certificate_key certs/priv.key; ssl_protocols TLSv1.3; add_header Alt-Svc 'h3=":443"; ma=86400'; location / { index index.html index.htm; } } } ...

2024-02-16 · 2 min · 215 words

Product of Array Except Self

问题: Given an array arr[] of n integers, construct a Product Array prod[] (of the same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. 解答: 遍历数组循环跳过当前元素,需要两层循环, 时间复杂度O(n^2),空间复杂度O(n) def arrayOfProducts(array): products = [1 for _ in range(len(array))] for i in range(len(array)): runningProduct = 1 for j in range(len(array)): if i != j: runningProduct = runningProduct * array[j] products[i] = runningProduct return products ...

2024-01-08 · 1 min · 185 words

io_uring echo-server

io_uring io_uring是Linux特有的异步I/O API。io_uring的名称来自用户空间和内核空间之间共享的环形缓冲区。在内核和用户空间之间进行通信使用环形缓冲区作为主要的通信模式。这种思想在业务系统中还是挺常见的: 比如用MQ、Kafka消息队列推送信息。一个接收队列, 一个发送队列,另外设计上也有Actor模型的影子, 应用和kernel, 分别是两个actor, 而actor的邮箱就是 SQ, CQ两个队列, 对于应用程序, 消息发送到SQ, 从CQ中获取结果,对于kernel是从SQ中获取信息, 将处理结果发送到CQ中。 传统echoserver 做一个简单echoserver。 #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define BUF_SIZE 512 int main(int argc, char const* argv[]) { int serverfd, clientfd; struct sockaddr_in server, client; int len; int port = 1234; char buffer[BUF_SIZE]; if (argc == 2) { port = atoi(argv[1]); } serverfd = socket(AF_INET, SOCK_STREAM, 0); if (serverfd < 0) { perror("create socket"); exit(1); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(port); len = sizeof(server); if (bind(serverfd, (struct sockaddr*)&server, len) < 0) { perror("bind socket"); exit(1); } if (listen(serverfd, 2048) < 0) { perror("listen socket"); exit(1); } while (1) { len = sizeof(client); if ((clientfd = accept(serverfd, (struct sockaddr*)&client, &len)) < 0) { perror("accept error"); exit(4); } memset(buffer, 0, sizeof(buffer)); int size = read(clientfd, buffer, sizeof(buffer)); if (size < 0) { perror("read error"); exit(5); } if (size == 0) { close(clientfd); continue; } if (write(clientfd, buffer, size) < 0) { perror("write error"); exit(6); } close(clientfd); } close(serverfd); return 0; } 使用python写一个客户端 ...

2024-01-07 · 8 min · 1623 words

C struct检查成员是否存在

如何判断C struct中是否存在某一成员,最开始想法是通过offsetof宏定义 #define offsetof(st, m) \ ((size_t)&(((st *)0)->m)) C语言的offsetof()宏是ANSI C标准库中的一个特性,位于stddef.h头文件中。它用于计算给定结构体或联合体类型中某个成员的偏移量(以字节为单位), 但是这种方式仅能检查存在的成员, 不存在编译会有下面类似报错 XXX.c:11:35: error: ‘struct MyStruct’ has no member named ‘member3’; did you mean ‘member1’? 生成成员名的map C/C++ 不支持反射,与例如 Java 或 C# 不同。这意味着在 C/C++ 中,你不能在运行时"检测"未知结构(或类)的成员,你必须在编译时知道它 - 通常是通过 #include 包含定义结构的头文件。实际上,你不能访问未定义结构的内容 - 只能传递指针。 from https://cplusplus.com/forum/beginner/283156/ 同样作者给出了一个解决方案,那就是把对象放到map中, 对于C语言的,可以使用脚本处理一下。 大概思路根据脚本解析结构体,生成包含成员的数组,通过数组判断 # generate_struct_member_array.py import re def filter_content(content): # Remove comments content = re.sub(r'\/\*[\s\S]*?\*\/|\/\/.*', '', content) # Remove #define statements content = re.sub(r'#define\s+\w+\s+\w+', '', content) return content def parse_header(header_content): struct_pattern = re.compile(r'struct\s+(\w+)\s*{([^}]*)};', re.DOTALL) structs = struct_pattern.findall(header_content) struct_member_arrays = [] for struct_name, struct_body in structs: members = re.findall(r'\b\w+\s+(\w+)\s*(?:\[\w*\])?;', struct_body) struct_member_arrays.append((struct_name, members)) return struct_member_arrays def generate_array_code(struct_member_arrays): code = "" for struct_name, members in struct_member_arrays: code += f"const char *{struct_name}_member_names[] = {{\n" for member in members: code += f'\t"{member}",\n ' code = code.rstrip(', ') code += "};\n" return code def write_to_file(file_path, content): with open(file_path, 'w') as output_file: output_file.write(content) def read_and_generate(input_file_path, output_file_path): # Read the input header file content with open(input_file_path, 'r') as header_file: header_content = header_file.read() # Filter content (remove comments and #define statements) filtered_content = filter_content(header_content) # Parse the header file struct_member_arrays = parse_header(filtered_content) # Generate code for arrays array_code = generate_array_code(struct_member_arrays) # Write generated code to a new file write_to_file(output_file_path, array_code) print(f"Generated code has been written to {output_file_path}") # Specify the path to the input header file and the output header file input_header_file_path = 'hello.h' output_header_file_path = 'generated_define.h' # Read the input header file, generate code, and write to the output header file read_and_generate(input_header_file_path, output_header_file_path) hello.h ...

2024-01-01 · 4 min · 753 words

SNI

SNI是TLS协议扩展, 在握手的开始标识其尝试连接的主机名, 当多个HTTPS服务部署在同一IP地址上,客户端就可以通过这个标识指定它将使用哪一个服务, 同时服务端也无需使用相同的证书,它在概念上相当于HTTP/1.1基于名称的虚拟主机。SNI扩展最早在2003年的RFC 3546中出现。 HTTP服务通过 Http header “Host”, 来选择指定服务, HTTPS服务就是通过这个SNI来区分。通过可以通过nginx来验证一下 #user nobody; worker_processes 1; #error_log logs/error.log; error_log logs/error.log debug; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { access_log logs/access.log ; server { listen 8443 ssl ; server_name test; ssl_certificate certs/cert.crt; ssl_certificate_key certs/cert.key; location / { return 200 "https-test\r\n"; } } server { listen 8443 ssl ; server_name garlic; ssl_certificate certs/cert2.crt; ssl_certificate_key certs/cert.key; location / { return 200 "https-garlic\r\n"; } } server { listen 8443 ssl ; server_name 10.10.10.10; ssl_certificate certs/cert3.crt; ssl_certificate_key certs/cert.key; location / { root html; index index.html index.htm; } } server { listen 9443 ssl ; server_name snitest; ssl_certificate certs/cert4.crt; ssl_certificate_key certs/cert.key; location / { root html; index index.html index.htm; } } server { listen 8080 ; server_name http-test; location / { return 200 "http-test\r\n"; } } server { listen 8080 ; server_name http-garlic; location / { return 200 "http-garlic\r\n"; } } } ...

2023-12-11 · 4 min · 648 words

LeetCode-longest-peak

问题 Given an array arr[] with N elements, the task is to find out the longest sub-array which has the shape of a mountain. A mountain sub-array consists of elements that are initially in ascending order until a peak element is reached and beyond the peak element all other elements of the sub-array are in decreasing order. Input: arr = [1, 3, 1, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5] Output: 11 Explanation: There are two sub-arrays that can be considered as mountain sub-arrays. The first one is from index 0 – 2 (3 elements) and next one is from index 2 – 12 (11 elements). As 11 > 2, our answer is 11. ...

2023-11-26 · 2 min · 299 words

systemctl 启动服务缓慢

服务启动缓慢 使用systemd配置服务启动慢 strace -s 1024 systemctl start AAA 有 -1 : EAGAIN (Resource temporarily unavailable) 报错及卡顿, 查看了服务配置依赖项 /etc/systemd/system/multi-user.target.wants 目录下找到服务配置文件, 查看对应配置文件 Unit配置项里的Requires值, 查找对应依赖服务发现有一个服务异常, 这个服务启动脚本中启动network服务, 由于配置网卡错误导致无法启动。 systemd systemd 是 Linux 操作系统的系统和服务管理器系统。 当作为启动时的第一个进程(作为 PID 1)运行时,它充当 启动和维护用户空间服务的 init 系统。为登录用户启动单独的实例来启动他们的服务。 garlic@garlic:~$ ls -l /sbin/init lrwxrwxrwx 1 root root 20 Mar 20 2023 /sbin/init -> /lib/systemd/systemd garlic@garlic:~$ uname -a Linux garlic 6.2.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Tue Oct 3 13:14:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux 创建一个服务 之前写的应用软件自己写daemon进程,然后通过数据库完成配置管理,然后通过一个工具实现服务器启动关闭。简单的应用也是够用的。 ...

2023-10-21 · 2 min · 286 words

linux sendfile

文件复制 使用c语言实现一下文件复制功能: 第一个版本:使用fgetc, fputc。 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/sendfile.h> #include <stdio.h> #include <unistd.h> int main(int argc, char **argv) { const char *fromfile = argv[1]; const char *tofile = argv[2]; char c; FILE *fromfd = fopen(fromfile, "r"); FILE *tofd = fopen(tofile, "w"); while ((c = fgetc(fromfd)) != EOF) { fputc(c, tofd); } fclose(fromfd); fclose(tofd); } 使用fallocate -l 512M a1.txt, 生成一个512M的文件 garlic@garlic:~/sendfile$ sudo strace -c ./fget a1.txt b1.txt % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 58.23 4.191305 31 131072 write 41.77 3.006373 22 131074 read 0.00 0.000051 12 4 close 0.00 0.000000 0 8 mmap 0.00 0.000000 0 3 mprotect 0.00 0.000000 0 1 munmap 0.00 0.000000 0 3 brk 0.00 0.000000 0 2 pread64 0.00 0.000000 0 1 1 access 0.00 0.000000 0 1 execve 0.00 0.000000 0 2 1 arch_prctl 0.00 0.000000 0 1 set_tid_address 0.00 0.000000 0 4 openat 0.00 0.000000 0 4 newfstatat 0.00 0.000000 0 1 set_robust_list 0.00 0.000000 0 1 prlimit64 0.00 0.000000 0 1 getrandom 0.00 0.000000 0 1 rseq ------ ----------- ----------- --------- --------- ---------------- 100.00 7.197729 27 262184 2 total 使用strace统计一下结果, 耗时7s, 主要系统调用是read和write, 分别进行了13万左右的调用。(第二次运行快一些,应该是这部分数据还没有换出内存) ...

2023-09-16 · 23 min · 4773 words

linux 生成指定大小文件

linux生成指定大小文件常用的几个命令: fallocate truncate dd head tail fallocate...

2023-08-19 · 1 min · 180 words

getopt_long

getopt_long可以用来解析命令行参数,也可以进行参数解析。例如 #include <stdio.h> /* for printf */ #include <stdlib.h> /* for exit */ #include <getopt.h> static int verbose_flag; int parse(int argc, char **argv) { int c; int digit_optind = 0; while (1) { int this_option_optind = optind ? optind : 1; int option_index = 0; static struct option long_options[] = { {"verbose", no_argument, &verbose_flag, 1}, {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } }; c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); if (c == -1) break; switch (c) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != this_option_optind) printf("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf("option %c\n", c); break; case 'a': printf("option a\n"); break; case 'b': printf("option b\n"); break; case 'c': printf("option c with value '%s'\n", optarg); break; case 'd': printf("option d with value '%s'\n", optarg); break; case '?': break; default: printf("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (verbose_flag) printf ("verbose flag is set\n"); return 0; } void main() { char *argv[6]={0}; int argc = 6; argv[0] = "prog1"; argv[1] = "--add"; argv[2] = "AA"; argv[3] = "-d"; argv[4] = "BB"; argv[5] = "--verbose"; parse(argc, argv); optind = 1; char *argv2[4]={0}; int argc2 = 4; argv2[0] = "prog2"; argv2[1] = "--file"; argv2[2] = "CC"; argv2[3] = "--verbose"; //argv2[3] = "--delete"; //argv2[4] = "DD"; parse(argc2, argv2); return ; } ...

2023-08-09 · 3 min · 617 words