X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/2db7e7236c61f8ee346800c82c800eafcd65de4e..8c6fa3fbaccc22127280bf77a48fab5a3ee0716e:/projects/rlm_stg/iface.cpp diff --git a/projects/rlm_stg/iface.cpp b/projects/rlm_stg/iface.cpp index 40a96f72..f97593f4 100644 --- a/projects/rlm_stg/iface.cpp +++ b/projects/rlm_stg/iface.cpp @@ -1,115 +1,121 @@ #include "iface.h" #include "stg_client.h" +#include "types.h" +#include "radlog.h" + +#include +#include + +#include + +namespace RLM = STG::RLM; + +using RLM::Client; +using RLM::PAIRS; +using RLM::RESULT; +using RLM::REQUEST_TYPE; namespace { -STG_PAIR* toPairs(const PAIRS& source) +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[sources.size()].key, sizeof(STG_PAIR::key)); - bzero(pairs[sources.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; } +PAIRS fromSTGPairs(const STG_PAIR* pairs) +{ + const STG_PAIR* pair = pairs; + PAIRS res; + + while (!emptyPair(pair)) { + res.push_back(std::pair(pair->key, pair->value)); + ++pair; + } + + return res; } -int stgInstantiateImpl(const char* server, uint16_t port, const char* password) +STG_RESULT toResult(const RESULT& source) { - if (STG_CLIENT::configure(server, port, password)) - return 1; + STG_RESULT result; + result.modify = toSTGPairs(source.modify); + result.reply = toSTGPairs(source.reply); + result.returnCode = source.returnCode; + return result; +} - return 0; +STG_RESULT emptyResult() +{ + STG_RESULT result = {NULL, NULL, STG_REJECT}; + return result; } -const STG_PAIR* stgAuthorizeImpl(const char* userName, const char* serviceType) +std::string toString(const char* value) { - STG_CLIENT* client = STG_CLIENT::get(); - if (client == NULL) { - // TODO: log "Not configured" - return NULL; - } - return toPairs(client->authorize(userName, serviceType)); + if (value == NULL) + return ""; + else + return value; } -const STG_PAIR* stgAuthenticateImpl(const char* userName, const char* serviceType) +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" - return NULL; + RadLog("Client is not configured."); + return emptyResult(); } - return toPairs(client->authenticate(userName, serviceType)); + try { + return toResult(client->request(type, toString(userName), toString(password), fromSTGPairs(pairs))); + } catch (const std::runtime_error& ex) { + RadLog("Error: '%s'.", ex.what()); + return emptyResult(); + } +} + } -const STG_PAIR* stgPostAuthImpl(const char* userName, const char* serviceType) +int stgInstantiateImpl(const char* address) { - STG_CLIENT* client = STG_CLIENT::get(); - if (client == NULL) { - // TODO: log "Not configured" - return NULL; - } - return toPairs(client->postAuth(userName, serviceType)); + if (Client::configure(toString(address))) + return 1; + + return 0; } -const STG_PAIR* stgPreAcctImpl(const char* userName, const char* serviceType) +STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - STG_CLIENT* client = STG_CLIENT::get(); - if (client == NULL) { - // TODO: log "Not configured" - return NULL; - } - return toPairs(client->preAcct(userName, serviceType)); + return stgRequest(RLM::AUTHORIZE, userName, password, pairs); } -const STG_PAIR* stgAccountingImpl(const char* userName, const char* serviceType, const char* statusType, const char* sessionId) +STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - STG_CLIENT* client = STG_CLIENT::get(); - if (client == NULL) { - // TODO: log "Not configured" - return NULL; - } - return toPairs(client->account(userName, serviceType, statusType, sessionId)); + return stgRequest(RLM::AUTHENTICATE, userName, password, pairs); } -int countValuePairs(const VALUE_PAIR* pairs) +STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - unsigned count = 0; - while (pairs != NULL) { - ++count; - pairs = pairs->next; - } - return count; + return stgRequest(RLM::POST_AUTH, userName, password, pairs); } -STG_PAIR* fromValuePairs(const VALUE_PAIR* pairs) +STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - unsigned size = countValuePairs(pairs); - STG_PAIR* res = new STG_PAIR[size + 1]; - size_t pos = 0; - while (pairs != NULL) { - bzero(res[pos].key, sizeof(STG_PAIR::key)); - bzero(res[pos].value, sizeof(STG_PAIR::value)); - strncpy(res[pos].key, pairs->name, sizeof(STG_PAIR::key)); - strncpy(res[pos].value, pairs->data.strvalue, sizeof(STG_PAIR::value)); - ++pos; - pairs = pairs->next; - } - bzero(res[pos].key, sizeof(STG_PAIR::key)); - bzero(res[pos].value, sizeof(STG_PAIR::value)); - return res; + return stgRequest(RLM::PRE_ACCT, userName, password, pairs); } -void deletePairs(const STG_PAIR* pairs) +STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs) { - delete[] pairs; + return stgRequest(RLM::ACCOUNT, userName, password, pairs); }