use libc::*;
use std::mem;
use std::ptr;

use *;

pub const TLS1_VERSION: u16 = 0x301;
pub const TLS1_1_VERSION: u16 = 0x302;
pub const TLS1_2_VERSION: u16 = 0x303;
pub const TLS1_3_VERSION: u16 = 0x304;

pub const TLS1_AD_DECODE_ERROR: c_int = 50;
pub const TLS1_AD_UNRECOGNIZED_NAME: c_int = 112;

pub const TLSEXT_NAMETYPE_host_name: c_int = 0;
pub const TLSEXT_STATUSTYPE_ocsp: c_int = 1;

extern "C" {
    pub fn SSL_get_servername(ssl: *const SSL, name_type: c_int) -> *const c_char;

    pub fn SSL_export_keying_material(
        s: *mut SSL,
        out: *mut c_uchar,
        olen: size_t,
        label: *const c_char,
        llen: size_t,
        context: *const c_uchar,
        contextlen: size_t,
        use_context: c_int,
    ) -> c_int;

    pub fn SSL_set_tlsext_host_name(s: *mut SSL, name: *const c_char) -> c_int;
    pub fn SSL_set_tlsext_status_type(s: *mut SSL, type_: c_int) -> c_int;
    pub fn SSL_get_tlsext_status_ocsp_resp(ssl: *const SSL, resp: *mut *const c_uchar) -> size_t;
    pub fn SSL_set_tlsext_status_ocsp_resp(ssl: *mut SSL, resp: *mut c_uchar, len: size_t)
        -> c_int;
}

extern "C" {
    pub fn SSL_CTX_set_tlsext_servername_callback(
        ctx: *mut SSL_CTX,
        // FIXME should have the right signature
        cb: Option<extern "C" fn(s: *mut SSL, out_alert: *mut c_int, arg: *mut c_void) -> c_int>,
    ) -> c_int;
}

pub const SSL_TLSEXT_ERR_OK: c_int = 0;
pub const SSL_TLSEXT_ERR_ALERT_WARNING: c_int = 1;
pub const SSL_TLSEXT_ERR_ALERT_FATAL: c_int = 2;
pub const SSL_TLSEXT_ERR_NOACK: c_int = 3;

extern "C" {
    pub fn SSL_CTX_set_tlsext_servername_arg(ctx: *mut SSL_CTX, arg: *mut c_void) -> c_int;
    pub fn SSL_CTX_set_tlsext_status_cb(
        ctx: *mut SSL_CTX,
        cb: Option<unsafe extern "C" fn(*mut SSL, *mut c_void) -> c_int>,
    ) -> c_int;
    pub fn SSL_CTX_set_tlsext_status_arg(ctx: *mut SSL_CTX, arg: *mut c_void) -> c_int;
}
