<commit_before>package libcoap

/*
#cgo LDFLAGS: -lcoap-1
#include <coap/coap.h>
*/
import "C"
import "time"
import "unsafe"

type DtlsParam struct {
    CaFilename          *string
    CaPath              *string
    CertificateFilename *string
    PrivateKeyFilename  *string
}

type Context struct {
    ptr     *C.coap_context_t
    handler ResponseHandler
    dtls    *C.coap_dtls_param_t
}

var contexts = make(map[*C.coap_context_t] *Context)

func Startup() {
    C.coap_startup()
    // C.coap_dtls_set_log_level(C.LOG_DEBUG)
    // C.coap_set_log_level(C.LOG_DEBUG)
}

func Cleanup() {
    C.coap_cleanup()
}

func NewContext(addr *Address) *Context {
    var caddr *C.coap_address_t = nil
    if addr != nil {
      caddr = &addr.value
    }

    ptr := C.coap_new_context(caddr)
    if ptr != nil {
        context := &Context{ ptr, nil, nil }
        contexts[ptr] = context
        return context
    } else {
        return nil
    }
}

func NewContextDtls(addr *Address, dtls *DtlsParam) *Context {
    var caddr *C.coap_address_t = nil
    if addr != nil {
      caddr = &addr.value
    }

    var cdtls *C.coap_dtls_param_t = nil
    if dtls != nil {
        cdtls = &C.coap_dtls_param_t{};
        if dtls.CaFilename != nil {
            cdtls.ca_filename = C.CString(*dtls.CaFilename)
        }
        if dtls.CaPath != nil {
            cdtls.ca_path = C.CString(*dtls.CaPath)
        }
        if dtls.CertificateFilename != nil {
            cdtls.cert_filename = C.CString(*dtls.CertificateFilename)
        }
        if dtls.PrivateKeyFilename != nil {
            cdtls.pkey_filename = C.CString(*dtls.PrivateKeyFilename)
        }
    }

    ptr := C.coap_new_context_dtls(caddr, cdtls)
    if ptr != nil {
        context := &Context{ ptr, nil, cdtls }
        contexts[ptr] = context
        return context
    } else {
        return nil
    }
}

func (context *Context) FreeContext() {
    ptr := context.ptr

    delete(contexts, ptr)
    context.ptr = nil
    C.coap_free_context(ptr)<commit_msg>Enable PKI for DTLS context<commit_after>package libcoap

/*
#cgo LDFLAGS: -lcoap-1
#include <coap/coap.h>
*/
import "C"
import "time"
import "unsafe"

type DtlsParam struct {
    CaFilename          *string
    CaPath              *string
    CertificateFilename *string
    PrivateKeyFilename  *string
}

type Context struct {
    ptr     *C.coap_context_t
    handler ResponseHandler
    dtls    *C.coap_dtls_param_t
}

var contexts = make(map[*C.coap_context_t] *Context)

func Startup() {
    C.coap_startup()
    // C.coap_dtls_set_log_level(C.LOG_DEBUG)
    // C.coap_set_log_level(C.LOG_DEBUG)
}

func Cleanup() {
    C.coap_cleanup()
}

func NewContext(addr *Address) *Context {
    var caddr *C.coap_address_t = nil
    if addr != nil {
      caddr = &addr.value
    }

    ptr := C.coap_new_context(caddr)
    if ptr != nil {
        context := &Context{ ptr, nil, nil }
        contexts[ptr] = context
        return context
    } else {
        return nil
    }
}

func NewContextDtls(addr *Address, dtls *DtlsParam) *Context {
    var caddr *C.coap_address_t = nil
    if addr != nil {
      caddr = &addr.value
    }

    var cdtls *C.coap_dtls_param_t = nil
    if dtls != nil {
        cdtls = &C.coap_dtls_param_t{};
        if dtls.CaFilename != nil {
            cdtls.ca_filename = C.CString(*dtls.CaFilename)
        }
        if dtls.CaPath != nil {
            cdtls.ca_path = C.CString(*dtls.CaPath)
        }
        if dtls.CertificateFilename != nil {
            cdtls.cert_filename = C.CString(*dtls.CertificateFilename)
        }
        if dtls.PrivateKeyFilename != nil {
            cdtls.pkey_filename = C.CString(*dtls.PrivateKeyFilename)
        }
    }

    ptr := C.coap_new_context_dtls(caddr, cdtls)
    if ptr != nil {
        // Enable PKI
        var setup_data *C.coap_dtls_pki_t = &C.coap_dtls_pki_t{}
        if dtls.CaFilename != nil {
            setup_data.ca_file  = C.CString(*dtls.CaFilename)
        }
        if dtls.CertificateFilename != nil {
            setup_data.public_cert = C.CString(*dtls.CertificateFilename)
        }
        if dtls.PrivateKeyFilename != nil {
            setup_data.private_key = C.CString(*dtls.PrivateKeyFilename)
        }
        ok := C.coap_dtls_context_set_pki(ptr, setup_data)

        if ok == 1 {
            context := &Context{ ptr, nil, cdtls }
            contexts[ptr] = context
            return context            
        } else {
            return nil
        }
        
    } else {
        return nil
    }
}

func (context *Context) FreeContext() {
    ptr := context.ptr

    delete(contexts, ptr)
    context.ptr = nil
    C.coap_free_context(ptr)
{'fraction_non_alphanumeric': 0.1072723395180209, 'fraction_numerical': 0.0006397952655150352, 'mean_word_length': 2.1080185553346587, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6156328', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1630, 'n_words': 329}