⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snap_interp.c

📁 一个学习SNMP项目:tmoerlan.
💻 C
📖 第 1 页 / 共 5 页
字号:
/* $Id: snap_interp.c,v 1.2 2003/09/17 11:26:10 tmoerlan Exp $ */#include <asm/types.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in_systm.h>#include <netinet/in.h>#include <netinet/ip.h>#include <sys/types.h>#include "bytecode.h"#include "config.h"#include "d_printf.h"#include "dyncheck.h"#include "io.h"#include "myassert.h"#include "packet.h"#include "timers.h"#include "exception.h"#include "wassert.h"#include "snap_kern_iface.h"#include "snap_svc_handler.h"#include "printval.h"#include "warn.h"struct iphdr  {#if __BYTE_ORDER == __LITTLE_ENDIAN    unsigned int ihl:4;    unsigned int version:4;#elif __BYTE_ORDER == __BIG_ENDIAN    unsigned int version:4;    unsigned int ihl:4;#else# error	"Please fix <bits/endian.h>"#endif    u_int8_t tos;    u_int16_t tot_len;    u_int16_t id;    u_int16_t frag_off;    u_int8_t ttl;    u_int8_t protocol;    u_int16_t check;    u_int32_t saddr;    u_int32_t daddr;    /*The options start here. */  };/*typedef struct ip struct iphdr;//#define iphdr ip#define ttl ip_ttl#define saddr (ip_src.s_addr)#define daddr (ip_dst.s_addr)*/#define NIPQUAD(addr) addr << 24 >> 24, addr << 16 >> 24, addr << 8 >> 24, addr >> 24#ifdef CONFIG_IP_SNAP#ifndef __KERNEL__extern int fprintf_value(packet_t *p, FILE *f, value_t *v);#endif/************************************************************************//* macros for use in interp_packet *//* Dynamic type checks */int heap_alloc(packet_t *p, int lenb, int flag, 	       heap_obj **ho, int *hoffset) {  int sizeb = lenb + sizeof(heap_obj);  DYNCHECK(((void *)p->stack_max - sizeb) > (void *)p->sp);  DYNCHECK(lenb <= MAX_HEAPOBJ_SZ);  p->stack_max = (value_t *)((void *)p->stack_max - sizeb);  *ho = (heap_obj *)((p)->stack_max);  *hoffset = (void *)(p)->stack_max - (p)->heap_min;  (*ho)->len = lenb;  (*ho)->flag = flag;  d_printf(100,"%s:%d: allocated %d-buf (f=%d) at %#x\n",	   __FILE__,__LINE__,lenb,flag,(unsigned int)ho);  return 0;}#ifdef __KERNEL__static void resize_to_max_buffer(packet_t *p, struct iphdr **interpiph) {  struct sk_buff *tmp;  struct iphdr *oldiph;  struct iphdr *newiph;  if (p->resized) {    /* already resized it */    printk(KERN_WARNING "%s:%d: resizing already resized buffer\n",	   __FILE__,__LINE__);    return;  }  if (!p->is_contiguous) {    /* uh-oh */    printk(KERN_WARNING "%s:%d: resizing non-contiguous buffer\n",	   __FILE__,__LINE__);    return;  }  d_printf(50,"%s:%d: growing buffer...\n",__FILE__,__LINE__);    tmp = skb_grow(p->skb,3000);	/* get the new buffer, copy data over */  /* see if there was any additional stack */  if ((void *)(p->sp) > (void *)(p->skb->tail)) {    memcpy(tmp->tail, p->skb->tail,	   (char *)(p->sp) - (char *)(p->skb->tail));  }  /* reset packet pointers */  oldiph = p->skb->nh.iph;  newiph = tmp->nh.iph;  *interpiph = newiph;    p->hdr = (header_t *)((void *)newiph + (newiph->ihl << 2));  p->code_min = (instr_t *)((void *)p->hdr + sizeof(header_t));  p->pc = (instr_t *)((void *)newiph +		      ((void *)p->pc - (void *)oldiph));  p->code_max = (instr_t *)((void *)p->code_min +			    ntohs(p->hdr->code_sizeb));  p->handler = (instr_t *)((void *)newiph +			   ((void *)p->handler - (void *)oldiph));  p->heap_min = (void *)p->code_max;  if (p->h_alloc_ptr != NULL) {    printk(KERN_WARNING "%s:%d: p->h_alloc_ptr should be NULL\n",	   __FILE__,__LINE__);  }  p->heap_max = p->heap_min + ntohs(p->hdr->heap_sizeb);  p->stack_min = (value_t *)p->heap_max;  p->sp = (value_t *)((void *)newiph +		      ((void *)p->sp - (void *)oldiph));  p->stack_max = (value_t *)tmp->end;  p->h_alloc_heap_max = tmp->end;  p->resized = 1;  p->is_contiguous = 1;  d_printf(50,"%s:%d: resized packet: %d instrs, %d stack vals\n",	  __FILE__,__LINE__,p->code_max - p->code_min, p->sp -	   p->stack_min);  d_printf(50,"%s:%d: hdr=0x%08x cmin=0x%08x pc=0x%08x cmax=0x%08x\n",	   __FILE__,__LINE__,	   (__u32)p->hdr, (__u32)p->code_min, (__u32)p->pc, (__u32)p->code_max);  d_printf(50,"%s:%d: hmin=0x%08x hmax=0x%08x\n",__FILE__,__LINE__,	   (__u32)p->heap_min, (__u32)p->heap_max);  d_printf(50,"%s:%d: smin=0x%08x sp=0x%08x smax=0x%08x\n",	   __FILE__,__LINE__,	   (__u32)p->stack_min, (__u32)p->sp, (__u32)p->stack_max);    /* make the new buffer live, chuck old buffer */  kfree_skb(p->skb);  p->skb = tmp;  return;}#define ENSURE_STACK_ROOM(p,iph) \  { if (p->sp >= p->stack_max && !p->resized) \      resize_to_max_buffer(p,&(iph)); \  }#define ENSURE_HEAP_ROOM(p,iph) \  { if (!p->resized) \      resize_to_max_buffer(p,&(iph)); \  }#else#define ENSURE_STACK_ROOM(p,iph)#define ENSURE_HEAP_ROOM(p,iph)#endif /* __KERNEL__ */#ifdef CONFIG_IP_SNAP#define DEBUGFMTCONCAT(fmt1,t,fmt2) \  (((t) == INTV) ? fmt1 "%d" fmt2 : \  (((t) == ADDRV) ? fmt1 "%d.%d.%d.%d" fmt2 : \  (((t) == TUPLEV) ? fmt1 "offs[%d]" fmt2 : \  (((t) == EXCV) ? fmt1 "exc(%d)" fmt2 : \  (((t) == STRV) ? fmt1 "\"%s\"" fmt2 : \  /* FLOATV */ fmt1 "%d.%06d" fmt2)))))#endif#ifdef ADDR_IN_HEAP#endif/* Instruction cases */#define INTOP(t,op,opname) {					\  value_t *top1 = p->sp - 1;					\  value_t *top2 = p->sp - 2;					\  int result;							\								\  DYNCHECK(top2 >= p->stack_min);				\  DYNCHECK_TAG(*top1,INTV);					\  DYNCHECK_TAG(*top2,INTV);					\  result = (t)GET_INT(*top2) op (t)GET_INT(*top1);		\  wassert((result <= MAX_VINT) && (result >= MIN_VINT));	\  d_printf(10,"%s:%d: pc=%d: %s %d %d = %d\n",                  \           __FILE__,__LINE__,(p->pc - p->code_min),opname,      \           GET_INT(*top2),GET_INT(*top1),result);               \  SET_TAG(*top2,INTV);						\  SET_INT(*top2,result);					\  p->sp--;							\  p->pc++;							\  break;							\}#define INTFLOATOP(op,opname) {					\  value_t *top1 = p->sp - 1;					\  value_t *top2 = p->sp - 2;					\								\  DYNCHECK(top2 >= p->stack_min);				\  if (GET_TAG(*top1) == INTV) {					\    int result;							\    DYNCHECK_TAG(*top2,INTV);					\    result = (int)GET_INT(*top2) op (int)GET_INT(*top1);	\    wassert((result <= MAX_VINT) && (result >= MIN_VINT));	\    d_printf(10,"%s:%d: pc=%d: %s %d %d = %d\n",                \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             GET_INT(*top2),GET_INT(*top1),result);             \    SET_TAG(*top2,INTV);					\    SET_INT(*top2,result);					\  } else {							\    float32 v1, v2, result;					\    ENSURE_HEAP_ROOM(p,iph);                                        \    top1 = p->sp - 1; \    top2 = p->sp - 2; \    DYNCHECK_TAG(*top1,FLOATV);					\    DYNCHECK_TAG(*top2,FLOATV);					\    GET_FLOAT(v1,p->heap_min,*top1);				\    GET_FLOAT(v2,p->heap_min,*top2);				\    result = v2 op v1;						\    d_printf(10,"%s:%d: pc=%d: %s %d.%06d %d.%06d = %d.%06d\n", \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             (int)v2,(int)((v2 - (int)v2) * 1000000),           \             (int)v1,(int)((v1 - (int)v1) * 1000000),           \             (int)result,(int)((result - (int)result) * 1000000)); \    SET_TAG(*top2,FLOATV);					\    SET_FLOAT(*top2,result,p);					\  }								\  p->sp--;							\  p->pc++;							\  break;							\}#define INTFLOATOPI(op,opname) {				\  value_t *top1 = p->sp - 1;					\  value_t *top2 = p->sp - 2;					\								\  DYNCHECK(top2 >= p->stack_min);				\  if (GET_TAG(*top1) == INTV) {					\    int result;							\    DYNCHECK_TAG(*top2,INTV);					\    result = (int)GET_INT(*top2) op (int)GET_INT(*top1);	\    wassert((result <= MAX_VINT) && (result >= MIN_VINT));	\    d_printf(10,"%s:%d: pc=%d: %s %d %d = %d\n",                \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             GET_INT(*top2),GET_INT(*top1),result);             \    SET_TAG(*top2,INTV);					\    SET_INT(*top2,result);					\  } else {							\    float32 v1, v2;						\    int result;							\    ENSURE_HEAP_ROOM(p,iph);                                        \    top1 = p->sp - 1; \    top2 = p->sp - 2; \    DYNCHECK_TAG(*top1,FLOATV);					\    DYNCHECK_TAG(*top2,FLOATV);					\    GET_FLOAT(v1,p->heap_min,*top1);				\    GET_FLOAT(v2,p->heap_min,*top2);				\    result = v2 op v1;						\    d_printf(10,"%s:%d: pc=%d: %s %d.%06d %d.%06d = %d.%06d\n", \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             (int)v2,(int)((v2 - (int)v2) * 1000000),           \             (int)v1,(int)((v1 - (int)v1) * 1000000),           \             (int)result,(int)((result - (int)result) * 1000000)); \    SET_TAG(*top2,INTV);					\    SET_INT(*top2,result);					\  }								\  p->sp--;							\  p->pc++;							\  break;							\}#define INTIMMOP(t,op,opname) {					\  value_t *top = p->sp - 1;					\  int lit;							\  int result;							\								\  DYNCHECK(top >= p->stack_min);				\  DYNCHECK_TAG(*top,INTV);					\  GET_LIT(lit,INTV,*p->pc);					\  result = (t)GET_INT(*top) op (t)lit;				\  wassert((result <= MAX_VINT) && (result >= MIN_VINT));	\  d_printf(10,"%s:%d: pc=%d: %s %d %d = %d\n",                  \           __FILE__,__LINE__,(p->pc - p->code_min),opname,      \           GET_INT(*top),lit,result);                           \  SET_TAG(*top,INTV);						\  SET_INT(*top,result);						\  p->pc++;							\  break;							\}#define INTFLOATIMMOP(op,opname) {				\  value_t *top = p->sp - 1;					\								\  DYNCHECK(top >= p->stack_min);				\  if (GET_TAG(*top) == INTV) {					\    int lit;							\    int result;							\    GET_LIT(lit,INTV,*p->pc);					\    result = GET_INT(*top) op lit;				\    wassert((result <= MAX_VINT) && (result >= MIN_VINT));	\    d_printf(10,"%s:%d: pc=%d: %s %d %d = %d\n",                \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             GET_INT(*top),lit,result);                         \    SET_TAG(*top,INTV);						\    SET_INT(*top,result);					\  } else {							\    float32 f1, lit, result;					\    value_t v = ZERO_VALUE_T;					\    ENSURE_HEAP_ROOM(p,iph);                                        \    top = p->sp - 1; \    DYNCHECK_TAG(*top,FLOATV);					\    COPY_LIT(v,FLOATV,*p->pc);					\    GET_FLOAT(lit,p->heap_min,v);				\    GET_FLOAT(f1,p->heap_min,*top);				\    result = f1 op lit;						\    d_printf(10,"%s:%d: pc=%d: %s %d.%06d %d.%06d = %d.%06d\n", \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             (int)f1,(int)((f1 - (int)f1) * 1000000),           \             (int)lit,(int)((lit - (int)lit) * 1000000),        \             (int)result,(int)((result - (int)result) * 1000000)); \    SET_TAG(*top,FLOATV);					\    SET_FLOAT(*top,result,p);					\  }								\  p->pc++;							\  break;							\}#define INTFLOATIMMOPI(op,opname) {				\  value_t *top = p->sp - 1;					\								\  DYNCHECK(top >= p->stack_min);				\  if (GET_TAG(*top) == INTV) {					\    int lit;							\    int result;							\    GET_LIT(lit,INTV,*p->pc);					\    result = GET_INT(*top) op lit;				\    wassert((result <= MAX_VINT) && (result >= MIN_VINT));	\    d_printf(10,"%s:%d: pc=%d: %s %d %d = %d\n",                \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             GET_INT(*top),lit,result);                         \    SET_TAG(*top,INTV);						\    SET_INT(*top,result);					\  } else {							\    float32 f1, lit;						\    int result;							\    value_t v = ZERO_VALUE_T;					\    ENSURE_HEAP_ROOM(p,iph);                                        \    top = p->sp - 1; \    DYNCHECK_TAG(*top,FLOATV);					\    COPY_LIT(v,FLOATV,*p->pc);					\    GET_FLOAT(lit,p->heap_min,v);				\    GET_FLOAT(f1,p->heap_min,*top);				\    result = f1 op lit;						\    d_printf(10,"%s:%d: pc=%d: %s %d.%06d %d.%06d = %d.%06d\n", \             __FILE__,__LINE__,(p->pc - p->code_min),opname,    \             (int)f1,(int)((f1 - (int)f1) * 1000000),           \             (int)lit,(int)((lit - (int)lit) * 1000000),        \             (int)result,(int)((result - (int)result) * 1000000)); \    SET_TAG(*top,INTV);						\    SET_INT(*top,result);					\  }								\  p->pc++;							\  break;							\}#define FLOATIMMOP(op,opname) {			    \  value_t *top = p->sp - 1;			    \  value_t v = ZERO_VALUE_T;			    \  float32 result, f1, lit;			    \						    \  ENSURE_HEAP_ROOM(p,iph);                              \  top = p->sp - 1; \  DYNCHECK(top >= p->stack_min);		    \  DYNCHECK_TAG(*top,FLOATV);			    \  COPY_LIT(v,FLOATV,*p->pc);			    \  GET_FLOAT(lit,p->heap_min,v);			    \  GET_FLOAT(f1,p->heap_min,*top);		    \  result = f1 op lit;				    \  d_printf(10,"%s:%d: pc=%d: %s %d.%06d %d.%06d = %d.%06d\n",    \           __FILE__,__LINE__,(p->pc - p->code_min),opname,                \          (int)f1,(int)((f1 - (int)f1) * 1000000),              \          (int)lit,(int)((lit - (int)lit) * 1000000),            \          (int)result,(int)((result - (int)result) * 1000000));     \  SET_TAG(*top,FLOATV);				    \  SET_FLOAT(*top,result,p);			    \  p->pc++;					    \  break;					    \}#define FLOATIMMOPI(op,opname) {		    \  value_t *top = p->sp - 1;			    \  value_t v = ZERO_VALUE_T;			    \  float32 f1, lit;				    \  int result;					    \                                                    \  ENSURE_HEAP_ROOM(p,iph);                              \  top = p->sp - 1; \  DYNCHECK(top >= p->stack_min);		    \  DYNCHECK_TAG(*top,FLOATV);			    \  COPY_LIT(v,FLOATV,*p->pc);			    \  GET_FLOAT(lit,p->heap_min,v);			    \  GET_FLOAT(f1,p->heap_min,*top);		    \  result = f1 op lit;				    \  d_printf(10,"%s:%d: pc=%d: %s %d.%06d %d.%06d = %d.%06d\n",    \

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -