]> git.stg.codes - stg.git/blob - projects/rlm_stg/iface.cpp
Clear completion flag before making request.
[stg.git] / projects / rlm_stg / iface.cpp
1 #include "iface.h"
2
3 #include "stg_client.h"
4 #include "radlog.h"
5
6 #include <cstring>
7
8 #include <strings.h>
9
10 namespace
11 {
12
13 struct Response
14 {
15     bool done;
16     pthread_mutex_t mutex;
17     pthread_cond_t cond;
18     RESULT result;
19     bool status;
20
21     static bool callback(void* data, const RESULT& result, bool status)
22     {
23         Response& resp = *static_cast<Response*>(data);
24         pthread_mutex_lock(&resp.mutex);
25         resp.result = result;
26         resp.status = status;
27         resp.done = true;
28         pthread_cond_signal(&resp.cond);
29         pthread_mutex_unlock(&resp.mutex);
30         return true;
31     }
32 } response;
33
34 STG_PAIR* toSTGPairs(const PAIRS& source)
35 {
36     STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
37     for (size_t pos = 0; pos < source.size(); ++pos) {
38         bzero(pairs[pos].key, sizeof(STG_PAIR::key));
39         bzero(pairs[pos].value, sizeof(STG_PAIR::value));
40         strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
41         strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
42     }
43     bzero(pairs[source.size()].key, sizeof(STG_PAIR::key));
44     bzero(pairs[source.size()].value, sizeof(STG_PAIR::value));
45
46     return pairs;
47 }
48
49 PAIRS fromSTGPairs(const STG_PAIR* pairs)
50 {
51     const STG_PAIR* pair = pairs;
52     PAIRS res;
53
54     while (!emptyPair(pair)) {
55         res.push_back(std::pair<std::string, std::string>(pair->key, pair->value));
56         ++pair;
57     }
58
59     return res;
60 }
61
62 STG_RESULT toResult(const RESULT& source)
63 {
64     STG_RESULT result;
65     result.modify = toSTGPairs(source.modify);
66     result.reply = toSTGPairs(source.reply);
67     return result;
68 }
69
70 STG_RESULT emptyResult()
71 {
72     STG_RESULT result = {NULL, NULL};
73     return result;
74 }
75
76 std::string toString(const char* value)
77 {
78     if (value == NULL)
79         return "";
80     else
81         return value;
82 }
83
84 STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* password, const STG_PAIR* pairs)
85 {
86     STG_CLIENT* client = STG_CLIENT::get();
87     if (client == NULL) {
88         RadLog("Client is not configured.");
89         return emptyResult();
90     }
91     try {
92         response.done = false;
93         client->request(type, toString(userName), toString(password), fromSTGPairs(pairs));
94         pthread_mutex_lock(&response.mutex);
95         while (!response.done)
96             pthread_cond_wait(&response.cond, &response.mutex);
97         pthread_mutex_unlock(&response.mutex);
98         if (!response.status)
99             return emptyResult();
100         return toResult(response.result);
101     } catch (const STG_CLIENT::Error& ex) {
102         RadLog("Error: '%s'.", ex.what());
103         return emptyResult();
104     }
105 }
106
107 }
108
109 int stgInstantiateImpl(const char* address)
110 {
111     pthread_mutex_init(&response.mutex, NULL);
112     pthread_cond_init(&response.cond, NULL);
113     response.done = false;
114
115     if (STG_CLIENT::configure(toString(address), &Response::callback, &response))
116         return 1;
117
118     return 0;
119 }
120
121 STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs)
122 {
123     return stgRequest(STG_CLIENT::AUTHORIZE, userName, password, pairs);
124 }
125
126 STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs)
127 {
128     return stgRequest(STG_CLIENT::AUTHENTICATE, userName, password, pairs);
129 }
130
131 STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs)
132 {
133     return stgRequest(STG_CLIENT::POST_AUTH, userName, password, pairs);
134 }
135
136 STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs)
137 {
138     return stgRequest(STG_CLIENT::PRE_ACCT, userName, password, pairs);
139 }
140
141 STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs)
142 {
143     return stgRequest(STG_CLIENT::ACCOUNT, userName, password, pairs);
144 }