1 /* See the DRL-LICENSE file for this file's software license. */
6 #include <libxml/xmlmemory.h>
7 #include <libxml/parser.h>
8 #include <netinet/in.h>
12 #include <sys/socket.h>
14 #include "raterouter.h"
15 #include "ratetypes.h"
20 void free_ident(ident_config *ident) {
22 while (ident->peers) {
23 ident_peer *tofree = ident->peers;
24 ident->peers = ident->peers->next;
29 while (ident->members) {
30 ident_member *tofree = ident->members;
31 ident->members = ident->members->next;
39 void free_ident_list(ident_config *list) {
49 static int xid_filter(const struct dirent *d) {
50 if (atoi(d->d_name) > 0)
56 int get_eligible_leaves(drl_instance_t *instance) {
57 struct dirent **names;
59 leaf_t *leaves = NULL;
60 map_handle leaf_map = allocate_map();
62 if (leaf_map == NULL) {
66 count = scandir("/proc/virtual", &names, xid_filter, alphasort);
72 leaves = malloc(count * sizeof(leaf_t));
74 /* Couldn't allocate leaves array. Need to free names memory. */
83 for (i = 0; i < count; ++i) {
84 leaves[i].xid = atoi(names[i]->d_name);
85 leaves[i].parent = NULL;
89 map_insert(leaf_map, &leaves[i].xid, sizeof(leaves[i].xid), &leaves[i]);
94 instance->leaf_map = leaf_map;
95 instance->leaves = leaves;
96 instance->leaf_count = count;
101 static int parse_common(xmlDocPtr doc, xmlNodePtr ident, ident_config *common) {
109 xmlNodePtr fields = ident->children;
110 ident_peer *current = NULL;
112 /* Make sure no required fields are missing. */
113 id = xmlGetProp(ident, (const xmlChar *) "id");
115 printlog(LOG_CRITICAL, "Ident missing globally unique identifier.\n");
118 common->id = atoi((const char *) id);
122 limit = xmlGetProp(ident, (const xmlChar *) "limit");
124 printlog(LOG_CRITICAL, "Ident missing global rate limit.\n");
127 common->limit = atoi((const char *) limit);
131 commfabric = xmlGetProp(ident, (const xmlChar *) "commfabric");
132 if (commfabric == NULL) {
133 printlog(LOG_CRITICAL, "Ident missing comm fabric specifier.\n");
136 if (!xmlStrcmp(commfabric, (const xmlChar *) "MESH")) {
137 common->commfabric = COMM_MESH;
138 } else if (!xmlStrcmp(commfabric, (const xmlChar *) "GOSSIP")) {
139 common->commfabric = COMM_GOSSIP;
141 printlog(LOG_CRITICAL, "Unknown/invalid comm fabric.\n");
148 /* Only care about branching factor if we're using gossip. */
149 if (common->commfabric == COMM_GOSSIP) {
150 branch = xmlGetProp(ident, (const xmlChar *) "branch");
151 if (branch == NULL) {
152 printlog(LOG_CRITICAL, "Ident missing gossip branch.\n");
155 common->branch = atoi((const char *) branch);
160 accounting = xmlGetProp(ident, (const xmlChar *) "accounting");
161 if (accounting == NULL) {
162 printlog(LOG_CRITICAL, "Ident missing accounting.\n");
165 if (!xmlStrcmp(accounting, (const xmlChar *) "STANDARD")) {
166 common->accounting = ACT_STANDARD;
167 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SAMPLEHOLD")) {
168 common->accounting = ACT_SAMPLEHOLD;
169 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SIMPLE")) {
170 common->accounting = ACT_SIMPLE;
172 printlog(LOG_CRITICAL, "Unknown/invalid accounting table.\n");
179 ewma = xmlGetProp(ident, (const xmlChar *) "ewma");
181 printlog(LOG_CRITICAL, "Ident missing ewma weight.\n");
184 common->fixed_ewma_weight = atof((const char *) ewma);
188 intervals = xmlGetProp(ident, (const xmlChar *) "intervals");
189 if (intervals == NULL) {
190 printlog(LOG_CRITICAL, "Ident missing interval count.\n");
193 common->intervals = atoi((const char *) intervals);
197 while (fields != NULL) {
198 if((!xmlStrcmp(fields->name, (const xmlChar *) "peer"))) {
199 xmlChar *ip = xmlNodeListGetString(doc, fields->children, 1);
200 if (current == NULL) {
202 common->peers = malloc(sizeof(ident_peer));
203 if (common->peers == NULL) {
206 common->peers->ip = inet_addr((const char *) ip);
207 common->peers->next = NULL;
208 common->peer_count += 1;
209 current = common->peers;
211 /* Add it to the list. */
212 current->next = malloc(sizeof(ident_peer));
213 if (current->next == NULL) {
216 current = current->next;
217 current->ip = inet_addr((const char *) ip);
218 common->peer_count += 1;
219 current->next = NULL;
223 fields = fields->next;
226 if (common->peers == NULL) {
227 printlog(LOG_CRITICAL, "Must have at least one peer.\n");
235 static ident_config *parse_machine(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
236 ident_config *common = malloc(sizeof(ident_config));
238 if (common == NULL) {
242 memset(common, 0, sizeof(ident_config));
243 if (parse_common(doc, ident, common)) {
248 /* No further information needed for machine-level identities. */
249 common->type = IDENT_MACHINE;
250 common->members = NULL;
253 if (configs->last_machine == NULL) {
254 configs->machines = common;
255 configs->last_machine = common;
257 configs->last_machine->next = common;
258 configs->last_machine = common;
261 configs->machine_count += 1;
266 static ident_config *parse_set(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
267 xmlNodePtr fields = ident->children;
268 ident_config *common = malloc(sizeof(ident_config));
269 ident_member *current = NULL;
271 if (common == NULL) {
275 memset(common, 0, sizeof(ident_config));
276 if (parse_common(doc, ident, common)) {
281 while (fields != NULL) {
282 ident_member *member = NULL;
284 if (!xmlStrcmp(fields->name, (const xmlChar *) "xid")) {
285 xmlChar *xid = xmlNodeListGetString(doc, fields->children, 1);
287 if (atoi((const char *) xid) >= 0) {
288 member = malloc(sizeof(ident_member));
289 if (member == NULL) {
294 member->type = MEMBER_XID;
295 sscanf((const char *) xid, "%x", &member->value);
304 } else if (!xmlStrcmp(fields->name, (const xmlChar *) "guid")) {
305 xmlChar *guid = xmlNodeListGetString(doc, fields->children, 1);
307 if (atoi((const char *) guid) >= 0) {
308 member = malloc(sizeof(ident_member));
309 if (member == NULL) {
314 member->type = MEMBER_GUID;
315 member->value = atoi((const char *) guid);
327 if (common->members == NULL) {
328 common->members = member;
331 current->next = member;
336 fields = fields->next;
339 /* A sliver set must have at least one member (xid or guid) or else it is
341 if (common->members == NULL) {
346 common->type = IDENT_SET;
349 if (configs->last_set == NULL) {
350 configs->sets = common;
351 configs->last_set = common;
353 configs->last_set->next = common;
354 configs->last_set = common;
357 configs->set_count += 1;
362 int parse_drl_config(const char *configfile, parsed_configs *configs) {
364 xmlNodePtr drl, ident;
366 configs->machines = NULL;
367 configs->sets = NULL;
368 configs->last_machine = NULL;
369 configs->last_set = NULL;
370 configs->machine_count = 0;
371 configs->set_count = 0;
373 if(!(doc = xmlParseFile(configfile))){
374 printlog(LOG_CRITICAL, "Config file (%s) not parsed successfully.\n", configfile);
379 if(!(drl = xmlDocGetRootElement(doc))){
380 printlog(LOG_CRITICAL, "Config file (%s) has no root element.\n", configfile);
384 if(xmlStrcmp(drl->name, (const xmlChar *) "drl")){
385 printlog(LOG_CRITICAL, "Config file (%s) of the wrong type, root node != drl\n", configfile);
390 ident = drl->children;
391 while(ident != NULL) {
392 ident_config *new = NULL;
394 if((!xmlStrcmp(ident->name, (const xmlChar *) "machine"))) {
395 new = parse_machine(doc, ident, configs);
396 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "set"))) {
397 new = parse_set(doc, ident, configs);
398 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "text"))) {
399 /* libxml seems to wrap everything inside two 'text's. */
405 /* FIXME: Make this more descriptive. :) */
406 printlog(LOG_CRITICAL, "Error occurred while parsing...\n");
408 free_ident_list(configs->machines);
409 free_ident_list(configs->sets);
411 configs->machines = NULL;
412 configs->sets = NULL;
413 configs->last_machine = NULL;
414 configs->last_set = NULL;
425 /* Return the list of parsed identities. */