]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
database: Implement searching for ASes that match a string
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Jan 2018 21:26:48 +0000 (21:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Jan 2018 21:26:48 +0000 (21:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/as.c
src/database.c
src/libloc.sym
src/loc/as.h
src/loc/database.h
src/test-as.c

index f1f01ac9c7f2e8e968a7c0f5ae24f3710796c140..8721eff7770077247f7678c9e0da37b12a1683d6 100644 (file)
--- a/src/as.c
+++ b/src/as.c
@@ -14,6 +14,7 @@
        Lesser General Public License for more details.
 */
 
+#include <ctype.h>
 #include <endian.h>
 #include <errno.h>
 #include <stdint.h>
@@ -128,3 +129,20 @@ int loc_as_to_database_v0(struct loc_as* as, struct loc_stringpool* pool,
 
        return 0;
 }
+
+int loc_as_match_string(struct loc_as* as, const char* string) {
+       int r = 1;
+
+       char* name = strdup(as->name);
+
+       // Convert string to lowercase
+       for (char* p = name; *p; p++)
+               *p = tolower(*p);
+
+       // Search if string is in name
+       if (strstr(as->name, string) != NULL)
+               r = 0;
+
+       free(name);
+       return r;
+}
index e37baa4d250443912e1b62047fae2837837c21ab..a74888cf763b3163a2dad971b18adc9744fa436e 100644 (file)
@@ -15,6 +15,7 @@
 */
 
 #include <arpa/inet.h>
+#include <ctype.h>
 #include <endian.h>
 #include <errno.h>
 #include <netinet/in.h>
@@ -65,6 +66,12 @@ struct loc_database_enumerator {
        struct loc_ctx* ctx;
        struct loc_database* db;
        int refcount;
+
+       // Search string
+       char* string;
+
+       // Index of the AS we are looking at
+       unsigned int as_index;
 };
 
 static int loc_database_read_magic(struct loc_database* db, FILE* f) {
@@ -616,6 +623,9 @@ static void loc_database_enumerator_free(struct loc_database_enumerator* enumera
        loc_database_unref(enumerator->db);
        loc_unref(enumerator->ctx);
 
+       if (enumerator->string)
+               free(enumerator->string);
+
        free(enumerator);
 }
 
@@ -629,3 +639,42 @@ LOC_EXPORT struct loc_database_enumerator* loc_database_enumerator_unref(struct
        loc_database_enumerator_free(enumerator);
        return NULL;
 }
+
+LOC_EXPORT int loc_database_enumerator_set_string(struct loc_database_enumerator* enumerator, const char* string) {
+       enumerator->string = strdup(string);
+
+       // Make the string lowercase
+       for (char *p = enumerator->string; *p; p++)
+               *p = tolower(*p);
+
+       return 0;
+}
+
+LOC_EXPORT struct loc_as* loc_database_enumerator_next_as(struct loc_database_enumerator* enumerator) {
+       struct loc_database* db = enumerator->db;
+       struct loc_as* as;
+
+       while (enumerator->as_index < db->as_count) {
+               // Fetch the next AS
+               int r = loc_database_fetch_as(db, &as, enumerator->as_index++);
+               if (r)
+                       return NULL;
+
+               r = loc_as_match_string(as, enumerator->string);
+               if (r == 0) {
+                       DEBUG(enumerator->ctx, "AS%d (%s) matches %s\n",
+                               loc_as_get_number(as), loc_as_get_name(as), enumerator->string);
+
+                       return as;
+               }
+
+               // No match
+               loc_as_unref(as);
+       }
+
+       // Reset the index
+       enumerator->as_index = 0;
+
+       // We have searched through all of them
+       return NULL;
+}
index d6028dc0aa857bbc063b38e3e5b2cad3422dba3c..42e89e5faacb715474237fe9aaa381c86da320a6 100644 (file)
@@ -50,6 +50,13 @@ global:
        loc_database_ref;
        loc_database_unref;
 
+       # Database Enumerator
+       loc_database_enumerator_new;
+       loc_database_enumerator_next_as;
+       loc_database_enumerator_ref;
+       loc_database_enumerator_set_string;
+       loc_database_enumerator_unref;
+
        # Network
        loc_network_get_asn;
        loc_network_get_country_code;
index 9990305c50971cb6e4848b90af760bf385016742..df0011934804bdd7a519c2b307afca1727f0fd67 100644 (file)
@@ -42,6 +42,8 @@ int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool
 int loc_as_to_database_v0(struct loc_as* as, struct loc_stringpool* pool,
                struct loc_database_as_v0* dbobj);
 
+int loc_as_match_string(struct loc_as* as, const char* string);
+
 #endif
 
 #endif
index e126f78ce0add1b87abcba8ec1ec3f8caab2cf34..37810eb9d31396066d823d315839418efc634360 100644 (file)
@@ -48,4 +48,7 @@ int loc_database_enumerator_new(struct loc_database_enumerator** enumerator, str
 struct loc_database_enumerator* loc_database_enumerator_ref(struct loc_database_enumerator* enumerator);
 struct loc_database_enumerator* loc_database_enumerator_unref(struct loc_database_enumerator* enumerator);
 
+int loc_database_enumerator_set_string(struct loc_database_enumerator* enumerator, const char* string);
+struct loc_as* loc_database_enumerator_next_as(struct loc_database_enumerator* enumerator);
+
 #endif
index 08c8730f419ca872e7b8cda4b454dd0dfd4e6219..8010a90bb3cab56a26af2eee24f7826f375cc9bf 100644 (file)
@@ -95,6 +95,25 @@ int main(int argc, char** argv) {
                loc_as_unref(as);
        }
 
+       // Enumerator
+
+       struct loc_database_enumerator* enumerator;
+       err = loc_database_enumerator_new(&enumerator, db);
+       if (err) {
+               fprintf(stderr, "Could not create a database enumerator\n");
+               exit(EXIT_FAILURE);
+       }
+
+       loc_database_enumerator_set_string(enumerator, "10");
+
+       as = loc_database_enumerator_next_as(enumerator);
+       while (as) {
+               printf("Found AS%d: %s\n", loc_as_get_number(as), loc_as_get_name(as));
+
+               as = loc_database_enumerator_next_as(enumerator);
+       }
+
+       loc_database_enumerator_unref(enumerator);
        loc_database_unref(db);
        loc_unref(ctx);