X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/3a9bc658f505e423b3be181948f1870a09915ea9..d241dd27df3a32b5477eadb1a9d4897d7fcf76a7:/projects/rlm_stg/iface.cpp?ds=sidebyside diff --git a/projects/rlm_stg/iface.cpp b/projects/rlm_stg/iface.cpp index caa44386..f97593f4 100644 --- a/projects/rlm_stg/iface.cpp +++ b/projects/rlm_stg/iface.cpp @@ -1,45 +1,35 @@ #include "iface.h" #include "stg_client.h" +#include "types.h" +#include "radlog.h" +#include #include #include -namespace -{ +namespace RLM = STG::RLM; + +using RLM::Client; +using RLM::PAIRS; +using RLM::RESULT; +using RLM::REQUEST_TYPE; -struct Response +namespace { - bool done; - pthread_mutex_t mutex; - pthread_cond_t cond; - RESULT result; - - static bool callback(void* data, const RESULT& res) - { - Response& resp = *static_cast(data); - pthread_mutex_lock(&resp.mutex); - resp.result = res; - resp.done = true; - pthread_cond_signal(&resp.cond); - pthread_mutex_unlock(&resp.mutex); - return true; - } -} response; STG_PAIR* toSTGPairs(const PAIRS& source) { STG_PAIR * pairs = new STG_PAIR[source.size() + 1]; for (size_t pos = 0; pos < source.size(); ++pos) { - bzero(pairs[pos].key, sizeof(STG_PAIR::key)); - bzero(pairs[pos].value, sizeof(STG_PAIR::value)); - strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key)); - strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value)); - ++pos; + bzero(pairs[pos].key, sizeof(pairs[pos].key)); + bzero(pairs[pos].value, sizeof(pairs[pos].value)); + strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(pairs[pos].key)); + strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(pairs[pos].value)); } - bzero(pairs[source.size()].key, sizeof(STG_PAIR::key)); - bzero(pairs[source.size()].value, sizeof(STG_PAIR::value)); + bzero(pairs[source.size()].key, sizeof(pairs[source.size()].key)); + bzero(pairs[source.size()].value, sizeof(pairs[source.size()].value)); return pairs; } @@ -62,12 +52,13 @@ STG_RESULT toResult(const RESULT& source) STG_RESULT result; result.modify = toSTGPairs(source.modify); result.reply = toSTGPairs(source.reply); + result.returnCode = source.returnCode; return result; } STG_RESULT emptyResult() { - STG_RESULT result = {NULL, NULL}; + STG_RESULT result = {NULL, NULL, STG_REJECT}; return result; } @@ -79,22 +70,17 @@ std::string toString(const char* value) return value; } -STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* password, const STG_PAIR* pairs) +STG_RESULT stgRequest(REQUEST_TYPE type, const char* userName, const char* password, const STG_PAIR* pairs) { - STG_CLIENT* client = STG_CLIENT::get(); + Client* client = Client::get(); if (client == NULL) { - // TODO: log "Not configured" + RadLog("Client is not configured."); return emptyResult(); } try { - client->request(type, toString(userName), toString(password), fromSTGPairs(pairs)); - pthread_mutex_lock(&response.mutex); - while (!response.done) - pthread_cond_wait(&response.cond, &response.mutex); - pthread_mutex_unlock(&response.mutex); - return toResult(response.result); - } catch (const STG_CLIENT::Error& ex) { - // TODO: log error + return toResult(client->request(type, toString(userName), toString(password), fromSTGPairs(pairs))); + } catch (const std::runtime_error& ex) { + RadLog("Error: '%s'.", ex.what()); return emptyResult(); } } @@ -103,11 +89,7 @@ STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* p int stgInstantiateImpl(const char* address) { - pthread_mutex_init(&response.mutex, NULL); - pthread_cond_init(&response.cond, NULL); - response.done = false; - - if (STG_CLIENT::configure(toString(address), &Response::callback, &response)) + if (Client::configure(toString(address))) return 1; return 0; @@ -115,25 +97,25 @@ int stgInstantiateImpl(const char* address) STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - return stgRequest(STG_CLIENT::AUTHORIZE, userName, password, pairs); + return stgRequest(RLM::AUTHORIZE, userName, password, pairs); } STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - return stgRequest(STG_CLIENT::AUTHENTICATE, userName, password, pairs); + return stgRequest(RLM::AUTHENTICATE, userName, password, pairs); } STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - return stgRequest(STG_CLIENT::POST_AUTH, userName, password, pairs); + return stgRequest(RLM::POST_AUTH, userName, password, pairs); } STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - return stgRequest(STG_CLIENT::PRE_ACCT, userName, password, pairs); + return stgRequest(RLM::PRE_ACCT, userName, password, pairs); } STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - return stgRequest(STG_CLIENT::ACCOUNT, userName, password, pairs); + return stgRequest(RLM::ACCOUNT, userName, password, pairs); }