error_code_t print_mem(lnf_mem_t *mem, size_t limit) { lnf_rec_t *rec; //record = line size_t rec_cntr = 0; //aka lines counter lnf_mem_cursor_t *cursor; //current record (line) cursor size_t fld_max_size = 0; //maximum data size length in bytes size_t data_max_strlen[LNF_FLD_TERM_] = {0}; //maximum data string len if (output_params.print_records != OUTPUT_ITEM_YES) { return E_OK; } first_item = first_item ? false : (putchar('\n'), false); secondary_errno = lnf_rec_init(&rec); if (secondary_errno != LNF_OK) { print_err(E_LNF, secondary_errno, "lnf_rec_init()"); return E_LNF; } /* * Find out maximum data type size of present fields, length of headers * and last present field ID. */ for (size_t i = 0; i < fields_cnt; ++i) { size_t header_str_len = strlen(field_get_name(fields[i].id)); MAX_ASSIGN(fld_max_size, fields[i].size); MAX_ASSIGN(data_max_strlen[fields[i].id], header_str_len); } /* Find out max data length, converted to string. */ lnf_mem_first_c(mem, &cursor); while (cursor != NULL) { //row loop char buff[fld_max_size]; lnf_mem_read_c(mem, cursor, rec); for (size_t i = 0; i < fields_cnt; ++i) { //column loop size_t data_str_len; //XXX: lnf_rec_fget() may return LNF_ERR_UNKFLD even if //field is present (e.g. if duration is zero). lnf_rec_fget(rec, fields[i].id, buff); data_str_len = strlen(field_to_str(fields[i].id, buff)); MAX_ASSIGN(data_max_strlen[fields[i].id], data_str_len); } if (++rec_cntr == limit) { break; } lnf_mem_next_c(mem, &cursor); } rec_cntr = 0; /* Actual printing: header. */ for (size_t i = 0; i < fields_cnt; ++i) { //column loop print_field(field_get_name(fields[i].id), data_max_strlen[fields[i].id], PRETTY_PRINT_COL_WIDTH, i == (fields_cnt - 1)); } /* Actual printing: field data converted to string. */ lnf_mem_first_c(mem, &cursor); while (cursor != NULL) { //row loop char buff[fld_max_size]; lnf_mem_read_c(mem, cursor, rec); for (size_t i = 0; i < fields_cnt; ++i) { //column loop //XXX: see above lnf_rec_fget() lnf_rec_fget(rec, fields[i].id, buff); print_field(field_to_str(fields[i].id, buff), data_max_strlen[fields[i].id], PRETTY_PRINT_COL_WIDTH, i == (fields_cnt - 1)); } if (++rec_cntr == limit) { break; } lnf_mem_next_c(mem, &cursor); } lnf_rec_free(rec); return E_OK; }
int main(int argc, char **argv) { lnf_file_t *filep; lnf_rec_t *recp; lnf_mem_t *memp; lnf_mem_cursor_t *cursor; lnf_brec1_t brec; int i = 0; int print = 1; int printa = 1; char *filename = FILENAME; char c; while ((c = getopt (argc, argv, "pPAf:")) != -1) { switch (c) { case 'p': print = 0; break; case 'P': print = 0; break; case 'f': filename = optarg; break; case 'A': printa = 0; break; case '?': printf("Usage: %s [ -P ] [ -A ] [ -f <input file name> ] \n", argv[0]); printf(" -P : do not print input records to stdout\n"); printf(" -A : do not aggregated records to stdout\n"); exit(1); } } if (lnf_open(&filep, filename, LNF_READ, NULL) != LNF_OK) { fprintf(stderr, "Can not open file %s\n", filename); exit(1); } lnf_rec_init(&recp); lnf_mem_init(&memp); /* set rules for aggregation srcip/24,srcport,dstas */ lnf_mem_fadd(memp, LNF_FLD_SRCADDR, LNF_AGGR_KEY|LNF_SORT_DESC, 24, 64); lnf_mem_fadd(memp, LNF_FLD_SRCPORT, LNF_AGGR_KEY, 0, 0); lnf_mem_fadd(memp, LNF_FLD_DSTAS, LNF_AGGR_KEY, 0, 0); lnf_mem_fadd(memp, LNF_FLD_FIRST, LNF_AGGR_MIN, 0, 0); lnf_mem_fadd(memp, LNF_FLD_LAST, LNF_AGGR_MAX, 0, 0); lnf_mem_fadd(memp, LNF_FLD_TCP_FLAGS, LNF_AGGR_OR, 0, 0); lnf_mem_fadd(memp, LNF_FLD_DOCTETS, LNF_AGGR_SUM, 0, 0); lnf_mem_fadd(memp, LNF_FLD_DPKTS, LNF_AGGR_SUM, 0, 0); while (lnf_read(filep, recp) != LNF_EOF) { i++; /* add to memory heap */ lnf_mem_write(memp,recp); if (print) { char sbuf[INET6_ADDRSTRLEN]; char dbuf[INET6_ADDRSTRLEN]; lnf_rec_fget(recp, LNF_FLD_BREC1, &brec); inet_ntop(AF_INET6, &brec.srcaddr, sbuf, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &brec.dstaddr, dbuf, INET6_ADDRSTRLEN); printf(" %s :%d -> %s :%d %llu %llu %llu\n", sbuf, brec.srcport, dbuf, brec.dstport, (LLUI)brec.first, (LLUI)brec.bytes, (LLUI)brec.pkts); } } printf("Total input records: %d\n", i); i = 0; printf("First read\n"); lnf_mem_first_c(memp, &cursor); while (cursor != NULL) { i++; lnf_mem_read_c(memp, cursor, recp); if (printa) { char sbuf[INET6_ADDRSTRLEN]; char dbuf[INET6_ADDRSTRLEN]; lnf_rec_fget(recp, LNF_FLD_BREC1, &brec); inet_ntop(AF_INET6, &brec.srcaddr, sbuf, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &brec.dstaddr, dbuf, INET6_ADDRSTRLEN); printf(" %s :%d -> %s :%d %llu %llu %llu\n", sbuf, brec.srcport, dbuf, brec.dstport, (LLUI)brec.first, (LLUI)brec.bytes, (LLUI)brec.pkts); } lnf_mem_next_c(memp, &cursor); } printf("Second read\n"); lnf_mem_first_c(memp, &cursor); while (cursor != NULL) { i++; lnf_mem_read_c(memp, cursor, recp); if (printa) { char sbuf[INET6_ADDRSTRLEN]; char dbuf[INET6_ADDRSTRLEN]; lnf_rec_fget(recp, LNF_FLD_BREC1, &brec); inet_ntop(AF_INET6, &brec.srcaddr, sbuf, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &brec.dstaddr, dbuf, INET6_ADDRSTRLEN); printf(" %s :%d -> %s :%d %llu %llu %llu\n", sbuf, brec.srcport, dbuf, brec.dstport, (LLUI)brec.first, (LLUI)brec.bytes, (LLUI)brec.pkts); } lnf_mem_next_c(memp, &cursor); } printf("Total aggregated records: %d\n", i); lnf_mem_free(memp); lnf_rec_free(recp); lnf_close(filep); return 0; }
int main(int argc, char **argv) { lnf_file_t *filep; lnf_rec_t *recp; lnf_mem_t *memp1; lnf_mem_t *memp2; lnf_mem_cursor_t *cursor; lnf_brec1_t brec; int i = 0; int print = 1; int printa = 1; char *filename = FILENAME; int c; char buff[LNF_MAX_RAW_LEN]; int datasize; while ((c = getopt (argc, argv, "pPAf:")) != -1) { switch (c) { case 'p': print = 0; break; case 'P': print = 0; break; case 'f': filename = optarg; break; case 'A': printa = 0; break; case '?': printf("Usage: %s [ -P ] [ -A ] [ -f <input file name> ] \n", argv[0]); printf(" -P : do not print input records to stdout\n"); printf(" -A : do not aggregated records to stdout\n"); exit(1); } } if (lnf_open(&filep, filename, LNF_READ, NULL) != LNF_OK) { fprintf(stderr, "Can not open file %s\n", filename); exit(1); } lnf_rec_init(&recp); lnf_mem_init(&memp1); lnf_mem_init(&memp2); lnf_mem_fastaggr(memp1, LNF_FAST_AGGR_BASIC); lnf_mem_fastaggr(memp2, LNF_FAST_AGGR_BASIC); /* set rules for aggregation srcip/24,srcport,dstas */ lnf_mem_fadd(memp1, LNF_FLD_SRCADDR, LNF_AGGR_KEY|LNF_SORT_DESC, 24, 64); lnf_mem_fadd(memp2, LNF_FLD_SRCADDR, LNF_AGGR_KEY|LNF_SORT_DESC, 24, 64); while (lnf_read(filep, recp) != LNF_EOF) { i++; /* add to memory heap */ lnf_mem_write(memp1, recp); if (print) { char sbuf[INET6_ADDRSTRLEN]; char dbuf[INET6_ADDRSTRLEN]; lnf_rec_fget(recp, LNF_FLD_BREC1, &brec); inet_ntop(AF_INET6, &brec.srcaddr, sbuf, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &brec.dstaddr, dbuf, INET6_ADDRSTRLEN); printf(" %s :%d -> %s :%d %llu %llu %llu\n", sbuf, brec.srcport, dbuf, brec.dstport, (LLUI)brec.first, (LLUI)brec.bytes, (LLUI)brec.pkts); } } printf("Total input records: %d\n", i); /* transfer data from memp1 to memp2 */ lnf_mem_first_c(memp1, &cursor); while (cursor != NULL) { lnf_mem_read_raw_c(memp1, cursor, buff, &datasize, LNF_MAX_RAW_LEN); lnf_mem_write_raw(memp2, buff, datasize); lnf_mem_next_c(memp1, &cursor); } /* all data are now in memp2) */ i = 0; lnf_mem_first_c(memp2, &cursor); while (cursor != NULL) { i++; lnf_mem_read_c(memp2, cursor, recp); if (printa) { char sbuf[INET6_ADDRSTRLEN]; char dbuf[INET6_ADDRSTRLEN]; lnf_rec_fget(recp, LNF_FLD_BREC1, &brec); inet_ntop(AF_INET6, &brec.srcaddr, sbuf, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &brec.dstaddr, dbuf, INET6_ADDRSTRLEN); printf(" %s :%d -> %s :%d %llu %llu %llu\n", sbuf, brec.srcport, dbuf, brec.dstport, (LLUI)brec.first, (LLUI)brec.bytes, (LLUI)brec.pkts); } lnf_mem_next_c(memp2, &cursor); } printf("Total aggregated records: %d\n", i); lnf_mem_free(memp1); lnf_mem_free(memp2); lnf_rec_free(recp); lnf_close(filep); return 0; }