blob: f3770f2116d20c79ea812fccafb61fe7b914d696 [file] [log] [blame]
Harald Welte77911b02018-08-14 23:47:30 +02001#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 Weltef1dd1622018-09-24 14:54:23 +020015#include "rspro_util.h"
Harald Weltecbd18962019-03-03 19:02:38 +010016#include "slotmap.h"
Harald Welte707c85a2019-03-09 12:56:35 +010017#include "client.h"
Harald Weltea4e0a232018-10-14 17:44:25 +020018#include "debug.h"
Harald Weltef94b9ee2018-09-25 15:04:21 +020019
Harald Welte77911b02018-08-14 23:47:30 +020020struct bankd;
21
Harald Welte8d858292018-08-15 23:36:46 +020022enum 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 Welteaf614732018-08-17 22:10:05 +020031 /* TCP established, client has identified itself, waiting for mapping */
32 BW_ST_CONN_CLIENT_WAIT_MAP,
Harald Welte8d858292018-08-15 23:36:46 +020033 /* 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 Welte00a96732019-03-11 17:18:02 +010037 /* TCP established, client identified, but mapping [meanwhile] removed */
38 BW_ST_CONN_CLIENT_UNMAPPED
Harald Welte8d858292018-08-15 23:36:46 +020039};
40
Harald Welte77911b02018-08-14 23:47:30 +020041
42/* bankd worker instance; one per card/slot, includes thread */
43struct bankd_worker {
44 /* global list of workers */
45 struct llist_head list;
46 /* back-pointer to bankd */
47 struct bankd *bankd;
48
Harald Welte8d858292018-08-15 23:36:46 +020049 /* thread number */
50 unsigned int num;
51 /* worker thread state */
52 enum bankd_worker_state state;
Harald Welte694df832018-10-03 22:47:52 +020053 /* timeout to use for blocking read */
54 unsigned int timeout;
Harald Welte8d858292018-08-15 23:36:46 +020055
Harald Welte77911b02018-08-14 23:47:30 +020056 /* slot number we are representing */
57 struct bank_slot slot;
58
59 /* thread of this worker. */
60 pthread_t thread;
61
62 /* File descriptor of the TCP connection to the remsim-client (modem) */
63 struct {
64 int fd;
65 struct sockaddr_storage peer_addr;
66 socklen_t peer_addr_len;
Harald Welte371d0262018-08-16 15:23:58 +020067 struct client_slot clslot;
Harald Welte77911b02018-08-14 23:47:30 +020068 } client;
69
70 struct {
71 const char *name;
72 union {
73 struct {
74 /* PC/SC context / application handle */
75 SCARDCONTEXT hContext;
76 /* PC/SC card handle */
77 SCARDHANDLE hCard;
78 } pcsc;
79 };
80 } reader;
81};
82
83
84/* global bank deamon */
85struct bankd {
86 struct {
87 uint16_t bank_id;
Harald Weltea0f39502019-03-09 20:59:34 +010088 uint16_t num_slots;
Harald Welte77911b02018-08-14 23:47:30 +020089 } cfg;
90
Harald Weltef1dd1622018-09-24 14:54:23 +020091 struct app_comp_id comp_id;
Harald Welte707c85a2019-03-09 12:56:35 +010092 /* RSPRO connection to the remsim-server */
93 struct rspro_server_conn srvc;
Harald Weltef1dd1622018-09-24 14:54:23 +020094
Harald Welte77911b02018-08-14 23:47:30 +020095 /* TCP socket at which we are listening */
96 int accept_fd;
97
Harald Weltecbd18962019-03-03 19:02:38 +010098 /* list of slot mappings. only ever modified in main thread! */
99 struct slotmaps *slotmaps;
Harald Welte77911b02018-08-14 23:47:30 +0200100
101 /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */
102 struct llist_head workers;
103 pthread_mutex_t workers_mutex;
Harald Welte45c948c2018-09-23 19:26:52 +0200104
105 struct llist_head pcsc_slot_names;
Harald Welte77911b02018-08-14 23:47:30 +0200106};
Harald Welte45c948c2018-09-23 19:26:52 +0200107
108int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file);
109const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot);