Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 1 | #include <osmocom/simtrace2/gsmtap.h> |
| 2 | |
| 3 | #include <osmocom/core/gsmtap.h> |
| 4 | #include <osmocom/core/gsmtap_util.h> |
| 5 | |
| 6 | #include <errno.h> |
| 7 | #include <string.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdio.h> |
| 10 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 11 | /*! global GSMTAP instance */ |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 12 | static struct gsmtap_inst *g_gti; |
| 13 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 14 | /*! initialize the global GSMTAP instance for SIM traces */ |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 15 | int osmo_st2_gsmtap_init(const char *gsmtap_host) |
| 16 | { |
| 17 | if (g_gti) |
| 18 | return -EEXIST; |
| 19 | |
| 20 | g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0); |
| 21 | if (!g_gti) { |
| 22 | perror("unable to open GSMTAP"); |
| 23 | return -EIO; |
| 24 | } |
| 25 | gsmtap_source_add_sink(g_gti); |
| 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 30 | /*! log one APDU via the global GSMTAP instance. |
| 31 | * \param[in] sub_type GSMTAP sub-type (GSMTAP_SIM_* constant) |
| 32 | * \param[in] apdu User-provided buffer with APDU to log |
| 33 | * \param[in] len Length of apdu in bytes |
| 34 | */ |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 35 | int osmo_st2_gsmtap_send_apdu(uint8_t sub_type, const uint8_t *apdu, unsigned int len) |
| 36 | { |
| 37 | struct gsmtap_hdr *gh; |
| 38 | unsigned int gross_len = len + sizeof(*gh); |
| 39 | uint8_t *buf = malloc(gross_len); |
| 40 | int rc; |
| 41 | |
| 42 | if (!buf) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | memset(buf, 0, sizeof(*gh)); |
| 46 | gh = (struct gsmtap_hdr *) buf; |
| 47 | gh->version = GSMTAP_VERSION; |
| 48 | gh->hdr_len = sizeof(*gh)/4; |
| 49 | gh->type = GSMTAP_TYPE_SIM; |
| 50 | gh->sub_type = sub_type; |
| 51 | |
| 52 | memcpy(buf + sizeof(*gh), apdu, len); |
| 53 | |
| 54 | rc = write(gsmtap_inst_fd(g_gti), buf, gross_len); |
| 55 | if (rc < 0) { |
| 56 | perror("write gsmtap"); |
| 57 | free(buf); |
| 58 | return rc; |
| 59 | } |
| 60 | |
| 61 | free(buf); |
| 62 | return 0; |
| 63 | } |