Skip to main content

custom_db/
custom_db.rs

1//! Demonstrates building a custom `.coi` IP database from Rust with overlapping CIDRs (LPM),
2//! explicit hole deletions, and build-time field pruning.
3//!
4//! Run with:
5//! `cargo run -p coip-builder --example custom_db`
6
7use std::collections::BTreeMap;
8use std::net::{Ipv4Addr, Ipv6Addr};
9
10use coip_builder::{BuildOptions, CoipBuilder, PruneConfig};
11use coip_format::{AliasRule, Value};
12
13fn make_record(org: &str, country: &str, name: &str, risk: u8) -> BTreeMap<String, Value> {
14    let mut country_map = BTreeMap::new();
15    country_map.insert("iso_code".into(), Value::Utf8(country.into()));
16    country_map.insert("name".into(), Value::Utf8(name.into()));
17
18    let mut root = BTreeMap::new();
19    root.insert("org".into(), Value::Utf8(org.into()));
20    root.insert("country".into(), Value::Map(country_map));
21    root.insert("risk_score".into(), Value::U8(risk));
22    root
23}
24
25fn main() -> Result<(), Box<dyn std::error::Error>> {
26    let mut builder = CoipBuilder::new(BuildOptions::compact());
27    builder.set_metadata_field("database_type", serde_json::json!("Custom-Threat-IP"));
28    builder.add_ipv6_alias(AliasRule {
29        prefix: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0],
30        prefix_len: 96,
31        v4_bit_offset: 96,
32    });
33
34    // Broad corporate /16 overridden by a specific /24 and an explicit deleted /32 hole
35    builder.insert_ipv4_cidr(
36        Ipv4Addr::new(10, 20, 0, 0),
37        16,
38        make_record("Corp-Default", "US", "United States", 10),
39    )?;
40    builder.insert_ipv4_cidr(
41        Ipv4Addr::new(10, 20, 30, 0),
42        24,
43        make_record("Corp-DMZ", "DE", "Germany", 85),
44    )?;
45    builder.delete_ipv4_cidr(Ipv4Addr::new(10, 20, 30, 1), 32)?;
46
47    // Native IPv6 /48 + exact /128 host override
48    builder.insert_ipv6_cidr(
49        "2001:db8:abcd::".parse::<Ipv6Addr>()?,
50        48,
51        make_record("IPv6-Pool", "JP", "Japan", 20),
52    )?;
53    builder.insert_ipv6_cidr(
54        "2001:db8:abcd::99".parse::<Ipv6Addr>()?,
55        128,
56        make_record("IPv6-Honeypot", "JP", "Japan", 99),
57    )?;
58
59    let (full_bytes, full_stats) = builder.build()?;
60    println!(
61        "Built full .coi: {} bytes ({} unique records, {} IPv4 transitions, {} IPv6 transitions)",
62        full_bytes.len(),
63        full_stats.record_count,
64        full_stats.v4_transitions,
65        full_stats.v6_transitions
66    );
67
68    // Same dataset with build-time field pruning (keep only country.iso_code, country.name, org)
69    let mut pruned_opts = BuildOptions::ultrafast();
70    pruned_opts.prune = Some(PruneConfig {
71        keep_fields: Some(vec![
72            "org".into(),
73            "country.iso_code".into(),
74            "country.name".into(),
75        ]),
76    });
77    let mut builder2 = CoipBuilder::new(pruned_opts);
78    builder2.insert_ipv4_cidr(
79        Ipv4Addr::new(10, 20, 0, 0),
80        16,
81        make_record("Corp-Default", "US", "United States", 10),
82    )?;
83    let (pruned_bytes, pruned_stats) = builder2.build()?;
84    println!(
85        "Built pruned UltraFast .coi: {} bytes ({} unique records)",
86        pruned_bytes.len(),
87        pruned_stats.record_count
88    );
89
90    Ok(())
91}