]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Add some convenience methods
authorVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 14 May 2025 11:00:22 +0000 (12:00 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 14 May 2025 11:01:01 +0000 (12:01 +0100)
src/libutil/radix.c
src/libutil/radix.h

index 2cae8e34af8acc1f75d6150c438efb59f2d42317..bdd722b499acbca34485912bda82badb543e42a8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2024 Vsevolod Stakhov
+ * Copyright 2025 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ radix_find_compressed(radix_compressed_t *tree, const uint8_t *key, gsize keylen
 
 uintptr_t
 radix_insert_compressed(radix_compressed_t *tree,
-                                               uint8_t *key, gsize keylen,
+                                               const uint8_t *key, gsize keylen,
                                                gsize masklen,
                                                uintptr_t value)
 {
@@ -128,6 +128,39 @@ radix_insert_compressed(radix_compressed_t *tree,
        return old;
 }
 
+uintptr_t
+radix_insert_compressed_addr(radix_compressed_t *tree,
+                                                        const rspamd_inet_addr_t *addr,
+                                                        uintptr_t value)
+{
+       const unsigned char *key;
+       unsigned int klen = 0;
+       unsigned char buf[16];
+
+       if (addr == NULL) {
+               return RADIX_NO_VALUE;
+       }
+
+       key = rspamd_inet_address_get_hash_key(addr, &klen);
+
+       if (key && klen) {
+               if (klen == 4) {
+                       /* Map to ipv6 */
+                       memset(buf, 0, 10);
+                       buf[10] = 0xffu;
+                       buf[11] = 0xffu;
+                       memcpy(buf + 12, key, klen);
+
+                       key = buf;
+                       klen = sizeof(buf);
+               }
+
+               return radix_insert_compressed(tree, key, klen, 0, value);
+       }
+
+       return RADIX_NO_VALUE;
+}
+
 
 radix_compressed_t *
 radix_create_compressed(const char *tree_name)
index c4fe9644158ed2bf99fde0042499e61262783607..8c122470702464417e36477ddb05d2c2b20b5a8f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2024 Vsevolod Stakhov
+ * Copyright 2025 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
 #include "mem_pool.h"
 #include "util.h"
 
-#define RADIX_NO_VALUE (uintptr_t) - 1
+#define RADIX_NO_VALUE (uintptr_t) -1
 
 #ifdef __cplusplus
 extern "C" {
@@ -39,10 +39,22 @@ typedef struct radix_tree_compressed radix_compressed_t;
  */
 uintptr_t
 radix_insert_compressed(radix_compressed_t *tree,
-                                               uint8_t *key, gsize keylen,
+                                               const uint8_t *key, gsize keylen,
                                                gsize masklen,
                                                uintptr_t value);
 
+/**
+ * Insert new address to the radix trie (works for IPv4 or IPv6 addresses)
+ * @param tree radix trie
+ * @param addr address to insert
+ * @param value opaque value pointer
+ * @return previous value of the key or `RADIX_NO_VALUE`
+ */
+uintptr_t
+radix_insert_compressed_addr(radix_compressed_t *tree,
+                                                        const rspamd_inet_addr_t *addr,
+                                                        uintptr_t value);
+
 /**
  * Find a key in a radix trie
  * @param tree radix trie