(Albert 2019.4.29)
目录
一、前言
在分析一些工具或者库的源码时,我们不妨先看看它的API文档,但是在nDPI的快速入门指南中,API文档只有少数的一些函数。但是在nDPI的src/include文件夹中,有一个头文件“ndpi_api.h”,里面声明了非常多的函数。在我们分析其流程之前,可以先分析浏览一下这个头文件中声明的函数,大概搞清楚他们的功能,哪怕是字面意义上,之后在我们分析函数主体时便于我们理解,遇到不清楚的也可以回来查阅,下面我将展示出“ndpi_api.h”的源代码,里面有我的一些注释,供大家参考:
二、ndpi_api.h 源代码分析及注释
/*相关宏定义*/
#ifndef __NDPI_API_H__
#define __NDPI_API_H__
#include "ndpi_main.h"
#ifdef __cplusplus
extern "C" {
#endif
/* 与nDPI动态链接的app可以通过使用下面的宏定义来确认数据结构,跨版本适配。
*/
#define NDPI_API_VERSION 1
#define SIZEOF_ID_STRUCT ( sizeof(struct ndpi_id_struct) )
#define SIZEOF_FLOW_STRUCT ( sizeof(struct ndpi_flow_struct) )
#define NDPI_DETECTION_ONLY_IPV4 ( 1 << 0 )
#define NDPI_DETECTION_ONLY_IPV6 ( 1 << 1 )
#define ADD_TO_DETECTION_BITMASK 1
#define NO_ADD_TO_DETECTION_BITMASK 0
#define SAVE_DETECTION_BITMASK_AS_UNKNOWN 1
#define NO_SAVE_DETECTION_BITMASK_AS_UNKNOWN 0
/**
*查看字符串是否使用了域名码的编码方式的函数
* ( https://tools.ietf.org/html/rfc3492 )
* 参数含义:
* @par buff = 指向检查的字符串的指针
* @par len = 字符串长度
* @return 1 如果这个字符串是域名码;
* else 0
*
*/
int check_punycode_string(char *buff, int len);
/**
* 获得流结构的长度大小
*
* @返回ndpi_flow_struct长度大小
*
*/
u_int32_t ndpi_detection_get_sizeof_ndpi_flow_struct(void);
/**
* 获得id struct长度大小
*
* @返回其长度大小
*
*/
u_int32_t ndpi_detection_get_sizeof_ndpi_id_struct(void);
/**
* nDPI 分配内存跟释放函数
**/
void * ndpi_malloc(size_t size);//动态分配长度为size的内存空间,返回指向这块内存的指针
void * ndpi_calloc(unsigned long count, size_t size); //分配内存,初始化,用于存数组
void * ndpi_realloc(void *ptr, size_t old_size, size_t new_size);//增加内存大小
char * ndpi_strdup(const char *s); //拷贝字符串
void ndpi_free(void *ptr);//释放内存
void * ndpi_flow_malloc(size_t size);
void ndpi_flow_free(void *ptr);
/**
* 搜索子字符串第一次出现的地方 -find- in -s-
* 搜索仅限于字符串的第一个-slen-字符
*
* @par s = 解析字符串
* @par find = 要与-s匹配的字符串
* @par slen =匹配 -s- and -find-的最大长度
* @返回一个指针,指向定位到的子字符串;
* 返回空指针则表示子字符串未找到
*
*/
char* ndpi_strnstr(const char *s, const char *find, size_t slen);
/**
* 与 ndpi_strnstr函数功能相似但是不区分大小写
*
* @par s =解析的字符串
* @par find = 要与 -s-匹配的字符串
* @par slen = max length to match between -s- and -find-
* @返回一个指针,指向定位到的子字符串;
* 返回空指针则表示子字符串未找到
*
*/
char* ndpi_strncasestr(const char *s, const char *find, size_t slen);
/**
*返回nDPI协议ID用于基于IP的协议检测
*
* @par ndpi_struct = 创建这个结构体用于协议检测
* @par pin =IP主机地址(必须按网络字节顺序):
*详情见Man(7)IP
* @返回nDPI协议ID
*
*/
u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_struct,struct in_addr *pin);
/**
* 初始化单个协议的匹配
*
* @par ndpi_mod = 创建这个结构体用于协议检测
* @par match = 结构传递进函数,用于协议匹配
*
*/
void ndpi_init_protocol_match(struct ndpi_detection_module_struct *ndpi_mod,
ndpi_protocol_match *match);
/**
* 模块初始化函数,返回一个新的初始化过的检测模块
*
* @返回一个初始化的检测检测模块
*
*/
struct ndpi_detection_module_struct *ndpi_init_detection_module(void);
/**
* 释放指定流中分配的内存
*
* @par flow = 需要被释放内存的流
*
*/
void ndpi_free_flow(struct ndpi_flow_struct *flow);
/**
*使能存储器支持.
* In nDPI is used for some protocol用于支持一些协议 (i.e. Skype)
*
* @par ndpi_mod =用于协议检测的结构体
* @par host = 域(主机)名字符串
* @par port = 端口名(无符号整型)
*
*/
void ndpi_enable_cache(struct ndpi_detection_module_struct *ndpi_mod,
char* host, u_int port);
/**
* 清除检测模块
*
* @par ndpi_struct = 需要清除的检测模块
*
*/
void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_struct);
/**
* 设置单个协议的位掩码
* 此函数不会增加回调缓冲区的索引
*
* @par label = 协议名字符串
* @par ndpi_struct = 检测模块
* @par detection_bitmask = 检测位掩码
* @par idx = 每个协议的回调函数的索引
* @par func = 协议搜索的函数指针
* @par ndpi_selection_bitmask = 被选择的位掩码
* @par b_save_bitmask_unknow = 如果被设置为“true”,将检测的位掩码保存为unknown
* @par b_add_detection_bitmask = 如果被设置为“true”添加这个协议的bitmask到detection bitmask中
*
*/
void ndpi_set_bitmask_protocol_detection(char *label,
struct ndpi_detection_module_struct *ndpi_struct,
const NDPI_PROTOCOL_BITMASK *detection_bitmask,
const u_int32_t idx,
u_int16_t ndpi_protocol_id,
void (*func) (struct ndpi_detection_module_struct *,
struct ndpi_flow_struct *flow),
const NDPI_SELECTION_BITMASK_PROTOCOL_SIZE ndpi_selection_bitmask,
u_int8_t b_save_bitmask_unknow,
u_int8_t b_add_detection_bitmask);
/**
*设置协议的 bitmask2
*
* @par ndpi_struct =检测模块
* @par detection_bitmask = the protocol bitmask to set
*
*/
void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *ndpi_struct,const NDPI_PROTOCOL_BITMASK * detection_bitmask);
/**
* 在未知匹配的情况下要调用的函数,以查看当前ndpi首选项配置是否阻止了部分匹配
*
* @par ndpi_struct = 检测模块
* @par flow = 检测模块检测的流
* 即使流不是完整的,也返回检测到的协议;
*
*/