在平凡中也会有很多的快乐;有梦想,人才不会孤单
学会放弃~
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

snort中的实用函数的说明

Posted on 2005-09-06 20:56  情走边锋  阅读(886)  评论(0编辑  收藏  举报

1,fatal.h记录致命错误,调用FatalError函数,一共有两个函数定义。
在util.c中的FatalError函数支持可变参数,在sfutil.c中的FatalError仅输出字符串。
2,smalloc.h定义了
#define MALLOC(ptr, cast, size, flags) 分配内存
#define FREE(ptr) 释放内存
3,Byte_extract.h和Byte_extract.c,
// 字符串转换函数,
int string_extract(
                   int bytes_to_grab,  //解析字符串的长度
                   int base,                 //进制(10,代表十进制)
                   u_int8_t *ptr,        //被解析的字符串
                   u_int8_t *start,     //开始位置
                   u_int8_t *end,        //结束位置
                   u_int32_t *value       //返回值
                  );
// 字节转换处理函数,高低字节的转换
int byte_extract(
        int endianess,             //字节顺序
        int bytes_to_grab,     //   解析字节的长度
        u_int8_t *ptr,            //被解析的字节的指针
        u_int8_t *start,         //开始位置
        u_int8_t *end,        //结束位置
        u_int32_t *value         //返回值
        );
4,Bounds.h

//判断是否在边界内,哈哈。。。。
static INLINE int inBounds(u_int8_t *start, u_int8_t *end, u_int8_t *p)
{
    
if(p >= start && p < end)
    
{
        
return 1;
    }

    
return 0;
}

//在进行memcpy之前做一些验证。
//注意start,end为dst的start和end。
static INLINE int SafeMemcpy(void *dst, void *src, size_t n, void *start, void *end)
{
     
if(n < 1)
     
{
         ERRORRET;
     }


     
if(!inBounds(start,end, dst) || !inBounds(start,end,((u_int8_t*)dst)+n))
     
{
         ERRORRET;
     }


     memcpy(dst, src, n);
     
return 1;
}

//指针内容赋值
//注意start,end为dst的start和end。
static INLINE int SafeWrite(u_int8_t *start, u_int8_t *end, u_int8_t *dst, u_int8_t *src)
{
    
if(!inBounds(start, end, dst))
    
{
        ERRORRET;
    }

     
    
*dst = *src;        
    
return 1;
}

//---------------
static inline int SafeRead(u_int8_t *start, u_int8_t *end, u_int8_t *src, u_int8_t *read)
{
    
if(!inBounds(start,end, src))
    
{
        ERRORRET;
    }

    
    
*read = *start;
    
return 1;
}

5,Codes.h和Codes.c

//unicode的处理,现在还不清楚为什么要这样做
char codes[65536];

void init_codes(){
    
int i;
    unicode_entry 
*ptr;
    
    
for (i=0;i<65536;i++)
        codes[i]
=0x0;  /*question mark*/
    
    
/*the first 128 entries are the same as ascii*/
    
for (i=0;i<128;i++)
        codes[i] 
= (char) i;
    
    ptr
=unicode_data;
    
while(ptr->index != 0{
        codes[ptr
->index] = (char) ptr->val;
        ptr
++;
    }

6,Packet_time.h和Packet_time.c
 

static time_t s_first_packet  = 0;
static time_t s_recent_packet = 0;
//更新最近时间
void packet_time_update(time_t cur)
{
    
if(s_first_packet == 0)
    
{
        s_first_packet 
= cur;
    }


    s_recent_packet 
= cur;
}

//返回最近时间
time_t packet_timeofday(void)
{
    
return s_recent_packet;
}

//返回第一次的时间
time_t packet_first_time(void)
{
    
return s_first_packet;
}

科为网络安全