在 linux 内核中使用正则表达式
Use regex in linux kernel
我想在 linux 内核模块中使用正则表达式。
我准备好正则表达式,并在用户 space 中用 regex.h
.
测试了它们
我知道 regex.h
是一个用户 space 库,但是有内核替代品吗?
谢谢!
编辑:我正在尝试在大学项目的 SMTP 连接中查找 C 代码。
内核已经有正则表达式引擎;这些函数在 kernel/trace/trace.h. It is part of the linux trace framework, and could relatively easily be adapted. You can see where it is currently used (identifier search).
中声明
另请注意,有人比您先到达那里。 The l7-filter
kernel component classifies L7 traffic based on (inter-alia) regex matching the packets. Their regex implementation has a man page.
您还可以使用 libipq
通过套接字将数据包传递到用户空间,对它们进行分类,然后将它们传回。这是一种更简单的方法,但速度较慢,而且显然不是您想要的(但可能对其他人有用 reader)。
linux 内核没有真正的正则表达式函数。它只有处理字符串中“*”的函数。
l7-filter kernel component提供了regex函数,但是match函数是递归形式的,不适合小栈的内核
你最好的选择是 kpcre kernel component,它支持无堆栈的即时编译。它提供 POSIX 正则表达式函数以及 PCRE(perl 兼容正则表达式)函数。
如果只需要简单的模式匹配,也可以使用glob匹配功能。
bool __pure glob_match(char const *pat, char const *str)
这是它可以支持的内容的联机帮助页:
https://www.man7.org/linux/man-pages/man7/glob.7.html
内核代码中的函数定义如下:
https://elixir.bootlin.com/linux/latest/source/lib/glob.c
您需要为此包含头文件 "linux/glob.h"
。
我想在 linux 内核模块中使用正则表达式。
我准备好正则表达式,并在用户 space 中用 regex.h
.
我知道 regex.h
是一个用户 space 库,但是有内核替代品吗?
谢谢!
编辑:我正在尝试在大学项目的 SMTP 连接中查找 C 代码。
内核已经有正则表达式引擎;这些函数在 kernel/trace/trace.h. It is part of the linux trace framework, and could relatively easily be adapted. You can see where it is currently used (identifier search).
中声明另请注意,有人比您先到达那里。 The l7-filter
kernel component classifies L7 traffic based on (inter-alia) regex matching the packets. Their regex implementation has a man page.
您还可以使用 libipq
通过套接字将数据包传递到用户空间,对它们进行分类,然后将它们传回。这是一种更简单的方法,但速度较慢,而且显然不是您想要的(但可能对其他人有用 reader)。
linux 内核没有真正的正则表达式函数。它只有处理字符串中“*”的函数。
l7-filter kernel component提供了regex函数,但是match函数是递归形式的,不适合小栈的内核
你最好的选择是 kpcre kernel component,它支持无堆栈的即时编译。它提供 POSIX 正则表达式函数以及 PCRE(perl 兼容正则表达式)函数。
如果只需要简单的模式匹配,也可以使用glob匹配功能。
bool __pure glob_match(char const *pat, char const *str)
这是它可以支持的内容的联机帮助页: https://www.man7.org/linux/man-pages/man7/glob.7.html
内核代码中的函数定义如下: https://elixir.bootlin.com/linux/latest/source/lib/glob.c
您需要为此包含头文件 "linux/glob.h"
。