void port_add(struct sock *ss, bool local) { struct port *sp; thash *ports; pthread_mutex_lock(&ss->td->mutex_ports); if (ss->type == SOCK_DGRAM) ports = ss->td->udp_ports; else if (ss->type == SOCK_STREAM) ports = ss->td->tcp_ports; else die("invalid ss->type\n"); sp = thash_uint_get(ports, ss->port); if (!sp) { sp = mmatic_zalloc(ss->td->mm, sizeof *sp); thash_uint_set(ports, ss->port, sp); dbg(3, "port %d/%s added\n", ss->port, ss->type == SOCK_STREAM ? "TCP" : "UDP"); } gettimeofday(&sp->since, NULL); sp->local = local; sp->socknum = ss->socknum; pthread_mutex_unlock(&ss->td->mutex_ports); }
/* NOTE: it might be necessary to time-out nd->ids entries in order to decrease memory consumption */ static struct ipoque_id_struct *getid(struct ndpi *nd, struct lfc_flow_addr *lfa) { struct ipoque_id_struct *id; id = thash_uint_get(nd->ids, (uint32_t) lfa->addr.ip4.s_addr); if (!id) { id = mmatic_zalloc(nd->mm, ipoque_detection_get_sizeof_ipoque_id_struct()); thash_uint_set(nd->ids, (uint32_t) lfa->addr.ip4.s_addr, id); } return id; }
struct pid *pid_get(struct tracedump *td, pid_t pid) { struct pid *sp; /* speed-up cache */ if (td->sp && td->sp->pid == pid) return td->sp; sp = thash_uint_get(td->pids, pid); if (!sp) { dbg(7, "new pid %d\n", pid); sp = mmatic_zalloc(td->mm, sizeof *sp); sp->td = td; sp->pid = pid; thash_uint_set(td->pids, pid, sp); } return sp; }
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"); } }