blob: d5cad7b4ee248c3250960124914971800771ecf4 [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));
26
27 return 0;
28}
29
30static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
31{
32 RsproPDU_t *resp;
33
34 OSMO_ASSERT(pdu);
35 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
36
37 printf("SET_ATR: %s\n", osmo_hexdump(pdu->msg.choice.setAtrReq.atr.buf,
38 pdu->msg.choice.setAtrReq.atr.size));
39
40 resp = rspro_gen_SetAtrRes(ResultCode_ok);
41 if (!resp)
42 return -ENOMEM;
43 server_conn_send_rspro(&bc->bankd_conn, resp);
44
45 return 0;
46}
47
48
49int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
50{
Harald Welte8e46ab62020-02-14 12:55:43 +010051 struct bankd_client *client = bankdc2bankd_client(bankdc);
Harald Welte1200c822020-02-13 20:43:27 +010052 switch (pdu->msg.present) {
53 case RsproPDUchoice_PR_tpduCardToModem:
Harald Welte8e46ab62020-02-14 12:55:43 +010054 bankd_handle_tpduCardToModem(client, pdu);
Harald Welte1200c822020-02-13 20:43:27 +010055 break;
56 case RsproPDUchoice_PR_setAtrReq:
Harald Welte8e46ab62020-02-14 12:55:43 +010057 bankd_handle_setAtrReq(client, pdu);
Harald Welte1200c822020-02-13 20:43:27 +010058 break;
59 default:
60 OSMO_ASSERT(0);
61 }
62 return 0;
63}
64
65/***********************************************************************
66 * Incoming command from the user application (stdin shell in our case)
67 ***********************************************************************/
68
69struct stdin_state {
70 struct osmo_fd ofd;
71 struct msgb *rx_msg;
Harald Welte8e46ab62020-02-14 12:55:43 +010072 struct bankd_client *bc;
Harald Welte1200c822020-02-13 20:43:27 +010073};
74
75/* called every time a command on stdin was received */
76static void handle_stdin_command(struct stdin_state *ss, char *cmd)
77{
Harald Welte8e46ab62020-02-14 12:55:43 +010078 struct bankd_client *bc = ss->bc;
Harald Welte1200c822020-02-13 20:43:27 +010079 RsproPDU_t *pdu;
80 BankSlot_t bslot;
81 uint8_t buf[1024];
82 int rc;
83
Harald Welte8e46ab62020-02-14 12:55:43 +010084 bank_slot2rspro(&bslot, &bc->bankd_slot);
Harald Welte1200c822020-02-13 20:43:27 +010085
86 OSMO_ASSERT(ss->rx_msg);
87
88 printf("stdin: `%s'\n", cmd);
89
90 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}