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