This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-kni/common/include/kni_utils.h

95 lines
2.8 KiB
C
Raw Normal View History

2019-05-09 15:14:01 +08:00
//TODO: 日志打印出文件名 + 行号
#pragma once
#include <stdio.h>
#include <stdlib.h>
2019-05-17 17:04:50 +08:00
#include <string.h>
2019-05-09 15:14:01 +08:00
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
2019-05-17 17:04:50 +08:00
#include <netinet/tcp.h>
2019-05-09 15:14:01 +08:00
#include <time.h>
#include "MESA/MESA_handle_logger.h"
#include "MESA/MESA_htable.h"
#include "MESA/MESA_prof_load.h"
#include "field_stat2.h"
#include "Maat_rule.h"
#include "Maat_command.h"
#define KNI_STRING_MAX 2048
#define KNI_PATH_MAX 256
#define KNI_SYMBOL_MAX 64
2019-05-17 17:04:50 +08:00
#define KNI_DOMAIN_MAX 256
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
2019-05-09 15:14:01 +08:00
#define likely(expr) __builtin_expect((expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0)
#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define FREE(p) {free(*p);*p=NULL;}
#define KNI_LOG_ERROR(handler, fmt, ...) \
do { \
char location[KNI_PATH_MAX]; \
snprintf(location, KNI_PATH_MAX, "%s: line %d", __FILE__, __LINE__); \
MESA_handle_runtime_log(handler, RLOG_LV_FATAL, location, fmt, ##__VA_ARGS__); } while(0)
#define KNI_LOG_INFO(handler, fmt, ...) \
do { \
char location[KNI_PATH_MAX]; \
snprintf(location, KNI_PATH_MAX, "%s: line %d", __FILE__, __LINE__); \
2019-05-17 17:04:50 +08:00
MESA_handle_runtime_log(handler, RLOG_LV_INFO, location, fmt, ##__VA_ARGS__); } while(0)
2019-05-09 15:14:01 +08:00
#define KNI_LOG_DEBUG(handler, fmt, ...) \
do { \
char location[KNI_PATH_MAX]; \
snprintf(location, KNI_PATH_MAX, "%s: line %d", __FILE__, __LINE__); \
MESA_handle_runtime_log(handler, RLOG_LV_DEBUG, location, fmt, ##__VA_ARGS__); } while(0)
2019-05-18 12:41:31 +08:00
2019-05-17 17:04:50 +08:00
//default tcp opt
#define KNI_DEFAULT_WINSCLE 0
#define KNI_DEFAULT_MSS 1460
#define KNI_DEFAULT_MTU 1500
#define KNI_MTU 3000
//TODO: 网络序
struct kni_tcpopt_info{
uint16_t mss;
uint8_t wscale;
uint8_t ts;
uint8_t sack;
};
2019-05-18 12:41:31 +08:00
//field_stat
#define KNI_FIELD_MAX 24
enum kni_field{
KNI_FIELD_TOT_PKT,
KNI_FIELD_BYP_PKT,
KNI_FIELD_INTCP_PKT,
KNI_FIELD_IPV6_PKT,
KNI_FIELD_NULL_PKT,
KNI_FIELD_NO_SYN_EXP,
KNI_FIELD_NO_SA_EXP,
KNI_FIELD_UNKNOWN_STATE_EXP,
KNI_FIELD_TOT_STM,
KNI_FIELD_BYP_STM,
KNI_FIELD_INTCP_STM,
KNI_FIELD_SSL_STM,
KNI_FIELD_HTTP_STM,
KNI_FIELD_UNKNOWN_STM,
};
struct kni_field_stat_handle{
screen_stat_handle_t handle;
int fields[KNI_FIELD_MAX];
};
2019-05-19 17:23:18 +08:00
2019-05-17 17:04:50 +08:00
uint16_t kni_ip_checksum(const void *buf, size_t hdr_len);
uint16_t kni_tcp_checksum(const void *_buf, size_t len, in_addr_t src_addr, in_addr_t dest_addr);
uint16_t kni_udp_checksum(const void *_buf, size_t len, in_addr_t src_addr, in_addr_t dest_addr);
struct kni_tcpopt_info* kni_get_tcpopt(struct tcphdr* tcphdr,int tcphdr_len);
MESA_htable_handle kni_create_htable(const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger);