Wireshark自定义协议字段:hf_register_info结构体详解

Wireshark自定义协议字段:hf_register_info结构体详解

【免费下载链接】wireshark Read-only mirror of Wireshark's Git repository at https://gitlab.com/wireshark/wireshark. ⚠️ GitHub won't let us disable pull requests. ⚠️ THEY WILL BE IGNORED HERE ⚠️ Upload them at GitLab instead. 【免费下载链接】wireshark 项目地址: https://gitcode.com/gh_mirrors/wi/wireshark

在网络协议分析中,准确解析协议字段是关键。Wireshark通过hf_register_info结构体实现协议字段的注册与管理,本文将系统讲解其使用方法与实战技巧。

结构体定义与核心成员

hf_register_info结构体定义于epan/proto.h,用于描述协议字段的元数据:

typedef struct _hf_register_info {
    int *id;                     /* 字段ID指针 */
    const char *name;            /* 字段显示名称 */
    const char *abbrev;          /* 字段缩写(用于过滤器) */
    ftenum_t type;               /* 字段数据类型 */
    int display;                 /* 显示格式 */
    const void *strings;         /* 枚举类型字符串表 */
    int bitmask;                 /* 位掩码(用于位域字段) */
    const char *blurb;           /* 字段描述 */
    int hf_flags;                /* 字段标志 */
    const struct _value_string *vals; /* 值字符串映射表 */
} hf_register_info;

主要成员功能说明:

  • id: 由Wireshark分配的唯一标识符,用于后续字段引用
  • abbrev: 必须符合Wireshark过滤语法规范,如http.request.method
  • type: 支持FT_UINT8/FT_STRING/FT_IPv4等20+数据类型(epan/ftypes/ftypes.h)
  • display: 控制字段显示格式,如BASE_DEC(十进制)、BASE_HEX(十六进制)

字段注册流程

协议字段注册需配合proto_register_field_array()函数,典型流程如下:

  1. 定义字段数组
static hf_register_info hf[] = {
    { &hf_myproto_version,
      { "Protocol Version", "myproto.version",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Version number of MyProtocol", HFILL }},
    // 更多字段...
};
  1. 调用注册函数
int proto_myproto = -1;

void proto_register_myproto(void) {
    proto_myproto = proto_register_protocol(
        "My Protocol", "MyProto", "myproto");
    proto_register_field_array(proto_myproto, hf, array_length(hf));
}

完整示例可参考doc/packet-PROTOABBREV.c模板文件。

高级应用技巧

位域字段处理

通过bitmask成员实现多字段共用一个字节:

{ &hf_myproto_flags_urg,
  { "Urgent Flag", "myproto.flags.urg",
    FT_BOOLEAN, 8, NULL, 0x02,  // 第2位
    "Urgent data indicator", HFILL }},

动态值解析

使用vals成员关联值字符串表:

static const value_string cmd_vals[] = {
    { 0x01, "CONNECT" },
    { 0x02, "DISCONNECT" },
    { 0, NULL }
};

{ &hf_myproto_command,
  { "Command", "myproto.command",
    FT_UINT8, BASE_HEX, VALS(cmd_vals), 0x0,
    "Command type", HFILL }},

字段依赖关系

通过hf_flags设置字段依赖:

{ &hf_myproto_checksum,
  { "Checksum", "myproto.checksum",
    FT_UINT16, BASE_HEX, NULL, 0x0,
    NULL, HF_REQUIRES_FRAME }},  // 仅在完整帧时显示

调试与验证

注册后的字段可通过以下方式验证:

  1. Wireshark过滤器:使用abbrev字段测试过滤,如myproto.version == 3
  2. 协议树视图:检查字段显示名称与描述是否正确
  3. 测试用例:参考test/suite_dissectors/编写验证用例

常见问题排查:

  • 字段不显示:检查proto_register_field_array()调用顺序
  • 过滤器无效:确认abbrev无特殊字符且唯一
  • 类型错误:参考epan/ftypes/确保类型与实际数据匹配

相关资源与参考

掌握hf_register_info结构体是开发Wireshark协议解析器的基础。合理设计字段结构不仅能提升解析效率,还能为后续统计分析与过滤提供强大支持。建议结合doc/wsug_src/用户指南,打造符合社区规范的高质量协议解析器。

【免费下载链接】wireshark Read-only mirror of Wireshark's Git repository at https://gitlab.com/wireshark/wireshark. ⚠️ GitHub won't let us disable pull requests. ⚠️ THEY WILL BE IGNORED HERE ⚠️ Upload them at GitLab instead. 【免费下载链接】wireshark 项目地址: https://gitcode.com/gh_mirrors/wi/wireshark

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值