/*

Copyright 2017 Jinjing Wang

This file is part of mtcp.

mtcp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

mtcp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with mtcp.  If not, see <http://www.gnu.org/licenses/>.

*/

use std::sync::mpsc;

use constant::*;
use structure::socket::udp::*;
use structure::socket::tcp::*;
use structure::socket::packet::*;

pub fn 

    (
        tun_in_receiver: TunReceiver,
        udp_sender: Option<mpsc::Sender<UDP>>,
        tcp_sender: Option<mpsc::Sender<TCP>>,
    )
{

    while let Ok(Some(received)) = tun_in_receiver.recv() {
        match parse_packet(&received) {
            Some(Packet::UDP(udp)) => {
                // debug!("Dispatch UDP: {:#?}", udp.connection);
                match udp_sender {
                    None => {}
                    Some(ref sender) => {
                        let _ = sender.send(udp);
                    }
                }
            }
            Some(Packet::TCP(tcp)) => {
                // debug!("Dispatch TCP: {:#?}", tcp.connection);

                match tcp_sender {
                    None => {}
                    Some(ref sender) => {
                        let _ = sender.send(tcp);
                    }
                }
            }
            _ => {}
        }
    }
}


// fn skip_tun_incoming(connection: Connection) -> bool {
//     let tun_ip: IpAddr = IpAddr::from_str("10.0.0.1").unwrap();
//     let source_ip = connection.source.ip();
//     let destination_ip = connection.destination.ip();

//     debug!("comparing {:#?} -> {:#?}, {:#?}", source_ip, destination_ip, tun_ip);

//     (source_ip == tun_ip) || (destination_ip == tun_ip);

//     false
// }

dispatch