Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 1 | #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 | |
| 18 | static 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 | |
| 30 | static 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 | |
| 49 | int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu) |
| 50 | { |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 51 | struct bankd_client *client = bankdc2bankd_client(bankdc); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 52 | switch (pdu->msg.present) { |
| 53 | case RsproPDUchoice_PR_tpduCardToModem: |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 54 | bankd_handle_tpduCardToModem(client, pdu); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 55 | break; |
| 56 | case RsproPDUchoice_PR_setAtrReq: |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 57 | bankd_handle_setAtrReq(client, pdu); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 58 | 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 | |
| 69 | struct stdin_state { |
| 70 | struct osmo_fd ofd; |
| 71 | struct msgb *rx_msg; |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 72 | struct bankd_client *bc; |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* called every time a command on stdin was received */ |
| 76 | static void handle_stdin_command(struct stdin_state *ss, char *cmd) |
| 77 | { |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 78 | struct bankd_client *bc = ss->bc; |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 79 | RsproPDU_t *pdu; |
| 80 | BankSlot_t bslot; |
| 81 | uint8_t buf[1024]; |
| 82 | int rc; |
| 83 | |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 84 | bank_slot2rspro(&bslot, &bc->bankd_slot); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 85 | |
| 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 Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 92 | pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot, |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 93 | true, false, false, true); |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 94 | server_conn_send_rspro(&bc->bankd_conn, pdu); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 95 | } 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 Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 102 | if (!bc->srv_conn.clslot) { |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 103 | fprintf(stderr, "Cannot send command; no client slot\n"); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | /* Send CMD APDU to [remote] card */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 108 | pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, buf, rc); |
| 109 | server_conn_send_rspro(&bc->bankd_conn, pdu); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /* call-back function for stdin read. Gather bytes in buffer until CR/LF received */ |
| 114 | static 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 Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 150 | int client_user_main(struct bankd_client *bc) |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 151 | { |
| 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 Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 158 | ss.bc = bc; |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 159 | |
| 160 | while (1) { |
| 161 | osmo_select_main(0); |
| 162 | } |
| 163 | } |