Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <errno.h> |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 4 | #include <pthread.h> |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 5 | |
| 6 | #include <jansson.h> |
| 7 | #include <ulfius.h> |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 8 | #include <orcania.h> |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 9 | |
| 10 | #include <osmocom/core/utils.h> |
| 11 | #include <osmocom/core/linuxlist.h> |
| 12 | |
| 13 | #define PREFIX "/api/backend/v1" |
| 14 | |
| 15 | #include "debug.h" |
| 16 | #include "rest_api.h" |
| 17 | #include "slotmap.h" |
| 18 | #include "rspro_server.h" |
| 19 | |
| 20 | static json_t *comp_id2json(const struct app_comp_id *comp_id) |
| 21 | { |
| 22 | json_t *ret = json_object(); |
| 23 | |
| 24 | static const char *type_names[] = { |
| 25 | [ComponentType_remsimClient] = "remsimClient", |
| 26 | [ComponentType_remsimServer] = "remsimServer", |
| 27 | [ComponentType_remsimBankd] = "remsimBankd" |
| 28 | }; |
| 29 | |
| 30 | json_object_set_new(ret, "type_", json_string(type_names[comp_id->type])); |
| 31 | json_object_set_new(ret, "name", json_string(comp_id->name)); |
| 32 | json_object_set_new(ret, "software", json_string(comp_id->software)); |
| 33 | json_object_set_new(ret, "swVersion", json_string(comp_id->sw_version)); |
| 34 | if (strlen(comp_id->hw_manufacturer)) |
| 35 | json_object_set_new(ret, "hwManufacturer", json_string(comp_id->hw_manufacturer)); |
| 36 | if (strlen(comp_id->hw_model)) |
| 37 | json_object_set_new(ret, "hwModel", json_string(comp_id->hw_model)); |
| 38 | if (strlen(comp_id->hw_serial_nr)) |
| 39 | json_object_set_new(ret, "hwSerialNr", json_string(comp_id->hw_serial_nr)); |
| 40 | if (strlen(comp_id->hw_version)) |
| 41 | json_object_set_new(ret, "hwVersion", json_string(comp_id->hw_version)); |
| 42 | if (strlen(comp_id->fw_version)) |
| 43 | json_object_set_new(ret, "fwVersion", json_string(comp_id->fw_version)); |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static json_t *client2json(const struct rspro_client_conn *conn) |
| 49 | { |
| 50 | json_t *ret = json_object(); |
| 51 | |
| 52 | json_object_set_new(ret, "peer", json_string(conn->fi->id)); |
| 53 | json_object_set_new(ret, "state", json_string(osmo_fsm_inst_state_name(conn->fi))); |
| 54 | /* FIXME: only in the right state */ |
| 55 | json_object_set_new(ret, "component_id", comp_id2json(&conn->comp_id)); |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | static json_t *bank2json(const struct rspro_client_conn *conn) |
| 61 | { |
| 62 | json_t *ret = client2json(conn); |
| 63 | json_object_set_new(ret, "bankId", json_integer(conn->bank.bank_id)); |
| 64 | json_object_set_new(ret, "numberOfSlots", json_integer(conn->bank.num_slots)); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | static json_t *bank_slot2json(const struct bank_slot *bslot) |
| 69 | { |
| 70 | json_t *ret = json_object(); |
| 71 | json_object_set_new(ret, "bankId", json_integer(bslot->bank_id)); |
| 72 | json_object_set_new(ret, "slotNr", json_integer(bslot->slot_nr)); |
| 73 | return ret; |
| 74 | } |
| 75 | static int json2bank_slot(struct bank_slot *bslot, json_t *in) |
| 76 | { |
| 77 | json_t *jbank_id, *jslot_nr; |
| 78 | |
| 79 | if (!json_is_object(in)) |
| 80 | return -EINVAL; |
| 81 | jbank_id = json_object_get(in, "bankId"); |
| 82 | if (!jbank_id || !json_is_integer(jbank_id)) |
| 83 | return -EINVAL; |
| 84 | jslot_nr = json_object_get(in, "slotNr"); |
| 85 | if (!jslot_nr || !json_is_integer(jslot_nr)) |
| 86 | return -EINVAL; |
| 87 | bslot->bank_id = json_integer_value(jbank_id); |
| 88 | bslot->slot_nr = json_integer_value(jslot_nr); |
Harald Welte | f43baba | 2020-02-20 18:45:59 +0100 | [diff] [blame] | 89 | if (bslot->bank_id > 1023 || bslot->slot_nr > 1023) |
| 90 | return -EINVAL; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static json_t *client_slot2json(const struct client_slot *bslot) |
| 95 | { |
| 96 | json_t *ret = json_object(); |
| 97 | json_object_set_new(ret, "clientId", json_integer(bslot->client_id)); |
| 98 | json_object_set_new(ret, "slotNr", json_integer(bslot->slot_nr)); |
| 99 | return ret; |
| 100 | } |
| 101 | static int json2client_slot(struct client_slot *cslot, json_t *in) |
| 102 | { |
| 103 | json_t *jclient_id, *jslot_nr; |
| 104 | |
| 105 | if (!json_is_object(in)) |
| 106 | return -EINVAL; |
| 107 | jclient_id = json_object_get(in, "clientId"); |
| 108 | if (!jclient_id || !json_is_integer(jclient_id)) |
| 109 | return -EINVAL; |
| 110 | jslot_nr = json_object_get(in, "slotNr"); |
| 111 | if (!jslot_nr || !json_is_integer(jslot_nr)) |
| 112 | return -EINVAL; |
| 113 | cslot->client_id = json_integer_value(jclient_id); |
| 114 | cslot->slot_nr = json_integer_value(jslot_nr); |
Harald Welte | f43baba | 2020-02-20 18:45:59 +0100 | [diff] [blame] | 115 | if (cslot->client_id > 1023 || cslot->slot_nr > 1023) |
| 116 | return -EINVAL; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static json_t *slotmap2json(const struct slot_mapping *slotmap) |
| 121 | { |
| 122 | json_t *ret = json_object(); |
| 123 | json_object_set_new(ret, "bank", bank_slot2json(&slotmap->bank)); |
| 124 | json_object_set_new(ret, "client", client_slot2json(&slotmap->client)); |
| 125 | json_object_set_new(ret, "state", json_string(slotmap_state_name(slotmap->state))); |
| 126 | return ret; |
| 127 | } |
| 128 | static int json2slotmap(struct slot_mapping *out, json_t *in) |
| 129 | { |
| 130 | json_t *jbank, *jclient; |
| 131 | int rc; |
| 132 | |
| 133 | if (!json_is_object(in)) |
| 134 | return -EINVAL; |
| 135 | jbank = json_object_get(in, "bank"); |
| 136 | if (!jbank || !json_is_object(jbank)) |
| 137 | return -EINVAL; |
| 138 | jclient = json_object_get(in, "client"); |
| 139 | if (!jclient || !json_is_object(jclient)) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | rc = json2bank_slot(&out->bank, jbank); |
| 143 | if (rc < 0) |
| 144 | return rc; |
| 145 | return json2client_slot(&out->client, jclient); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | extern struct rspro_server *g_rps; |
| 150 | |
| 151 | static int api_cb_rest_ctr_get(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 152 | { |
| 153 | return U_CALLBACK_CONTINUE; |
| 154 | } |
| 155 | |
| 156 | static int api_cb_banks_get(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 157 | { |
| 158 | struct rspro_client_conn *conn; |
| 159 | json_t *json_body = json_object(); |
| 160 | json_t *json_banks = json_array(); |
| 161 | |
| 162 | pthread_rwlock_rdlock(&g_rps->rwlock); |
| 163 | llist_for_each_entry(conn, &g_rps->banks, list) { |
| 164 | json_array_append_new(json_banks, bank2json(conn)); |
| 165 | } |
| 166 | pthread_rwlock_unlock(&g_rps->rwlock); |
| 167 | |
| 168 | json_object_set_new(json_body, "banks", json_banks); |
| 169 | ulfius_set_json_body_response(resp, 200, json_body); |
| 170 | json_decref(json_body); |
| 171 | |
| 172 | return U_CALLBACK_COMPLETE; |
| 173 | } |
| 174 | |
| 175 | static int api_cb_bank_get(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 176 | { |
| 177 | const char *bank_id_str = u_map_get(req->map_url, "bank_id"); |
| 178 | struct rspro_client_conn *conn; |
| 179 | json_t *json_body = NULL; |
| 180 | unsigned long bank_id; |
| 181 | int status; |
| 182 | |
| 183 | if (!bank_id_str) { |
| 184 | status = 400; |
| 185 | goto out_err; |
| 186 | } |
| 187 | |
| 188 | bank_id = strtoul(bank_id_str, NULL, 10); |
| 189 | if (bank_id > 0xffff) { |
| 190 | status = 400; |
| 191 | goto out_err; |
| 192 | } |
| 193 | |
| 194 | pthread_rwlock_rdlock(&g_rps->rwlock); |
| 195 | llist_for_each_entry(conn, &g_rps->banks, list) { |
| 196 | if (conn->bank.bank_id == bank_id) { |
| 197 | json_body = bank2json(conn); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | pthread_rwlock_unlock(&g_rps->rwlock); |
| 202 | |
| 203 | if (json_body) { |
| 204 | ulfius_set_json_body_response(resp, 200, json_body); |
| 205 | json_decref(json_body); |
| 206 | } else { |
| 207 | ulfius_set_json_body_response(resp, 404, json_body); |
| 208 | } |
| 209 | |
| 210 | return U_CALLBACK_COMPLETE; |
| 211 | |
| 212 | out_err: |
| 213 | ulfius_set_empty_body_response(resp, status); |
| 214 | return U_CALLBACK_COMPLETE; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | static int api_cb_clients_get(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 219 | { |
| 220 | struct rspro_client_conn *conn; |
| 221 | json_t *json_body = json_object(); |
| 222 | json_t *json_clients = json_array(); |
| 223 | |
| 224 | pthread_rwlock_rdlock(&g_rps->rwlock); |
| 225 | llist_for_each_entry(conn, &g_rps->clients, list) { |
| 226 | json_array_append_new(json_clients, client2json(conn)); |
| 227 | } |
| 228 | pthread_rwlock_unlock(&g_rps->rwlock); |
| 229 | |
| 230 | json_object_set_new(json_body, "clients", json_clients); |
| 231 | ulfius_set_json_body_response(resp, 200, json_body); |
| 232 | json_decref(json_body); |
| 233 | |
| 234 | return U_CALLBACK_COMPLETE; |
| 235 | } |
| 236 | |
| 237 | static int api_cb_client_get(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 238 | { |
| 239 | const char *client_id_str = u_map_get(req->map_url, "client_id"); |
| 240 | struct rspro_client_conn *conn; |
| 241 | json_t *json_body = NULL; |
| 242 | unsigned long client_id; |
| 243 | int status; |
| 244 | |
| 245 | if (!client_id_str) { |
| 246 | status = 400; |
| 247 | goto out_err; |
| 248 | } |
| 249 | |
| 250 | client_id = strtoul(client_id_str, NULL, 10); |
| 251 | if (client_id > 0xffff) { |
| 252 | status = 400; |
| 253 | goto out_err; |
| 254 | } |
| 255 | |
| 256 | pthread_rwlock_rdlock(&g_rps->rwlock); |
| 257 | llist_for_each_entry(conn, &g_rps->clients, list) { |
| 258 | if (conn->bank.bank_id == client_id) { /* FIXME */ |
| 259 | json_body = client2json(conn); |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | pthread_rwlock_unlock(&g_rps->rwlock); |
| 264 | |
| 265 | if (json_body) { |
| 266 | ulfius_set_json_body_response(resp, 200, json_body); |
| 267 | json_decref(json_body); |
| 268 | } else { |
| 269 | ulfius_set_json_body_response(resp, 404, json_body); |
| 270 | } |
| 271 | |
| 272 | return U_CALLBACK_COMPLETE; |
| 273 | |
| 274 | out_err: |
| 275 | ulfius_set_empty_body_response(resp, status); |
| 276 | return U_CALLBACK_COMPLETE; |
| 277 | } |
| 278 | |
| 279 | static int api_cb_slotmaps_get(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 280 | { |
| 281 | struct slot_mapping *map; |
| 282 | json_t *json_body = json_object(); |
| 283 | json_t *json_maps = json_array(); |
| 284 | |
| 285 | slotmaps_rdlock(g_rps->slotmaps); |
| 286 | llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) { |
| 287 | json_array_append_new(json_maps, slotmap2json(map)); |
| 288 | } |
| 289 | slotmaps_unlock(g_rps->slotmaps); |
| 290 | |
| 291 | json_object_set_new(json_body, "slotmaps", json_maps); |
| 292 | ulfius_set_json_body_response(resp, 200, json_body); |
| 293 | json_decref(json_body); |
| 294 | |
| 295 | return U_CALLBACK_COMPLETE; |
| 296 | } |
| 297 | |
| 298 | extern struct osmo_fd g_event_ofd; |
| 299 | /* trigger our main thread select() loop */ |
| 300 | static void trigger_main_thread_via_eventfd(void) |
| 301 | { |
| 302 | uint64_t one = 1; |
| 303 | int rc; |
| 304 | |
| 305 | rc = write(g_event_ofd.fd, &one, sizeof(one)); |
| 306 | if (rc < 8) |
| 307 | fprintf(stderr, "Error writing to eventfd(): %d\n", rc); |
| 308 | } |
| 309 | |
| 310 | static int api_cb_slotmaps_post(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 311 | { |
| 312 | struct rspro_server *srv = g_rps; |
| 313 | struct slot_mapping slotmap, *map; |
| 314 | struct rspro_client_conn *conn; |
| 315 | json_error_t json_err; |
Harald Welte | 3591382 | 2019-07-21 20:00:33 +0200 | [diff] [blame] | 316 | json_t *json_req = NULL; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 317 | int rc; |
| 318 | |
| 319 | json_req = ulfius_get_json_body_request(req, &json_err); |
| 320 | if (!json_req) { |
| 321 | fprintf(stderr, "REST: No JSON Body\n"); |
| 322 | goto err; |
| 323 | } |
| 324 | |
| 325 | rc = json2slotmap(&slotmap, json_req); |
| 326 | if (rc < 0) |
| 327 | goto err; |
| 328 | map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client); |
| 329 | if (!map) { |
| 330 | fprintf(stderr, "REST: Cannot add slotmap\n"); |
| 331 | goto err; |
| 332 | } |
| 333 | slotmap_state_change(map, SLMAP_S_NEW, NULL); |
| 334 | |
| 335 | /* check if any already-connected bankd matches this new map. If yes, associate it */ |
| 336 | pthread_rwlock_rdlock(&srv->rwlock); |
| 337 | llist_for_each_entry(conn, &srv->banks, list) { |
| 338 | if (conn->bank.bank_id == slotmap.bank.bank_id) { |
| 339 | slotmap_state_change(map, SLMAP_S_NEW, &conn->bank.maps_new); |
Harald Welte | f511667 | 2019-04-02 20:55:06 +0200 | [diff] [blame] | 340 | /* Notify the conn FSM about some new maps being available */ |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 341 | trigger_main_thread_via_eventfd(); |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | pthread_rwlock_unlock(&srv->rwlock); |
| 346 | |
| 347 | |
Harald Welte | 3591382 | 2019-07-21 20:00:33 +0200 | [diff] [blame] | 348 | json_decref(json_req); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 349 | ulfius_set_empty_body_response(resp, 201); |
| 350 | |
| 351 | return U_CALLBACK_COMPLETE; |
| 352 | err: |
Harald Welte | 3591382 | 2019-07-21 20:00:33 +0200 | [diff] [blame] | 353 | json_decref(json_req); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 354 | ulfius_set_empty_body_response(resp, 400); |
| 355 | return U_CALLBACK_COMPLETE; |
| 356 | } |
| 357 | |
| 358 | /* caller is holding a write lock on slotmaps->rwlock */ |
| 359 | static void _slotmap_mark_deleted(struct slot_mapping *map) |
| 360 | { |
| 361 | struct rspro_client_conn *conn = bankd_conn_by_id(g_rps, map->bank.bank_id); |
| 362 | |
| 363 | /* delete map from global list to ensure it's not found by further lookups, |
| 364 | * particularly in case somebody wants to create a new map for the same bank/slot */ |
| 365 | llist_del(&map->list); |
| 366 | /* safely initialize list head to avoid trouble when del_slotmap() does another llist_del() */ |
| 367 | INIT_LLIST_HEAD(&map->list); |
| 368 | |
| 369 | switch (map->state) { |
| 370 | case SLMAP_S_NEW: |
| 371 | /* new map, not yet sent to bank: we can remove it immediately */ |
| 372 | /* delete from bank list (if any) */ |
| 373 | llist_del(&map->bank_list); |
| 374 | /* safely initialize list head to avoid trouble when del_slotmap() does another llist_del() */ |
| 375 | INIT_LLIST_HEAD(&map->bank_list); |
| 376 | _slotmap_del(map->maps, map); |
| 377 | break; |
| 378 | case SLMAP_S_UNACKNOWLEDGED: |
| 379 | /* map has been sent to bank already, but wasn't acknowledged yet */ |
| 380 | /* FIXME: what to do now? If we keep it unchanged, it will not be deleted. If we |
| 381 | * move it to DELETE_REQ, */ |
| 382 | break; |
| 383 | case SLMAP_S_ACTIVE: |
| 384 | /* map is fully active. Need to move it to DELETE_REQ state + trigger rspro thread, |
| 385 | * so the deletion can propagate to the bankd */ |
| 386 | _slotmap_state_change(map, SLMAP_S_DELETE_REQ, &conn->bank.maps_delreq); |
| 387 | trigger_main_thread_via_eventfd(); |
| 388 | break; |
| 389 | case SLMAP_S_DELETE_REQ: |
| 390 | /* REST had already requested deletion, but RSPRO thread hasn't issued the delete |
| 391 | * command to the bankd yet: Do nothing */ |
| 392 | break; |
| 393 | case SLMAP_S_DELETING: |
| 394 | /* we had already requested deletion of this map previously: Do nothing */ |
| 395 | break; |
| 396 | default: |
| 397 | OSMO_ASSERT(0); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static int api_cb_slotmaps_del(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 402 | { |
| 403 | const char *slotmap_id_str = u_map_get(req->map_url, "slotmap_id"); |
| 404 | struct slot_mapping *map; |
| 405 | int status = 404; |
| 406 | unsigned long map_id; |
| 407 | |
| 408 | if (!slotmap_id_str) { |
| 409 | status = 400; |
| 410 | goto err; |
| 411 | } |
| 412 | map_id = strtoul(slotmap_id_str, NULL, 10); |
| 413 | if (map_id < 0) { |
| 414 | status = 400; |
| 415 | goto err; |
| 416 | } |
| 417 | |
| 418 | slotmaps_wrlock(g_rps->slotmaps); |
| 419 | llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) { |
| 420 | if (slotmap_get_id(map) == map_id) { |
| 421 | _slotmap_mark_deleted(map); |
| 422 | status = 200; |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | slotmaps_unlock(g_rps->slotmaps); |
| 427 | trigger_main_thread_via_eventfd(); |
| 428 | |
| 429 | |
| 430 | ulfius_set_empty_body_response(resp, status); |
| 431 | return U_CALLBACK_COMPLETE; |
| 432 | err: |
| 433 | ulfius_set_empty_body_response(resp, status); |
| 434 | return U_CALLBACK_COMPLETE; |
| 435 | } |
| 436 | |
| 437 | static int api_cb_global_reset_post(const struct _u_request *req, struct _u_response *resp, void *user_data) |
| 438 | { |
| 439 | struct slot_mapping *map, *map2; |
| 440 | |
| 441 | LOGP(DMAIN, LOGL_NOTICE, "Global RESET from REST API\n"); |
| 442 | |
| 443 | /* mark all slot mappings as deleted */ |
| 444 | slotmaps_wrlock(g_rps->slotmaps); |
| 445 | llist_for_each_entry_safe(map, map2, &g_rps->slotmaps->mappings, list) { |
| 446 | _slotmap_mark_deleted(map); |
| 447 | } |
| 448 | slotmaps_unlock(g_rps->slotmaps); |
| 449 | trigger_main_thread_via_eventfd(); |
| 450 | |
| 451 | ulfius_set_empty_body_response(resp, 200); |
| 452 | return U_CALLBACK_COMPLETE; |
| 453 | } |
| 454 | |
| 455 | static const struct _u_endpoint api_endpoints[] = { |
| 456 | /* get the current restart counter */ |
| 457 | { "GET", PREFIX, "/restart-counter", 0, &api_cb_rest_ctr_get, NULL }, |
| 458 | /* get a list of SIM banks */ |
| 459 | { "GET", PREFIX, "/banks", 0, &api_cb_banks_get, NULL }, |
| 460 | { "GET", PREFIX, "/banks/:bank_id", 0, &api_cb_bank_get, NULL }, |
| 461 | /* get a list of SIM clients */ |
| 462 | { "GET", PREFIX, "/clients", 0, &api_cb_clients_get, NULL }, |
| 463 | { "GET", PREFIX, "/clients/:client_id", 0, &api_cb_client_get, NULL }, |
| 464 | /* get a list of mappings */ |
| 465 | { "GET", PREFIX, "/slotmaps", 0, &api_cb_slotmaps_get, NULL }, |
| 466 | { "POST", PREFIX, "/slotmaps", 0, &api_cb_slotmaps_post, NULL }, |
| 467 | { "DELETE", PREFIX, "/slotmaps/:slotmap_id", 0, &api_cb_slotmaps_del, NULL }, |
| 468 | { "POST", PREFIX, "/global-reset", 0, &api_cb_global_reset_post, NULL }, |
| 469 | }; |
| 470 | |
| 471 | static struct _u_instance g_instance; |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 472 | static pthread_mutex_t g_tall_lock = PTHREAD_MUTEX_INITIALIZER; |
| 473 | static void *g_tall_rest; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 474 | |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 475 | static void *my_o_malloc(size_t sz) |
| 476 | { |
| 477 | void *obj; |
| 478 | pthread_mutex_lock(&g_tall_lock); |
| 479 | obj = talloc_size(g_tall_rest, sz); |
| 480 | pthread_mutex_unlock(&g_tall_lock); |
| 481 | return obj; |
| 482 | } |
| 483 | |
| 484 | static void *my_o_realloc(void *obj, size_t sz) |
| 485 | { |
| 486 | pthread_mutex_lock(&g_tall_lock); |
| 487 | obj = talloc_realloc_size(g_tall_rest, obj, sz); |
| 488 | pthread_mutex_unlock(&g_tall_lock); |
| 489 | return obj; |
| 490 | } |
| 491 | |
| 492 | static void my_o_free(void *obj) |
| 493 | { |
| 494 | pthread_mutex_lock(&g_tall_lock); |
| 495 | talloc_free(obj); |
| 496 | pthread_mutex_unlock(&g_tall_lock); |
| 497 | } |
| 498 | |
| 499 | int rest_api_init(void *ctx, uint16_t port) |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 500 | { |
| 501 | int i; |
| 502 | |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 503 | g_tall_rest = ctx; |
| 504 | o_set_alloc_funcs(my_o_malloc, my_o_realloc, my_o_free); |
| 505 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 506 | if (ulfius_init_instance(&g_instance, port, NULL, NULL) != U_OK) |
| 507 | return -1; |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 508 | g_instance.mhd_response_copy_data = 1; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 509 | |
| 510 | for (i = 0; i < ARRAY_SIZE(api_endpoints); i++) |
| 511 | ulfius_add_endpoint(&g_instance, &api_endpoints[i]); |
| 512 | |
| 513 | if (ulfius_start_framework(&g_instance) != U_OK) { |
| 514 | fprintf(stderr, "Cannot start REST API on port %u\n", port); |
| 515 | return -1; |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | void rest_api_fini(void) |
| 521 | { |
| 522 | ulfius_stop_framework(&g_instance); |
| 523 | ulfius_clean_instance(&g_instance); |
| 524 | } |