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
|
#include <stdlib.h>
#include <string.h>
#include <json_object.h>
#include "util.h"
#include "vali.h"
#include "org.varlink.service.h"
struct vali_registry {
char *vendor;
char *product;
char *version;
char *url;
struct array interfaces; // struct vali_registry_entry
};
struct vali_registry_entry {
const struct vali_registry_interface *interface;
struct vali_service_call_handler call_handler;
};
static const struct vali_registry_entry *registry_get(struct vali_registry *registry, const char *iface_name, size_t iface_name_len) {
const struct vali_registry_entry *interfaces = registry->interfaces.data;
size_t interfaces_len = registry->interfaces.size / sizeof(interfaces[0]);
for (size_t i = 0; i < interfaces_len; i++) {
const struct vali_registry_entry *iface = &interfaces[i];
if (strncmp(iface->interface->name, iface_name, iface_name_len) == 0) {
return iface;
}
}
return NULL;
}
static void handle_GetInfo(struct org_varlink_service_GetInfo_service_call call, const struct org_varlink_service_GetInfo_in *in) {
struct vali_registry *registry = vali_service_call_get_user_data(call.base);
struct org_varlink_service_GetInfo_out out = {
.vendor = registry->vendor,
.product = registry->product,
.version = registry->version,
.url = registry->url,
};
const struct vali_registry_entry *interfaces = registry->interfaces.data;
size_t interfaces_len = registry->interfaces.size / sizeof(interfaces[0]);
out.interfaces.data = calloc(interfaces_len, sizeof(out.interfaces.data[0]));
if (out.interfaces.data == NULL) {
vali_service_conn_disconnect(vali_service_call_get_conn(call.base));
return;
}
for (size_t i = 0; i < interfaces_len; i++) {
out.interfaces.data[i] = (char *)interfaces[i].interface->name;
out.interfaces.len++;
}
org_varlink_service_GetInfo_close_with_reply(call, &out);
free(out.interfaces.data);
}
static void handle_GetInterfaceDescription(struct org_varlink_service_GetInterfaceDescription_service_call call, const struct org_varlink_service_GetInterfaceDescription_in *in) {
struct vali_registry *registry = vali_service_call_get_user_data(call.base);
const struct vali_registry_entry *iface = registry_get(registry, in->interface, strlen(in->interface));
if (iface == NULL) {
vali_service_conn_disconnect(vali_service_call_get_conn(call.base));
return; // TODO: send proper error
}
struct org_varlink_service_GetInterfaceDescription_out out = {
.description = (char *)iface->interface->definition,
};
org_varlink_service_GetInterfaceDescription_close_with_reply(call, &out);
}
static const struct org_varlink_service_handler org_varlink_service_handler = {
.GetInfo = handle_GetInfo,
.GetInterfaceDescription = handle_GetInterfaceDescription,
};
static void org_varlink_service_handle_call(struct vali_service_call *call, void *user_data) {
struct vali_registry *registry = user_data;
vali_service_call_set_user_data(call, registry);
struct vali_service_call_handler call_handler = org_varlink_service_get_call_handler(&org_varlink_service_handler);
call_handler.func(call, call_handler.user_data);
}
struct vali_registry *vali_registry_create(const struct vali_registry_options *options) {
struct vali_registry *registry = calloc(1, sizeof(*registry));
if (registry == NULL) {
return NULL;
}
registry->vendor = strdup(options->vendor);
registry->product = strdup(options->product);
registry->version = strdup(options->version);
registry->url = strdup(options->url);
if (registry->vendor == NULL || registry->product == NULL || registry->version == NULL || registry->url == NULL) {
vali_registry_destroy(registry);
return NULL;
}
const struct vali_service_call_handler call_handler = {
.func = org_varlink_service_handle_call,
.user_data = registry,
};
if (!vali_registry_add(registry, &org_varlink_service_interface, call_handler)) {
vali_registry_destroy(registry);
return NULL;
}
return registry;
}
void vali_registry_destroy(struct vali_registry *registry) {
array_finish(®istry->interfaces);
free(registry->vendor);
free(registry->product);
free(registry->version);
free(registry->url);
free(registry);
}
static void registry_handle_call(struct vali_service_call *call, void *user_data) {
struct vali_registry *registry = user_data;
const char *method = vali_service_call_get_method(call);
const char *last_dot = strrchr(method, '.');
if (last_dot == NULL) {
vali_service_conn_disconnect(vali_service_call_get_conn(call));
return; // TODO: send proper error
}
size_t iface_name_len = (size_t)(last_dot - method);
const struct vali_registry_entry *iface = registry_get(registry, method, iface_name_len);
if (iface != NULL) {
iface->call_handler.func(call, iface->call_handler.user_data);
return;
}
char *iface_name = strndup(method, iface_name_len);
if (iface_name == NULL) {
vali_service_conn_disconnect(vali_service_call_get_conn(call));
return;
}
const struct org_varlink_service_error_InterfaceNotFound params = {
.interface = iface_name,
};
org_varlink_service_error_InterfaceNotFound_close_service_call(call, ¶ms);
free(iface_name);
}
struct vali_service_call_handler vali_registry_get_call_handler(struct vali_registry *registry) {
return (struct vali_service_call_handler){
.func = registry_handle_call,
.user_data = registry,
};
}
bool vali_registry_add(struct vali_registry *registry, const struct vali_registry_interface *iface, struct vali_service_call_handler call_handler) {
if (registry_get(registry, iface->name, strlen(iface->name))) {
abort();
}
struct vali_registry_entry *dst = array_add(®istry->interfaces, sizeof(*dst));
if (dst == NULL) {
return false;
}
*dst = (struct vali_registry_entry){
.interface = iface,
.call_handler = call_handler,
};
return true;
}
|