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