const char *asn_fcget(thash *hash, const char *path) { char *value; value = (char *) thash_get(hash, path); if (value == NULL) die("asn_fcget(): could not get the value of '%s'\n", path); return value; }
struct spi_ep *ep_new_pkt(struct spi_source *source, spi_epaddr_t epa, const struct timeval *ts, void *data, uint32_t size) { struct spi *spi = source->spi; struct spi_ep *ep; char *key; struct spi_pkt *pkt; mmatic *mm; key = _k(source, epa); ep = thash_get(spi->eps, key); if (!ep) { mm = mmatic_create(); ep = mmatic_zalloc(mm, sizeof *ep); ep->mm = mm; ep->source = source; ep->epa = epa; thash_set(spi->eps, key, ep); source->eps++; dbg(8, "new ep %s\n", spi_epa2a(epa)); } /* make packet */ pkt = mmatic_zalloc(ep->mm, sizeof *pkt); pkt->size = size; pkt->payload = mmatic_zalloc(ep->mm, spi->options.N); memcpy(pkt->payload, data, spi->options.N); memcpy(&pkt->ts, ts, sizeof(struct timeval)); /* update last packet time */ memcpy(&ep->last, ts, sizeof(struct timeval)); /* store packet */ if (!ep->pkts) ep->pkts = tlist_create(mmatic_free, ep->mm); tlist_push(ep->pkts, pkt); /* generate event if pkts big enough */ if (ep->gclock1 == 0 && tlist_count(ep->pkts) >= spi->options.C) { ep->gclock1++; spi_announce(spi, "endpointPacketsReady", 0, ep, false); dbg(7, "ep %s ready\n", spi_epa2a(epa)); } return ep; }
long _likely_trace(bool cond, bool expect, const char *condstr, const char *file, unsigned int line) { struct trace *p, trace; init_trace(&trace, condstr, file, line, expect); p = thash_get(&htable, &trace); if (!p) p = add_trace(&trace); p->count++; if (cond == expect) p->right++; return cond; }
/** Common part of request parser, usually after JSON representation is made available in req->params * @param req the request * @param leave leave the req->params */ static bool common(struct req *req, bool leave) { ut *ut; /* guarantee that req->params is ok */ if (!ut_ok(req->params)) { req->reply = req->params; req->params = NULL; return false; } else if (ut_type(req->params) != T_HASH) { return errcode(JSON_RPC_INVALID_REQUEST); } /* JSON-RPC argument check */ if ((ut = uth_get(req->params, "service"))) /* used by Qooxdoo */ req->service = ut_char(ut); if ((ut = uth_get(req->params, "method"))) req->method = ut_char(ut); if ((ut = uth_get(req->params, "id"))) req->id = ut_char(ut); /* XXX: dont check jsonrpc=2.0 */ if (!leave) { if ((ut = uth_get(req->params, "params"))) req->params = ut; else req->params = uth_set_thash(req->params, "params", NULL); /* create empty hash */ if (ut_is_tlist(req->params) && thash_get(req->http.headers, "X-Qooxdoo-Response-Type")) { tlist *tl = ut_tlist(req->params); req->params = tlist_shift(tl); } } return true; }
bool readhttp(struct req *req) { enum http_type ht; char first[256], buf[BUFSIZ], *ct, *cl, *ac, *uri, *auth; int len; xstr *xs = xstr_create("", req); /* read query */ if (!fgets(first, sizeof(first), stdin) || first[0] == '\n') exit(0); /* eof */ if (strncmp(first, "POST ", 5) == 0) { ht = POST; uri = first + 5; } else if (strncmp(first, "OPTIONS ", 8) == 0) { ht = OPTIONS; uri = first + 8; } else if (strncmp(first, "GET ", 4) == 0 && O.http.htdocs) { /* @1 */ ht = GET; uri = first + 4; } else { dbg(4, "invalid method: %s\n", first); return errmsg("Invalid HTTP method"); } /* read headers */ while (fgets(buf, sizeof(buf), stdin)) { if (!buf[0] || buf[0] == '\n' || buf[0] == '\r') break; xstr_append(xs, buf); } req->http.headers = rfc822_parse(xstr_string(xs), req); /* fetch authentication information ASAP */ auth = thash_get(req->http.headers, "Authorization"); if (auth && strncmp(auth, "Basic ", 6) == 0) { xstr *ad = asn_b64_dec(auth+6, req); char *pass = strchr(xstr_string(ad), ':'); if (pass) { *pass++ = '\0'; req->http.user = xstr_string(ad); req->http.pass = pass; } } const char *cc = thash_get(req->http.headers, "Connection"); if (cc && (streq(cc, "close") || streq(cc, "Close"))) req->last = true; if (ht == OPTIONS) return errcode(JSON_RPC_HTTP_OPTIONS); /* handle static query, note that htdocs!=NULL checked @1 */ if (ht == GET) { req->http.needauth = true; char *space = strchr(uri, ' '); if (space) *space = '\0'; char *params = strchr(uri, '?'); if (params) *params = '\0'; if (streq(uri, "/")) { uri = "/index.html"; } else if (strstr(uri, "..")) { dbg(4, "invalid uri: '%s'\n", uri); return errcode(JSON_RPC_HTTP_NOT_FOUND); } req->http.uripath = mmatic_printf(req, "%s%s", O.http.htdocs, uri); if (asn_isfile(req->http.uripath) > 0) { dbg(4, "GET '%s'\n", req->http.uripath); return errcode(JSON_RPC_HTTP_GET); } dbg(4, "not found: '%s'\n", uri); req->http.uripath = NULL; return errcode(JSON_RPC_HTTP_NOT_FOUND); } /* = POST - ie. normal RPC call = */ ct = thash_get(req->http.headers, "Content-Type"); if (!ct) return errmsg("Content-Type needed"); if (strncmp(ct, "application/json", 16) != 0) return errmsg("Unsupported Content-Type"); ac = thash_get(req->http.headers, "Accept"); if (!ac) return errmsg("Accept needed"); if (!strstr(ac, "application/json") && !strstr(ac, "*/*")) return errmsg("Unsupported Accept"); /* read the query */ cl = thash_get(req->http.headers, "Content-Length"); if (!cl) return errmsg("Content-Length needed"); len = atoi(cl); if (len < 0) return errmsg("Unsupported Content-Length"); return readjson_len(req, len); }
static void pkt(struct lfc *lfc, void *pdata, struct lfc_flow *lf, void *data, double ts, bool up, bool is_new, libtrace_packet_t *pkt) { struct flow *f = data; char *name, *uri; libtrace_out_t *out; if (f->ignore) return; if (is_new) { /* find the flow by its id in the ARFF file, get output file name */ name = thash_uint_get(fd->cache, lf->id); if (!name) { cache_update(); name = thash_uint_get(fd->cache, lf->id); if (!name) { f->ignore = true; thash_uint_set(fd->cache, lf->id, NULL); return; } } /* ignore flows with column values we are not interested in */ if (fd->value && !streq(fd->value, name)) { f->ignore = true; thash_uint_set(fd->cache, lf->id, NULL); return; } /* get libtrace output file */ out = thash_get(fd->out_files, name); if (!out) { uri = mmatic_sprintf(fd->mm, "pcap:%s/%s.pcap", fd->dir, name); out = trace_create_output(uri); if (!out) { cleanup(); die("trace_create_output(%s) failed\n", uri); } if (trace_is_err_output(out)) { trace_perror_output(out, "Opening output trace file"); cleanup(); die("trace_create_output(%s) failed\n", uri); } if (trace_start_output(out) == -1) { trace_perror_output(out, "Starting output trace"); cleanup(); die("trace_start_output(%s) failed\n", uri); } thash_set(fd->out_files, name, out); } f->out = out; /* remove id from cache */ thash_uint_set(fd->cache, lf->id, NULL); } trace_write_packet(f->out, pkt); if (trace_is_err_output(f->out)) { trace_perror_output(f->out, "Writing packet to output trace file"); cleanup(); die("trace_write_packet() failed\n"); } }
static inline struct spi_flow *_get_flow(struct spi_source *source, spi_epaddr_t src, spi_epaddr_t dst) { return thash_get(source->spi->flows, _k(source, src, dst)); }