blob: abe15427f7c9b836f65fdce7be190fec66e06758 [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
Harald Welte1200c822020-02-13 20:43:27 +010090 if (!strcasecmp(cmd, "RESET")) {
91 /* reset the [remote] card */
Harald Welte8e46ab62020-02-14 12:55:43 +010092 pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
Harald Welte1200c822020-02-13 20:43:27 +010093 true, false, false, true);
Harald Welte8e46ab62020-02-14 12:55:43 +010094 server_conn_send_rspro(&bc->bankd_conn, pdu);
Harald Welte1200c822020-02-13 20:43:27 +010095 } else {
96 /* we assume the user has entered a C-APDU as hex string. parse + send */
97 rc = osmo_hexparse(cmd, buf, sizeof(buf));
98 if (rc < 0) {
99 fprintf(stderr, "ERROR parsing C-APDU `%s'!\n", cmd);
100 return;
101 }
Harald Welte8e46ab62020-02-14 12:55:43 +0100102 if (!bc->srv_conn.clslot) {
Harald Welte1200c822020-02-13 20:43:27 +0100103 fprintf(stderr, "Cannot send command; no client slot\n");
104 return;
105 }
106
107 /* Send CMD APDU to [remote] card */
Harald Welte8e46ab62020-02-14 12:55:43 +0100108 pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, buf, rc);
109 server_conn_send_rspro(&bc->bankd_conn, pdu);
Harald Welte1200c822020-02-13 20:43:27 +0100110 }
111}
112
113/* call-back function for stdin read. Gather bytes in buffer until CR/LF received */
114static int stdin_fd_cb(struct osmo_fd *ofd, unsigned int what)
115{
116 struct stdin_state *ss = ofd->data;
117 char *cur;
118 int rc, i;
119
120 OSMO_ASSERT(what & OSMO_FD_READ);
121
122 if (!ss->rx_msg) {
123 ss->rx_msg = msgb_alloc(1024, "stdin");
124 OSMO_ASSERT(ss->rx_msg);
125 }
126
127 cur = (char *) ss->rx_msg->tail;
128 rc = read(ofd->fd, cur, msgb_tailroom(ss->rx_msg));
129 if (rc < 0)
130 return rc;
131 msgb_put(ss->rx_msg, rc);
132
133 for (i = 0; i < rc; i++) {
134 if (cur[i] == '\r' || cur[i] == '\n') {
135 cur[i] = '\0';
136 /* dispatch the command */
137 handle_stdin_command(ss, cur);
138 /* FIXME: possibly other commands */
139 msgb_free(ss->rx_msg);
140 ss->rx_msg = NULL;
141 }
142 }
143
144 return 0;
145}
146
147
148
149/* main function */
Harald Welte8e46ab62020-02-14 12:55:43 +0100150int client_user_main(struct bankd_client *bc)
Harald Welte1200c822020-02-13 20:43:27 +0100151{
152 struct stdin_state ss;
153
154 /* register stdin file descriptor with osmocom select loop abstraction */
155 memset(&ss, 0, sizeof(ss));
156 osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0);
157 osmo_fd_register(&ss.ofd);
Harald Welte8e46ab62020-02-14 12:55:43 +0100158 ss.bc = bc;
Harald Welte1200c822020-02-13 20:43:27 +0100159
160 while (1) {
161 osmo_select_main(0);
162 }
163}