tor_multipath_voip/src/cap_utils.c

44 lines
1.2 KiB
C
Raw Normal View History

2019-06-03 12:32:52 +00:00
#include "cap_utils.h"
void cap_load(struct cap_file *cf, char* filename) {
if ((cf->fd = fopen(filename, "r")) == NULL) {
perror("Unable to open file");
exit(EXIT_FAILURE);
}
2019-06-03 15:09:40 +00:00
cf->filename = filename;
2019-06-03 12:32:52 +00:00
fseek(cf->fd, 0, SEEK_END);
cf->sz = ftell(cf->fd);
fseek(cf->fd, 0, SEEK_SET);
2019-06-03 15:09:40 +00:00
cf->sz_obj = cf->sz / sizeof(struct buffer_packet);
2019-06-03 12:32:52 +00:00
}
void cap_next_bp(struct cap_file *cf, struct buffer_packet* bp) {
fread(bp, sizeof(struct buffer_packet), 1, cf->fd);
}
2019-06-03 15:09:40 +00:00
void cap_npeek_bp(struct cap_file *cf, int c, struct buffer_packet* bp) {
//fprintf(stdout, "c: %d, current pos: %ld, ", c, ftell(cf->fd));
ssize_t cbytes = c*sizeof(struct buffer_packet);
fseek(cf->fd, cbytes, SEEK_CUR);
2019-06-03 12:32:52 +00:00
size_t n = fread(bp, sizeof(struct buffer_packet), 1, cf->fd);
2019-06-03 15:09:40 +00:00
fseek(cf->fd, -(n*sizeof(struct buffer_packet) + cbytes), SEEK_CUR);
//fprintf(stdout, "cbytes: %ld, n: %ld, final pos: %ld\n", cbytes, n, ftell(cf->fd));
}
void cap_peek_bp(struct cap_file *cf, struct buffer_packet* bp) {
cap_npeek_bp(cf, 0, bp);
2019-06-03 12:32:52 +00:00
}
size_t cap_count_bp(struct cap_file *cf) {
return cf->sz / sizeof(struct buffer_packet);
}
void cap_unload(struct cap_file *cf) {
fclose(cf->fd);
}
2019-06-18 15:01:37 +00:00
void cap_begin(struct cap_file *cf) {
fseek(cf->fd, 0L, SEEK_SET);
}