snort中的实用函数的说明

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;
}

你可能感兴趣的:(函数)