[insert project logo here (125x200px max)] Navigator Mailinglists Please report any errors or ommissions you find to our `Help' mailinglist, or post a message in the Forums. Copyright and Licensing Information Snap is (c) Jonathan T. Moore, 1999-2002 and licensed under the GNU General Public License (GPL). All other parts of Splash are (c) Willem de Bruijn, 2002-2003 and licensed under the BSD Open Source License. All sourcecode is made publicly available. Acknowledgement Splash and the Splash website are hosted by SourceForge.net |
Splash - DocumentationSNMP Plus a Lightweight API for SNAP Handlingsnap-1.1-wjdb/lib/snap_svc_conversion.cGo to the documentation of this file.00001 /* 00002 snap service handler 00003 type conversion source file 00004 (c) 2003 Willem de Bruijn 00005 some code has been copied from the base SNAP package by Jon Moore 00006 all other code falls under the BSD License 00007 */ 00008 00009 #include <stdlib.h> 00010 #include <string.h> 00011 #include <arpa/inet.h> 00012 #include <sys/types.h> 00013 00014 #include "d_printf.h" 00015 #include "interp.h" /* solely needed for a macro referring to heap_alloc(..) */ 00016 #include "snap_svc_handler.h" 00017 #include "snap_svc_conversion.h" 00018 00019 void* snap_svc_convert_stack2returnstruct(packet_t *p, value_t *src) { 00020 00021 struct hval* returnptr; 00022 heap_obj *ho; 00023 00024 if (p == NULL ||src == NULL){ 00025 d_printf(50,"snap_svc : illegal call of convert_stack2returnstruct\n"); 00026 return NULL; 00027 } 00028 00029 00030 switch(GET_TAG(*src)) { 00031 case INTV: 00032 case EXCV: 00033 d_printf(50,"%s:%d: stack2returnstruct: copying an %s (value is %d) (location is %u)\n",__FILE__,__LINE__, 00034 (GET_TAG(*src) == INTV) ? "INTV" : "EXCV", GET_INT(*src),(unsigned int) src); 00035 /*returnptr = malloc(sizeof(int));*/ 00036 returnptr = (void*) *src; 00037 break; 00038 case ADDRV: 00039 ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); 00040 /*DYNCHECK_IN_HEAP(ho);*/ 00041 returnptr = (void*) (uint32_t)(*(uint32_t *)(&(ho->s))); 00042 d_printf(50,"%s:%d: stack2returnstruct: copying an ADDRV (value=%u)\n",__FILE__,__LINE__,returnptr); 00043 break; 00044 case FLOATV: { 00045 d_printf(50,"%s:%d: stack2returnstruct: copying a FLOATV\n",__FILE__,__LINE__); 00046 ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); 00047 /*DYNCHECK_IN_HEAP(ho);*/ 00048 memcpy(&returnptr, &(ho->s), sizeof(unsigned long)); 00049 break; 00050 } 00051 case STRV: { 00052 int slen; 00053 00054 d_printf(50,"%s:%d: stack2returnstruct: copying a STRV, offs=%d\n", 00055 __FILE__,__LINE__,GET_OFFS(*src)); 00056 ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); 00057 /*DYNCHECK_IN_HEAP(ho);*/ 00058 slen = ho->len; 00059 d_printf(50,"%s:%d: stack2returnstruct: slen = %d bytes, string reads [%s]\n", 00060 __FILE__,__LINE__,slen, ho->s); 00061 /*returnptr = (void*) malloc(slen + 1); 00062 if (returnptr == NULL) { 00063 return NULL; 00064 } 00065 memcpy(returnptr, &(ho->s), slen); 00066 ((char*) returnptr)[slen] = '\0';*/ 00067 returnptr = (void*) ho->s; 00068 break; 00069 } 00070 case TUPLEV: { 00071 int n; 00072 int i; 00073 struct snap_htup *t; 00074 00075 /* recursively copy the tuple out */ 00076 ho = (heap_obj *)(p->heap_min + GET_OFFS(*src)); 00077 /* DYNCHECK_IN_HEAP(ho);*/ 00078 n = ho->len / sizeof(value_t); 00079 d_printf(50,"%s:%d: stack2returnstruct: copying a %d-TUPLEV\n", 00080 __FILE__,__LINE__,n); 00081 t = (struct snap_htup*) malloc(sizeof(struct snap_htup)); 00082 if (t == NULL) { 00083 free(returnptr); 00084 return NULL; 00085 } 00086 t->n = n; 00087 t->vals = (struct snap_hval**) malloc(sizeof(struct snap_hval *) * n); 00088 if (t->vals == NULL) { 00089 free(t); 00090 free(returnptr); 00091 returnptr = NULL; 00092 } 00093 for(i=0; i<n; i++) { 00094 if ( (t->vals[i] = (struct snap_hval*) snap_svc_convert_stack2returnstruct(p,((value_t *)(ho+1)) + i)) == NULL) { 00095 free(t->vals); 00096 free(t); 00097 free(returnptr); 00098 returnptr = NULL; 00099 } 00100 } 00101 returnptr = (void*) t; 00102 00103 /* SINCE WE DON't HANDLE TUPLEVS I WILL NOT FREE THIS MEMORY */ 00104 /* 00105 anyone using the above case must free the memory inside the 00106 called function to avoid memory leaks. 00107 */ 00108 break; 00109 } 00110 default: 00111 free(returnptr); 00112 return NULL; 00113 } 00114 00115 /* success */ 00116 return returnptr; 00117 } 00118 00119 /* place returnstruct elements on the stack */ 00120 int snap_svc_convert_returnstruct2stack(packet_t *p, value_t *dst, struct svc_returnitem *item) { 00121 int hosize; 00122 int offs; 00123 heap_obj *ho; 00124 00125 if (p == NULL || item == NULL || p->sp == NULL || p->stack_max == NULL){ 00126 d_printf(50,"snap_svc : called snap_svc_convert_returnstruct2stack incorrectly\n"); 00127 return -1; 00128 } 00129 00130 d_printf(50,"%s:%d: found returnitem with type %d\n",__FILE__,__LINE__,item->type); 00131 switch(item->type) { 00132 case SVC_SNMP_TYPE_INT : 00133 SET_TAG(*dst, INTV); 00134 SET_INT(*dst, *((int*)item->data)); 00135 p->sp++; 00136 /* debug: show the new stackvalue */ 00137 d_printf(50,"%s:%d: returnstruct2pval: copying an INTV (value is %d) (location is %u)\n",__FILE__,__LINE__, 00138 GET_INT(*dst),(unsigned int) dst); 00139 break; 00140 case SVC_SNMP_TYPE_ADDR : 00141 SET_TAG(*dst, ADDRV); 00142 SET_ADDR(*dst,*((uint32_t*)item->data), p ); 00143 p->sp++; 00144 /* debug: show the new stackvalue */ 00145 d_printf(50,"%s:%d: returnstruct2pval: copying an ADDRV (value_prev is %u)(value_new is %u) (location is %u)\n",__FILE__,__LINE__, 00146 *((int*)item->data),GET_ADDR_VAL((heap_obj *)p->heap_min,*dst)); 00147 break; 00148 case SVC_SNMP_TYPE_LONG : 00149 SET_TAG(*dst, INTV); 00150 SET_INT(*dst, *((int*)item->data)); 00151 SET_INT(*dst, *((int*)item->data)); 00152 p->sp++; 00153 /* debug: show the new stackvalue */ 00154 d_printf(50,"%s:%d: returnstruct2pval: copying an LONG (value is %d) (location is %u)\n",__FILE__,__LINE__, 00155 GET_INT(*dst),(unsigned int) dst); 00156 break; 00157 case SVC_SNMP_TYPE_STRING: 00158 hosize = sizeof(heap_obj) + (sizeof(u_char) * item->length); 00159 p->stack_max = (value_t *)((void *)p->stack_max - hosize); 00160 ho = (heap_obj *)p->stack_max; 00161 offs = (void *)ho - p->heap_min; 00162 ho->len = hosize - sizeof(heap_obj); 00163 ho->flag = 0; 00164 memcpy(&ho->s, (unsigned char*) item->data , item->length); 00165 SET_TAG(*dst, STRV); 00166 SET_OFFS(*dst, offs); 00167 p->sp++; 00168 /* debug: show the new stackvalue */ 00169 d_printf(50,"%s:%d: returnstruct2pval: copying a STRV, offs=%d\n", 00170 __FILE__,__LINE__,GET_OFFS(*dst)); 00171 d_printf(50,"%s:%d: returnstruct2pval: slen = %d bytes, string reads [%s]\n", 00172 __FILE__,__LINE__,ho->len, ho->s); 00173 break; 00174 case 0: /* 0 == SVC_SNMP_TYPE_NULL */ 00175 break; 00176 default: 00177 d_printf(50,"%s:%d: returnstruct2pval : unsupported typetag %d\n", 00178 __FILE__,__LINE__, item->type); 00179 return -1; 00180 } 00181 return 0; 00182 } 00183 00184 /* 00185 convert direct variables into returnstructs and 00186 place them on the stack 00187 */ 00188 int snap_svc_convert_direct2stack(packet_t *p, void* value, int dSvcType) { 00189 struct svc_returnitem* pSvcItem; 00190 00191 /* prepare a new returnitem structure */ 00192 pSvcItem = (struct svc_returnitem*) calloc (sizeof(struct svc_returnitem), 1); 00193 pSvcItem->type = dSvcType; 00194 switch (dSvcType){ 00195 case SVC_SNMP_TYPE_STRING : 00196 pSvcItem->length = strlen((char*) value); 00197 pSvcItem->data = value; 00198 break; 00199 case SVC_SNMP_TYPE_LONG : 00200 pSvcItem->length = 1; 00201 pSvcItem->data = (unsigned long*) malloc ( sizeof(unsigned long)); 00202 memcpy(pSvcItem->data, &value, sizeof(unsigned long)); 00203 default : /* 32bit value, not necessarily a uint32_t */ 00204 pSvcItem->length = 1; 00205 pSvcItem->data = (uint32_t*) malloc (sizeof(uint32_t)); 00206 memcpy(pSvcItem->data, &value, sizeof(uint32_t)); 00207 } 00208 pSvcItem->oid = NULL; 00209 pSvcItem->oid_length = 0; 00210 snap_svc_convert_returnstruct2stack(p, p->sp, pSvcItem); 00211 free (pSvcItem); 00212 pSvcItem = NULL; 00213 00214 return 0; 00215 } 00216 00217 /* 00218 convert stack values to arguments 00219 */ 00220 void** snap_svc_convert_stack2arguments(packet_t *p, int dArgCount){ 00221 void** returnArgs = NULL; 00222 int i; 00223 00224 if(dArgCount < 0 || p->sp == NULL || p->stack_min == NULL){ 00225 d_printf(50,"snap_svc : called snap_svc_convert_stack2arguments incorrectly, skipping\n"); 00226 return NULL; 00227 } 00228 00229 if(p->sp - dArgCount < p->stack_min){ 00230 d_printf(10,"snap_svc : trying to pop more arguments than are on the stack, skipping call\n"); 00231 p->sp = p->stack_min; 00232 /* TODO : drop the packet */ 00233 return NULL; 00234 } 00235 00236 if (dArgCount > 0) { 00237 d_printf(50,"%s:%d: marshalling arguments\n",__FILE__,__LINE__); 00238 returnArgs = (void**) malloc(sizeof(void*) * dArgCount); 00239 if (returnArgs == NULL) 00240 return NULL; 00241 for(i=0; i<dArgCount; i++) { 00242 returnArgs[dArgCount-i-1] = (void*) snap_svc_convert_stack2returnstruct(p,p->sp -i-1); 00243 } 00244 } 00245 00246 p->sp -= dArgCount; 00247 00248 return returnArgs; 00249 } |