int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private);
/* Converts the INET/INET6 source address to a stick_table key usable for table
- * lookups. <type> can be STKTABLE_TYPE_IP or STKTABLE_TYPE_IPV6. The function
+ * lookups. <type> can be SMP_T_IPV4 or SMP_T_IPV6. The function
* try to convert the incoming IP to the type expected by the sticktable.
* Returns either NULL if the source cannot be converted (eg: not IPv4) or a
* pointer to the converted result in static_table_key in the appropriate format
switch (addr->ss_family) {
case AF_INET:
/* Convert IPv4 to IPv4 key. */
- if (type == STKTABLE_TYPE_IP) {
+ if (type == SMP_T_IPV4) {
static_table_key->key = (void *)&((struct sockaddr_in *)addr)->sin_addr;
break;
}
/* Convert IPv4 to IPv6 key. */
- if (type == STKTABLE_TYPE_IPV6) {
+ if (type == SMP_T_IPV6) {
v4tov6(&static_table_key->data.ipv6, &((struct sockaddr_in *)addr)->sin_addr);
static_table_key->key = &static_table_key->data.ipv6;
break;
case AF_INET6:
/* Convert IPv6 to IPv4 key. This conversion can be failed. */
- if (type == STKTABLE_TYPE_IP) {
+ if (type == SMP_T_IPV4) {
if (!v6tov4(&static_table_key->data.ipv4, &((struct sockaddr_in6 *)addr)->sin6_addr))
return NULL;
static_table_key->key = &static_table_key->data.ipv4;
break;
}
/* Convert IPv6 to IPv6 key. */
- if (type == STKTABLE_TYPE_IPV6) {
+ if (type == SMP_T_IPV6) {
static_table_key->key = (void *)&((struct sockaddr_in6 *)addr)->sin6_addr;
break;
}
#include <types/freq_ctr.h>
#include <types/sample.h>
-/* stick table key types */
-enum {
- STKTABLE_TYPE_IP = 0, /* table key is ipv4 */
- STKTABLE_TYPE_IPV6, /* table key is ipv6 */
- STKTABLE_TYPE_INTEGER, /* table key is unsigned 32bit integer */
- STKTABLE_TYPE_STRING, /* table key is a null terminated string */
- STKTABLE_TYPE_BINARY, /* table key is a buffer of data */
- STKTABLE_TYPES /* Number of types, must always be last */
-};
-
/* The types of extra data we can store in a stick table */
enum {
STKTABLE_DT_SERVER_ID, /* the server ID to use with this stream if > 0 */
chunk_appendf(msg, "%p:", entry);
- if (proxy->table.type == STKTABLE_TYPE_IP) {
+ if (proxy->table.type == SMP_T_IPV4) {
char addr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
chunk_appendf(msg, " key=%s", addr);
}
- else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
+ else if (proxy->table.type == SMP_T_IPV6) {
char addr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
chunk_appendf(msg, " key=%s", addr);
}
- else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
+ else if (proxy->table.type == SMP_T_SINT) {
chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
}
- else if (proxy->table.type == STKTABLE_TYPE_STRING) {
+ else if (proxy->table.type == SMP_T_STR) {
chunk_appendf(msg, " key=");
dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
}
}
switch (px->table.type) {
- case STKTABLE_TYPE_IP:
+ case SMP_T_IPV4:
uint32_key = htonl(inetaddr_host(args[4]));
static_table_key->key = &uint32_key;
break;
- case STKTABLE_TYPE_IPV6:
+ case SMP_T_IPV6:
inet_pton(AF_INET6, args[4], ip6_key);
static_table_key->key = &ip6_key;
break;
- case STKTABLE_TYPE_INTEGER:
+ case SMP_T_SINT:
{
char *endptr;
unsigned long val;
break;
}
break;
- case STKTABLE_TYPE_STRING:
+ case SMP_T_STR:
static_table_key->key = args[4];
static_table_key->key_len = strlen(args[4]);
break;
}
/* encode the key */
- if (st->table->type == STKTABLE_TYPE_STRING) {
+ if (st->table->type == SMP_T_STR) {
int stlen = strlen((char *)ts->key.key);
intencode(stlen, &cursor);
memcpy(cursor, ts->key.key, stlen);
cursor += stlen;
}
- else if (st->table->type == STKTABLE_TYPE_INTEGER) {
+ else if (st->table->type == SMP_T_SINT) {
netinteger = htonl(*((uint32_t *)ts->key.key));
memcpy(cursor, &netinteger, sizeof(netinteger));
cursor += sizeof(netinteger);
if (!newts)
goto ignore_msg;
- if (st->table->type == STKTABLE_TYPE_STRING) {
+ if (st->table->type == SMP_T_STR) {
unsigned int to_read, to_store;
to_read = intdecode(&msg_cur, msg_end);
newts->key.key[to_store] = 0;
msg_cur += to_read;
}
- else if (st->table->type == STKTABLE_TYPE_INTEGER) {
+ else if (st->table->type == SMP_T_SINT) {
unsigned int netinteger;
if (msg_cur + sizeof(netinteger) > msg_end) {
*/
void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
{
- if (t->type != STKTABLE_TYPE_STRING)
+ if (t->type != SMP_T_STR)
memcpy(ts->key.key, key->key, t->key_size);
else {
memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
{
struct ebmb_node *eb;
- if (t->type == STKTABLE_TYPE_STRING)
+ if (t->type == SMP_T_STR)
eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
else
eb = ebmb_lookup(&t->keys, key->key, t->key_size);
{
struct ebmb_node *eb;
- if (t->type == STKTABLE_TYPE_STRING)
+ if (t->type == SMP_T_STR)
eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
else
eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
/*
* Configuration keywords of known table types
*/
-struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
- { "ipv6", 0, 16 },
- { "integer", 0, 4 },
- { "string", STK_F_CUSTOM_KEYSIZE, 32 },
- { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
-
+struct stktable_type stktable_types[SMP_TYPES] = {
+ [SMP_T_SINT] = { "integer", 0, 4 },
+ [SMP_T_IPV4] = { "ip", 0, 4 },
+ [SMP_T_IPV6] = { "ipv6", 0, 16 },
+ [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
+ [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
+};
/*
* Parse table type configuration.
*/
int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
{
- for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
+ for (*type = 0; *type < SMP_TYPES; (*type)++) {
+ if (!stktable_types[*type].kw)
+ continue;
if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
continue;
*key_size = atol(args[*myidx]);
if (!*key_size)
break;
- if (*type == STKTABLE_TYPE_STRING) {
+ if (*type == SMP_T_STR) {
/* null terminated string needs +1 for '\0'. */
(*key_size)++;
}
*/
struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
{
- int type;
-
- /* Map stick table type to sample types. */
- switch (t->type) {
- case STKTABLE_TYPE_IP: type = SMP_T_IPV4; break;
- case STKTABLE_TYPE_IPV6: type = SMP_T_IPV6; break;
- case STKTABLE_TYPE_INTEGER: type = SMP_T_SINT; break;
- case STKTABLE_TYPE_STRING: type = SMP_T_STR; break;
- case STKTABLE_TYPE_BINARY: type = SMP_T_BIN; break;
- default: /* impossible case. */
- return NULL;
- }
-
/* Convert sample. */
- if (!sample_convert(smp, type))
+ if (!sample_convert(smp, t->type))
return NULL;
/* Fill static_table_key. */
switch (t->type) {
- case STKTABLE_TYPE_IP:
+ case SMP_T_IPV4:
static_table_key->key = &smp->data.u.ipv4;
static_table_key->key_len = 4;
break;
- case STKTABLE_TYPE_IPV6:
+ case SMP_T_IPV6:
static_table_key->key = &smp->data.u.ipv6;
static_table_key->key_len = 16;
break;
- case STKTABLE_TYPE_INTEGER:
+ case SMP_T_SINT:
/* The stick table require a 32bit unsigned int, "sint" is a
* signed 64 it, so we can convert it inplace.
*/
static_table_key->key_len = 4;
break;
- case STKTABLE_TYPE_STRING:
+ case SMP_T_STR:
/* Must be NULL terminated. */
if (smp->data.u.str.len >= smp->data.u.str.size ||
smp->data.u.str.str[smp->data.u.str.len] != '\0') {
static_table_key->key_len = smp->data.u.str.len;
break;
- case STKTABLE_TYPE_BINARY:
+ case SMP_T_BIN:
if (smp->data.u.str.len < t->key_size) {
/* This type needs padding with 0. */
if (smp->data.u.str.size < t->key_size)
{
int out_type;
- if (table_type >= STKTABLE_TYPES)
+ if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
return 0;
out_type = smp_expr_output_type(expr);
- /* Map stick table type to sample types. */
- switch (table_type) {
- case STKTABLE_TYPE_IP: table_type = SMP_T_IPV4; break;
- case STKTABLE_TYPE_IPV6: table_type = SMP_T_IPV6; break;
- case STKTABLE_TYPE_INTEGER: table_type = SMP_T_SINT; break;
- case STKTABLE_TYPE_STRING: table_type = SMP_T_STR; break;
- case STKTABLE_TYPE_BINARY: table_type = SMP_T_BIN; break;
- default: /* impossible case. */
- return 0;
- }
-
/* Convert sample. */
if (!sample_casts[out_type][table_type])
return 0;