Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdbool.h> |
| 4 | #include <sys/socket.h> |
| 5 | #include <arpa/inet.h> |
| 6 | #include <netinet/in.h> |
| 7 | |
| 8 | #include <pthread.h> |
| 9 | |
| 10 | #include <wintypes.h> |
| 11 | #include <winscard.h> |
| 12 | |
| 13 | #include <osmocom/core/linuxlist.h> |
| 14 | |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 15 | #include "rspro_util.h" |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 16 | #include "slotmap.h" |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 17 | #include "client.h" |
Harald Welte | a4e0a23 | 2018-10-14 17:44:25 +0200 | [diff] [blame] | 18 | #include "debug.h" |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 19 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 20 | extern struct value_string worker_state_names[]; |
| 21 | |
| 22 | #define LOGW(w, fmt, args...) \ |
| 23 | printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \ |
| 24 | __FILE__, __LINE__, ## args) |
| 25 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 26 | struct bankd; |
| 27 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 28 | enum bankd_worker_state { |
| 29 | /* just started*/ |
| 30 | BW_ST_INIT, |
| 31 | /* blocking in the accept() call on the server socket fd */ |
| 32 | BW_ST_ACCEPTING, |
| 33 | /* TCP established, but peer not yet identified itself */ |
| 34 | BW_ST_CONN_WAIT_ID, |
| 35 | /* TCP established, client has identified itself, no mapping */ |
| 36 | BW_ST_CONN_CLIENT, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 37 | /* TCP established, client has identified itself, waiting for mapping */ |
| 38 | BW_ST_CONN_CLIENT_WAIT_MAP, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 39 | /* TCP established, client has identified itself, mapping exists */ |
| 40 | BW_ST_CONN_CLIENT_MAPPED, |
| 41 | /* TCP established, client identified, mapping exists, card opened */ |
| 42 | BW_ST_CONN_CLIENT_MAPPED_CARD, |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 43 | /* TCP established, client identified, but mapping [meanwhile] removed */ |
| 44 | BW_ST_CONN_CLIENT_UNMAPPED |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 47 | |
| 48 | /* bankd worker instance; one per card/slot, includes thread */ |
| 49 | struct bankd_worker { |
| 50 | /* global list of workers */ |
| 51 | struct llist_head list; |
| 52 | /* back-pointer to bankd */ |
| 53 | struct bankd *bankd; |
| 54 | |
Harald Welte | 286a2be | 2019-03-11 17:36:58 +0100 | [diff] [blame] | 55 | /* name of the worker */ |
| 56 | char *name; |
| 57 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 58 | /* thread number */ |
| 59 | unsigned int num; |
| 60 | /* worker thread state */ |
| 61 | enum bankd_worker_state state; |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 62 | /* timeout to use for blocking read */ |
| 63 | unsigned int timeout; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 64 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 65 | /* slot number we are representing */ |
| 66 | struct bank_slot slot; |
| 67 | |
| 68 | /* thread of this worker. */ |
| 69 | pthread_t thread; |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 70 | /* top talloc context for this worker/thread */ |
| 71 | void *tall_ctx; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 72 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 73 | const struct bankd_driver_ops *ops; |
| 74 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 75 | /* File descriptor of the TCP connection to the remsim-client (modem) */ |
| 76 | struct { |
| 77 | int fd; |
| 78 | struct sockaddr_storage peer_addr; |
| 79 | socklen_t peer_addr_len; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 80 | struct client_slot clslot; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 81 | } client; |
| 82 | |
| 83 | struct { |
| 84 | const char *name; |
| 85 | union { |
| 86 | struct { |
| 87 | /* PC/SC context / application handle */ |
| 88 | SCARDCONTEXT hContext; |
| 89 | /* PC/SC card handle */ |
| 90 | SCARDHANDLE hCard; |
| 91 | } pcsc; |
| 92 | }; |
| 93 | } reader; |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 94 | |
| 95 | struct { |
| 96 | uint8_t atr[MAX_ATR_SIZE]; |
| 97 | unsigned int atr_len; |
| 98 | } card; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 101 | /* bankd card reader driver operations */ |
| 102 | struct bankd_driver_ops { |
| 103 | /* open a given card/slot: called once client + mapping exists */ |
| 104 | int (*open_card)(struct bankd_worker *worker); |
| 105 | int (*transceive)(struct bankd_worker *worker, const uint8_t *out, size_t out_len, |
| 106 | uint8_t *in, size_t *in_len); |
| 107 | /* called at cleanup time of a worker thread: clear any driver related state */ |
| 108 | void (*cleanup)(struct bankd_worker *worker); |
| 109 | }; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 110 | |
| 111 | /* global bank deamon */ |
| 112 | struct bankd { |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 113 | struct app_comp_id comp_id; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 114 | /* RSPRO connection to the remsim-server */ |
| 115 | struct rspro_server_conn srvc; |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 116 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 117 | /* TCP socket at which we are listening */ |
| 118 | int accept_fd; |
| 119 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 120 | /* list of slot mappings. only ever modified in main thread! */ |
| 121 | struct slotmaps *slotmaps; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 122 | |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 123 | /* pthread ID of main thread */ |
| 124 | pthread_t main; |
| 125 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 126 | /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */ |
| 127 | struct llist_head workers; |
| 128 | pthread_mutex_t workers_mutex; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 129 | |
| 130 | struct llist_head pcsc_slot_names; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 131 | }; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 132 | |
| 133 | int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file); |
| 134 | const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot); |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 135 | |
| 136 | extern const struct bankd_driver_ops pcsc_driver_ops; |