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" |
| 16 | |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 17 | enum { |
| 18 | DMAIN, |
| 19 | }; |
| 20 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 21 | struct bankd; |
| 22 | |
| 23 | struct bank_slot { |
| 24 | uint16_t bank_id; |
| 25 | uint16_t slot_nr; |
| 26 | }; |
| 27 | |
| 28 | static inline bool bank_slot_equals(const struct bank_slot *a, const struct bank_slot *b) |
| 29 | { |
| 30 | if (a->bank_id == b->bank_id && a->slot_nr == b->slot_nr) |
| 31 | return true; |
| 32 | else |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | struct client_slot { |
| 37 | uint16_t client_id; |
| 38 | uint16_t slot_nr; |
| 39 | }; |
| 40 | |
| 41 | static inline bool client_slot_equals(const struct client_slot *a, const struct client_slot *b) |
| 42 | { |
| 43 | if (a->client_id == b->client_id && a->slot_nr == b->slot_nr) |
| 44 | return true; |
| 45 | else |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | /* slot mappings are created / removed by the server */ |
| 50 | struct bankd_slot_mapping { |
| 51 | /* global lits of bankd slot mappings */ |
| 52 | struct llist_head list; |
| 53 | /* slot on bank side */ |
| 54 | struct bank_slot bank; |
| 55 | /* slot on client side */ |
| 56 | struct client_slot client; |
| 57 | }; |
| 58 | |
| 59 | /* thread-safe lookup of map by client:slot */ |
| 60 | struct bankd_slot_mapping *bankd_slotmap_by_client(struct bankd *bankd, |
| 61 | const struct client_slot *client); |
| 62 | |
| 63 | /* thread-safe lookup of map by bank:slot */ |
| 64 | struct bankd_slot_mapping *bankd_slotmap_by_bank(struct bankd *bankd, const struct bank_slot *bank); |
| 65 | |
| 66 | /* thread-safe creating of a new bank<->client map */ |
| 67 | int bankd_slotmap_add(struct bankd *bankd, const struct bank_slot *bank, |
| 68 | const struct client_slot *client); |
| 69 | |
| 70 | /* thread-safe removal of a bank<->client map */ |
| 71 | void bankd_slotmap_del(struct bankd *bankd, struct bankd_slot_mapping *map); |
| 72 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 73 | enum bankd_worker_state { |
| 74 | /* just started*/ |
| 75 | BW_ST_INIT, |
| 76 | /* blocking in the accept() call on the server socket fd */ |
| 77 | BW_ST_ACCEPTING, |
| 78 | /* TCP established, but peer not yet identified itself */ |
| 79 | BW_ST_CONN_WAIT_ID, |
| 80 | /* TCP established, client has identified itself, no mapping */ |
| 81 | BW_ST_CONN_CLIENT, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 82 | /* TCP established, client has identified itself, waiting for mapping */ |
| 83 | BW_ST_CONN_CLIENT_WAIT_MAP, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 84 | /* TCP established, client has identified itself, mapping exists */ |
| 85 | BW_ST_CONN_CLIENT_MAPPED, |
| 86 | /* TCP established, client identified, mapping exists, card opened */ |
| 87 | BW_ST_CONN_CLIENT_MAPPED_CARD, |
| 88 | }; |
| 89 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 90 | |
| 91 | /* bankd worker instance; one per card/slot, includes thread */ |
| 92 | struct bankd_worker { |
| 93 | /* global list of workers */ |
| 94 | struct llist_head list; |
| 95 | /* back-pointer to bankd */ |
| 96 | struct bankd *bankd; |
| 97 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 98 | /* thread number */ |
| 99 | unsigned int num; |
| 100 | /* worker thread state */ |
| 101 | enum bankd_worker_state state; |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 102 | /* timeout to use for blocking read */ |
| 103 | unsigned int timeout; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 104 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 105 | /* slot number we are representing */ |
| 106 | struct bank_slot slot; |
| 107 | |
| 108 | /* thread of this worker. */ |
| 109 | pthread_t thread; |
| 110 | |
| 111 | /* File descriptor of the TCP connection to the remsim-client (modem) */ |
| 112 | struct { |
| 113 | int fd; |
| 114 | struct sockaddr_storage peer_addr; |
| 115 | socklen_t peer_addr_len; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 116 | struct client_slot clslot; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 117 | } client; |
| 118 | |
| 119 | struct { |
| 120 | const char *name; |
| 121 | union { |
| 122 | struct { |
| 123 | /* PC/SC context / application handle */ |
| 124 | SCARDCONTEXT hContext; |
| 125 | /* PC/SC card handle */ |
| 126 | SCARDHANDLE hCard; |
| 127 | } pcsc; |
| 128 | }; |
| 129 | } reader; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | /* global bank deamon */ |
| 134 | struct bankd { |
| 135 | struct { |
| 136 | uint16_t bank_id; |
| 137 | } cfg; |
| 138 | |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 139 | struct app_comp_id comp_id; |
| 140 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 141 | /* TCP socket at which we are listening */ |
| 142 | int accept_fd; |
| 143 | |
| 144 | /* list of slit mappings. only ever modified in main thread! */ |
| 145 | struct llist_head slot_mappings; |
| 146 | pthread_rwlock_t slot_mappings_rwlock; |
| 147 | |
| 148 | /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */ |
| 149 | struct llist_head workers; |
| 150 | pthread_mutex_t workers_mutex; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 151 | |
| 152 | struct llist_head pcsc_slot_names; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 153 | }; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 154 | |
| 155 | int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file); |
| 156 | const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot); |