Harald Welte | b3487b1 | 2012-11-15 00:12:56 +0100 | [diff] [blame^] | 1 | /* read PCAP file with meas_feed data and write it to sqlite3 database */ |
| 2 | |
| 3 | /* (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <errno.h> |
| 24 | #include <unistd.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include <netinet/in.h> |
| 29 | #include <netinet/ip.h> |
| 30 | #include <netinet/udp.h> |
| 31 | |
| 32 | #include <cdk/cdk.h> |
| 33 | |
| 34 | #include <osmocom/core/socket.h> |
| 35 | #include <osmocom/core/utils.h> |
| 36 | #include <osmocom/core/msgb.h> |
| 37 | #include <osmocom/core/select.h> |
| 38 | #include <osmocom/core/talloc.h> |
| 39 | |
| 40 | #include <osmocom/gsm/gsm_utils.h> |
| 41 | |
| 42 | #include <openbsc/meas_feed.h> |
| 43 | |
| 44 | #include <pcap/pcap.h> |
| 45 | |
| 46 | #include "meas_db.h" |
| 47 | |
| 48 | static struct meas_db_state *db; |
| 49 | |
| 50 | static void handle_mfm(const struct pcap_pkthdr *h, |
| 51 | const struct meas_feed_meas *mfm) |
| 52 | { |
| 53 | const char *scenario; |
| 54 | |
| 55 | if (strlen(mfm->scenario)) |
| 56 | scenario = mfm->scenario; |
| 57 | else |
| 58 | scenario = NULL; |
| 59 | |
| 60 | meas_db_insert(db, mfm->imsi, mfm->name, h->ts.tv_sec, |
| 61 | scenario, &mfm->mr); |
| 62 | } |
| 63 | |
| 64 | static void pcap_cb(u_char *user, const struct pcap_pkthdr *h, |
| 65 | const u_char *bytes) |
| 66 | { |
| 67 | const char *cur = bytes; |
| 68 | const struct iphdr *ip; |
| 69 | const struct udphdr *udp; |
| 70 | const struct meas_feed_meas *mfm; |
| 71 | uint16_t udplen; |
| 72 | |
| 73 | if (h->caplen < 14+20+8) |
| 74 | return; |
| 75 | |
| 76 | /* Check if there is IPv4 in the Ethernet */ |
| 77 | if (cur[12] != 0x08 || cur[13] != 0x00) |
| 78 | return; |
| 79 | |
| 80 | cur += 14; /* ethernet header */ |
| 81 | ip = (struct iphdr *) cur; |
| 82 | |
| 83 | if (ip->version != 4) |
| 84 | return; |
| 85 | cur += ip->ihl * 4; |
| 86 | |
| 87 | if (ip->protocol != IPPROTO_UDP) |
| 88 | return; |
| 89 | |
| 90 | udp = (struct udphdr *) cur; |
| 91 | |
| 92 | if (udp->dest != htons(8888)) |
| 93 | return; |
| 94 | |
| 95 | udplen = ntohs(udp->len); |
| 96 | if (udplen != sizeof(*udp) + sizeof(*mfm)) |
| 97 | return; |
| 98 | cur += sizeof(*udp); |
| 99 | |
| 100 | mfm = (const struct meas_feed_meas *) cur; |
| 101 | |
| 102 | handle_mfm(h, mfm); |
| 103 | } |
| 104 | |
| 105 | int main(int argc, char **argv) |
| 106 | { |
| 107 | char errbuf[PCAP_ERRBUF_SIZE+1]; |
| 108 | char *pcap_fname, *db_fname; |
| 109 | pcap_t *pc; |
| 110 | int rc; |
| 111 | |
| 112 | if (argc < 3) { |
| 113 | fprintf(stderr, "You need to specify PCAP and database file\n"); |
| 114 | exit(2); |
| 115 | } |
| 116 | |
| 117 | pcap_fname = argv[1]; |
| 118 | db_fname = argv[2]; |
| 119 | |
| 120 | pc = pcap_open_offline(pcap_fname, errbuf); |
| 121 | if (!pc) { |
| 122 | fprintf(stderr, "Cannot open %s: %s\n", pcap_fname, errbuf); |
| 123 | exit(1); |
| 124 | } |
| 125 | |
| 126 | db = meas_db_open(NULL, db_fname); |
| 127 | if (!db) |
| 128 | exit(0); |
| 129 | |
| 130 | rc = meas_db_begin(db); |
| 131 | if (rc < 0) { |
| 132 | fprintf(stderr, "Error during BEGIN\n"); |
| 133 | exit(1); |
| 134 | } |
| 135 | |
| 136 | pcap_loop(pc, 0 , pcap_cb, NULL); |
| 137 | |
| 138 | meas_db_commit(db); |
| 139 | |
| 140 | exit(0); |
| 141 | } |