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
use cassandra::util::Protected;
use cassandra::error::*;
use cassandra_sys::CassInet as _Inet;
use cassandra_sys::cass_inet_from_string;
use cassandra_sys::cass_inet_init_v4;
use cassandra_sys::cass_inet_init_v6;
use cassandra_sys::cass_inet_string;
use std::default::Default;
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::mem;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use std::string::ToString;

#[repr(C)]
/// Cassandra's version of an IP address
#[derive(Copy,Clone)]
pub struct Inet(_Inet);

impl Debug for Inet {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.to_string()) }
}

impl PartialEq for Inet {
    fn eq(&self, other: &Inet) -> bool {
        if self.0.address_length != other.0.address_length {
            return false;
        }
        let length = self.0.address_length as usize;
        self.0.address[0..length] == other.0.address[0..length]
    }
}

impl Protected<_Inet> for Inet {
    fn inner(&self) -> _Inet { self.0 }
    fn build(inner: _Inet) -> Self { Inet(inner) }
}

impl Default for Inet {
    fn default() -> Inet { unsafe { ::std::mem::zeroed() } }
}

impl Inet {
    /// Constructs an inet v4 object.
    pub fn cass_inet_init_v4(address: &Ipv4Addr) -> Inet {
        unsafe { Inet(cass_inet_init_v4(address.octets().as_ptr())) }
    }

    /// Constructs an inet v6 object.
    pub fn cass_inet_init_v6(address: &Ipv6Addr) -> Inet {
        unsafe { Inet(cass_inet_init_v6(address.octets().as_ptr())) }
    }
}

impl<'a> From<&'a IpAddr> for Inet {
    fn from(ip_addr: &IpAddr) -> Inet {
        match *ip_addr {
            IpAddr::V4(ref ipv4_addr) => Inet::cass_inet_init_v4(ipv4_addr),
            IpAddr::V6(ref ipv6_addr) => Inet::cass_inet_init_v6(ipv6_addr),
        }
    }
}

/// The types of errors that can occur when trying to parse an Inet String
// pub enum InetParseError {
//    ///Don't put a null in a string, silly!
//    NulInString(NulError),
//    ///Not a valiid address
//    LibBadParams(CassLibError),
// }

impl FromStr for Inet {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        unsafe {
            let mut inet = mem::zeroed();

            let str = CString::new(s)?;
            cass_inet_from_string(str.as_ptr(), &mut inet).to_result(())
                .and_then(|_| Ok(Inet(inet)))
        }
    }
}

impl ToString for Inet {
    fn to_string(&self) -> String {
        unsafe {
            let mut inet_str = mem::zeroed();
            cass_inet_string(self.0, &mut inet_str);
            CStr::from_ptr(&inet_str).to_string_lossy().into_owned()
        }
    }
}

impl<'a> From<&'a Inet> for IpAddr {
    fn from(inet: &Inet) -> Self {
        match inet.0.address_length {
            4 => {
                let mut octets = [0u8; 4];
                octets.copy_from_slice(&inet.0.address[0..4]);
                IpAddr::from(octets)
            },
            16 => IpAddr::from(inet.0.address),
            unsupported => panic!("impossible inet type: {}", unsupported),
        }
    }
}

#[test]
fn ipv4_conversion() {
     let ipv4_in = Ipv4Addr::new(127, 0, 0, 1);
     let inet = Inet::cass_inet_init_v4(&ipv4_in);
     let ip_out = IpAddr::from(&inet);
     assert_eq!(IpAddr::V4(ipv4_in), ip_out);
}

#[test]
fn ipv6_conversion() {
     let ipv6_in = Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 1);
     let inet = Inet::cass_inet_init_v6(&ipv6_in);
     let ip_out = IpAddr::from(&inet);
     assert_eq!(IpAddr::V6(ipv6_in), ip_out);
}