blob: 11601d6a358b778a9e6c9e971627e4a02146dffa [file] [log] [blame]
Harald Welte1200c822020-02-13 20:43:27 +01001#include <errno.h>
2#include <unistd.h>
3
4#include <osmocom/core/select.h>
5
6#include "client.h"
7
8/* This is a remsim-client with an interactive 'shell', where the user
9 * can type in C-APDUs in hex formats, which will be sent to the bankd /
10 * SIM-card. Responses received from SIM Card via bankd will be printed
11 * in return. */
12
13
14/***********************************************************************
15 * Incoming RSPRO messages from bank-daemon (SIM card)
16 ***********************************************************************/
17
18static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
19{
20 OSMO_ASSERT(pdu);
21 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
22
23 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
24
25 printf("R-APDU: %s\n", osmo_hexdump(card2modem->data.buf, card2modem->data.size));
Harald Welte82368402020-02-18 22:59:37 +010026 fflush(stdout);
Harald Welte1200c822020-02-13 20:43:27 +010027
28 return 0;
29}
30
31static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
32{
33 RsproPDU_t *resp;
34
35 OSMO_ASSERT(pdu);
36 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
37
38 printf("SET_ATR: %s\n", osmo_hexdump(pdu->msg.choice.setAtrReq.atr.buf,
39 pdu->msg.choice.setAtrReq.atr.size));
Harald Welte82368402020-02-18 22:59:37 +010040 fflush(stdout);
Harald Welte1200c822020-02-13 20:43:27 +010041
42 resp = rspro_gen_SetAtrRes(ResultCode_ok);
43 if (!resp)
44 return -ENOMEM;
45 server_conn_send_rspro(&bc->bankd_conn, resp);
46
47 return 0;
48}
49
50
51int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
52{
Harald Welte8e46ab62020-02-14 12:55:43 +010053 struct bankd_client *client = bankdc2bankd_client(bankdc);
Harald Welte1200c822020-02-13 20:43:27 +010054 switch (pdu->msg.present) {
55 case RsproPDUchoice_PR_tpduCardToModem:
Harald Welte8e46ab62020-02-14 12:55:43 +010056 bankd_handle_tpduCardToModem(client, pdu);
Harald Welte1200c822020-02-13 20:43:27 +010057 break;
58 case RsproPDUchoice_PR_setAtrReq:
Harald Welte8e46ab62020-02-14 12:55:43 +010059 bankd_handle_setAtrReq(client, pdu);
Harald Welte1200c822020-02-13 20:43:27 +010060 break;
61 default:
62 OSMO_ASSERT(0);
63 }
64 return 0;
65}
66
67/***********************************************************************
68 * Incoming command from the user application (stdin shell in our case)
69 ***********************************************************************/
70
71struct stdin_state {
72 struct osmo_fd ofd;
73 struct msgb *rx_msg;
Harald Welte8e46ab62020-02-14 12:55:43 +010074 struct bankd_client *bc;
Harald Welte1200c822020-02-13 20:43:27 +010075};
76
77/* called every time a command on stdin was received */
78static void handle_stdin_command(struct stdin_state *ss, char *cmd)
79{
Harald Welte8e46ab62020-02-14 12:55:43 +010080 struct bankd_client *bc = ss->bc;
Harald Welte1200c822020-02-13 20:43:27 +010081 RsproPDU_t *pdu;
82 BankSlot_t bslot;
83 uint8_t buf[1024];
84 int rc;
85
Harald Welte8e46ab62020-02-14 12:55:43 +010086 bank_slot2rspro(&bslot, &bc->bankd_slot);
Harald Welte1200c822020-02-13 20:43:27 +010087
88 OSMO_ASSERT(ss->rx_msg);
89
90 printf("stdin: `%s'\n", cmd);
91
92 if (!strcasecmp(cmd, "RESET")) {
93 /* reset the [remote] card */
Harald Welte8e46ab62020-02-14 12:55:43 +010094 pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
Harald Welte1200c822020-02-13 20:43:27 +010095 true, false, false, true);
Harald Welte8e46ab62020-02-14 12:55:43 +010096 server_conn_send_rspro(&bc->bankd_conn, pdu);
Harald Welte1200c822020-02-13 20:43:27 +010097 } else {
98 /* we assume the user has entered a C-APDU as hex string. parse + send */
99 rc = osmo_hexparse(cmd, buf, sizeof(buf));
100 if (rc < 0) {
101 fprintf(stderr, "ERROR parsing C-APDU `%s'!\n", cmd);
102 return;
103 }
Harald Welte8e46ab62020-02-14 12:55:43 +0100104 if (!bc->srv_conn.clslot) {
Harald Welte1200c822020-02-13 20:43:27 +0100105 fprintf(stderr, "Cannot send command; no client slot\n");
106 return;
107 }
108
109 /* Send CMD APDU to [remote] card */
Harald Welte8e46ab62020-02-14 12:55:43 +0100110 pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, buf, rc);
111 server_conn_send_rspro(&bc->bankd_conn, pdu);
Harald Welte1200c822020-02-13 20:43:27 +0100112 }
113}
114
115/* call-back function for stdin read. Gather bytes in buffer until CR/LF received */
116static int stdin_fd_cb(struct osmo_fd *ofd, unsigned int what)
117{
118 struct stdin_state *ss = ofd->data;
119 char *cur;
120 int rc, i;
121
122 OSMO_ASSERT(what & OSMO_FD_READ);
123
124 if (!ss->rx_msg) {
125 ss->rx_msg = msgb_alloc(1024, "stdin");
126 OSMO_ASSERT(ss->rx_msg);
127 }
128
129 cur = (char *) ss->rx_msg->tail;
130 rc = read(ofd->fd, cur, msgb_tailroom(ss->rx_msg));
131 if (rc < 0)
132 return rc;
133 msgb_put(ss->rx_msg, rc);
134
135 for (i = 0; i < rc; i++) {
136 if (cur[i] == '\r' || cur[i] == '\n') {
137 cur[i] = '\0';
138 /* dispatch the command */
139 handle_stdin_command(ss, cur);
140 /* FIXME: possibly other commands */
141 msgb_free(ss->rx_msg);
142 ss->rx_msg = NULL;
143 }
144 }
145
146 return 0;
147}
148
149
150
151/* main function */
Harald Welte8e46ab62020-02-14 12:55:43 +0100152int client_user_main(struct bankd_client *bc)
Harald Welte1200c822020-02-13 20:43:27 +0100153{
154 struct stdin_state ss;
155
156 /* register stdin file descriptor with osmocom select loop abstraction */
157 memset(&ss, 0, sizeof(ss));
158 osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0);
159 osmo_fd_register(&ss.ofd);
Harald Welte8e46ab62020-02-14 12:55:43 +0100160 ss.bc = bc;
Harald Welte1200c822020-02-13 20:43:27 +0100161
162 while (1) {
163 osmo_select_main(0);
164 }
165}