ez_netflow_lib 0.1.2

EZ-Netflow is an all-in-one netflow server and library that's really easy to use. This is the lib crate, which contains the core server and database code. Use this crate directly if you want to incorporate EZ-Netflow into your project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

use std::io::ErrorKind;
use std::sync::{Arc, Mutex, MutexGuard};
use std::net::Ipv4Addr;
use log::{error, info, debug};

use rusqlite::ffi::Error;
use rusqlite::OptionalExtension;
use rusqlite::{ErrorCode, Connection, params};
use tabled::{builder::Builder, settings::Style};
use chrono::prelude::*;

use crate::settings::*;
use crate::templates::*;
use crate::utils::*;
use crate::fields::*;



pub fn setup_db(conn_type: &ConnType) -> Connection {

    let db_conn: Connection = match conn_type {
        ConnType::InMemory => {
            Connection::open_in_memory().expect("Unable to open SQLITE db connection in memory")
        },
        ConnType::InFile => {
            Connection::open("./eznf_db.sqlite").expect("Unable to open SQLITE db connection in memory")
        }
    };

    db_conn.execute("PRAGMA foreign_keys = ON", []).unwrap();

    //create tables
    db_conn.execute("CREATE TABLE IF NOT EXISTS senders (
        ip TEXT PRIMARY KEY
        )",
        [],
        ).expect("Unable to create senders table in DB");

    db_conn.execute("CREATE TABLE IF NOT EXISTS flows (
        id INTEGER PRIMARY KEY,
        sender_ip TEXT NOT NULL,
        src_addr TEXT,
        dst_addr TEXT,
        protocol INTEGER,
        src_port INTEGER,
        dst_port INTEGER,
        tcp_flags INTEGER,
        input_snmp INTEGER,
        output_snmp INTEGER,
        in_octets INTEGER,
        in_pkts INTEGER,
        src_tos INTEGER,
        src_mask INTEGER,
        dst_mask INTEGER,
        next_hop TEXT,
        icmp TEXT,
        traffic_type TEXT,
        created_time TEXT,
        updated_time TEXT,
         FOREIGN KEY (sender_ip) REFERENCES senders(ip)
        )",
        [],
        ).expect("Unable to create flows table in DB");

    db_conn
}

pub fn update_senders_in_db(db_conn: &mut Arc<Mutex<Connection>>, sender_ip: &str) {
    let db_conn_unlocked: MutexGuard<Connection> = db_conn.lock().unwrap();
    db_conn_unlocked.execute( 
        "INSERT OR IGNORE INTO senders (ip) VALUES (?)",
        [sender_ip.to_string()],
        ).expect("Unable to execute SQL in update_senders_in_db");
}


pub fn create_flow_in_db(db_conn: &mut Connection, flow: &NetFlow, sender_ip: &String, current_time: DateTime<Local>) {

    //let traffic_type = handle_traffic_cast(&flow.src_and_dst_ip.0.to_string(), &flow.src_and_dst_ip.1.to_string());
    //let traffic_type = handle_traffic_type(&flow);
    //moved traffic type processing to the same func that processes the flow
    
    let traffic_type = match flow.traffic_type {
        TrafficType::Broadcast => "Broadcast",
        TrafficType::Multicast => "Multicast",
        TrafficType::Unicast => "Unicast",
    };

    db_conn.execute( 
        "INSERT INTO flows 
            (sender_ip, src_addr, dst_addr, src_port, dst_port, protocol, in_octets, in_pkts, traffic_type) 
            VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
        (sender_ip.to_string(), 
            flow.src_and_dst_ip.0.to_string(), 
            flow.src_and_dst_ip.1.to_string(),
            flow.src_and_dst_port.0, 
            flow.src_and_dst_port.1, 
            flow.protocol, 
            flow.in_octets, 
            flow.in_packets,
            traffic_type,
            current_time.to_rfc3339()),
        ).expect("Unable to execute SQL in create_flow_in_db");
}

//I can't remove the "WHERE sender_ip = ?1" because it will update all of the flows
//I first need to make sure a flow is not created twice, no matter the sender
pub fn update_flow_in_db(db_conn: &mut Connection, flow: &NetFlow, sender_ip: &String) {
    db_conn.execute( 
        "UPDATE flows SET 
            in_octets = ?1, 
            in_pkts = ?2
            WHERE src_addr = ?3
            AND dst_addr = ?4
            AND src_port = ?5
            AND dst_port = ?6
            AND protocol = ?7",
        params![
            flow.in_octets, 
            flow.in_packets,
            flow.src_and_dst_ip.0.to_string(), 
            flow.src_and_dst_ip.1.to_string(),
            flow.src_and_dst_port.0, 
            flow.src_and_dst_port.1, 
            flow.protocol, 
            ]
        ).expect("Unable to execute SQL in update_flow_in_db");
}

pub fn check_if_flow_exists_in_db(db_conn: &mut Connection, flow: &NetFlow) -> bool {

    let row_result = db_conn.query_row(
            "SELECT * FROM flows WHERE 
             ((src_addr = ?1 AND dst_addr = ?2) OR (src_addr = ?2 AND dst_addr = ?1)) AND
             ((src_port = ?3 AND dst_port = ?4) OR (src_port = ?4 AND dst_port = ?3)) AND
            protocol = ?5",
            params![
            &flow.src_and_dst_ip.0.to_string(), 
            &flow.src_and_dst_ip.1.to_string(), 
            &flow.src_and_dst_port.0, 
            &flow.src_and_dst_port.1,
            &flow.protocol],
            |row| row.get::<_, i32>(0),
    );

    match row_result {
        Ok(s) => {
            //println!("existing flow found, id is {s}");
            true 
        },
         Err(rusqlite::Error::QueryReturnedNoRows) => {
            //println!("existing flow NOT found because query returned no rows");
            false
         },
         Err(e) => {
            println!("Error in checking for existing fow in SQL, setting flow as existing,  error is {e}");
            error!("Error in checking for existing fow in SQL, setting flow as existing,  error is {e}");
            false
         }
    }
    
}


pub fn get_all_flows_from_sender(db_conn_cli: &mut Arc<Mutex<Connection>>, server_settings: &ServerSettings) -> tabled::Table {

    let mut builder = Builder::new();
    builder.push_record([
        "sender_ip", 
        "src_addr", 
        "dst_addr", 
        "protocol", 
        "src_port", 
        "dst_port", 
        "in_pkts", 
        "in_bytes",
        "icmp_type",
        "traffic_type",
        ]);
    
    let mut conn: MutexGuard<Connection> = db_conn_cli.lock().unwrap();

    let flow_limit = match server_settings.flow_limit {
        FlowsToShow::Limit { flows } => flows,
        FlowsToShow::NoLimit => 1000,
    };

    let select_statement = "SELECT * FROM flows ".to_string();
    let filter_statement = match server_settings.unicast_only {
        true => {
            "WHERE traffic_type = \'Unicast\'"
        },
        false => {""},
    };
    
    let order_statement = match server_settings.sort_by {
        SortBy::Bytes => { 
            " ORDER BY in_octets DESC LIMIT ?"
        },
        SortBy::Pkts => {
            "SELECT * FROM flows ORDER BY in_pkts DESC LIMIT ?"
        },
        SortBy::None => {
            "SELECT * FROM flows LIMIT ?"
        },
    } ;

    let joined_statement = select_statement + filter_statement + order_statement;

    let mut stmt: rusqlite::Statement = conn.prepare(&joined_statement)
        .expect("Unable to prepare query");


    let mut rows = stmt.query(params![flow_limit])
        .expect("Unable to query rows");

       while let Some(row) = rows.next().expect("no more rows") {
          let sender_ip: String = row.get(1).expect("Unable to open column 1");
          //println!("sender_ip is {sender_ip}");
          let src_addr: String = row.get(2).expect("Unable to open column 2");
          //println!("src_addr is {src_addr}");
          let dst_addr: String = row.get(3).expect("Unable to open column 3");
          //println!("dst_addr is {dst_addr}");
          let protocol: i32 = row.get(4).expect("Unable to open column 4");
          //println!("protocol is {protocol}");
          let src_port: i32 = row.get(5).expect("Unable to open column 5");
          //println!("src_port is {src_port}");
          let dst_port: i32 = row.get(6).expect("Unable to open column 6");
          //println!("dst_port is {dst_port}");
          let in_pkts: i32 = row.get(10).expect("Unable to open column 10");
          //println!("in_pkts is {in_pkts}");
          let in_bytes: i32 = row.get(11).expect("Unable to open column 11");
          //println!("in_bytes is {in_bytes}");
          let traffic_cast: String = row.get(17).expect("Unable to open column 17");

          let (icmp_type, src_port2,dst_port2) = handle_icmp_code(protocol, src_port, dst_port);
       
          //let ip_cast = handle_traffic_cast(&src_addr, &dst_addr);

            builder.push_record([
                sender_ip, 
                src_addr, 
                dst_addr, 
                protocol.to_string(), 
                src_port2.to_string(), 
                dst_port2.to_string(), 
                in_pkts.to_string(), 
                in_bytes.to_string(),
                icmp_type,
                traffic_cast,
                ]);

        }


        let mut table = builder.build();
        table.with(Style::ascii_rounded());
        table

}


pub fn get_all_hosts_as_json(db_conn_cli: &mut Arc<Mutex<Connection>>, server_settings: &ServerSettings) -> String {

    let mut all_hosts: Vec<String> = Vec::new();
   
    let mut conn: MutexGuard<Connection> = db_conn_cli.lock().unwrap();

    let flow_limit = 10;

    let select_statement = "SELECT src_ip FROM flows ".to_string();


    let mut stmt: rusqlite::Statement = conn.prepare(&select_statement)
        .expect("Unable to prepare query");


    let mut rows = stmt.query(params![flow_limit])
        .expect("Unable to query rows");

       while let Some(row) = rows.next().expect("no more rows") {
          let src_addr: String = row.get(2).expect("Unable to open column 2");
         // build json here and add it to vec
            all_hosts.push(src_addr);
    
        }

    serde_json::to_string(&all_hosts).unwrap()

}





// pub fn get_all_flows_as_json(db_conn_cli: &mut Arc<Mutex<Connection>>, server_settings: &ServerSettings) -> String {

//     let mut all_flows: Vec<NetflowJson> = Vec::new();
   
//     let mut conn: MutexGuard<Connection> = db_conn_cli.lock().unwrap();

//     let flow_limit = match server_settings.flow_limit {
//         FlowsToShow::Limit { flows } => flows,
//         FlowsToShow::NoLimit => 1000,
//     };

//     let select_statement = "SELECT * FROM flows ".to_string();
//     let filter_statement = match server_settings.unicast_only {
//         true => {
//             "WHERE traffic_type = \'Unicast\'"
//         },
//         false => {""},
//     };
    
//     let order_statement = match server_settings.sort_by {
//         SortBy::Bytes => { 
//             " ORDER BY in_octets DESC LIMIT ?"
//         },
//         SortBy::Pkts => {
//             "SELECT * FROM flows ORDER BY in_pkts DESC LIMIT ?"
//         },
//         SortBy::None => {
//             "SELECT * FROM flows LIMIT ?"
//         },
//     } ;

//     let joined_statement = select_statement + filter_statement + order_statement;

//     let mut stmt: rusqlite::Statement = conn.prepare(&joined_statement)
//         .expect("Unable to prepare query");


//     let mut rows = stmt.query(params![flow_limit])
//         .expect("Unable to query rows");

//        while let Some(row) = rows.next().expect("no more rows") {
//           let sender_ip: String = row.get(1).expect("Unable to open column 1");
//           //println!("sender_ip is {sender_ip}");
//           let src_addr: String = row.get(2).expect("Unable to open column 2");
//           //println!("src_addr is {src_addr}");
//           let dst_addr: String = row.get(3).expect("Unable to open column 3");
//           //println!("dst_addr is {dst_addr}");
//           let proto: i32 = row.get(4).expect("Unable to open column 4");
//           //println!("protocol is {protocol}");
//           let src_port: i32 = row.get(5).expect("Unable to open column 5");
//           //println!("src_port is {src_port}");
//           let dst_port: i32 = row.get(6).expect("Unable to open column 6");
//           //println!("dst_port is {dst_port}");
//           let in_pkts: i32 = row.get(10).expect("Unable to open column 10");
//           //println!("in_pkts is {in_pkts}");
//           let in_bytes: i32 = row.get(11).expect("Unable to open column 11");
//           //println!("in_bytes is {in_bytes}");
//           let traffic_cast: String = row.get(17).expect("Unable to open column 17");

//           let (icmp_type, src_port2,dst_port2) = handle_icmp_code(proto, src_port, dst_port);
       
//           //let ip_cast = handle_traffic_cast(&src_addr, &dst_addr);
//           let unicast_str: String = String::from("Unicast");
//           let multicast_str: String = String::from("Multicast");
//           let broadcast_str: String = String::from("Broadcoast");

//           let traffic_type_as_enum: TrafficType = match traffic_cast {
//             unicast_str => TrafficType::Unicast,
//             multicast_str => TrafficType::Multicast,
//             broadcast_str => TrafficType::Broadcast,
//             _ => TrafficType::Unicast,
//           };

//         // let current_flow: NetFlow =  NetFlow {
//         //     src_and_dst_ip: (convert_string_to_ipv4(&src_addr).unwrap(), convert_string_to_ipv4(&dst_addr).unwrap()),
//         //     src_and_dst_port: (src_port.try_into().unwrap(), dst_port.try_into().unwrap()),
//         //     protocol: proto.try_into().unwrap(),
//         //     in_octets: in_bytes.try_into().unwrap(),
//         //     in_packets: in_pkts.try_into().unwrap(),
//         //     in_db: true,
//         //     traffic_type: traffic_type_as_enum,
//         // };

//         let current_flow = NetflowJson {
//             flow_src_ip: src_addr,
//             flow_bytes: in_bytes,
//         };
//         //{"src_ip": "192.168.1.1", "bytes": 1000}

//         // build json here and add it to vec
//         all_flows.push(current_flow);

//     }

//     serde_json::to_string(&all_flows).unwrap()

// }



// pub fn get_all_senders_in_db() {
//             // {
//         //     let mut conn: MutexGuard<Connection> = db_conn_cli.lock().unwrap();
//         //     let mut stmt: rusqlite::Statement = conn.prepare("SELECT * FROM senders")
//         //         .expect("Unable to prepare query");

//         //     let mut rows = stmt.query([])
//         //         .expect("Unable to query rows");

//         //    while let Some(row) = rows.next().expect("no more rows") {
//         //       let ip_from_db: String = row.get(0).expect("Unable to open column 0");
//         //       //println!("ip_from_db is {ip_from_db}");
//         //     }
//         // }
// }