aboutsummaryrefslogtreecommitdiff
path: root/net/nginx-util/src/ubus-cxx.hpp
blob: 6c193cfc312aa0d9ef157b762eaeeb9c4093e707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#ifndef _UBUS_CXX_HPP
#define _UBUS_CXX_HPP

#include <libubus.h>
#include <cassert>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>

#ifndef NDEBUG
#include <iostream>
#endif

namespace ubus {

static constexpr int call_timeout = 500;

using msg_ptr = std::shared_ptr<const blob_attr>;

using strings = std::vector<std::string>;

inline auto concat(strings dest)
{
    return dest;
}

template <class... Strings>
inline auto concat(strings dest, strings src, Strings... more)
{
    dest.reserve(dest.size() + src.size());
    dest.insert(std::end(dest), std::make_move_iterator(std::begin(src)),
                std::make_move_iterator(std::end(src)));
    return concat(std::move(dest), std::move(more)...);
}

template <class S, class... Strings>
inline auto concat(strings dest, S src, Strings... more)
{
    dest.emplace_back(std::move(src));
    return concat(std::move(dest), std::move(more)...);
}

class iterator {
  private:
    const strings& keys;

    const size_t n = 0;

    size_t i = 0;

    const blob_attr* pos = nullptr;

    std::unique_ptr<iterator> cur{};

    iterator* parent = nullptr;

    size_t rem = 0;

    [[nodiscard]] inline auto matches() const -> bool
    {
        return (keys[i].empty() || blobmsg_name(cur->pos) == keys[i]);
    }

    explicit iterator(iterator* par)
        : keys{par->keys}, n{par->n}, pos{par->pos}, cur{this}, parent{par}
    {
        if (pos != nullptr) {
            rem = blobmsg_data_len(pos);
            pos = static_cast<blob_attr*>(blobmsg_data(pos));
        }
    }

  public:
    explicit iterator(const blob_attr* msg, const strings& key_filter = {""})
        : keys{key_filter}, n{keys.size() - 1}, pos{msg}, cur{this}
    {
        if (pos != nullptr) {
            rem = blobmsg_data_len(pos);
            pos = static_cast<blob_attr*>(blobmsg_data(pos));

            if (rem == 0) {
                pos = nullptr;
            }
            else if (i != n || !matches()) {
                ++*this;
            }
        }
    }

    inline iterator(iterator&&) noexcept = default;

    inline iterator(const iterator&) = delete;

    inline auto operator=(const iterator&) -> iterator& = delete;

    inline auto operator=(iterator &&) -> iterator& = delete;

    inline auto operator*()
    {
        return cur->pos;
    }

    inline auto operator!=(const iterator& rhs)
    {
        return (cur->rem != rhs.cur->rem || cur->pos != rhs.cur->pos);
    }

    auto operator++() -> iterator&;

    inline ~iterator()
    {
        if (cur.get() == this) {
            static_cast<void>(cur.release());
        }
    }
};

class message {
  private:
    const msg_ptr msg{};  // initialized by callback.

    const strings keys{};

  public:
    inline explicit message(msg_ptr message_ptr, strings key_filter = {""})
        : msg{std::move(message_ptr)}, keys{std::move(key_filter)}
    {}

    inline message(message&&) = default;

    inline message(const message&) = delete;

    inline auto operator=(message &&) -> message& = delete;

    inline auto operator=(const message&) -> message& = delete;

    [[nodiscard]] inline auto begin() const -> iterator
    {
        return iterator{msg.get(), keys};
    }

    [[nodiscard]] inline auto end() const -> iterator
    {
        return iterator{nullptr, keys};
    }

    inline explicit operator bool() const
    {
        return begin() != end();
    }

    template <class... Strings>
    auto filter(Strings... key_filter)
    {
        strings both{};
        if (keys.size() != 1 || !keys[0].empty()) {
            both = keys;
        }
        both = concat(std::move(both), std::move(key_filter)...);
        return std::move(message{msg, std::move(both)});
    }

    inline ~message() = default;
};

class lock_shared_resources {
  private:
    static std::mutex inuse;

  public:
    inline lock_shared_resources()
    {
        inuse.lock();
    }

    inline lock_shared_resources(lock_shared_resources&&) noexcept = default;

    inline lock_shared_resources(const lock_shared_resources&) = delete;

    inline auto operator=(const lock_shared_resources&) -> auto& = delete;

    inline auto operator=(lock_shared_resources &&) -> auto&& = delete;

    // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
    inline auto get_context() -> ubus_context*  // is member to enforce inuse.
    {
        static auto ubus_freeing = [](ubus_context* ctx) { ubus_free(ctx); };
        static std::unique_ptr<ubus_context, decltype(ubus_freeing)> lazy_ctx{ubus_connect(nullptr),
                                                                              ubus_freeing};

        if (!lazy_ctx) {  // it could be available on a later call:

            lazy_ctx.reset(ubus_connect(nullptr));

            if (!lazy_ctx) {
                throw std::runtime_error("ubus error: cannot connect context");
            }
        }

        return lazy_ctx.get();
    }

    // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
    inline auto get_blob_buf() -> blob_buf*  // is member to enforce inuse.
    {
        static blob_buf buf;

        static auto blob_buf_freeing = [](blob_buf* b) { blob_buf_free(b); };
        static std::unique_ptr<blob_buf, decltype(blob_buf_freeing)>
            created_to_free_on_the_end_of_life{&buf, blob_buf_freeing};

        blob_buf_init(&buf, 0);

        return &buf;
    }

    inline ~lock_shared_resources()
    {
        inuse.unlock();
    }
};

template <class F>
auto call(const char* path, const char* method, F set_arguments, int timeout = call_timeout)
    -> message;

inline auto call(const char* path, const char* method, int timeout = call_timeout) -> message
{
    return call(
        path, method, [](blob_buf* /*buf*/) { return 0; }, timeout);
}

inline auto call(const char* path, int timeout = call_timeout) -> message
{
    return call(path, "", timeout);
}

// ------------------------- implementation: ----------------------------------

std::mutex lock_shared_resources::inuse;

inline auto iterator::operator++() -> iterator&
{
    for (;;) {
#ifndef NDEBUG
        std::cout << std::string(i, '>') << " look for " << keys[i] << " at ";
        std::cout << blobmsg_name(cur->pos) << std::endl;
#endif

        auto id = blob_id(cur->pos);
        if ((id == BLOBMSG_TYPE_TABLE || id == BLOBMSG_TYPE_ARRAY) && i < n && matches() &&
            blobmsg_data_len(cur->pos) > 0)
        {  // immmerge:
            ++i;

            auto* tmp = cur.release();

            struct new_iterator : public iterator  // use private constructor:
            {
                explicit new_iterator(iterator* par) : iterator{par} {}
            };
            cur = std::make_unique<new_iterator>(tmp);
        }
        else {
            while (true) {
                cur->rem -= blob_pad_len(cur->pos);
                cur->pos = blob_next(cur->pos);
                auto len = blob_pad_len(cur->pos);

                if (cur->rem > 0 && len <= cur->rem && len >= sizeof(blob_attr)) {
                    break;
                }

                // emerge:
                auto* tmp = cur->parent;

                if (tmp == nullptr) {
                    cur->pos = nullptr;
                    return *cur;
                }

                cur.reset(tmp);

                --i;
            }
        }
        if (i == n && matches()) {
            return *cur;
        }
    }
}

template <class F>
inline auto call(const char* path, const char* method, F set_arguments, int timeout) -> message
{
    auto shared = lock_shared_resources{};

    auto* ctx = shared.get_context();

    uint32_t id = 0;
    int err = ubus_lookup_id(ctx, path, &id);

    if (err == 0) {  // call
        ubus_request request{};

        auto* buf = shared.get_blob_buf();
        err = set_arguments(buf);
        if (err == 0) {
            err = ubus_invoke_async(ctx, id, method, buf->head, &request);
        }

        if (err == 0) {
            msg_ptr message_ptr;

            /* Cannot capture message_ptr, the lambda would be another type.
             * Pass a location where to save the message as priv pointer when
             * invoking and get it back here:
             */
            request.priv = &message_ptr;

            request.data_cb = [](ubus_request* req, int /*type*/, blob_attr* msg) {
                if (req == nullptr || msg == nullptr) {
                    return;
                }

                auto* saved = static_cast<msg_ptr*>(req->priv);
                if (saved == nullptr || *saved) {
                    return;
                }

                saved->reset(blob_memdup(msg), free);
                if (!*saved) {
                    throw std::bad_alloc();
                }
            };

            err = ubus_complete_request(ctx, &request, timeout);

            if (err == 0) {
                return message{message_ptr};
            }
        }
    }

    std::string errmsg = "ubus::call error: cannot invoke";
    errmsg += " (" + std::to_string(err) + ") " + path + " " + method;
    throw std::runtime_error(errmsg);
}

}  // namespace ubus

#endif