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 | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 20 | struct bankd; |
| 21 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 22 | enum bankd_worker_state { |
| 23 | /* just started*/ |
| 24 | BW_ST_INIT, |
| 25 | /* blocking in the accept() call on the server socket fd */ |
| 26 | BW_ST_ACCEPTING, |
| 27 | /* TCP established, but peer not yet identified itself */ |
| 28 | BW_ST_CONN_WAIT_ID, |
| 29 | /* TCP established, client has identified itself, no mapping */ |
| 30 | BW_ST_CONN_CLIENT, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 31 | /* TCP established, client has identified itself, waiting for mapping */ |
| 32 | BW_ST_CONN_CLIENT_WAIT_MAP, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 33 | /* TCP established, client has identified itself, mapping exists */ |
| 34 | BW_ST_CONN_CLIENT_MAPPED, |
| 35 | /* TCP established, client identified, mapping exists, card opened */ |
| 36 | BW_ST_CONN_CLIENT_MAPPED_CARD, |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 37 | /* TCP established, client identified, but mapping [meanwhile] removed */ |
| 38 | BW_ST_CONN_CLIENT_UNMAPPED |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 41 | |
| 42 | /* bankd worker instance; one per card/slot, includes thread */ |
| 43 | struct bankd_worker { |
| 44 | /* global list of workers */ |
| 45 | struct llist_head list; |
| 46 | /* back-pointer to bankd */ |
| 47 | struct bankd *bankd; |
| 48 | |
Harald Welte | 286a2be | 2019-03-11 17:36:58 +0100 | [diff] [blame] | 49 | /* name of the worker */ |
| 50 | char *name; |
| 51 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 52 | /* thread number */ |
| 53 | unsigned int num; |
| 54 | /* worker thread state */ |
| 55 | enum bankd_worker_state state; |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 56 | /* timeout to use for blocking read */ |
| 57 | unsigned int timeout; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 58 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 59 | /* slot number we are representing */ |
| 60 | struct bank_slot slot; |
| 61 | |
| 62 | /* thread of this worker. */ |
| 63 | pthread_t thread; |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 64 | /* top talloc context for this worker/thread */ |
| 65 | void *tall_ctx; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 66 | |
| 67 | /* File descriptor of the TCP connection to the remsim-client (modem) */ |
| 68 | struct { |
| 69 | int fd; |
| 70 | struct sockaddr_storage peer_addr; |
| 71 | socklen_t peer_addr_len; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 72 | struct client_slot clslot; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 73 | } client; |
| 74 | |
| 75 | struct { |
| 76 | const char *name; |
| 77 | union { |
| 78 | struct { |
| 79 | /* PC/SC context / application handle */ |
| 80 | SCARDCONTEXT hContext; |
| 81 | /* PC/SC card handle */ |
| 82 | SCARDHANDLE hCard; |
| 83 | } pcsc; |
| 84 | }; |
| 85 | } reader; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | /* global bank deamon */ |
| 90 | struct bankd { |
| 91 | struct { |
| 92 | uint16_t bank_id; |
Harald Welte | a0f3950 | 2019-03-09 20:59:34 +0100 | [diff] [blame] | 93 | uint16_t num_slots; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 94 | } cfg; |
| 95 | |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 96 | struct app_comp_id comp_id; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 97 | /* RSPRO connection to the remsim-server */ |
| 98 | struct rspro_server_conn srvc; |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 99 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 100 | /* TCP socket at which we are listening */ |
| 101 | int accept_fd; |
| 102 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 103 | /* list of slot mappings. only ever modified in main thread! */ |
| 104 | struct slotmaps *slotmaps; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 105 | |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 106 | /* pthread ID of main thread */ |
| 107 | pthread_t main; |
| 108 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 109 | /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */ |
| 110 | struct llist_head workers; |
| 111 | pthread_mutex_t workers_mutex; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 112 | |
| 113 | struct llist_head pcsc_slot_names; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 114 | }; |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 115 | |
| 116 | int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file); |
| 117 | const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot); |