uint8_t * bstr_create_from_str(const char *s) { int msize; uint8_t * mem; int l; l= strlen(s); if (s[0]!='.') return bstr_create((uint8_t*)s,l); if (l<=2) return bstr_create((uint8_t*)s,l); if (s[1]=='.') return bstr_create((uint8_t*)s+1,l-1); if (s[1]!='x') return bstr_create((uint8_t*)s,l); /* the string starts with ".x" - read hexbytes */ l-=2; msize=l/2; if(l&1) msize++; mem = malloc(1+msize); *((uint8_t*)mem)=msize; cw_format_scan_hex_bytes(mem+2,s+2,l); return mem; }
static int read_subelem(struct ac_info* acinfo,int subtype,uint8_t *elem, int len) { switch (subtype){ case 4: /* hardware version */ bstr_replace(&acinfo->hardware_version,bstr_create(elem,len)); break; case 5: /* software version */ bstr_replace(&acinfo->software_version,bstr_create(elem,len)); break; } return 1; }
static struct cw_KTV *from_str ( struct cw_KTV * data, const char *src ) { struct sockaddr_storage addr; uint8_t * s, * addrptr; int rc,l; rc = sock_strtoaddr(src,(struct sockaddr*)&addr); if (!rc) return NULL; switch(((struct sockaddr*)&addr)->sa_family){ case AF_INET: addrptr = (uint8_t*)&(((struct sockaddr_in*)(&addr))->sin_addr); l = 4; break; case AF_INET6: addrptr = (uint8_t*)&(((struct sockaddr_in6*)(&addr))->sin6_addr); l = 16; break; default: return NULL; } s = bstr_create(addrptr,l); if ( s == NULL ) return NULL; data->type = &cw_type_ipaddress; data->val.ptr = s; return data; }
static int read_subelem_cisco(struct ac_info* acinfo,int subtype,uint8_t * elem, int len) { switch (subtype) { case 0: /* hardware version */ bstr_replace(&acinfo->hardware_version,bstr_create(elem,len)); break; case 1: /* software version */ bstr_replace(&acinfo->software_version,bstr_create(elem,len)); break; default: //printf("What? %d\n",subtype); break; } return 1; }
static struct cw_KTV *get ( struct cw_KTV * data, const uint8_t * src, int len ) { uint8_t * s; s = bstr_create ( src, len ); if ( !s ) return NULL; data->type = &cw_type_ipaddress; data->val.ptr = s; return data; }
/** * Create a new DBM wrapper over already created DB map. * * If value_data_size is 0, the length for value_size is used. * * @param dm The database (already opened) * @param name Database name, for logs * @param value_size Maximum value size, in bytes (structure) * @param value_data_size Maximum value size, in bytes (serialized form) * @param pack Serialization routine for values * @param unpack Deserialization routine for values * @param valfree Free routine for value (or NULL if none needed) * @param cache_size Amount of items to cache (0 = no cache, 1 = default) * @param hash_func Key hash function * @param eq_func Key equality test function * * If serialization and deserialization routines are NULL pointers, data * will be stored and retrieved as-is. In that case, they must be both * NULL. */ dbmw_t * dbmw_create(dbmap_t *dm, const char *name, size_t value_size, size_t value_data_size, dbmw_serialize_t pack, dbmw_deserialize_t unpack, dbmw_free_t valfree, size_t cache_size, GHashFunc hash_func, GEqualFunc eq_func) { dbmw_t *dw; g_assert(pack == NULL || value_size); g_assert((pack != NULL) == (unpack != NULL)); g_assert(valfree == NULL || unpack != NULL); g_assert(dm); WALLOC0(dw); dw->magic = DBMW_MAGIC; dw->dm = dm; dw->name = name; dw->key_size = dbmap_key_size(dm); dw->key_len = dbmap_key_length(dm); dw->value_size = value_size; dw->value_data_size = 0 == value_data_size ? value_size : value_data_size; /* Make sure we do not violate the SDBM constraint */ g_assert(sdbm_is_storable(dw->key_size, dw->value_data_size)); /* * There must be a serialization routine if the serialized length is not * the same as the structure length. */ g_assert(dw->value_size == dw->value_data_size || pack != NULL); /* * For a small amount of items, a PATRICIA tree is more efficient * than a hash table although it uses more memory. */ if ( NULL == dw->key_len && dw->key_size * 8 <= PATRICIA_MAXBITS && cache_size <= DBMW_CACHE ) { dw->values = map_create_patricia(dw->key_size * 8); } else { dw->values = map_create_hash(hash_func, eq_func); } dw->keys = hash_list_new(hash_func, eq_func); dw->pack = pack; dw->unpack = unpack; dw->valfree = valfree; /* * If a serialization routine is provided, we'll also have a need for * deserialization. Allocate the message in/out streams. * * We're allocating one more byte than necessary to be able to check * whether serialization stays within the imposed boundaries. */ if (dw->pack) { dw->bs = bstr_create(); dw->mb = pmsg_new(PMSG_P_DATA, NULL, dw->value_data_size + 1); } /* * If cache_size is zero, we won't cache anything but the latest * value requested, in deserialized form. If modified, it will be * written back immediately. * * If cache_size is one, use the default (DBMW_CACHE). * * Any other value is used as-is. */ if (0 == cache_size) dw->max_cached = 1; /* No cache, only keep latest around */ else if (cache_size == 1) dw->max_cached = DBMW_CACHE; else dw->max_cached = cache_size; if (common_dbg) g_debug("DBMW created \"%s\" with %s back-end " "(max cached = %lu, key=%lu bytes, value=%lu bytes, " "%lu max serialized)", dw->name, dbmw_map_type(dw) == DBMAP_SDBM ? "sdbm" : "map", (gulong) dw->max_cached, (gulong) dw->key_size, (gulong) dw->value_size, (gulong) dw->value_data_size); return dw; }