static int dropbox_append(void *data, struct str *str, bool wordy) { struct dropbox_section *section = data; struct timespec tp; char *c; int ret; if (!section->running) return 0; ret = clock_gettime(CLOCK_MONOTONIC, &tp); if (ret) { perror("clock_gettime"); return -1; } if (section->uptodate || tp.tv_sec % 2) ret = str_append_icon(str, "dropbox_idle"); else ret = str_append_icon(str, "dropbox_busy"); if (ret) return -1; if (wordy) { if (str_append(str, " ")) return -1; c = strchrnul(section->status.buf, '\n'); if (str_append_buf(str, section->status.buf, c - section->status.buf)) return -1; } return str_separator(str); }
static knot_dname_t *synth_ptrname(const char *addr_str, synth_template_t *tpl) { /* PTR right-hand value is [prefix][address][zone] */ char ptrname[KNOT_DNAME_MAXLEN] = {'\0'}; int prefix_len = strlen(tpl->prefix); int addr_len = strlen(addr_str); int zone_len = strlen(tpl->zone); /* Check required space (zone requires extra leading dot). */ if (prefix_len + addr_len + zone_len + 1 >= KNOT_DNAME_MAXLEN) { return NULL; } /* Write prefix string. */ memcpy(ptrname, tpl->prefix, prefix_len); int written = prefix_len; /* Write address with substituted separator to '-'. */ char sep = str_separator(tpl->subnet.ss.ss_family); memcpy(ptrname + written, addr_str, addr_len); str_subst(ptrname + written, addr_len, sep, '-'); written += addr_len; /* Write zone name. */ ptrname[written] = '.'; written += 1; memcpy(ptrname + written, tpl->zone, zone_len); /* Convert to domain name. */ return knot_dname_from_str(ptrname); }
/*! \brief Parse address from reverse query QNAME and return address family. */ static int reverse_addr_parse(struct query_data *qdata, synth_template_t *tpl, char *addr_str) { /* QNAME required format is [address].[subnet/zone] * f.e. [1.0...0].[h.g.f.e.0.0.0.0.d.c.b.a.ip6.arpa] represents * [abcd:0:efgh::1] */ const knot_dname_t* label = qdata->name; const uint8_t *query_wire = qdata->query->wire; /* Push labels on stack for reverse walkthrough. */ const uint8_t* label_stack[KNOT_DNAME_MAXLABELS]; const uint8_t** sp = label_stack; int label_count = knot_dname_labels(label, query_wire); while(label_count > ARPA_ZONE_LABELS) { *sp++ = label; label = knot_wire_next_label(label, query_wire); --label_count; } /* Write formatted address string. */ char sep = str_separator(tpl->subnet.ss.ss_family); int sep_frequency = 1; if (sep == ':') { sep_frequency = 4; /* Separator per 4 hexdigits. */ } char *dst = addr_str; label_count = 0; while(sp != label_stack) { label = *--sp; /* Write separator for each Nth label. */ if (label_count == sep_frequency) { *dst = sep; dst += 1; label_count = 0; } /* Write label */ memcpy(dst, label + 1, label[0]); dst += label[0]; label_count += 1; } return KNOT_EOK; }
static int forward_addr_parse(struct query_data *qdata, synth_template_t *tpl, char *addr_str) { /* Find prefix label count (additive to prefix length). */ const knot_dname_t *addr_label = qdata->name; /* Mismatch if label shorter/equal than prefix. */ int prefix_len = strlen(tpl->prefix); if (addr_label == NULL || addr_label[0] <= prefix_len) { return KNOT_EINVAL; } int addr_len = *addr_label - prefix_len; memcpy(addr_str, addr_label + 1 + prefix_len, addr_len); /* Restore correct address format. */ char sep = str_separator(tpl->subnet.ss.ss_family); str_subst(addr_str, addr_len, '-', sep); return KNOT_EOK; }
static int power_append(void *data, struct str *str, bool wordy) { struct power_section *section = data; const double high_thresh = 55.0; const double low_thresh = 20.0; int ret; if (section->ac_online) ret = str_append_icon(str, "ac"); else if (section->battery_capacity >= high_thresh) ret = str_append_icon(str, "bat_full"); else if (section->battery_capacity >= low_thresh) ret = str_append_icon(str, "bat_low"); else ret = str_append_icon(str, "bat_empty"); if (ret) return -1; if (str_appendf(str, " %.0f%%", section->battery_capacity)) return -1; return str_separator(str); }