blob: 103f2fc9cf687a6ec1b731d55642d72bad7aeb5d [file] [log] [blame]
Harald Welte964cda32019-11-24 22:27:10 +01001#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 Welte208890a2019-11-24 22:46:51 +010011/*! global GSMTAP instance */
Harald Welte964cda32019-11-24 22:27:10 +010012static struct gsmtap_inst *g_gti;
13
Harald Welte208890a2019-11-24 22:46:51 +010014/*! initialize the global GSMTAP instance for SIM traces */
Harald Welte964cda32019-11-24 22:27:10 +010015int 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 Welte208890a2019-11-24 22:46:51 +010030/*! 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 Welte964cda32019-11-24 22:27:10 +010035int 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}