📄 snap_svc_conversion.c
字号:
/* snap service handler type conversion source file (c) 2003 Willem de Bruijn some code has been copied from the base SNAP package by Jon Moore all other code falls under the BSD License*/#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include "d_printf.h"#include "interp.h" /* solely needed for a macro referring to heap_alloc(..) */#include "snap_svc_handler.h"#include "snap_svc_conversion.h"void* snap_svc_convert_stack2returnstruct(packet_t *p, value_t *src) { struct hval* returnptr; heap_obj *ho; if (p == NULL ||src == NULL){ d_printf(50,"snap_svc : illegal call of convert_stack2returnstruct\n"); return NULL; } switch(GET_TAG(*src)) { case INTV: case EXCV: d_printf(50,"%s:%d: stack2returnstruct: copying an %s (value is %d) (location is %u)\n",__FILE__,__LINE__, (GET_TAG(*src) == INTV) ? "INTV" : "EXCV", GET_INT(*src),(unsigned int) src); /*returnptr = malloc(sizeof(int));*/ returnptr = (void*) *src; break; case ADDRV: ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); /*DYNCHECK_IN_HEAP(ho);*/ returnptr = (void*) (uint32_t)(*(uint32_t *)(&(ho->s))); d_printf(50,"%s:%d: stack2returnstruct: copying an ADDRV (value=%u)\n",__FILE__,__LINE__,returnptr); break; case FLOATV: { d_printf(50,"%s:%d: stack2returnstruct: copying a FLOATV\n",__FILE__,__LINE__); ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); /*DYNCHECK_IN_HEAP(ho);*/ memcpy(&returnptr, &(ho->s), sizeof(unsigned long)); break; } case STRV: { int slen; d_printf(50,"%s:%d: stack2returnstruct: copying a STRV, offs=%d\n", __FILE__,__LINE__,GET_OFFS(*src)); ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); /*DYNCHECK_IN_HEAP(ho);*/ slen = ho->len; d_printf(50,"%s:%d: stack2returnstruct: slen = %d bytes, string reads [%s]\n", __FILE__,__LINE__,slen, ho->s); /*returnptr = (void*) malloc(slen + 1); if (returnptr == NULL) { return NULL; } memcpy(returnptr, &(ho->s), slen); ((char*) returnptr)[slen] = '\0';*/ returnptr = (void*) ho->s; break; } case TUPLEV: { int n; int i; struct snap_htup *t; /* recursively copy the tuple out */ ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); /* DYNCHECK_IN_HEAP(ho);*/ n = ho->len / sizeof(value_t); d_printf(50,"%s:%d: stack2returnstruct: copying a %d-TUPLEV\n", __FILE__,__LINE__,n); t = (struct snap_htup*) malloc(sizeof(struct snap_htup)); if (t == NULL) { free(returnptr); return NULL; } t->n = n; t->vals = (struct snap_hval**) malloc(sizeof(struct snap_hval *) * n); if (t->vals == NULL) { free(t); free(returnptr); returnptr = NULL; } for(i=0; i<n; i++) { if ( (t->vals[i] = (struct snap_hval*) snap_svc_convert_stack2returnstruct(p,((value_t *)(ho+1)) + i)) == NULL) { free(t->vals); free(t); free(returnptr); returnptr = NULL; } } returnptr = (void*) t; /* SINCE WE DON't HANDLE TUPLEVS I WILL NOT FREE THIS MEMORY */ /* anyone using the above case must free the memory inside the called function to avoid memory leaks. */ break; } default: free(returnptr); return NULL; } /* success */ return returnptr;}/* place returnstruct elements on the stack */int snap_svc_convert_returnstruct2stack(packet_t *p, value_t *dst, struct svc_returnitem *item) { int hosize; int offs; heap_obj *ho; if (p == NULL || item == NULL || p->sp == NULL || p->stack_max == NULL){ d_printf(50,"snap_svc : called snap_svc_convert_returnstruct2stack incorrectly\n"); return -1; } d_printf(50,"%s:%d: found returnitem with type %d\n",__FILE__,__LINE__,item->type); switch(item->type) { case SVC_SNMP_TYPE_INT : SET_TAG(*dst, INTV); SET_INT(*dst, *((int*)item->data)); p->sp++; /* debug: show the new stackvalue */ d_printf(50,"%s:%d: returnstruct2pval: copying an INTV (value is %d) (location is %u)\n",__FILE__,__LINE__, GET_INT(*dst),(unsigned int) dst); break; case SVC_SNMP_TYPE_ADDR : SET_TAG(*dst, ADDRV); SET_ADDR(*dst,*((uint32_t*)item->data), p ); p->sp++; /* debug: show the new stackvalue */ d_printf(50,"%s:%d: returnstruct2pval: copying an ADDRV (value_prev is %u)(value_new is %u) (location is %u)\n",__FILE__,__LINE__, *((int*)item->data),GET_ADDR_VAL((heap_obj *)p->heap_min,*dst)); break; case SVC_SNMP_TYPE_LONG : SET_TAG(*dst, INTV); SET_INT(*dst, *((int*)item->data)); SET_INT(*dst, *((int*)item->data)); p->sp++; /* debug: show the new stackvalue */ d_printf(50,"%s:%d: returnstruct2pval: copying an LONG (value is %d) (location is %u)\n",__FILE__,__LINE__, GET_INT(*dst),(unsigned int) dst); break; case SVC_SNMP_TYPE_STRING: hosize = sizeof(heap_obj) + (sizeof(u_char) * item->length); p->stack_max = (value_t *)((void *)p->stack_max - hosize); ho = (heap_obj *)p->stack_max; offs = (void *)ho - p->heap_min; ho->len = hosize - sizeof(heap_obj); ho->flag = 0; d_printf(100, "item->length = %d\n", item->length); memcpy(&ho->s, (unsigned char*) item->data , item->length); SET_TAG(*dst, STRV); SET_OFFS(*dst, offs); p->sp++; /* debug: show the new stackvalue */ d_printf(50,"%s:%d: returnstruct2pval: copying a STRV, offs=%d\n", __FILE__,__LINE__,GET_OFFS(*dst)); d_printf(50,"%s:%d: returnstruct2pval: slen = %d bytes, string reads [%s]\n", __FILE__,__LINE__,ho->len, ho->s); break; case 0: /* 0 == SVC_SNMP_TYPE_NULL */ break; default: d_printf(50,"%s:%d: returnstruct2pval : unsupported typetag %d\n", __FILE__,__LINE__, item->type); return -1; } return 0;}/* convert direct variables into returnstructs and place them on the stack */int snap_svc_convert_direct2stack(packet_t *p, void* value, int dSvcType) { struct svc_returnitem* pSvcItem; /* prepare a new returnitem structure */ pSvcItem = (struct svc_returnitem*) calloc (sizeof(struct svc_returnitem), 1); pSvcItem->type = dSvcType; switch (dSvcType){ case SVC_SNMP_TYPE_STRING : pSvcItem->length = strlen((char*) value) + 1; pSvcItem->data = value; break; case SVC_SNMP_TYPE_LONG : pSvcItem->length = 1; pSvcItem->data = (unsigned long*) malloc ( sizeof(unsigned long)); memcpy(pSvcItem->data, &value, sizeof(unsigned long)); default : /* 32bit value, not necessarily a uint32_t */ pSvcItem->length = 1; pSvcItem->data = (uint32_t*) malloc (sizeof(uint32_t)); memcpy(pSvcItem->data, &value, sizeof(uint32_t)); } pSvcItem->oid = NULL; pSvcItem->oid_length = 0; snap_svc_convert_returnstruct2stack(p, p->sp, pSvcItem); free (pSvcItem); pSvcItem = NULL; return 0;}/* convert stack values to arguments*/void** snap_svc_convert_stack2arguments(packet_t *p, int dArgCount){ void** returnArgs = NULL; int i; if(dArgCount < 0 || p->sp == NULL || p->stack_min == NULL){ d_printf(50,"snap_svc : called snap_svc_convert_stack2arguments incorrectly, skipping\n"); return NULL; } if(p->sp - dArgCount < p->stack_min){ d_printf(10,"snap_svc : trying to pop more arguments than are on the stack, skipping call\n"); p->sp = p->stack_min; /* TODO : drop the packet */ return NULL; } if (dArgCount > 0) { d_printf(50,"%s:%d: marshalling arguments\n",__FILE__,__LINE__); returnArgs = (void**) malloc(sizeof(void*) * dArgCount); if (returnArgs == NULL) return NULL; for(i=0; i<dArgCount; i++) { returnArgs[dArgCount-i-1] = (void*) snap_svc_convert_stack2returnstruct(p,p->sp -i-1); } } p->sp -= dArgCount; return returnArgs;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -