Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 1 | #include <time.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/stat.h> |
| 7 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 8 | #include <osmocom/core/msgb.h> |
Harald Welte | dbb0f5a | 2016-10-18 23:51:54 +0200 | [diff] [blame] | 9 | #include <osmocom/core/talloc.h> |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 10 | #include <osmocom/abis/e1_input.h> |
| 11 | |
| 12 | #include "storage.h" |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 13 | #include "recorder.h" |
| 14 | |
| 15 | static int g_out_fd = -1;; |
| 16 | static uint64_t g_written_bytes; |
| 17 | |
| 18 | static const char *storage_gen_filename(void) |
| 19 | { |
| 20 | static char buf[32]; |
| 21 | time_t t; |
| 22 | struct tm *tmp; |
| 23 | |
| 24 | t = time(NULL); |
| 25 | tmp = localtime(&t); |
| 26 | strftime(buf, sizeof(buf), "%Y%m%d%H%M%S.e1cap", tmp); |
| 27 | return buf; |
| 28 | } |
| 29 | |
| 30 | static int storage_reopen_if_needed(void) |
| 31 | { |
| 32 | if (g_written_bytes / (1024*1024) >= g_recorder.max_file_size_mb) { |
| 33 | close(g_out_fd); |
| 34 | g_out_fd = -1; |
| 35 | } |
| 36 | |
| 37 | if (g_out_fd < 0) { |
| 38 | int rc; |
| 39 | const char *fname = storage_gen_filename(); |
| 40 | rc = chdir(g_recorder.storage_path); |
| 41 | if (rc < 0) { |
| 42 | LOGP(DMAIN, LOGL_ERROR, "Unable to chdir(%s): %s\n", |
| 43 | g_recorder.storage_path, strerror(errno)); |
| 44 | return -1; |
| 45 | } |
| 46 | g_out_fd = open(fname, O_WRONLY|O_CREAT, 0664); |
| 47 | if (g_out_fd < 0) { |
| 48 | LOGP(DMAIN, LOGL_ERROR, "Unable to open(%s): %s\n", |
| 49 | fname, strerror(errno)); |
| 50 | } |
| 51 | g_written_bytes = 0; |
| 52 | } |
| 53 | |
| 54 | return g_out_fd; |
| 55 | } |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 56 | |
| 57 | int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode) |
| 58 | { |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 59 | struct osmo_e1cap_pkthdr _h, *h = &_h; |
| 60 | int rc; |
| 61 | struct iovec iov[2] = { |
| 62 | { |
| 63 | .iov_base = h, |
| 64 | .iov_len = sizeof(*h), |
| 65 | }, { |
| 66 | .iov_base = msg->data, |
| 67 | .iov_len = msg->len, |
| 68 | } |
| 69 | }; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 70 | |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 71 | h->len = htonl(msg->len); |
| 72 | gettimeofday(&h->ts, NULL); |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 73 | h->line_nr = ts->line->num; |
| 74 | h->ts_nr = ts->num; |
| 75 | h->capture_mode = mode; |
| 76 | h->flags = 0; |
| 77 | |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 78 | storage_reopen_if_needed(); |
| 79 | |
| 80 | rc = writev(g_out_fd, iov, ARRAY_SIZE(iov)); |
| 81 | if (rc < 0) |
| 82 | return rc; |
| 83 | |
| 84 | g_written_bytes += rc; |
| 85 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Harald Welte | dbb0f5a | 2016-10-18 23:51:54 +0200 | [diff] [blame] | 88 | |
| 89 | /* reading */ |
| 90 | |
| 91 | |
| 92 | struct osmo_e1cap_file { |
| 93 | int fd; |
| 94 | }; |
| 95 | |
| 96 | #define OSMO_E1REC_ALLOC_SIZE 1024 |
| 97 | |
| 98 | struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path) |
| 99 | { |
| 100 | struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file); |
| 101 | if (!f) |
| 102 | return NULL; |
| 103 | |
| 104 | f->fd = open(path, O_RDONLY); |
| 105 | if (f->fd < 0) { |
| 106 | talloc_free(f); |
| 107 | return NULL; |
| 108 | } |
| 109 | return f; |
| 110 | } |
| 111 | |
| 112 | struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f) |
| 113 | { |
| 114 | struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE); |
| 115 | int rc; |
| 116 | |
| 117 | if (!pkt) |
| 118 | return NULL; |
| 119 | |
| 120 | /* read header */ |
| 121 | rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt)); |
| 122 | if (rc < sizeof(*pkt)) { |
| 123 | talloc_free(pkt); |
| 124 | return NULL; |
| 125 | } |
| 126 | pkt->len = ntohl(pkt->len); |
Harald Welte | dbb0f5a | 2016-10-18 23:51:54 +0200 | [diff] [blame] | 127 | /* read data */ |
| 128 | if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) { |
| 129 | pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len); |
| 130 | if (!pkt) |
| 131 | return NULL; |
| 132 | } |
| 133 | rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len); |
| 134 | if (rc < pkt->len) { |
| 135 | talloc_free(pkt); |
| 136 | return NULL; |
| 137 | } |
| 138 | return pkt; |
| 139 | } |