diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-16 10:46:28 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-16 10:46:28 +0200 |
| commit | 4bf1273c3f446ac3195fb700ec41c7cae7d20ac9 (patch) | |
| tree | cec8d9e405b7d6786023ca9b921a6f0473d28a71 /exports | |
| parent | 02db081c85e56e6472c2f39e6a623fa4cdf359c4 (diff) | |
Discovery: Expose c types
Diffstat (limited to 'exports')
| -rw-r--r-- | exports/c/disco.h | 19 | ||||
| -rw-r--r-- | exports/c/servers.h | 1 | ||||
| -rw-r--r-- | exports/disco.go | 98 | ||||
| -rw-r--r-- | exports/exports.go | 21 | ||||
| -rw-r--r-- | exports/servers.go | 65 |
5 files changed, 174 insertions, 30 deletions
diff --git a/exports/c/disco.h b/exports/c/disco.h index 41d59fa..8fa07a4 100644 --- a/exports/c/disco.h +++ b/exports/c/disco.h @@ -1,6 +1,25 @@ // for size_t #include <stddef.h> +typedef struct discoveryServer { + const char* authentication_url_template; + const char* base_url; + const char* country_code; + const char* display_name; + const char* keyword_list; + const char** public_key_list; + size_t total_public_keys; + const char* server_type; + const char** support_contact; + size_t total_support_contact; +} discoveryServer; + +typedef struct discoveryServers { + unsigned long long int version; + discoveryServer** servers; + size_t total_servers; +} discoveryServers; + typedef struct discoveryOrganization { const char* display_name; const char* org_id; diff --git a/exports/c/servers.h b/exports/c/servers.h index 39e52a2..1b6cca9 100644 --- a/exports/c/servers.h +++ b/exports/c/servers.h @@ -26,6 +26,7 @@ typedef struct serverLocations { typedef struct server { const char* identifier; const char* display_name; + const char* server_type; const char* country_code; const char** support_contact; size_t total_support_contact; diff --git a/exports/disco.go b/exports/disco.go index 9ee2af9..a08f8ba 100644 --- a/exports/disco.go +++ b/exports/disco.go @@ -33,9 +33,8 @@ func getCPtrDiscoOrganizations( organizations *types.DiscoveryOrganizations, ) (C.size_t, **C.discoveryOrganization) { totalOrganizations := C.size_t(len(organizations.List)) - var organizationsPtr **C.discoveryOrganization if totalOrganizations > 0 { - organizationsPtr = (**C.discoveryOrganization)( + organizationsPtr := (**C.discoveryOrganization)( C.malloc(totalOrganizations * C.size_t(unsafe.Sizeof(uintptr(0)))), ) cOrganizations := (*[1<<30 - 1]*C.discoveryOrganization)(unsafe.Pointer(organizationsPtr))[:totalOrganizations:totalOrganizations] @@ -45,8 +44,52 @@ func getCPtrDiscoOrganizations( cOrganizations[index] = cOrganization index += 1 } + return totalOrganizations, organizationsPtr } - return totalOrganizations, organizationsPtr + return 0, nil +} + +func getCPtrDiscoServer( + state *eduvpn.VPNState, + server *types.DiscoveryServer, +) *C.discoveryServer { + returnedStruct := (*C.discoveryServer)( + C.malloc(C.size_t(unsafe.Sizeof(C.discoveryServer{}))), + ) + returnedStruct.authentication_url_template = C.CString(server.AuthenticationURLTemplate) + returnedStruct.base_url = C.CString(server.BaseURL) + returnedStruct.country_code = C.CString(server.CountryCode) + returnedStruct.display_name = C.CString(state.GetTranslated(server.DisplayName)) + returnedStruct.keyword_list = C.CString(state.GetTranslated(server.KeywordList)) + returnedStruct.total_public_keys, returnedStruct.public_key_list = getCPtrListStrings( + server.PublicKeyList, + ) + returnedStruct.server_type = C.CString(server.Type) + returnedStruct.total_support_contact, returnedStruct.support_contact = getCPtrListStrings( + server.SupportContact, + ) + return returnedStruct +} + +func getCPtrDiscoServers( + state *eduvpn.VPNState, + servers *types.DiscoveryServers, +) (C.size_t, **C.discoveryServer) { + totalServers := C.size_t(len(servers.List)) + if totalServers > 0 { + serversPtr := (**C.discoveryServer)( + C.malloc(totalServers * C.size_t(unsafe.Sizeof(uintptr(0)))), + ) + cServers := (*[1<<30 - 1]*C.discoveryServer)(unsafe.Pointer(serversPtr)) + index := 0 + for _, server := range servers.List { + cServer := getCPtrDiscoServer(state, &server) + cServers[index] = cServer + index += 1 + } + return totalServers, serversPtr + } + return 0, nil } func freeDiscoOrganization(cOrganization *C.discoveryOrganization) { @@ -57,6 +100,30 @@ func freeDiscoOrganization(cOrganization *C.discoveryOrganization) { C.free(unsafe.Pointer(cOrganization)) } +func freeDiscoServer(cServer *C.discoveryServer) { + C.free(unsafe.Pointer(cServer.authentication_url_template)) + C.free(unsafe.Pointer(cServer.base_url)) + C.free(unsafe.Pointer(cServer.country_code)) + C.free(unsafe.Pointer(cServer.display_name)) + C.free(unsafe.Pointer(cServer.keyword_list)) + freeCListStrings(cServer.public_key_list, cServer.total_public_keys) + C.free(unsafe.Pointer(cServer.server_type)) + freeCListStrings(cServer.support_contact, cServer.total_support_contact) + C.free(unsafe.Pointer(cServer)) +} + +//export FreeDiscoServers +func FreeDiscoServers(cServers *C.discoveryServers) { + if cServers.total_servers > 0 { + servers := (*[1<<30 - 1]*C.discoveryServer)(unsafe.Pointer(cServers.servers))[:cServers.total_servers:cServers.total_servers] + for i := C.size_t(0); i < cServers.total_servers; i++ { + freeDiscoServer(servers[i]) + } + C.free(unsafe.Pointer(cServers.servers)) + } + C.free(unsafe.Pointer(cServers)) +} + //export FreeDiscoOrganizations func FreeDiscoOrganizations(cOrganizations *C.discoveryOrganizations) { if cOrganizations.total_organizations > 0 { @@ -69,6 +136,31 @@ func FreeDiscoOrganizations(cOrganizations *C.discoveryOrganizations) { C.free(unsafe.Pointer(cOrganizations)) } +//export GetDiscoServers +func GetDiscoServers(name *C.char) *C.discoveryServers { + nameStr := C.GoString(name) + state, stateErr := GetVPNState(nameStr) + // TODO + if stateErr != nil { + panic(stateErr) + } + servers, serversErr := state.GetDiscoServers() + // TODO + if serversErr != nil { + panic(serversErr) + } + + returnedStruct := (*C.discoveryServers)( + C.malloc(C.size_t(unsafe.Sizeof(C.discoveryServers{}))), + ) + returnedStruct.version = C.ulonglong(servers.Version) + returnedStruct.total_servers, returnedStruct.servers = getCPtrDiscoServers( + state, + servers, + ) + return returnedStruct +} + //export GetDiscoOrganizations func GetDiscoOrganizations(name *C.char) *C.discoveryOrganizations { nameStr := C.GoString(name) diff --git a/exports/exports.go b/exports/exports.go index 797a192..0144500 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -38,6 +38,16 @@ func GetStateData( } case eduvpn.STATE_ASK_LOCATION: return (unsafe.Pointer)(getTransitionSecureLocations(data)) + case eduvpn.STATE_ASK_PROFILE: + return (unsafe.Pointer)(getTransitionProfiles(data)) + case eduvpn.STATE_DISCONNECTED: + return (unsafe.Pointer)(getTransitionServer(state, data)) + case eduvpn.STATE_DISCONNECTING: + return (unsafe.Pointer)(getTransitionServer(state, data)) + case eduvpn.STATE_CONNECTING: + return (unsafe.Pointer)(getTransitionServer(state, data)) + case eduvpn.STATE_CONNECTED: + return (unsafe.Pointer)(getTransitionServer(state, data)) default: return nil } @@ -229,17 +239,6 @@ func GetConfigCustomServer(name *C.char, url *C.char, forceTCP C.int) (*C.char, return getConfigJSON(config, configType), C.CString(ErrorToString(configErr)) } -//export GetDiscoServers -func GetDiscoServers(name *C.char) (*C.char, *C.char) { - nameStr := C.GoString(name) - state, stateErr := GetVPNState(nameStr) - if stateErr != nil { - return nil, C.CString(ErrorToString(stateErr)) - } - servers, serversErr := state.GetDiscoServers() - return C.CString(servers), C.CString(ErrorToString(serversErr)) -} - //export SetProfileID func SetProfileID(name *C.char, data *C.char) *C.char { nameStr := C.GoString(name) diff --git a/exports/servers.go b/exports/servers.go index f92c08e..d33ac1f 100644 --- a/exports/servers.go +++ b/exports/servers.go @@ -51,7 +51,7 @@ func getCPtrProfiles(serverProfiles *server.ServerProfileInfo) *C.serverProfiles profiles[index] = getCPtrProfile(&profile) index += 1 } - // TODO: DO CURRENT PROFILE + cProfiles.current = C.int(serverProfiles.GetCurrentProfileIndex()) cProfiles.profiles = (**C.serverProfile)(profilesPtr) } return cProfiles @@ -59,7 +59,8 @@ func getCPtrProfiles(serverProfiles *server.ServerProfileInfo) *C.serverProfiles // Free the profiles by looping through them if there are any // Also free the pointer itself -func freeCProfiles(profiles *C.serverProfiles) { +//export FreeProfiles +func FreeProfiles(profiles *C.serverProfiles) { // We should only free the profiles if we have them (which we should) if profiles.total_profiles > 0 { // Convert it to a go slice @@ -119,12 +120,21 @@ func freeCListStrings(allStrings **C.char, totalStrings C.size_t) { // Function for getting the server, // It gets the main state as a pointer as we need to convert some string maps to localized strings // It gets the base information for a server as well -func getServer(state *eduvpn.VPNState, base *eduvpn.VPNServerBase) *C.server { +func getCPtrServer(state *eduvpn.VPNState, base *eduvpn.VPNServerBase) *C.server { // Allocation using malloc and the size of the struct server := (*C.server)(C.malloc(C.size_t(unsafe.Sizeof(C.server{})))) // String allocation and translate the display name - server.identifier = C.CString(base.URL) + identifier := base.URL + countryCode := "" + if base.Type == "secure_internet" { + identifier = state.Servers.SecureInternetHomeServer.HomeOrganizationID + countryCode = state.Servers.SecureInternetHomeServer.CurrentLocation + } + + server.identifier = C.CString(identifier) server.display_name = C.CString(state.GetTranslated(base.DisplayName)) + server.country_code = C.CString(countryCode) + server.server_type = C.CString(base.Type) // Call the helper to get the list of support contacts server.total_support_contact, server.support_contact = getCPtrListStrings( base.SupportContact, @@ -133,22 +143,26 @@ func getServer(state *eduvpn.VPNState, base *eduvpn.VPNServerBase) *C.server { // No endtime is given if we get servers when it has been partially initialised if base.EndTime.IsZero() { server.expire_time = C.ulonglong(0) + } else { + // The expire time should be stored as an unsigned long long in unix itme + server.expire_time = C.ulonglong(base.EndTime.Unix()) } - // The expire time should be stored as an unsigned long long in unix itme - server.expire_time = C.ulonglong(base.EndTime.Unix()) return server } // Function for freeing a single server // Gets the pointer to C struct -func freeServer(info *C.server) { +//export FreeServer +func FreeServer(info *C.server) { // Free strings C.free(unsafe.Pointer(info.identifier)) C.free(unsafe.Pointer(info.display_name)) + C.free(unsafe.Pointer(info.country_code)) + C.free(unsafe.Pointer(info.server_type)) // Free arrays freeCListStrings(info.support_contact, info.total_support_contact) - freeCProfiles(info.profiles) + FreeProfiles(info.profiles) // Free the struct itself C.free(unsafe.Pointer(info)) @@ -166,10 +180,11 @@ func getCPtrServers( servers := (*[1<<30 - 1]*C.server)(unsafe.Pointer(serversPtr))[:totalServers:totalServers] index := 0 for _, server := range serverMap { - cServer := getServer(state, &server.Base) + cServer := getCPtrServer(state, &server.Base) servers[index] = cServer index += 1 } + return totalServers, serversPtr } return C.size_t(0), nil } @@ -182,7 +197,7 @@ func FreeServers(cServers *C.servers) { if cServers.total_custom > 0 { customServers := (*[1<<30 - 1]*C.server)(unsafe.Pointer(cServers.custom_servers))[:cServers.total_custom:cServers.total_custom] for i := C.size_t(0); i < cServers.total_custom; i++ { - freeServer(customServers[i]) + FreeServer(customServers[i]) } C.free(unsafe.Pointer(cServers.custom_servers)) } @@ -191,14 +206,13 @@ func FreeServers(cServers *C.servers) { instituteServers := (*[1<<30 - 1]*C.server)(unsafe.Pointer(cServers.institute_servers))[:cServers.total_institute:cServers.total_institute] for i := C.size_t(0); i < cServers.total_institute; i++ { - freeServer(instituteServers[i]) + FreeServer(instituteServers[i]) } C.free(unsafe.Pointer(cServers.institute_servers)) } // Free the secure internet server if there is one if cServers.secure_internet_server != nil { - C.free(unsafe.Pointer(cServers.secure_internet_server.country_code)) - freeServer(cServers.secure_internet_server) + FreeServer(cServers.secure_internet_server) } // Free the structure itself C.free(unsafe.Pointer(cServers)) @@ -219,7 +233,7 @@ func getSavedServersWithOptions(state *eduvpn.VPNState, servers *server.Servers) secureInternetBase, secureInternetBaseErr := servers.SecureInternetHomeServer.GetBase() if secureInternetBaseErr == nil && secureInternetBase != nil { // FIXME: log error? - secureServerPtr = getServer(state, secureInternetBase) + secureServerPtr = getCPtrServer(state, secureInternetBase) // Give a new identifier C.free(unsafe.Pointer(secureServerPtr.identifier)) secureServerPtr.identifier = C.CString(servers.SecureInternetHomeServer.HomeOrganizationID) @@ -259,11 +273,11 @@ func getTransitionDataServers(state *eduvpn.VPNState, data interface{}) *C.serve //export FreeSecureLocations func FreeSecureLocations(locations *C.serverLocations) { - freeCListStrings(locations.locations, locations.total_locations); + freeCListStrings(locations.locations, locations.total_locations) C.free(unsafe.Pointer(locations)) } -func getTransitionSecureLocations(data interface{}) (*C.serverLocations) { +func getTransitionSecureLocations(data interface{}) *C.serverLocations { if locations, ok := data.([]string); ok { returnedStruct := (*C.serverLocations)(C.malloc(C.size_t(unsafe.Sizeof(C.servers{})))) returnedStruct.total_locations, returnedStruct.locations = getCPtrListStrings(locations) @@ -271,3 +285,22 @@ func getTransitionSecureLocations(data interface{}) (*C.serverLocations) { } return nil } + +func getTransitionProfiles(data interface{}) *C.serverProfiles { + if profiles, ok := data.(*server.ServerProfileInfo); ok { + return getCPtrProfiles(profiles) + } + return nil +} + +func getTransitionServer(state *eduvpn.VPNState, data interface{}) *C.server { + if server, ok := data.(server.Server); ok { + base, baseErr := server.GetBase() + if baseErr != nil { + // TODO: LOG + return nil + } + return getCPtrServer(state, base) + } + return nil +} |
