]> git.stg.codes - stg.git/blob - projects/rlm_stg/iface.cpp
Moved all sync/async boundary code into the client.
[stg.git] / projects / rlm_stg / iface.cpp
1 #include "iface.h"
2
3 #include "stg_client.h"
4 #include "types.h"
5 #include "radlog.h"
6
7 #include <stdexcept>
8 #include <cstring>
9
10 #include <strings.h>
11
12 namespace RLM = STG::RLM;
13
14 using RLM::Client;
15 using RLM::PAIRS;
16 using RLM::RESULT;
17 using RLM::REQUEST_TYPE;
18
19 namespace
20 {
21
22 STG_PAIR* toSTGPairs(const PAIRS& source)
23 {
24     STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
25     for (size_t pos = 0; pos < source.size(); ++pos) {
26         bzero(pairs[pos].key, sizeof(pairs[pos].key));
27         bzero(pairs[pos].value, sizeof(pairs[pos].value));
28         strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(pairs[pos].key));
29         strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(pairs[pos].value));
30     }
31     bzero(pairs[source.size()].key, sizeof(pairs[source.size()].key));
32     bzero(pairs[source.size()].value, sizeof(pairs[source.size()].value));
33
34     return pairs;
35 }
36
37 PAIRS fromSTGPairs(const STG_PAIR* pairs)
38 {
39     const STG_PAIR* pair = pairs;
40     PAIRS res;
41
42     while (!emptyPair(pair)) {
43         res.push_back(std::pair<std::string, std::string>(pair->key, pair->value));
44         ++pair;
45     }
46
47     return res;
48 }
49
50 STG_RESULT toResult(const RESULT& source)
51 {
52     STG_RESULT result;
53     result.modify = toSTGPairs(source.modify);
54     result.reply = toSTGPairs(source.reply);
55     return result;
56 }
57
58 STG_RESULT emptyResult()
59 {
60     STG_RESULT result = {NULL, NULL};
61     return result;
62 }
63
64 std::string toString(const char* value)
65 {
66     if (value == NULL)
67         return "";
68     else
69         return value;
70 }
71
72 STG_RESULT stgRequest(REQUEST_TYPE type, const char* userName, const char* password, const STG_PAIR* pairs)
73 {
74     Client* client = Client::get();
75     if (client == NULL) {
76         RadLog("Client is not configured.");
77         return emptyResult();
78     }
79     try {
80         return toResult(client->request(type, toString(userName), toString(password), fromSTGPairs(pairs)));
81     } catch (const std::runtime_error& ex) {
82         RadLog("Error: '%s'.", ex.what());
83         return emptyResult();
84     }
85 }
86
87 }
88
89 int stgInstantiateImpl(const char* address)
90 {
91     if (Client::configure(toString(address)))
92         return 1;
93
94     return 0;
95 }
96
97 STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs)
98 {
99     return stgRequest(RLM::AUTHORIZE, userName, password, pairs);
100 }
101
102 STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs)
103 {
104     return stgRequest(RLM::AUTHENTICATE, userName, password, pairs);
105 }
106
107 STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs)
108 {
109     return stgRequest(RLM::POST_AUTH, userName, password, pairs);
110 }
111
112 STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs)
113 {
114     return stgRequest(RLM::PRE_ACCT, userName, password, pairs);
115 }
116
117 STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs)
118 {
119     return stgRequest(RLM::ACCOUNT, userName, password, pairs);
120 }