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