Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <pthread.h> |
| 7 | |
| 8 | #include <wintypes.h> |
| 9 | #include <winscard.h> |
| 10 | #include <pcsclite.h> |
| 11 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 12 | #include <osmocom/core/socket.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 13 | #include <osmocom/core/linuxlist.h> |
| 14 | |
| 15 | #include <osmocom/gsm/ipa.h> |
| 16 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 17 | |
| 18 | #include <asn1c/asn_application.h> |
| 19 | #include <osmocom/rspro/RsproPDU.h> |
| 20 | |
| 21 | #include "bankd.h" |
| 22 | |
| 23 | __thread void *talloc_asn1_ctx; |
| 24 | |
| 25 | static void *worker_main(void *arg); |
| 26 | |
| 27 | /*********************************************************************** |
| 28 | * bankd core / main thread |
| 29 | ***********************************************************************/ |
| 30 | |
| 31 | static void bankd_init(struct bankd *bankd) |
| 32 | { |
| 33 | /* intialize members of 'bankd' */ |
| 34 | INIT_LLIST_HEAD(&bankd->slot_mappings); |
| 35 | pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL); |
| 36 | INIT_LLIST_HEAD(&bankd->workers); |
| 37 | pthread_mutex_init(&bankd->workers_mutex, NULL); |
| 38 | } |
| 39 | |
| 40 | /* create + start a new bankd_worker thread */ |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 41 | static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 42 | { |
| 43 | struct bankd_worker *worker; |
| 44 | int rc; |
| 45 | |
| 46 | worker = talloc_zero(bankd, struct bankd_worker); |
| 47 | if (!worker) |
| 48 | return NULL; |
| 49 | |
| 50 | worker->bankd = bankd; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 51 | worker->num = i; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 52 | |
| 53 | /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */ |
| 54 | |
| 55 | rc = pthread_create(&worker->thread, NULL, worker_main, worker); |
| 56 | if (rc != 0) { |
| 57 | talloc_free(worker); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | pthread_mutex_lock(&bankd->workers_mutex); |
| 62 | llist_add_tail(&worker->list, &bankd->workers); |
| 63 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 64 | |
| 65 | return worker; |
| 66 | } |
| 67 | |
| 68 | static bool terminate = false; |
| 69 | |
| 70 | int main(int argc, char **argv) |
| 71 | { |
| 72 | struct bankd *bankd = talloc_zero(NULL, struct bankd); |
| 73 | int i, rc; |
| 74 | |
| 75 | OSMO_ASSERT(bankd); |
| 76 | bankd_init(bankd); |
| 77 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 78 | /* create listening socket */ |
| 79 | rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND); |
| 80 | if (rc < 0) |
| 81 | exit(1); |
| 82 | bankd->accept_fd = rc; |
| 83 | |
| 84 | /* create worker threads. FIXME: one per reader/slot! */ |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 85 | for (i = 0; i < 10; i++) { |
| 86 | struct bankd_worker *w; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 87 | w = bankd_create_worker(bankd, i); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 88 | if (!w) |
| 89 | exit(21); |
| 90 | } |
| 91 | |
| 92 | while (1) { |
| 93 | if (terminate) |
| 94 | break; |
Harald Welte | 7f684a0 | 2018-08-16 14:43:50 +0200 | [diff] [blame] | 95 | sleep(1); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | talloc_free(bankd); |
| 99 | exit(0); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | /*********************************************************************** |
| 105 | * bankd worker thread |
| 106 | ***********************************************************************/ |
| 107 | |
| 108 | #define PCSC_ERROR(rv, text) \ |
| 109 | if (rv != SCARD_S_SUCCESS) { \ |
| 110 | fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ |
| 111 | goto end; \ |
| 112 | } else { \ |
| 113 | printf(text ": OK\n\n"); \ |
| 114 | } |
| 115 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 116 | #define LOGW(w, fmt, args...) \ |
| 117 | printf("[%u] " fmt, (w)->num, args) |
| 118 | |
| 119 | struct value_string worker_state_names[] = { |
| 120 | { BW_ST_INIT, "INIT" }, |
| 121 | { BW_ST_ACCEPTING, "ACCEPTING" }, |
| 122 | { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" }, |
| 123 | { BW_ST_CONN_CLIENT, "CONN_CLIENT" }, |
| 124 | { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" }, |
| 125 | { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" }, |
| 126 | { 0, NULL } |
| 127 | }; |
| 128 | |
| 129 | static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state) |
| 130 | { |
| 131 | LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state)); |
| 132 | worker->state = new_state; |
| 133 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 134 | |
| 135 | static void worker_cleanup(void *arg) |
| 136 | { |
| 137 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 138 | struct bankd *bankd = worker->bankd; |
| 139 | |
| 140 | /* FIXME: should we still do this? in the thread ?!? */ |
| 141 | pthread_mutex_lock(&bankd->workers_mutex); |
| 142 | llist_del(&worker->list); |
| 143 | talloc_free(worker); /* FIXME: is this safe? */ |
| 144 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | #if 0 |
| 149 | /* function running inside a worker thread; doing some initialization */ |
| 150 | static void worker_init(struct bankd_worker *worker) |
| 151 | { |
| 152 | int rc; |
| 153 | |
| 154 | /* push cleanup helper */ |
| 155 | pthread_cleanup_push(&worker_cleanup, worker); |
| 156 | |
| 157 | /* The PC/SC context must be created inside the thread where we'll later use it */ |
| 158 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext); |
| 159 | PCSC_ERROR(rc, "SCardEstablishContext") |
| 160 | |
| 161 | rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED, |
| 162 | SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL); |
| 163 | PCSC_ERROR(rc, "SCardConnect") |
| 164 | |
| 165 | return; |
| 166 | end: |
| 167 | pthread_exit(NULL); |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | |
| 172 | static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size) |
| 173 | { |
| 174 | struct ipaccess_head *hh; |
| 175 | uint16_t len; |
| 176 | int needed, rc; |
| 177 | |
| 178 | if (buf_size < sizeof(*hh)) |
| 179 | return -1; |
| 180 | |
| 181 | hh = (struct ipaccess_head *) buf; |
| 182 | |
| 183 | /* 1) blocking read from the socket (IPA header) */ |
| 184 | rc = read(fd, buf, sizeof(*hh)); |
| 185 | if (rc < sizeof(*hh)) |
| 186 | return -2; |
| 187 | |
| 188 | len = ntohs(hh->len); |
| 189 | needed = len; //- sizeof(*hh); |
| 190 | |
| 191 | /* 2) blocking read from the socket (payload) */ |
| 192 | rc = read(fd, buf+sizeof(*hh), needed); |
| 193 | if (rc < needed) |
| 194 | return -3; |
| 195 | |
| 196 | return len; |
| 197 | } |
| 198 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame^] | 199 | static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 200 | { |
| 201 | OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq); |
| 202 | |
| 203 | const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity; |
| 204 | |
| 205 | LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n", |
| 206 | cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf); |
| 207 | /* FIXME: store somewhere? */ |
| 208 | |
| 209 | if (worker->state != BW_ST_CONN_WAIT_ID) { |
| 210 | LOGW(worker, "Unexpected connectClientReq\n"); |
| 211 | return -102; |
| 212 | } |
| 213 | |
| 214 | if (!pdu->msg.choice.connectClientReq.clientId) { |
| 215 | LOGW(worker, "missing clientID, aborting\n"); |
| 216 | return -103; |
| 217 | } |
| 218 | worker->client.id = *pdu->msg.choice.connectClientReq.clientId; |
| 219 | worker_set_state(worker, BW_ST_CONN_CLIENT); |
| 220 | |
| 221 | /* FIXME: resolve mapping */ |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 226 | /* handle one incoming RSPRO message from a client inside a worker thread */ |
| 227 | static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 228 | { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame^] | 229 | int rc = -100; |
| 230 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 231 | switch (pdu->msg.present) { |
| 232 | case RsproPDUchoice_PR_connectClientReq: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame^] | 233 | rc = worker_handle_connectClientReq(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 234 | break; |
| 235 | case RsproPDUchoice_PR_tpduModemToCard: |
| 236 | /* FIXME */ |
| 237 | break; |
| 238 | case RsproPDUchoice_PR_clientSlotStatusInd: |
| 239 | /* FIXME */ |
| 240 | break; |
| 241 | default: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame^] | 242 | rc = -101; |
| 243 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame^] | 246 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* body of the main transceive loop */ |
| 250 | static int worker_transceive_loop(struct bankd_worker *worker) |
| 251 | { |
| 252 | struct ipaccess_head *hh; |
| 253 | struct ipaccess_head_ext *hh_ext; |
| 254 | uint8_t buf[65536]; /* maximum length expressed in 16bit length field */ |
| 255 | asn_dec_rval_t rval; |
| 256 | int data_len, rc; |
| 257 | RsproPDU_t *pdu; |
| 258 | |
| 259 | /* 1) blocking read of entire IPA message from the socket */ |
| 260 | rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf)); |
| 261 | if (rc < 0) |
| 262 | return rc; |
| 263 | data_len = rc; |
| 264 | |
| 265 | hh = (struct ipaccess_head *) buf; |
| 266 | if (hh->proto != IPAC_PROTO_OSMO) |
| 267 | return -4; |
| 268 | |
| 269 | hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh); |
| 270 | if (data_len < sizeof(*hh_ext)) |
| 271 | return -5; |
| 272 | data_len -= sizeof(*hh_ext); |
| 273 | if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) |
| 274 | return -6; |
| 275 | |
| 276 | /* 2) ASN1 BER decode of the message */ |
| 277 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len); |
| 278 | if (rval.code != RC_OK) |
| 279 | return -7; |
| 280 | |
| 281 | /* 3) handling of the message, possibly resulting in PCSC commands */ |
| 282 | rc = worker_handle_rspro(worker, pdu); |
| 283 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
| 284 | if (rc < 0) |
| 285 | return rc; |
| 286 | |
| 287 | /* everything OK if we reach here */ |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* worker thread main function */ |
| 292 | static void *worker_main(void *arg) |
| 293 | { |
| 294 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 295 | void *top_ctx; |
| 296 | int rc; |
| 297 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 298 | worker_set_state(worker, BW_ST_INIT); |
| 299 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 300 | /* not permitted in multithreaded environment */ |
| 301 | talloc_disable_null_tracking(); |
| 302 | top_ctx = talloc_named_const(NULL, 0, "top"); |
| 303 | talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1"); |
| 304 | |
| 305 | /* push cleanup helper */ |
| 306 | pthread_cleanup_push(&worker_cleanup, worker); |
| 307 | |
| 308 | /* we continuously perform the same loop here, recycling the worker thread |
| 309 | * once the client connection is gone or we have some trouble with the card/reader */ |
| 310 | while (1) { |
| 311 | worker->client.peer_addr_len = sizeof(worker->client.peer_addr); |
| 312 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 313 | worker_set_state(worker, BW_ST_ACCEPTING); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 314 | /* first wait for an incoming TCP connection */ |
| 315 | rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr, |
| 316 | &worker->client.peer_addr_len); |
| 317 | if (rc < 0) { |
| 318 | continue; |
| 319 | } |
| 320 | worker->client.fd = rc; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 321 | worker_set_state(worker, BW_ST_CONN_WAIT_ID); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 322 | |
| 323 | /* run the main worker transceive loop body until there was some error */ |
| 324 | while (1) { |
| 325 | rc = worker_transceive_loop(worker); |
| 326 | if (rc < 0) |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | /* clean-up: reset to sane state */ |
| 331 | if (worker->reader.pcsc.hCard) { |
| 332 | SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD); |
| 333 | worker->reader.pcsc.hCard = 0; |
| 334 | } |
| 335 | if (worker->reader.pcsc.hContext) { |
| 336 | SCardReleaseContext(worker->reader.pcsc.hContext); |
| 337 | worker->reader.pcsc.hContext = 0; |
| 338 | } |
| 339 | if (worker->client.fd >= 0) |
| 340 | close(worker->client.fd); |
| 341 | worker->client.fd = -1; |
| 342 | } |
| 343 | |
| 344 | pthread_cleanup_pop(1); |
| 345 | talloc_free(top_ctx); |
| 346 | pthread_exit(NULL); |
| 347 | } |