mirror of
https://github.com/vale981/libblobpack
synced 2025-03-05 09:51:42 -05:00
Initial import
This commit is contained in:
commit
458279e4ce
15 changed files with 2216 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.so
|
28
Makefile
Normal file
28
Makefile
Normal file
|
@ -0,0 +1,28 @@
|
|||
CC?=gcc
|
||||
CFLAGS?=-O2
|
||||
CFLAGS+=-std=gnu99 -Wall -Werror -pedantic -fpic
|
||||
LDFLAGS?=
|
||||
LIBNL=-lnl-tiny
|
||||
PREFIX=/usr
|
||||
INCLUDE_DIR=$(PREFIX)/include/libubox
|
||||
LIBDIR=$(PREFIX)/lib
|
||||
CPPFLAGS=
|
||||
|
||||
all: libubox.so
|
||||
|
||||
libubox.so: ucix.c blob.c blobmsg.c hash.c uhtbl.c usock.c uloop.c unl.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -shared -Wl,-soname,libubox.so $^ $(LDFLAGS) -luci $(LIBNL)
|
||||
|
||||
install-headers:
|
||||
mkdir -p $(INCLUDE_DIR)
|
||||
cp *.h $(INCLUDE_DIR)/
|
||||
|
||||
install-lib:
|
||||
mkdir -p $(LIBDIR)
|
||||
cp libubox.so $(LIBDIR)/
|
||||
|
||||
install: install-lib install-headers
|
||||
|
||||
clean:
|
||||
rm -f *.so
|
||||
|
177
blob.c
Normal file
177
blob.c
Normal file
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* blob - library for generating/parsing tagged binary data
|
||||
*
|
||||
* Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License version 2.1
|
||||
* as published by the Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "blob.h"
|
||||
|
||||
static bool
|
||||
blob_buffer_grow(struct blob_buf *buf, int minlen)
|
||||
{
|
||||
buf->buflen += ((minlen / 256) + 1) * 256;
|
||||
buf->buf = realloc(buf->buf, buf->buflen);
|
||||
return !!buf->buf;
|
||||
}
|
||||
|
||||
static void
|
||||
blob_init(struct blob_attr *attr, int id, unsigned int len)
|
||||
{
|
||||
len &= BLOB_ATTR_LEN_MASK;
|
||||
len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
|
||||
attr->id_len = cpu_to_be32(len);
|
||||
}
|
||||
|
||||
static inline struct blob_attr *
|
||||
offset_to_attr(struct blob_buf *buf, int offset)
|
||||
{
|
||||
void *ptr = (char *)buf->buf + offset;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static inline int
|
||||
attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
|
||||
{
|
||||
return (char *)attr - (char *) buf->buf;
|
||||
}
|
||||
|
||||
static struct blob_attr *
|
||||
blob_add(struct blob_buf *buf, struct blob_attr *pos, int id, int payload)
|
||||
{
|
||||
int offset = attr_to_offset(buf, pos);
|
||||
int required = (offset + sizeof(struct blob_attr) + payload) - buf->buflen;
|
||||
struct blob_attr *attr;
|
||||
|
||||
if (required > 0) {
|
||||
int offset_head = attr_to_offset(buf, buf->head);
|
||||
|
||||
if (!buf->grow || !buf->grow(buf, required))
|
||||
return NULL;
|
||||
|
||||
buf->head = offset_to_attr(buf, offset_head);
|
||||
attr = offset_to_attr(buf, offset);
|
||||
} else {
|
||||
attr = pos;
|
||||
}
|
||||
|
||||
blob_init(attr, id, payload + sizeof(struct blob_attr));
|
||||
return attr;
|
||||
}
|
||||
|
||||
int
|
||||
blob_buf_init(struct blob_buf *buf, int id)
|
||||
{
|
||||
if (!buf->grow)
|
||||
buf->grow = blob_buffer_grow;
|
||||
|
||||
buf->head = buf->buf;
|
||||
if (blob_add(buf, buf->buf, id, 0) == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct blob_attr *
|
||||
blob_new(struct blob_buf *buf, int id, int payload)
|
||||
{
|
||||
struct blob_attr *attr;
|
||||
|
||||
attr = blob_add(buf, blob_next(buf->head), id, payload);
|
||||
if (!attr)
|
||||
return NULL;
|
||||
|
||||
blob_set_raw_len(buf->head, blob_pad_len(buf->head) + blob_pad_len(attr));
|
||||
return attr;
|
||||
}
|
||||
|
||||
struct blob_attr *
|
||||
blob_put(struct blob_buf *buf, int id, const void *ptr, int len)
|
||||
{
|
||||
struct blob_attr *attr;
|
||||
|
||||
attr = blob_new(buf, id, len);
|
||||
if (!attr)
|
||||
return NULL;
|
||||
|
||||
if (ptr)
|
||||
memcpy(blob_data(attr), ptr, len);
|
||||
return attr;
|
||||
}
|
||||
|
||||
void *
|
||||
blob_nest_start(struct blob_buf *buf, int id)
|
||||
{
|
||||
unsigned long offset = attr_to_offset(buf, buf->head);
|
||||
buf->head = blob_new(buf, id, 0);
|
||||
return (void *) offset;
|
||||
}
|
||||
|
||||
void
|
||||
blob_nest_end(struct blob_buf *buf, void *cookie)
|
||||
{
|
||||
struct blob_attr *attr = offset_to_attr(buf, (unsigned long) cookie);
|
||||
blob_set_raw_len(attr, blob_pad_len(attr) + blob_len(buf->head));
|
||||
buf->head = attr;
|
||||
}
|
||||
|
||||
static const int blob_type_minlen[BLOB_ATTR_LAST] = {
|
||||
[BLOB_ATTR_STRING] = 1,
|
||||
[BLOB_ATTR_INT8] = sizeof(uint8_t),
|
||||
[BLOB_ATTR_INT16] = sizeof(uint16_t),
|
||||
[BLOB_ATTR_INT32] = sizeof(uint32_t),
|
||||
[BLOB_ATTR_INT64] = sizeof(uint64_t),
|
||||
};
|
||||
|
||||
int
|
||||
blob_parse(struct blob_attr *attr, struct blob_attr **data, struct blob_attr_info *info, int max)
|
||||
{
|
||||
struct blob_attr *pos;
|
||||
int found = 0;
|
||||
int rem;
|
||||
|
||||
memset(data, 0, sizeof(struct blob_attr *) * max);
|
||||
blob_for_each_attr(pos, attr, rem) {
|
||||
int id = blob_id(pos);
|
||||
int len = blob_len(pos);
|
||||
|
||||
if (id >= max)
|
||||
continue;
|
||||
|
||||
if (info) {
|
||||
int type = info[id].type;
|
||||
if (type < BLOB_ATTR_LAST) {
|
||||
if (type >= BLOB_ATTR_INT8 && type <= BLOB_ATTR_INT64) {
|
||||
if (len != blob_type_minlen[type])
|
||||
continue;
|
||||
} else {
|
||||
if (len < blob_type_minlen[type])
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (info[id].minlen && len < info[id].minlen)
|
||||
continue;
|
||||
|
||||
if (info[id].maxlen && len > info[id].maxlen)
|
||||
continue;
|
||||
|
||||
if (info[id].validate && !info[id].validate(&info[id], attr))
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!data[id])
|
||||
found++;
|
||||
|
||||
data[id] = pos;
|
||||
}
|
||||
return found;
|
||||
}
|
271
blob.h
Normal file
271
blob.h
Normal file
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
* blob - library for generating/parsing tagged binary data
|
||||
*
|
||||
* Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License version 2.1
|
||||
* as published by the Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _BLOB_H__
|
||||
#define _BLOB_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
|
||||
#if defined(__linux__) || defined(__CYGWIN__)
|
||||
#include <byteswap.h>
|
||||
#include <endian.h>
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
#include <machine/endian.h>
|
||||
#include <machine/byte_order.h>
|
||||
#define bswap_16(x) OSSwapInt16(x)
|
||||
#define bswap_32(x) OSSwapInt32(x)
|
||||
#define bswap_64(x) OSSwapInt64(x)
|
||||
#elif defined(__FreeBSD__)
|
||||
#include <sys/endian.h>
|
||||
#define bswap_16(x) bswap16(x)
|
||||
#define bswap_32(x) bswap32(x)
|
||||
#define bswap_64(x) bswap64(x)
|
||||
#else
|
||||
#include <machine/endian.h>
|
||||
#define bswap_16(x) swap16(x)
|
||||
#define bswap_32(x) swap32(x)
|
||||
#define bswap_64(x) swap64(x)
|
||||
#endif
|
||||
|
||||
#ifndef __BYTE_ORDER
|
||||
#define __BYTE_ORDER BYTE_ORDER
|
||||
#endif
|
||||
#ifndef __BIG_ENDIAN
|
||||
#define __BIG_ENDIAN BIG_ENDIAN
|
||||
#endif
|
||||
#ifndef __LITTLE_ENDIAN
|
||||
#define __LITTLE_ENDIAN LITTLE_ENDIAN
|
||||
#endif
|
||||
|
||||
#define cpu_to_be64(x) bswap_64(x)
|
||||
#define cpu_to_be32(x) bswap_32(x)
|
||||
#define cpu_to_be16(x) bswap_16(x)
|
||||
|
||||
#define be64_to_cpu(x) bswap_64(x)
|
||||
#define be32_to_cpu(x) bswap_32(x)
|
||||
#define be16_to_cpu(x) bswap_16(x)
|
||||
|
||||
#else
|
||||
|
||||
#define cpu_to_be64(x) (x)
|
||||
#define cpu_to_be32(x) (x)
|
||||
#define cpu_to_be16(x) (x)
|
||||
|
||||
#define be64_to_cpu(x) (x)
|
||||
#define be32_to_cpu(x) (x)
|
||||
#define be16_to_cpu(x) (x)
|
||||
|
||||
#endif
|
||||
|
||||
enum {
|
||||
BLOB_ATTR_UNSPEC,
|
||||
BLOB_ATTR_NESTED,
|
||||
BLOB_ATTR_BINARY,
|
||||
BLOB_ATTR_STRING,
|
||||
BLOB_ATTR_INT8,
|
||||
BLOB_ATTR_INT16,
|
||||
BLOB_ATTR_INT32,
|
||||
BLOB_ATTR_INT64,
|
||||
BLOB_ATTR_LAST
|
||||
};
|
||||
|
||||
#define BLOB_ATTR_ID_MASK 0xff000000
|
||||
#define BLOB_ATTR_ID_SHIFT 24
|
||||
#define BLOB_ATTR_LEN_MASK 0x00ffffff
|
||||
#define BLOB_ATTR_ALIGN 4
|
||||
|
||||
#ifndef __packed
|
||||
#define __packed __attribute__((packed))
|
||||
#endif
|
||||
|
||||
struct blob_attr {
|
||||
uint32_t id_len;
|
||||
char data[];
|
||||
} __packed;
|
||||
|
||||
struct blob_attr_info {
|
||||
unsigned int type;
|
||||
unsigned int minlen;
|
||||
unsigned int maxlen;
|
||||
bool (*validate)(struct blob_attr_info *, struct blob_attr *);
|
||||
};
|
||||
|
||||
struct blob_buf {
|
||||
struct blob_attr *head;
|
||||
bool (*grow)(struct blob_buf *buf, int minlen);
|
||||
int buflen;
|
||||
void *buf;
|
||||
void *priv;
|
||||
};
|
||||
|
||||
/*
|
||||
* blob_data: returns the data pointer for an attribute
|
||||
*/
|
||||
static inline void *
|
||||
blob_data(struct blob_attr *attr)
|
||||
{
|
||||
return attr->data;
|
||||
}
|
||||
|
||||
/*
|
||||
* blob_id: returns the id of an attribute
|
||||
*/
|
||||
static inline unsigned int
|
||||
blob_id(struct blob_attr *attr)
|
||||
{
|
||||
int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
* blob_len: returns the length of the attribute's payload
|
||||
*/
|
||||
static inline unsigned int
|
||||
blob_len(struct blob_attr *attr)
|
||||
{
|
||||
return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
|
||||
}
|
||||
|
||||
/*
|
||||
* blob_pad_len: returns the complete length of an attribute (including the header)
|
||||
*/
|
||||
static inline unsigned int
|
||||
blob_raw_len(struct blob_attr *attr)
|
||||
{
|
||||
return blob_len(attr) + sizeof(struct blob_attr);
|
||||
}
|
||||
|
||||
/*
|
||||
* blob_pad_len: returns the padded length of an attribute (including the header)
|
||||
*/
|
||||
static inline unsigned int
|
||||
blob_pad_len(struct blob_attr *attr)
|
||||
{
|
||||
int len = blob_raw_len(attr);
|
||||
len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline void
|
||||
blob_set_raw_len(struct blob_attr *attr, unsigned int len)
|
||||
{
|
||||
int id = blob_id(attr);
|
||||
len &= BLOB_ATTR_LEN_MASK;
|
||||
len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
|
||||
attr->id_len = cpu_to_be32(len);
|
||||
}
|
||||
|
||||
static inline uint8_t
|
||||
blob_get_int8(struct blob_attr *attr)
|
||||
{
|
||||
return *((uint8_t *) attr->data);
|
||||
}
|
||||
|
||||
static inline uint16_t
|
||||
blob_get_int16(struct blob_attr *attr)
|
||||
{
|
||||
uint16_t *tmp = (uint16_t*)attr->data;
|
||||
return be16_to_cpu(*tmp);
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
blob_get_int32(struct blob_attr *attr)
|
||||
{
|
||||
uint32_t *tmp = (uint32_t*)attr->data;
|
||||
return be32_to_cpu(*tmp);
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
blob_get_int64(struct blob_attr *attr)
|
||||
{
|
||||
uint64_t *tmp = (uint64_t*)attr->data;
|
||||
return be64_to_cpu(*tmp);
|
||||
}
|
||||
|
||||
static inline const char *
|
||||
blob_get_string(struct blob_attr *attr)
|
||||
{
|
||||
return attr->data;
|
||||
}
|
||||
|
||||
static inline struct blob_attr *
|
||||
blob_next(struct blob_attr *attr)
|
||||
{
|
||||
return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
|
||||
}
|
||||
|
||||
extern int blob_buf_init(struct blob_buf *buf, int id);
|
||||
extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
|
||||
extern void *blob_nest_start(struct blob_buf *buf, int id);
|
||||
extern void blob_nest_end(struct blob_buf *buf, void *cookie);
|
||||
extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, int len);
|
||||
extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, struct blob_attr_info *info, int max);
|
||||
|
||||
static inline struct blob_attr *
|
||||
blob_put_string(struct blob_buf *buf, int id, const char *str)
|
||||
{
|
||||
return blob_put(buf, id, str, strlen(str) + 1);
|
||||
}
|
||||
|
||||
static inline struct blob_attr *
|
||||
blob_put_int8(struct blob_buf *buf, int id, uint8_t val)
|
||||
{
|
||||
return blob_put(buf, id, &val, sizeof(val));
|
||||
}
|
||||
|
||||
static inline struct blob_attr *
|
||||
blob_put_int16(struct blob_buf *buf, int id, uint16_t val)
|
||||
{
|
||||
val = cpu_to_be16(val);
|
||||
return blob_put(buf, id, &val, sizeof(val));
|
||||
}
|
||||
|
||||
static inline struct blob_attr *
|
||||
blob_put_int32(struct blob_buf *buf, int id, uint32_t val)
|
||||
{
|
||||
val = cpu_to_be32(val);
|
||||
return blob_put(buf, id, &val, sizeof(val));
|
||||
}
|
||||
|
||||
static inline struct blob_attr *
|
||||
blob_put_int64(struct blob_buf *buf, int id, uint64_t val)
|
||||
{
|
||||
val = cpu_to_be64(val);
|
||||
return blob_put(buf, id, &val, sizeof(val));
|
||||
}
|
||||
|
||||
#define __blob_for_each_attr(pos, attr, rem) \
|
||||
for (pos = (void *) attr; \
|
||||
(blob_pad_len(pos) <= rem) && \
|
||||
(blob_pad_len(pos) >= sizeof(struct blob_attr)); \
|
||||
rem -= blob_pad_len(pos), pos = blob_next(pos))
|
||||
|
||||
|
||||
#define blob_for_each_attr(pos, attr, rem) \
|
||||
for (rem = blob_len(attr), pos = blob_data(attr); \
|
||||
(blob_pad_len(pos) <= rem) && \
|
||||
(blob_pad_len(pos) >= sizeof(struct blob_attr)); \
|
||||
rem -= blob_pad_len(pos), pos = blob_next(pos))
|
||||
|
||||
|
||||
#endif
|
148
blobmsg.c
Normal file
148
blobmsg.c
Normal file
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* blobmsg - library for generating/parsing structured blob messages
|
||||
*
|
||||
* Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License version 2.1
|
||||
* as published by the Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "blobmsg.h"
|
||||
|
||||
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
|
||||
struct blob_attr **tb, void *data, int len)
|
||||
{
|
||||
struct blobmsg_hdr *hdr;
|
||||
struct blob_attr *attr;
|
||||
uint8_t *pslen;
|
||||
int i;
|
||||
|
||||
memset(tb, 0, policy_len * sizeof(*tb));
|
||||
pslen = alloca(policy_len);
|
||||
for (i = 0; i < policy_len; i++) {
|
||||
if (!policy[i].name)
|
||||
continue;
|
||||
|
||||
pslen[i] = strlen(policy[i].name);
|
||||
}
|
||||
|
||||
__blob_for_each_attr(attr, data, len) {
|
||||
hdr = blob_data(attr);
|
||||
for (i = 0; i < policy_len; i++) {
|
||||
if (!policy[i].name)
|
||||
continue;
|
||||
|
||||
if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
|
||||
blob_id(attr) != policy[i].type)
|
||||
continue;
|
||||
|
||||
if (hdr->namelen != pslen[i])
|
||||
continue;
|
||||
|
||||
if (!hdr->namelen)
|
||||
return -1;
|
||||
|
||||
if (sizeof(*attr) + blobmsg_hdrlen(hdr->namelen) > blob_pad_len(attr))
|
||||
return -1;
|
||||
|
||||
if (hdr->name[hdr->namelen] != 0)
|
||||
return -1;
|
||||
|
||||
if (tb[i])
|
||||
return -1;
|
||||
|
||||
if (strcmp(policy[i].name, (char *) hdr->name) != 0)
|
||||
continue;
|
||||
|
||||
tb[i] = attr;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static struct blob_attr *
|
||||
blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
|
||||
{
|
||||
struct blob_attr *attr;
|
||||
struct blobmsg_hdr *hdr;
|
||||
int attrlen, namelen;
|
||||
|
||||
if (blob_id(buf->head) == BLOBMSG_TYPE_ARRAY && !name) {
|
||||
attr = blob_new(buf, type, payload_len);
|
||||
*data = blob_data(attr);
|
||||
return attr;
|
||||
}
|
||||
|
||||
if (blob_id(buf->head) != BLOBMSG_TYPE_TABLE || !name)
|
||||
return NULL;
|
||||
|
||||
namelen = strlen(name);
|
||||
attrlen = blobmsg_hdrlen(namelen) + payload_len;
|
||||
attr = blob_new(buf, type, attrlen);
|
||||
if (!attr)
|
||||
return NULL;
|
||||
|
||||
hdr = blob_data(attr);
|
||||
hdr->namelen = namelen;
|
||||
strcpy((char *) hdr->name, (const char *)name);
|
||||
*data = blobmsg_data(attr);
|
||||
|
||||
return attr;
|
||||
}
|
||||
|
||||
static inline int
|
||||
attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
|
||||
{
|
||||
return (char *)attr - (char *) buf->buf;
|
||||
}
|
||||
|
||||
|
||||
void *
|
||||
blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
|
||||
{
|
||||
struct blob_attr *head = buf->head;
|
||||
int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
|
||||
unsigned long offset = attr_to_offset(buf, buf->head);
|
||||
void *data;
|
||||
|
||||
if (blob_id(head) == BLOBMSG_TYPE_ARRAY && !name)
|
||||
return blob_nest_start(buf, type);
|
||||
|
||||
if (blob_id(head) == BLOBMSG_TYPE_TABLE && name) {
|
||||
head = blobmsg_new(buf, type, name, 0, &data);
|
||||
blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
|
||||
buf->head = head;
|
||||
return (void *)offset;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
|
||||
const void *data, int len)
|
||||
{
|
||||
struct blob_attr *attr;
|
||||
void *data_dest;
|
||||
|
||||
if (type == BLOBMSG_TYPE_ARRAY ||
|
||||
type == BLOBMSG_TYPE_TABLE)
|
||||
return -1;
|
||||
|
||||
attr = blobmsg_new(buf, type, name, len, &data_dest);
|
||||
if (!attr)
|
||||
return -1;
|
||||
|
||||
if (len > 0)
|
||||
memcpy(data_dest, data, len);
|
||||
|
||||
return 0;
|
||||
}
|
133
blobmsg.h
Normal file
133
blobmsg.h
Normal file
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* blobmsg - library for generating/parsing structured blob messages
|
||||
*
|
||||
* Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License version 2.1
|
||||
* as published by the Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef __BLOBMSG_H
|
||||
#define __BLOBMSG_H
|
||||
|
||||
#include "blob.h"
|
||||
|
||||
#define BLOBMSG_ALIGN 2
|
||||
#define BLOBMSG_PADDING(len) (((len) + (1 << BLOBMSG_ALIGN) - 1) & ~((1 << BLOBMSG_ALIGN) - 1))
|
||||
|
||||
enum blobmsg_type {
|
||||
BLOBMSG_TYPE_UNSPEC,
|
||||
BLOBMSG_TYPE_ARRAY,
|
||||
BLOBMSG_TYPE_TABLE,
|
||||
BLOBMSG_TYPE_STRING,
|
||||
BLOBMSG_TYPE_INT64,
|
||||
BLOBMSG_TYPE_INT32,
|
||||
BLOBMSG_TYPE_INT16,
|
||||
BLOBMSG_TYPE_INT8
|
||||
};
|
||||
|
||||
struct blobmsg_hdr {
|
||||
uint8_t namelen;
|
||||
uint8_t name[];
|
||||
} __packed;
|
||||
|
||||
struct blobmsg_policy {
|
||||
const char *name;
|
||||
enum blobmsg_type type;
|
||||
};
|
||||
|
||||
static inline int blobmsg_hdrlen(int namelen)
|
||||
{
|
||||
return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr) + namelen + 1);
|
||||
}
|
||||
|
||||
static inline void *blobmsg_data(struct blob_attr *attr)
|
||||
{
|
||||
struct blobmsg_hdr *hdr = blob_data(attr);
|
||||
return &hdr->name[blobmsg_hdrlen(hdr->namelen) - 1];
|
||||
}
|
||||
|
||||
static inline int blobmsg_data_len(struct blob_attr *attr)
|
||||
{
|
||||
uint8_t *start, *end;
|
||||
|
||||
start = blob_data(attr);
|
||||
end = blobmsg_data(attr);
|
||||
|
||||
return blob_len(attr) - (end - start);
|
||||
}
|
||||
|
||||
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
|
||||
struct blob_attr **tb, void *data, int len);
|
||||
|
||||
int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
|
||||
const void *data, int len);
|
||||
|
||||
static inline int
|
||||
blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
|
||||
{
|
||||
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT8, name, &val, 1);
|
||||
}
|
||||
|
||||
static inline int
|
||||
blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
|
||||
{
|
||||
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT16, name, &val, 2);
|
||||
}
|
||||
|
||||
static inline int
|
||||
blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
|
||||
{
|
||||
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT32, name, &val, 4);
|
||||
}
|
||||
|
||||
static inline int
|
||||
blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
|
||||
{
|
||||
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT64, name, &val, 8);
|
||||
}
|
||||
|
||||
static inline int
|
||||
blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
|
||||
{
|
||||
return blobmsg_add_field(buf, BLOBMSG_TYPE_STRING, name, string, strlen(string) + 1);
|
||||
}
|
||||
|
||||
void *blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array);
|
||||
|
||||
static inline void *
|
||||
blobmsg_open_array(struct blob_buf *buf, const char *name)
|
||||
{
|
||||
return blobmsg_open_nested(buf, name, true);
|
||||
}
|
||||
|
||||
static inline void *
|
||||
blobmsg_open_table(struct blob_buf *buf, const char *name)
|
||||
{
|
||||
return blobmsg_open_nested(buf, name, false);
|
||||
}
|
||||
|
||||
static inline void
|
||||
blobmsg_close_array(struct blob_buf *buf, void *cookie)
|
||||
{
|
||||
blob_nest_end(buf, cookie);
|
||||
}
|
||||
|
||||
static inline void
|
||||
blobmsg_close_table(struct blob_buf *buf, void *cookie)
|
||||
{
|
||||
blob_nest_end(buf, cookie);
|
||||
}
|
||||
|
||||
static inline int blobmsg_buf_init(struct blob_buf *buf)
|
||||
{
|
||||
return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
|
||||
}
|
||||
|
||||
#endif
|
54
hash.c
Normal file
54
hash.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "hash.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// MurmurHashNeutral2, by Austin Appleby
|
||||
|
||||
// Same as MurmurHash2, but endian- and alignment-neutral.
|
||||
uint32_t hash_murmur2(const void * key, int len)
|
||||
{
|
||||
const unsigned int seed = 0xdeadc0de;
|
||||
const unsigned int m = 0x5bd1e995;
|
||||
const int r = 24;
|
||||
|
||||
unsigned int h = seed ^ len;
|
||||
|
||||
const unsigned char * data = (const unsigned char *)key;
|
||||
|
||||
while(len >= 4)
|
||||
{
|
||||
unsigned int k;
|
||||
|
||||
k = data[0];
|
||||
k |= data[1] << 8;
|
||||
k |= data[2] << 16;
|
||||
k |= data[3] << 24;
|
||||
|
||||
k *= m;
|
||||
k ^= k >> r;
|
||||
k *= m;
|
||||
|
||||
h *= m;
|
||||
h ^= k;
|
||||
|
||||
data += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
switch(len)
|
||||
{
|
||||
case 3: h ^= data[2] << 16;
|
||||
case 2: h ^= data[1] << 8;
|
||||
case 1: h ^= data[0];
|
||||
h *= m;
|
||||
};
|
||||
|
||||
h ^= h >> 13;
|
||||
h *= m;
|
||||
h ^= h >> 15;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
|
8
hash.h
Normal file
8
hash.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef _HASH_H__
|
||||
#define _HASH_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t hash_murmur2(const void * key, int len);
|
||||
|
||||
#endif
|
36
uapi.h
Normal file
36
uapi.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* uapi - Common API macros
|
||||
* Copyright (C) 2010 Steven Barth <steven@midlink.org>
|
||||
* Copyright (C) 2010 John Crispin <blogic@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UAPI_H_
|
||||
#define UAPI_H_
|
||||
|
||||
#define API_CTOR __attribute__((constructor))
|
||||
#define API_DTOR __attribute__((destructor))
|
||||
#define API_HIDDEN __attribute__((visibility("hidden")))
|
||||
#define API_INTERNAL __attribute__((visibility("internal")))
|
||||
#define API_DEFAULT __attribute__((visibility("default")))
|
||||
#define API_ALLOC __attribute__((malloc))
|
||||
#define API_NONNULL(...) __attribute__((nonnull(__VA_ARGS_)))
|
||||
#define API_FORCEINLINE __attribute__((always_inline)) inline
|
||||
|
||||
#define API_PACKED __attribute__((packed))
|
||||
#define API_CLEANUP(gc) __attribute__((cleanup(gc)))
|
||||
#endif /* UAPI_H_ */
|
176
ucix.c
Normal file
176
ucix.c
Normal file
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* ucix
|
||||
* Copyright (C) 2010 John Crispin <blogic@openwrt.org>
|
||||
* Copyright (C) 2010 Steven Barth <steven@midlink.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ucix.h"
|
||||
|
||||
struct uci_ptr uci_ptr;
|
||||
|
||||
int ucix_get_ptr(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
|
||||
{
|
||||
memset(&uci_ptr, 0, sizeof(uci_ptr));
|
||||
uci_ptr.package = p;
|
||||
uci_ptr.section = s;
|
||||
uci_ptr.option = o;
|
||||
uci_ptr.value = t;
|
||||
return uci_lookup_ptr(ctx, &uci_ptr, NULL, true);
|
||||
}
|
||||
|
||||
struct uci_context* ucix_init(const char *config_file, int state)
|
||||
{
|
||||
struct uci_context *ctx = uci_alloc_context();
|
||||
uci_set_confdir(ctx, "/etc/config");
|
||||
if(state)
|
||||
uci_set_savedir(ctx, "/var/state/");
|
||||
else
|
||||
uci_set_savedir(ctx, "/tmp/.uci/");
|
||||
if(uci_load(ctx, config_file, NULL) != UCI_OK)
|
||||
{
|
||||
printf("%s/%s is missing or corrupt\n", ctx->confdir, config_file);
|
||||
return NULL;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
struct uci_context* ucix_init_path(const char *vpath, const char *config_file, int state)
|
||||
{
|
||||
struct uci_context *ctx;
|
||||
char buf[256];
|
||||
if(!vpath)
|
||||
return ucix_init(config_file, state);
|
||||
ctx = uci_alloc_context();
|
||||
buf[255] = '\0';
|
||||
snprintf(buf, 255, "%s%s", vpath, "/etc/config");
|
||||
uci_set_confdir(ctx, buf);
|
||||
snprintf(buf, 255, "%s%s", vpath, (state)?("/var/state"):("/tmp/.uci"));
|
||||
uci_add_delta_path(ctx, buf);
|
||||
if(uci_load(ctx, config_file, NULL) != UCI_OK)
|
||||
{
|
||||
printf("%s/%s is missing or corrupt\n", ctx->confdir, config_file);
|
||||
return NULL;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
int ucix_get_option_list(struct uci_context *ctx, const char *p,
|
||||
const char *s, const char *o, struct list_head *l)
|
||||
{
|
||||
struct uci_element *e = NULL;
|
||||
if(ucix_get_ptr(ctx, p, s, o, NULL))
|
||||
return 1;
|
||||
if (!(uci_ptr.flags & UCI_LOOKUP_COMPLETE))
|
||||
return 1;
|
||||
e = uci_ptr.last;
|
||||
switch (e->type)
|
||||
{
|
||||
case UCI_TYPE_OPTION:
|
||||
switch(uci_ptr.o->type) {
|
||||
case UCI_TYPE_LIST:
|
||||
uci_foreach_element(&uci_ptr.o->v.list, e)
|
||||
{
|
||||
struct ucilist *ul = malloc(sizeof(struct ucilist));
|
||||
ul->val = strdup((e->name)?(e->name):(""));
|
||||
list_add_tail(&ul->list, l);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o)
|
||||
{
|
||||
struct uci_element *e = NULL;
|
||||
const char *value = NULL;
|
||||
if(ucix_get_ptr(ctx, p, s, o, NULL))
|
||||
return NULL;
|
||||
if (!(uci_ptr.flags & UCI_LOOKUP_COMPLETE))
|
||||
return NULL;
|
||||
e = uci_ptr.last;
|
||||
switch (e->type)
|
||||
{
|
||||
case UCI_TYPE_SECTION:
|
||||
value = uci_to_section(e)->type;
|
||||
break;
|
||||
case UCI_TYPE_OPTION:
|
||||
switch(uci_ptr.o->type) {
|
||||
case UCI_TYPE_STRING:
|
||||
value = uci_ptr.o->v.string;
|
||||
break;
|
||||
default:
|
||||
value = NULL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (value) ? (strdup(value)):(NULL);
|
||||
}
|
||||
|
||||
void ucix_add_list(struct uci_context *ctx, const char *p, const char *s, const char *o, struct list_head *vals)
|
||||
{
|
||||
struct list_head *q;
|
||||
list_for_each(q, vals)
|
||||
{
|
||||
struct ucilist *ul = container_of(q, struct ucilist, list);
|
||||
if(ucix_get_ptr(ctx, p, s, o, (ul->val)?(ul->val):("")))
|
||||
return;
|
||||
uci_add_list(ctx, &uci_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void ucix_for_each_section_type(struct uci_context *ctx,
|
||||
const char *p, const char *t,
|
||||
void (*cb)(const char*, void*), void *priv)
|
||||
{
|
||||
struct uci_element *e;
|
||||
if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
|
||||
return;
|
||||
uci_foreach_element(&uci_ptr.p->sections, e)
|
||||
if (!strcmp(t, uci_to_section(e)->type))
|
||||
cb(e->name, priv);
|
||||
}
|
||||
|
||||
void ucix_for_each_section_option(struct uci_context *ctx,
|
||||
const char *p, const char *s,
|
||||
void (*cb)(const char*, const char*, void*), void *priv)
|
||||
{
|
||||
struct uci_element *e;
|
||||
if(ucix_get_ptr(ctx, p, s, NULL, NULL))
|
||||
return;
|
||||
uci_foreach_element(&uci_ptr.s->options, e)
|
||||
{
|
||||
struct uci_option *o = uci_to_option(e);
|
||||
cb(o->e.name, o->v.string, priv);
|
||||
}
|
||||
}
|
||||
|
||||
|
133
ucix.h
Normal file
133
ucix.h
Normal file
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* ucix
|
||||
* Copyright (C) 2010 John Crispin <blogic@openwrt.org>
|
||||
* Copyright (C) 2010 Steven Barth <steven@midlink.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _UCI_H__
|
||||
#define _UCI_H__
|
||||
#include <uci.h>
|
||||
#include "list.h"
|
||||
|
||||
struct ucilist {
|
||||
struct list_head list;
|
||||
char *val;
|
||||
};
|
||||
|
||||
extern struct uci_ptr uci_ptr;
|
||||
|
||||
int ucix_get_ptr(struct uci_context *ctx, const char *p,
|
||||
const char *s, const char *o, const char *t);
|
||||
struct uci_context* ucix_init(const char *config_file, int state);
|
||||
struct uci_context* ucix_init_path(const char *vpath, const char *config_file, int state);
|
||||
int ucix_save_state(struct uci_context *ctx, const char *p);
|
||||
char* ucix_get_option(struct uci_context *ctx,
|
||||
const char *p, const char *s, const char *o);
|
||||
int ucix_get_option_list(struct uci_context *ctx, const char *p,
|
||||
const char *s, const char *o, struct list_head *l);
|
||||
void ucix_for_each_section_type(struct uci_context *ctx,
|
||||
const char *p, const char *t,
|
||||
void (*cb)(const char*, void*), void *priv);
|
||||
void ucix_for_each_section_option(struct uci_context *ctx,
|
||||
const char *p, const char *s,
|
||||
void (*cb)(const char*, const char*, void*), void *priv);
|
||||
void ucix_add_list(struct uci_context *ctx, const char *p,
|
||||
const char *s, const char *o, struct list_head *vals);
|
||||
|
||||
static inline void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
|
||||
{
|
||||
if (!ucix_get_ptr(ctx, p, s, o, NULL))
|
||||
uci_delete(ctx, &uci_ptr);
|
||||
}
|
||||
|
||||
static inline void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
|
||||
{
|
||||
if (!ucix_get_ptr(ctx, p, s, o, NULL))
|
||||
uci_revert(ctx, &uci_ptr);
|
||||
}
|
||||
|
||||
static inline void ucix_add_list_single(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
|
||||
{
|
||||
if (ucix_get_ptr(ctx, p, s, o, t))
|
||||
return;
|
||||
uci_add_list(ctx, &uci_ptr);
|
||||
}
|
||||
|
||||
static inline void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
|
||||
{
|
||||
if (ucix_get_ptr(ctx, p, s, o, t))
|
||||
return;
|
||||
uci_set(ctx, &uci_ptr);
|
||||
}
|
||||
|
||||
static inline void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
|
||||
{
|
||||
if(ucix_get_ptr(ctx, p, s, NULL, t))
|
||||
return;
|
||||
uci_set(ctx, &uci_ptr);
|
||||
}
|
||||
|
||||
static inline void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
|
||||
{
|
||||
char tmp[64];
|
||||
snprintf(tmp, 64, "%d", t);
|
||||
ucix_add_option(ctx, p, s, o, tmp);
|
||||
}
|
||||
|
||||
static inline void ucix_add_list_single_int(struct uci_context *ctx, const char *p, const char *s, const char *o, const int t)
|
||||
{
|
||||
char tmp[64];
|
||||
snprintf(tmp, 64, "%d", t);
|
||||
ucix_add_list_single(ctx, p, s, o, tmp);
|
||||
}
|
||||
|
||||
static inline int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
|
||||
{
|
||||
char *tmp = ucix_get_option(ctx, p, s, o);
|
||||
int ret = def;
|
||||
|
||||
if (tmp)
|
||||
{
|
||||
ret = atoi(tmp);
|
||||
free(tmp);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline int ucix_save(struct uci_context *ctx, const char *p)
|
||||
{
|
||||
if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
|
||||
return 1;
|
||||
uci_save(ctx, uci_ptr.p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int ucix_commit(struct uci_context *ctx, const char *p)
|
||||
{
|
||||
if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
|
||||
return 1;
|
||||
return uci_commit(ctx, &uci_ptr.p, false);
|
||||
}
|
||||
|
||||
static inline void ucix_cleanup(struct uci_context *ctx)
|
||||
{
|
||||
uci_free_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
340
uhtbl.c
Normal file
340
uhtbl.c
Normal file
|
@ -0,0 +1,340 @@
|
|||
/**
|
||||
* uhtbl - Generic coalesced hash table implementation
|
||||
* Copyright (C) 2010 Steven Barth <steven@midlink.org>
|
||||
* Copyright (C) 2010 John Crispin <blogic@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "uhtbl.h"
|
||||
|
||||
/* Forward static helpers */
|
||||
UHTBL_INLINE uhtbl_size_t _uhtbl_address(uhtbl_t *tbl, uhtbl_bucket_t *bucket);
|
||||
UHTBL_INLINE uhtbl_bucket_t* _uhtbl_bucket(uhtbl_t *tbl, uhtbl_size_t address);
|
||||
static uhtbl_bucket_t* _uhtbl_allocate(uhtbl_t *tbl);
|
||||
static uhtbl_bucket_t* _uhtbl_find(uhtbl_t *tbl, const void *key,
|
||||
long len, uhtbl_bucket_t **previous, uhtbl_size_t *mainaddress);
|
||||
|
||||
|
||||
|
||||
UHTBL_API int uhtbl_init(uhtbl_t *tbl, uint32_t bucketsize,
|
||||
uhtbl_size_t sizehint, uhtbl_hash_t *fct_hash, uhtbl_gc_t *fct_gc) {
|
||||
sizehint = (sizehint) ? sizehint : UHTBL_MINIMUMSIZE;
|
||||
tbl->bucketsize = bucketsize;
|
||||
tbl->fct_hash = fct_hash;
|
||||
tbl->fct_gc = fct_gc;
|
||||
if (!tbl->fct_hash || tbl->bucketsize < sizeof(uhtbl_bucket_t)) {
|
||||
return UHTBL_EINVAL;
|
||||
}
|
||||
tbl->payload = 0;
|
||||
tbl->buckets = NULL;
|
||||
tbl->used = 0;
|
||||
|
||||
return uhtbl_resize(tbl, sizehint);
|
||||
}
|
||||
|
||||
UHTBL_API void uhtbl_clear(uhtbl_t *tbl) {
|
||||
for (uhtbl_size_t i = 0; i < tbl->size; i++) {
|
||||
uhtbl_bucket_t *bucket = _uhtbl_bucket(tbl, i);
|
||||
if (tbl->fct_gc && bucket->head.flags & UHTBL_FLAG_OCCUPIED) {
|
||||
tbl->fct_gc(bucket);
|
||||
}
|
||||
bucket->head.flags = 0;
|
||||
}
|
||||
tbl->used = 0;
|
||||
tbl->nextfree = tbl->size - 1;
|
||||
}
|
||||
|
||||
UHTBL_API void* uhtbl_get (uhtbl_t *tbl, const void *key, long len) {
|
||||
return _uhtbl_find(tbl, key, len, NULL, NULL);
|
||||
}
|
||||
|
||||
UHTBL_API void* uhtbl_set (uhtbl_t *tbl, void *key, long len) {
|
||||
int localkey = 0;
|
||||
uint16_t keysize = len, flags;
|
||||
if (!key) { /* Check whether key is treated as a pointer */
|
||||
key = &len;
|
||||
keysize = sizeof(len);
|
||||
localkey = 1;
|
||||
}
|
||||
|
||||
uhtbl_size_t mainaddr;
|
||||
uhtbl_bucket_t *resolve, *match, *prev = NULL;
|
||||
uhtbl_bucket_t *lookup = _uhtbl_find(tbl, key, keysize, &prev, &mainaddr);
|
||||
|
||||
if (lookup) {
|
||||
match = lookup;
|
||||
flags = match->head.flags;
|
||||
if (flags & UHTBL_FLAG_OCCUPIED) { /* We are replacing an entry */
|
||||
if (tbl->fct_gc) {
|
||||
tbl->fct_gc(match);
|
||||
}
|
||||
tbl->used--;
|
||||
}
|
||||
} else {
|
||||
match = prev;
|
||||
flags = match->head.flags;
|
||||
if ((flags & UHTBL_FLAG_STRANGER)
|
||||
&& _uhtbl_address(tbl, match) == mainaddr) {
|
||||
/* Mainposition occupied by key with different hash -> move it away */
|
||||
|
||||
/* Find old prev and update its next pointer */
|
||||
if ((flags & UHTBL_FLAG_LOCALKEY)) {
|
||||
_uhtbl_find(tbl, 0,
|
||||
match->head.key.handle, &prev, NULL);
|
||||
} else {
|
||||
_uhtbl_find(tbl, match->head.key.ptr,
|
||||
match->head.keysize, &prev, NULL);
|
||||
}
|
||||
|
||||
if (!(resolve = _uhtbl_allocate(tbl))) {
|
||||
if (!uhtbl_resize(tbl, tbl->payload * UHTBL_GROWFACTOR)) {
|
||||
return uhtbl_set(tbl, (localkey) ? NULL : key, len);
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
memcpy(resolve, match, tbl->bucketsize); /* Copy bucket data */
|
||||
prev->head.next = _uhtbl_address(tbl, resolve);
|
||||
flags = 0;
|
||||
} else if ((flags & UHTBL_FLAG_OCCUPIED) &&
|
||||
(match->head.keysize != keysize || memcmp((flags & UHTBL_FLAG_LOCALKEY)
|
||||
? &match->head.key.handle : match->head.key.ptr, key, keysize))) {
|
||||
/* Mainposition has different key (but same hash) */
|
||||
if (!(resolve = _uhtbl_allocate(tbl))) {
|
||||
if (!uhtbl_resize(tbl, tbl->payload * UHTBL_GROWFACTOR)) {
|
||||
return uhtbl_set(tbl, (localkey) ? NULL : key, len);
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
prev = match;
|
||||
match = resolve;
|
||||
flags = UHTBL_FLAG_STRANGER; /* We will not be in main position */
|
||||
prev->head.flags |= UHTBL_FLAG_WITHNEXT; /* Main now has next */
|
||||
prev->head.next = _uhtbl_address(tbl, match); /* main->next = us */
|
||||
}
|
||||
}
|
||||
|
||||
if (localkey) {
|
||||
match->head.key.handle = len;
|
||||
flags |= UHTBL_FLAG_LOCALKEY;
|
||||
} else {
|
||||
match->head.key.ptr = key;
|
||||
flags &= ~UHTBL_FLAG_LOCALKEY;
|
||||
}
|
||||
match->head.keysize = keysize;
|
||||
flags |= UHTBL_FLAG_OCCUPIED;
|
||||
match->head.flags = flags;
|
||||
|
||||
tbl->used++;
|
||||
return match;
|
||||
}
|
||||
|
||||
UHTBL_API void* uhtbl_next(uhtbl_t *tbl, uhtbl_size_t *iter) {
|
||||
for (; *iter < tbl->size; (*iter)++) {
|
||||
if (_uhtbl_bucket(tbl, *iter)->head.flags & UHTBL_FLAG_OCCUPIED) {
|
||||
return _uhtbl_bucket(tbl, (*iter)++);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
UHTBL_API int uhtbl_remove(uhtbl_t *tbl, uhtbl_head_t *head) {
|
||||
void *key;
|
||||
long len;
|
||||
uhtbl_key(head, &key, &len);
|
||||
return uhtbl_unset(tbl, key, len);
|
||||
}
|
||||
|
||||
UHTBL_API int uhtbl_unset(uhtbl_t *tbl, const void *key, long len) {
|
||||
uhtbl_bucket_t *prev = NULL;
|
||||
uhtbl_bucket_t *bucket = _uhtbl_find(tbl, key, len, &prev, NULL);
|
||||
if (!bucket) {
|
||||
return UHTBL_ENOENT;
|
||||
}
|
||||
|
||||
if (tbl->fct_gc) {
|
||||
tbl->fct_gc(bucket);
|
||||
}
|
||||
|
||||
bucket->head.flags &= ~UHTBL_FLAG_OCCUPIED;
|
||||
tbl->used--;
|
||||
|
||||
/* If not in main position, get us out of the next-list */
|
||||
if (bucket->head.flags & UHTBL_FLAG_STRANGER) {
|
||||
if (bucket->head.flags & UHTBL_FLAG_WITHNEXT) {/* We had next buckets */
|
||||
prev->head.next = bucket->head.next; /* Give them to out prev */
|
||||
} else {
|
||||
prev->head.flags &= ~UHTBL_FLAG_WITHNEXT;/* We were the only next */
|
||||
}
|
||||
bucket->head.flags = 0;
|
||||
}
|
||||
|
||||
uhtbl_size_t address = _uhtbl_address(tbl, bucket);
|
||||
if (address > tbl->nextfree) {
|
||||
tbl->nextfree = address;
|
||||
}
|
||||
|
||||
return UHTBL_OK;
|
||||
}
|
||||
|
||||
UHTBL_API int uhtbl_resize(uhtbl_t *tbl, uhtbl_size_t payload) {
|
||||
uhtbl_size_t size = payload / UHTBL_PAYLOADFACTOR;
|
||||
if (size < payload || size < tbl->used) {
|
||||
return UHTBL_EINVAL;
|
||||
}
|
||||
if (payload == tbl->payload) {
|
||||
return UHTBL_OK;
|
||||
}
|
||||
|
||||
void *buckets = calloc(size, tbl->bucketsize);
|
||||
if (!buckets) {
|
||||
return UHTBL_ENOMEM;
|
||||
}
|
||||
|
||||
uhtbl_t oldtbl; /* Save essentials of table for rehashing */
|
||||
oldtbl.buckets = tbl->buckets;
|
||||
oldtbl.bucketsize = tbl->bucketsize;
|
||||
oldtbl.size = tbl->size;
|
||||
oldtbl.used = tbl->used;
|
||||
|
||||
tbl->buckets = buckets;
|
||||
tbl->payload = payload;
|
||||
tbl->size = size;
|
||||
tbl->used = 0;
|
||||
tbl->nextfree = size - 1;
|
||||
|
||||
if (oldtbl.used) { /* Rehash if table had entries before */
|
||||
uhtbl_size_t iter = 0;
|
||||
uhtbl_bucket_t *old, *new;
|
||||
while ((old = uhtbl_next(&oldtbl, &iter))) {
|
||||
new = uhtbl_set(tbl, (old->head.flags & UHTBL_FLAG_LOCALKEY)
|
||||
? NULL : old->head.key.ptr,
|
||||
(old->head.flags & UHTBL_FLAG_LOCALKEY)
|
||||
? old->head.key.handle : old->head.keysize);
|
||||
new->head.user = old->head.user;
|
||||
memcpy(((char*)new) + sizeof(uhtbl_head_t),
|
||||
((char*)old) + sizeof(uhtbl_head_t),
|
||||
tbl->bucketsize - sizeof(uhtbl_head_t));
|
||||
}
|
||||
}
|
||||
|
||||
free(oldtbl.buckets);
|
||||
return UHTBL_OK;
|
||||
}
|
||||
|
||||
UHTBL_API void uhtbl_finalize(uhtbl_t *tbl) {
|
||||
uhtbl_clear(tbl);
|
||||
free(tbl->buckets);
|
||||
tbl->buckets = NULL;
|
||||
}
|
||||
|
||||
UHTBL_API void uhtbl_key(uhtbl_head_t *head, void **key, long *len) {
|
||||
if (key) {
|
||||
*key = (head->flags & UHTBL_FLAG_LOCALKEY)
|
||||
? NULL : head->key.ptr;
|
||||
}
|
||||
if (len) {
|
||||
*len = (head->flags & UHTBL_FLAG_LOCALKEY)
|
||||
? head->key.handle : head->keysize;
|
||||
}
|
||||
}
|
||||
|
||||
UHTBL_API void uhtbl_gc_key(void *bucket) {
|
||||
void *key;
|
||||
uhtbl_key(bucket, &key, NULL);
|
||||
free(key);
|
||||
}
|
||||
|
||||
|
||||
/* Static auxiliary */
|
||||
|
||||
UHTBL_INLINE uhtbl_size_t _uhtbl_address(uhtbl_t *tbl, uhtbl_bucket_t *bucket) {
|
||||
return ((uint8_t*)bucket - (uint8_t*)tbl->buckets) / tbl->bucketsize;
|
||||
}
|
||||
|
||||
UHTBL_INLINE uhtbl_bucket_t* _uhtbl_bucket(uhtbl_t *tbl, uhtbl_size_t address) {
|
||||
return (uhtbl_bucket_t*)
|
||||
((uint8_t*)tbl->buckets + (address * tbl->bucketsize));
|
||||
}
|
||||
|
||||
static uhtbl_bucket_t* _uhtbl_allocate(uhtbl_t *tbl) {
|
||||
uhtbl_size_t address = tbl->nextfree;
|
||||
do {
|
||||
uhtbl_bucket_t *bucket = _uhtbl_bucket(tbl, address);
|
||||
if (!(bucket->head.flags & UHTBL_FLAG_OCCUPIED)) {
|
||||
if (bucket->head.flags & UHTBL_FLAG_WITHNEXT) {
|
||||
/* Empty bucket still has a successor -> swap it with its */
|
||||
/* successor and return the old successor-bucket as free */
|
||||
uhtbl_bucket_t *old = bucket;
|
||||
bucket = _uhtbl_bucket(tbl, old->head.next);
|
||||
memcpy(old, bucket, tbl->bucketsize);
|
||||
old->head.flags &= ~UHTBL_FLAG_STRANGER; /* sucessor now main */
|
||||
}
|
||||
/* WARN: If set will ever fail in the future we'd take care here */
|
||||
tbl->nextfree = (address) ? address - 1 : 0;
|
||||
return bucket;
|
||||
}
|
||||
} while(address--);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static uhtbl_bucket_t* _uhtbl_find(uhtbl_t *tbl, const void *key,
|
||||
long len, uhtbl_bucket_t **previous, uhtbl_size_t *mainaddress) {
|
||||
uint16_t keysize = len;
|
||||
if (!key) {
|
||||
key = &len;
|
||||
keysize = sizeof(len);
|
||||
}
|
||||
uhtbl_size_t address = tbl->fct_hash(key, keysize) % tbl->payload;
|
||||
uhtbl_bucket_t *buck = _uhtbl_bucket(tbl, address);
|
||||
if (mainaddress) {
|
||||
*mainaddress = address;
|
||||
if (!(buck->head.flags & UHTBL_FLAG_OCCUPIED)) {
|
||||
return buck;
|
||||
}
|
||||
}
|
||||
if (buck->head.flags & UHTBL_FLAG_STRANGER) {
|
||||
if (previous) {
|
||||
*previous = buck;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
for (;; buck = _uhtbl_bucket(tbl, address)) {
|
||||
if (buck->head.flags & UHTBL_FLAG_OCCUPIED
|
||||
&& buck->head.keysize == keysize
|
||||
&& !memcmp((buck->head.flags & UHTBL_FLAG_LOCALKEY)
|
||||
? &buck->head.key.handle : buck->head.key.ptr, key, keysize)) {
|
||||
return buck;
|
||||
}
|
||||
if (!(buck->head.flags & UHTBL_FLAG_WITHNEXT)) {
|
||||
if (previous) {
|
||||
*previous = buck;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
address = buck->head.next;
|
||||
if (previous) {
|
||||
*previous = buck;
|
||||
}
|
||||
}
|
||||
}
|
375
uhtbl.h
Normal file
375
uhtbl.h
Normal file
|
@ -0,0 +1,375 @@
|
|||
/**
|
||||
* uhtbl - Generic coalesced hash table implementation
|
||||
* Copyright (C) 2010 Steven Barth <steven@midlink.org>
|
||||
* Copyright (C) 2010 John Crispin <blogic@openwrt.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* uhtbl is a coalesced cellared generic hash table implementation with the aim
|
||||
* to be both small in code size and heap memory requirements. This hash table
|
||||
* uses a hybrid approach called coalesced addressing which means that pointers
|
||||
* to other buckets will be used in the case of a collisions. In this case no
|
||||
* linked lists have to be used and less allocation calls have to be done.
|
||||
* To improve performance this hash table carries a cellar for collision
|
||||
* handling which is an additional address area that lies behind the
|
||||
* hash-addressable space.
|
||||
*
|
||||
* Overhead (on x86 32bit):
|
||||
* Bookkeeping: 32 Bytes (per hash table)
|
||||
* Metadata: 12 Bytes including pointer to key and keysize (per bucket)
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UHTBL_H_
|
||||
#define UHTBL_H_
|
||||
|
||||
|
||||
/* compile-time configurables */
|
||||
|
||||
#ifndef UHTBL_PAYLOADFACTOR
|
||||
#define UHTBL_PAYLOADFACTOR 0.86
|
||||
#endif
|
||||
|
||||
#ifndef UHTBL_GROWFACTOR
|
||||
#define UHTBL_GROWFACTOR 2
|
||||
#endif
|
||||
|
||||
#ifndef UHTBL_MINIMUMSIZE
|
||||
#define UHTBL_MINIMUMSIZE 16
|
||||
#endif
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Internal flags and values */
|
||||
#define UHTBL_FLAG_OCCUPIED 0x01
|
||||
#define UHTBL_FLAG_STRANGER 0x02
|
||||
#define UHTBL_FLAG_WITHNEXT 0x04
|
||||
#define UHTBL_FLAG_LOCALKEY 0x08
|
||||
#define UHTBL_MAXIMUMSIZE 2147483648
|
||||
|
||||
/* Status codes */
|
||||
#define UHTBL_OK 0
|
||||
#define UHTBL_EINVAL -1
|
||||
#define UHTBL_ENOMEM -2
|
||||
#define UHTBL_ENOENT -3
|
||||
|
||||
/* API */
|
||||
#if __GNUC__ >= 4
|
||||
#ifndef UHTBL_API
|
||||
#define UHTBL_API
|
||||
#endif
|
||||
#define UHTBL_INLINE static inline __attribute__((always_inline))
|
||||
#else
|
||||
#ifndef UHTBL_API
|
||||
#define UHTBL_API
|
||||
#endif
|
||||
#define UHTBL_INLINE static inline
|
||||
#endif
|
||||
|
||||
|
||||
typedef union uhtbl_key uhtbl_key_t;
|
||||
typedef struct uhtbl_head uhtbl_head_t;
|
||||
typedef struct uhtbl_bucket uhtbl_bucket_t;
|
||||
typedef struct uhtbl_config uhtbl_config_t;
|
||||
typedef struct uhtbl uhtbl_t;
|
||||
typedef uint32_t uhtbl_size_t;
|
||||
|
||||
typedef uhtbl_size_t(uhtbl_hash_t)(const void*, int len);
|
||||
typedef void(uhtbl_gc_t)(void *bucket);
|
||||
|
||||
union uhtbl_key {
|
||||
void *ptr;
|
||||
long handle;
|
||||
};
|
||||
|
||||
struct uhtbl_head {
|
||||
uint8_t user;
|
||||
uint8_t flags;
|
||||
uint16_t keysize;
|
||||
uhtbl_size_t next;
|
||||
uhtbl_key_t key;
|
||||
};
|
||||
|
||||
struct uhtbl_bucket {
|
||||
uhtbl_head_t head;
|
||||
};
|
||||
|
||||
struct uhtbl {
|
||||
uint32_t bucketsize;
|
||||
uhtbl_size_t size;
|
||||
uhtbl_size_t used;
|
||||
uhtbl_size_t payload;
|
||||
uhtbl_size_t nextfree;
|
||||
uhtbl_hash_t *fct_hash;
|
||||
uhtbl_gc_t *fct_gc;
|
||||
void *buckets;
|
||||
};
|
||||
|
||||
/**
|
||||
* uhtbl_init() - Initialize a hash table.
|
||||
* @tbl: hash table
|
||||
* @bucketsize: size of a bucket
|
||||
* @sizehint: estimated maximum of needed buckets (optional)
|
||||
* @fct_hash: hash function
|
||||
* @fct_gc: bucket garbage collector (optional)
|
||||
*
|
||||
* Initializes a new hash table and preallocates memory.
|
||||
*
|
||||
* bucketsize is the size in Bytes each bucket will use but note the following:
|
||||
* Each bucket needs to begin with a struct uhtbl_head_t that keeps its metadata
|
||||
* in addition to the payload you want it to carry. You are advised to define a
|
||||
* bucket struct with the first element being a uhtbl_head_t followed by your
|
||||
* desired payload and pass the size of this struct to bucketsize.
|
||||
*
|
||||
* sizehint is a hint on how many distinct entries will be stored in the hash
|
||||
* table. This will be used to preallocate space for the buckets and is useful
|
||||
* if you know how many entries will be stored in the hash table as it avoids
|
||||
* expensive rehashing cycles. sizehint should be a power of 2.
|
||||
*
|
||||
* fct_hash is the hash function used. It takes a constant void pointer and a
|
||||
* integer as size parameter and returns an unsigned (32bit) int.
|
||||
*
|
||||
* fct_gc is the garbage collector for buckets. Every time a bucket gets unset
|
||||
* or the hash table gets cleared or finalized the garbage collector function
|
||||
* taking a pointer to a bucket will take care of doing any finalization for
|
||||
* the buckets' payload and key data. You may use uhtbl_key() to get a reference
|
||||
* to your key pointer or handle for deallocation or cleaning up any other
|
||||
* references. There is an optionally selectable garbage collector that will
|
||||
* take care of free()ing key pointers if your keys point to memory areas.
|
||||
* You have to pass uhtbl_gc_key as fct_gc parameter to use it. You may also
|
||||
* call this function in your custom garbage collector.
|
||||
*
|
||||
* WARNING: Your garbage collector function must otherwise not change the
|
||||
* metadata in the uhtbl_head_t structure of the bucket else behaviour will be
|
||||
* undefined for all subsequent actions.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* struct mybucket {
|
||||
* uhtbl_head_t head;
|
||||
* int mypayload1;
|
||||
* int mypayload2;
|
||||
* }
|
||||
*
|
||||
* uhtbl_t table;
|
||||
* uhtbl_init(&table, sizeof(struct mybucket), 32, MurmurHash2, NULL);
|
||||
*
|
||||
* Returns 0 on success or a negative error code.
|
||||
*/
|
||||
UHTBL_API int uhtbl_init(uhtbl_t *tbl, uint32_t bucketsize,
|
||||
uhtbl_size_t sizehint, uhtbl_hash_t *fct_hash, uhtbl_gc_t *fct_gc);
|
||||
|
||||
/**
|
||||
* uhtbl_get() - Get a bucket by its key.
|
||||
* @tbl: hash table
|
||||
* @key: key
|
||||
* @len: length of key
|
||||
*
|
||||
* Finds and returns the bucket with a given key.
|
||||
*
|
||||
* Key can either be:
|
||||
* 1. A pointer to a memory area then len is its length (must be < 64KB)
|
||||
* 2. A NULL-pointer then len is a locally stored numerical key
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* struct mybucket *bucket;
|
||||
* bucket = uhtbl_get(table, "foo", sizeof("foo"));
|
||||
* printf("%i", bucket->mypayload1);
|
||||
*
|
||||
* bucket = uhtbl_get(table, NULL, 42);
|
||||
* printf("%i", bucket->mypayload1);
|
||||
*
|
||||
* Returns the bucket or NULL if no bucket with given key was found.
|
||||
*/
|
||||
UHTBL_API void* uhtbl_get(uhtbl_t *tbl, const void *key, long len);
|
||||
|
||||
/**
|
||||
* uhtbl_set() - Sets a bucket for given key.
|
||||
* @tbl: hash table
|
||||
* @key: key
|
||||
* @len: length of key
|
||||
*
|
||||
* Sets a new bucket for the given key and returns a pointer to the bucket for
|
||||
* you to assign your payload data. If there is already a bucket with that key
|
||||
* it will be unset first.
|
||||
*
|
||||
* Key can either be:
|
||||
* 1. A pointer to a memory area then len is its length (must be < 64KB)
|
||||
* 2. A NULL-pointer then len is a locally stored numerical key
|
||||
*
|
||||
* NOTE: If key is a pointer memory management of it will be your business.
|
||||
* You might want to use a garbage collection function (see uhtbl_init())
|
||||
*
|
||||
* NOTE: The payload area of your bucket is NOT initialized to zeroes.
|
||||
*
|
||||
* WARNING: Note the following side effects when setting previously unset keys:
|
||||
* 1. A set may trigger several moving actions changing the order of buckets.
|
||||
* 2. A set may trigger a rehashing cycle if all buckets are occupied.
|
||||
* Therefore accessing any previously acquired pointers to any bucket results in
|
||||
* undefined behaviour. In addition iterations which have started before may
|
||||
* result in unwanted behaviour (e.g. buckets may be skipped or visited twice).
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* struct mybucket *bucket;
|
||||
* bucket = uhtbl_set(table, "foo", sizeof("foo"));
|
||||
* bucket->mypayload1 = 42:
|
||||
*
|
||||
* bucket = uhtbl_set(table, NULL, 42);
|
||||
* bucket->mypayload1 = 1337;
|
||||
*
|
||||
*
|
||||
* Returns the bucket or NULL if no bucket could be allocated (out of memory).
|
||||
*/
|
||||
UHTBL_API void* uhtbl_set(uhtbl_t *tbl, void *key, long len);
|
||||
|
||||
/**
|
||||
* uhtbl_next() - Iterates over all entries of the hash table.
|
||||
* @tbl: hash table
|
||||
* @iter: Iteration counter
|
||||
*
|
||||
* Iterates over all entries of the hash table.
|
||||
*
|
||||
* iter is a pointer to a numeric variable that should be set to zero before
|
||||
* the first call and will save the iteration state.
|
||||
*
|
||||
* NOTE: You may safely do several iterations in parallel. You may also safely
|
||||
* unset any buckets of the hashtable or set keys that are currently in the
|
||||
* hash table. However setting buckets with keys that don't have an assigned
|
||||
* bucket yet results in undefined behaviour.
|
||||
*
|
||||
* Example:
|
||||
* uint32_t iter = 0;
|
||||
* struct mybucket *bucket;
|
||||
* while ((bucket = uhtbl_next(table, &iter))) {
|
||||
* printf("%i", bucket->mypayload1);
|
||||
* }
|
||||
*
|
||||
* Return the next bucket or NULL if all buckets were already visited.
|
||||
*/
|
||||
UHTBL_API void* uhtbl_next(uhtbl_t *tbl, uhtbl_size_t *iter);
|
||||
|
||||
/**
|
||||
* uhtbl_unset() - Unsets the bucket with given key.
|
||||
* @tbl: hash table
|
||||
* @key: key
|
||||
* @len: length of key (optional)
|
||||
*
|
||||
* Unsets the bucket with given key and calls the garbage collector to free
|
||||
* any payload resources - if any.
|
||||
*
|
||||
* Key can either be:
|
||||
* 1. A pointer to a memory area then len is its length (must be < 64KB)
|
||||
* 2. A NULL-pointer then len is a locally stored numerical key
|
||||
*
|
||||
* Example:
|
||||
* uhtbl_unset(table, NULL, 42);
|
||||
*
|
||||
* Returns 0 on success or a negative error code if there was no matching bucket
|
||||
*/
|
||||
UHTBL_API int uhtbl_unset(uhtbl_t *tbl, const void *key, long len);
|
||||
|
||||
/**
|
||||
* uhtbl_remove() - Unsets a bucket.
|
||||
* @tbl: hash table
|
||||
* @head: bucket head
|
||||
*
|
||||
* Unsets the bucket with given address and calls the garbage collector to free
|
||||
* any payload resources - if any.
|
||||
*
|
||||
* Example:
|
||||
* uhtbl_remove(table, &bucket->head);
|
||||
*
|
||||
* Returns 0 on success or a negative error code if the bucket was not found
|
||||
*/
|
||||
UHTBL_API int uhtbl_remove(uhtbl_t *tbl, uhtbl_head_t *head);
|
||||
|
||||
/**
|
||||
* uhtbl_clear() - Clears the hashtable without freeing its memory.
|
||||
* @tbl: hash table
|
||||
*
|
||||
* Clears all buckets of the hashtable invoking the garbage collector - if any
|
||||
* but does not free the memory of the hash table. This is usually more
|
||||
* efficient than iterating and using unset.
|
||||
*
|
||||
* Returns nothing.
|
||||
*/
|
||||
UHTBL_API void uhtbl_clear(uhtbl_t *tbl);
|
||||
|
||||
/**
|
||||
* uhtbl_resize() - Resizes and rehashes the hash table.
|
||||
* @tbl: hash table
|
||||
* @payload: Buckets to reserve.
|
||||
*
|
||||
* Resizes the hash table and rehashes its entries.
|
||||
*
|
||||
* payload is the number of buckets the hashtable should allocate. It must be
|
||||
* greater or at least equal to the number of buckets currently occupied.
|
||||
*
|
||||
* NOTE: Rehashing is an expensive process which should be avoided if possible.
|
||||
* However resizing will be automatically done if you try to set a new bucket
|
||||
* but all buckets are already occupied. In this case the payload bucket count
|
||||
* is usually doubled. There is currently no automatic resizing done when the
|
||||
* bucket usage count decreases. You have to take care of this by yourself.
|
||||
*
|
||||
* Returns 0 on success or a negative error code if out of memory.
|
||||
*/
|
||||
UHTBL_API int uhtbl_resize(uhtbl_t *tbl, uhtbl_size_t payload);
|
||||
|
||||
/**
|
||||
* uhtbl_clear() - Clears the hashtable and frees the bucket memory.
|
||||
* @tbl: hash table
|
||||
*
|
||||
* Clears all buckets of the hashtable invoking the garbage collector - if any
|
||||
* and frees the memory occupied by the buckets.
|
||||
*
|
||||
* Returns nothing.
|
||||
*/
|
||||
UHTBL_API void uhtbl_finalize(uhtbl_t *tbl);
|
||||
|
||||
/**
|
||||
* uhtbl_key() - Returns the key parameters as passed to set.
|
||||
* @head: Bucket head
|
||||
* @key: Pointer where key pointer should be stored (optional)
|
||||
* @len: Pointer where key len should be stored (optional)
|
||||
*
|
||||
* This function might be useful to obtain the key parameters of a bucket
|
||||
* when doing garbage collection.
|
||||
*
|
||||
* Returns nothing.
|
||||
*/
|
||||
UHTBL_API void uhtbl_key(uhtbl_head_t *head, void **key, long *len);
|
||||
|
||||
/**
|
||||
* uhtbl_gc_key() - Basic garbage collector that frees key memory.
|
||||
* @bucket: Bucket
|
||||
*
|
||||
* This function is a basic garbage collector that will free any key pointers.
|
||||
* However it will not touch your payload data.
|
||||
*
|
||||
* WARNING: You must not call this function directly on any bucket otherwise
|
||||
* behaviour will be unspecified. Instead you may pass this function to the
|
||||
* uhtbl_init function. You may also call this function from inside a custom
|
||||
* garbage collector.
|
||||
*
|
||||
* Returns nothing.
|
||||
*/
|
||||
UHTBL_API void uhtbl_gc_key(void *bucket);
|
||||
|
||||
#endif /* UHTBL_H_ */
|
290
unl.c
Normal file
290
unl.c
Normal file
|
@ -0,0 +1,290 @@
|
|||
#include <netlink/netlink.h>
|
||||
#include <netlink/genl/genl.h>
|
||||
#include <netlink/genl/ctrl.h>
|
||||
#include <netlink/genl/family.h>
|
||||
#include <sys/types.h>
|
||||
#include <net/if.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/nl80211.h>
|
||||
|
||||
#include "unl.h"
|
||||
|
||||
static int unl_init(struct unl *unl)
|
||||
{
|
||||
unl->sock = nl_socket_alloc();
|
||||
if (!unl->sock)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int unl_genl_init(struct unl *unl, const char *family)
|
||||
{
|
||||
memset(unl, 0, sizeof(*unl));
|
||||
|
||||
if (unl_init(unl))
|
||||
goto error_out;
|
||||
|
||||
unl->hdrlen = NLMSG_ALIGN(sizeof(struct genlmsghdr));
|
||||
unl->family_name = strdup(family);
|
||||
if (!unl->family_name)
|
||||
goto error;
|
||||
|
||||
if (genl_connect(unl->sock))
|
||||
goto error;
|
||||
|
||||
if (genl_ctrl_alloc_cache(unl->sock, &unl->cache))
|
||||
goto error;
|
||||
|
||||
unl->family = genl_ctrl_search_by_name(unl->cache, family);
|
||||
if (!unl->family)
|
||||
goto error;
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
unl_free(unl);
|
||||
error_out:
|
||||
return -1;
|
||||
}
|
||||
|
||||
void unl_free(struct unl *unl)
|
||||
{
|
||||
if (unl->family_name)
|
||||
free(unl->family_name);
|
||||
|
||||
if (unl->sock)
|
||||
nl_socket_free(unl->sock);
|
||||
|
||||
if (unl->cache)
|
||||
nl_cache_free(unl->cache);
|
||||
|
||||
memset(unl, 0, sizeof(*unl));
|
||||
}
|
||||
|
||||
static int
|
||||
ack_handler(struct nl_msg *msg, void *arg)
|
||||
{
|
||||
int *err = arg;
|
||||
*err = 0;
|
||||
return NL_STOP;
|
||||
}
|
||||
|
||||
static int
|
||||
finish_handler(struct nl_msg *msg, void *arg)
|
||||
{
|
||||
int *err = arg;
|
||||
*err = 0;
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
static int
|
||||
error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
|
||||
{
|
||||
int *ret = arg;
|
||||
*ret = err->error;
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
struct nl_msg *unl_genl_msg(struct unl *unl, int cmd, bool dump)
|
||||
{
|
||||
struct nl_msg *msg;
|
||||
int flags = 0;
|
||||
|
||||
msg = nlmsg_alloc();
|
||||
if (!msg)
|
||||
goto out;
|
||||
|
||||
if (dump)
|
||||
flags |= NLM_F_DUMP;
|
||||
|
||||
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ,
|
||||
genl_family_get_id(unl->family), 0, flags, cmd, 0);
|
||||
|
||||
out:
|
||||
return msg;
|
||||
}
|
||||
|
||||
int unl_genl_request(struct unl *unl, struct nl_msg *msg, unl_cb handler, void *arg)
|
||||
{
|
||||
struct nlmsghdr *nlh;
|
||||
struct nl_cb *cb;
|
||||
int err;
|
||||
|
||||
cb = nl_cb_alloc(NL_CB_CUSTOM);
|
||||
nlh = nlmsg_hdr(msg);
|
||||
|
||||
err = nl_send_auto_complete(unl->sock, msg);
|
||||
if (err < 0)
|
||||
goto out;
|
||||
|
||||
err = 1;
|
||||
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
|
||||
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
|
||||
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
|
||||
if (handler)
|
||||
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, handler, arg);
|
||||
|
||||
while (err > 0)
|
||||
nl_recvmsgs(unl->sock, cb);
|
||||
|
||||
out:
|
||||
nlmsg_free(msg);
|
||||
nl_cb_put(cb);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int request_single_cb(struct nl_msg *msg, void *arg)
|
||||
{
|
||||
struct nl_msg **dest = arg;
|
||||
|
||||
if (!*dest) {
|
||||
nlmsg_get(msg);
|
||||
*dest = msg;
|
||||
}
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
int unl_genl_request_single(struct unl *unl, struct nl_msg *msg, struct nl_msg **dest)
|
||||
{
|
||||
*dest = NULL;
|
||||
return unl_genl_request(unl, msg, request_single_cb, dest);
|
||||
}
|
||||
|
||||
static int no_seq_check(struct nl_msg *msg, void *arg)
|
||||
{
|
||||
return NL_OK;
|
||||
}
|
||||
|
||||
void unl_genl_loop(struct unl *unl, unl_cb handler, void *arg)
|
||||
{
|
||||
struct nl_cb *cb;
|
||||
|
||||
cb = nl_cb_alloc(NL_CB_CUSTOM);
|
||||
unl->loop_done = false;
|
||||
nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
|
||||
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, handler, arg);
|
||||
|
||||
while (!unl->loop_done)
|
||||
nl_recvmsgs(unl->sock, cb);
|
||||
|
||||
nl_cb_put(cb);
|
||||
}
|
||||
|
||||
static int unl_genl_multicast_id(struct unl *unl, const char *name)
|
||||
{
|
||||
struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1];
|
||||
struct nlattr *groups, *group;
|
||||
struct nl_msg *msg;
|
||||
int ctrlid;
|
||||
int ret = -1;
|
||||
int rem;
|
||||
|
||||
msg = nlmsg_alloc();
|
||||
if (!msg)
|
||||
return -1;
|
||||
|
||||
ctrlid = genl_ctrl_resolve(unl->sock, "nlctrl");
|
||||
genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0);
|
||||
NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, unl->family_name);
|
||||
unl_genl_request_single(unl, msg, &msg);
|
||||
if (!msg)
|
||||
goto nla_put_failure;
|
||||
|
||||
groups = unl_find_attr(unl, msg, CTRL_ATTR_MCAST_GROUPS);
|
||||
if (!groups)
|
||||
goto fail;
|
||||
|
||||
nla_for_each_nested(group, groups, rem) {
|
||||
const char *gn;
|
||||
|
||||
nla_parse(tb, CTRL_ATTR_MCAST_GRP_MAX, nla_data(group),
|
||||
nla_len(group), NULL);
|
||||
|
||||
if (!tb[CTRL_ATTR_MCAST_GRP_NAME] ||
|
||||
!tb[CTRL_ATTR_MCAST_GRP_ID])
|
||||
continue;
|
||||
|
||||
gn = nla_data(tb[CTRL_ATTR_MCAST_GRP_NAME]);
|
||||
if (strcmp(gn, name) != 0)
|
||||
continue;
|
||||
|
||||
ret = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
|
||||
break;
|
||||
}
|
||||
|
||||
fail:
|
||||
nlmsg_free(msg);
|
||||
nla_put_failure:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int unl_genl_subscribe(struct unl *unl, const char *name)
|
||||
{
|
||||
int mcid;
|
||||
|
||||
mcid = unl_genl_multicast_id(unl, name);
|
||||
if (mcid < 0)
|
||||
return mcid;
|
||||
|
||||
return nl_socket_add_membership(unl->sock, mcid);
|
||||
}
|
||||
|
||||
int unl_genl_unsubscribe(struct unl *unl, const char *name)
|
||||
{
|
||||
int mcid;
|
||||
|
||||
mcid = unl_genl_multicast_id(unl, name);
|
||||
if (mcid < 0)
|
||||
return mcid;
|
||||
|
||||
return nl_socket_drop_membership(unl->sock, mcid);
|
||||
}
|
||||
|
||||
int unl_nl80211_phy_lookup(const char *name)
|
||||
{
|
||||
char buf[32];
|
||||
int fd, pos;
|
||||
|
||||
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
|
||||
|
||||
fd = open(buf, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
pos = read(fd, buf, sizeof(buf) - 1);
|
||||
if (pos < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
close(fd);
|
||||
return atoi(buf);
|
||||
}
|
||||
|
||||
int unl_nl80211_wdev_to_phy(struct unl *unl, int wdev)
|
||||
{
|
||||
struct nl_msg *msg;
|
||||
struct nlattr *attr;
|
||||
int ret = -1;
|
||||
|
||||
msg = unl_genl_msg(unl, NL80211_CMD_GET_INTERFACE, false);
|
||||
if (!msg)
|
||||
return -1;
|
||||
|
||||
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev);
|
||||
if (unl_genl_request_single(unl, msg, &msg) < 0)
|
||||
return -1;
|
||||
|
||||
attr = unl_find_attr(unl, msg, NL80211_ATTR_WIPHY);
|
||||
if (!attr)
|
||||
goto out;
|
||||
|
||||
ret = nla_get_u32(attr);
|
||||
out:
|
||||
nla_put_failure:
|
||||
nlmsg_free(msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
46
unl.h
Normal file
46
unl.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
#ifndef __UNL_H
|
||||
#define __UNL_H
|
||||
|
||||
#include <netlink/netlink.h>
|
||||
#include <netlink/genl/genl.h>
|
||||
#include <netlink/genl/family.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct unl {
|
||||
struct nl_sock *sock;
|
||||
struct nl_cache *cache;
|
||||
struct genl_family *family;
|
||||
char *family_name;
|
||||
int hdrlen;
|
||||
bool loop_done;
|
||||
};
|
||||
|
||||
int unl_genl_init(struct unl *unl, const char *family);
|
||||
void unl_free(struct unl *unl);
|
||||
|
||||
typedef int (*unl_cb)(struct nl_msg *, void *);
|
||||
|
||||
struct nl_msg *unl_genl_msg(struct unl *unl, int cmd, bool dump);
|
||||
int unl_genl_request(struct unl *unl, struct nl_msg *msg, unl_cb handler, void *arg);
|
||||
int unl_genl_request_single(struct unl *unl, struct nl_msg *msg, struct nl_msg **dest);
|
||||
void unl_genl_loop(struct unl *unl, unl_cb handler, void *arg);
|
||||
|
||||
int unl_genl_subscribe(struct unl *unl, const char *name);
|
||||
int unl_genl_unsubscribe(struct unl *unl, const char *name);
|
||||
|
||||
int unl_nl80211_phy_lookup(const char *name);
|
||||
int unl_nl80211_wdev_to_phy(struct unl *unl, int wdev);
|
||||
struct nl_msg *unl_nl80211_phy_msg(struct unl *unl, int phy, int cmd, bool dump);
|
||||
struct nl_msg *unl_nl80211_vif_msg(struct unl *unl, int dev, int cmd, bool dump);
|
||||
|
||||
static inline void unl_loop_done(struct unl *unl)
|
||||
{
|
||||
unl->loop_done = true;
|
||||
}
|
||||
|
||||
static inline struct nlattr *unl_find_attr(struct unl *unl, struct nl_msg *msg, int attr)
|
||||
{
|
||||
return nlmsg_find_attr(nlmsg_hdr(msg), unl->hdrlen, attr);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue