static int simple_cat(void) { uint16_t type; void *data; int i, rc; for(i=0; i<infile_cnt; i++) { while((rc = scamper_file_read(infiles[i], filter, &type, &data)) == 0) { /* EOF */ if(data == NULL) break; if(write_obj(type, data) != 0) { return -1; } } /* error when reading the input file */ if(rc != 0) { return -1; } scamper_file_close(infiles[i]); infiles[i] = NULL; } return 0; }
int main(int argc, char *argv[]) { scamper_file_t *file[2]; scamper_file_filter_t *filter; scamper_trace_t *trace; tracepair_t *pair, fm; uint16_t type = SCAMPER_FILE_OBJ_TRACE; char buf[256]; int i, filec_open; #ifdef _WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); #endif if(check_options(argc, argv) != 0) goto err; if((filter = scamper_file_filter_alloc(&type, 1)) == NULL) { fprintf(stderr, "could not allocate filter\n"); goto err; } memset(file, 0, sizeof(file)); for(i=0; i<filec; i++) { if((file[i] = scamper_file_open(files[i], 'r', NULL)) == NULL) { fprintf(stderr, "could not open %s\n", files[i]); goto err; } } filec_open = filec; if((pairs = splaytree_alloc(tracepair_cmp)) == NULL) { fprintf(stderr, "could not alloc tracepair tree\n"); goto err; } splaytree_onremove(pairs, (splaytree_onremove_t)tracepair_onremove); while(filec_open != 0) { for(i=0; i<filec; i++) { if(file[i] == NULL) continue; if(scamper_file_read(file[i], filter, &type, (void *)&trace) != 0) { fprintf(stderr, "could not read from %s\n", files[i]); goto err; } if(trace == NULL) { filec_open--; scamper_file_close(file[i]); continue; } assert(type == SCAMPER_FILE_OBJ_TRACE); fm.tracec = 1; fm.traces[0] = trace; if((pair = splaytree_find(pairs, &fm)) == NULL) { if((pair = malloc_zero(sizeof(tracepair_t))) == NULL) goto err; pair->traces[i] = trace; pair->tracec = 1; if((pair->node = splaytree_insert(pairs, pair)) == NULL) goto err; } else { if(pair->traces[i] != NULL) { fprintf(stderr, "repeated trace for %s\n", scamper_addr_tostr(trace->dst, buf, sizeof(buf))); goto err; } pair->traces[i] = trace; pair->tracec++; } if(pair->tracec != filec) continue; splaytree_remove_node(pairs, pair->node); tracepair_process(pair); tracepair_free(pair); } } return 0; err: return -1; }
int main(int argc, char *argv[]) { uint16_t types[] = { SCAMPER_FILE_OBJ_CYCLE_START, SCAMPER_FILE_OBJ_CYCLE_STOP, SCAMPER_FILE_OBJ_PING, SCAMPER_FILE_OBJ_TRACE, SCAMPER_FILE_OBJ_DEALIAS, SCAMPER_FILE_OBJ_TBIT, }; scamper_file_t *in, *out; scamper_file_filter_t *filter; char **files = NULL; int filec; uint16_t type; void *data; int i; if((out = scamper_file_openfd(STDOUT_FILENO, NULL, 'w', "json")) == NULL) { fprintf(stderr, "could not associate stdout\n"); return -1; } filter = scamper_file_filter_alloc(types, sizeof(types)/sizeof(uint16_t)); if(filter == NULL) { fprintf(stderr, "could not allocate filter\n"); return -1; } filec = argc - 1; if(filec > 0) files = argv + 1; for(i=0; i<=filec; i++) { if(filec == 0) { if((in = scamper_file_openfd(STDIN_FILENO,"-",'r',"warts")) == NULL) { fprintf(stderr, "could not use stdin\n"); return -1; } } else if(i < filec) { if((in = scamper_file_open(files[i], 'r', NULL)) == NULL) { fprintf(stderr, "could not open %s: %s\n", files[i], strerror(errno)); return -1; } } else break; while(scamper_file_read(in, filter, &type, (void *)&data) == 0) { if(data == NULL) break; /* EOF */ if(scamper_file_write_obj(out, type, data) != 0) return -1; if(type == SCAMPER_FILE_OBJ_PING) scamper_ping_free(data); else if(type == SCAMPER_FILE_OBJ_TRACE) scamper_trace_free(data); else if(type == SCAMPER_FILE_OBJ_DEALIAS) scamper_dealias_free(data); else if(type == SCAMPER_FILE_OBJ_TBIT) scamper_tbit_free(data); } scamper_file_close(in); } scamper_file_filter_free(filter); scamper_file_close(out); return 0; }
static int sort_cat_fill(heap_t *heap, sort_struct_t *s) { int i = s->file; if(scamper_file_read(infiles[i], filter, &s->type, &s->data) == 0) { /* EOF */ if(s->data == NULL) { scamper_file_close(infiles[i]); infiles[i] = NULL; return 0; } switch(s->type) { case SCAMPER_FILE_OBJ_TRACELB: timeval_cpy(&s->tv, &((scamper_tracelb_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_TRACE: timeval_cpy(&s->tv, &((scamper_trace_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_PING: timeval_cpy(&s->tv, &((scamper_ping_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_DEALIAS: timeval_cpy(&s->tv, &((scamper_dealias_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_TBIT: timeval_cpy(&s->tv, &((scamper_tbit_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_NEIGHBOURDISC: timeval_cpy(&s->tv, &((scamper_neighbourdisc_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_STING: timeval_cpy(&s->tv, &((scamper_sting_t *)s->data)->start); break; case SCAMPER_FILE_OBJ_CYCLE_START: s->tv.tv_sec = ((scamper_cycle_t *)s->data)->start_time; s->tv.tv_usec = 0; break; case SCAMPER_FILE_OBJ_CYCLE_STOP: s->tv.tv_sec = ((scamper_cycle_t *)s->data)->stop_time; s->tv.tv_usec = 1000000; break; } if(heap_insert(heap, s) == NULL) { return -1; } } else return -1; return 0; }