]> git.stg.codes - stg.git/blob - projects/rlm_stg/iface.cpp
Some fixes in C-C++ bridge in rlm_stg.
[stg.git] / projects / rlm_stg / iface.cpp
1 #include "iface.h"
2
3 #include "stg_client.h"
4
5 namespace
6 {
7
8 STG_PAIR* toPairs(const PAIRS& source)
9 {
10     STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
11     for (size_t pos = 0; pos < source.size(); ++pos) {
12         bzero(pairs[pos].key, sizeof(STG_PAIR::key));
13         bzero(pairs[pos].value, sizeof(STG_PAIR::value));
14         strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
15         strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
16         ++pos;
17     }
18     bzero(pairs[sources.size()].key, sizeof(STG_PAIR::key));
19     bzero(pairs[sources.size()].value, sizeof(STG_PAIR::value));
20
21     return pairs;
22 }
23
24 }
25
26 int stgInstantiateImpl(const char* server, uint16_t port, const char* password)
27 {
28     if (STG_CLIENT::configure(server, port, password))
29         return 1;
30
31     return 0;
32 }
33
34 const STG_PAIR* stgAuthorizeImpl(const char* userName, const char* serviceType)
35 {
36     STG_CLIENT* client = STG_CLIENT::get();
37     if (client == NULL) {
38         // TODO: log "Not configured"
39         return NULL;
40     }
41     return toPairs(client->authorize(userName, serviceType));
42 }
43
44 const STG_PAIR* stgAuthenticateImpl(const char* userName, const char* serviceType)
45 {
46     STG_CLIENT* client = STG_CLIENT::get();
47     if (client == NULL) {
48         // TODO: log "Not configured"
49         return NULL;
50     }
51     return toPairs(client->authenticate(userName, serviceType));
52 }
53
54 const STG_PAIR* stgPostAuthImpl(const char* userName, const char* serviceType)
55 {
56     STG_CLIENT* client = STG_CLIENT::get();
57     if (client == NULL) {
58         // TODO: log "Not configured"
59         return NULL;
60     }
61     return toPairs(client->postAuth(userName, serviceType));
62 }
63
64 const STG_PAIR* stgPreAcctImpl(const char* userName, const char* serviceType)
65 {
66     STG_CLIENT* client = STG_CLIENT::get();
67     if (client == NULL) {
68         // TODO: log "Not configured"
69         return NULL;
70     }
71     return toPairs(client->preAcct(userName, serviceType));
72 }
73
74 const STG_PAIR* stgAccountingImpl(const char* userName, const char* serviceType, const char* statusType, const char* sessionId)
75 {
76     STG_CLIENT* client = STG_CLIENT::get();
77     if (client == NULL) {
78         // TODO: log "Not configured"
79         return NULL;
80     }
81     return toPairs(client->account(userName, serviceType, statusType, sessionId));
82 }
83
84 int countValuePairs(const VALUE_PAIR* pairs)
85 {
86     unsigned count = 0;
87     while (pairs != NULL) {
88         ++count;
89         pairs = pairs->next;
90     }
91     return count;
92 }
93
94 STG_PAIR* fromValuePairs(const VALUE_PAIR* pairs)
95 {
96     unsigned size = countValuePairs(pairs);
97     STG_PAIR* res = new STG_PAIR[size + 1];
98     size_t pos = 0;
99     while (pairs != NULL) {
100         bzero(res[pos].key, sizeof(STG_PAIR::key));
101         bzero(res[pos].value, sizeof(STG_PAIR::value));
102         strncpy(res[pos].key, pairs->name, sizeof(STG_PAIR::key));
103         strncpy(res[pos].value, pairs->data.strvalue, sizeof(STG_PAIR::value));
104         ++pos;
105         pairs = pairs->next;
106     }
107     bzero(res[pos].key, sizeof(STG_PAIR::key));
108     bzero(res[pos].value, sizeof(STG_PAIR::value));
109     return res;
110 }
111
112 void deletePairs(const STG_PAIR* pairs)
113 {
114     delete[] pairs;
115 }