blob: 4c53c74b96c04beefe01a8c74152e02de69a5fd6 [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,
37};
38
Harald Welte77911b02018-08-14 23:47:30 +020039
40/* bankd worker instance; one per card/slot, includes thread */
41struct bankd_worker {
42 /* global list of workers */
43 struct llist_head list;
44 /* back-pointer to bankd */
45 struct bankd *bankd;
46
Harald Welte8d858292018-08-15 23:36:46 +020047 /* thread number */
48 unsigned int num;
49 /* worker thread state */
50 enum bankd_worker_state state;
Harald Welte694df832018-10-03 22:47:52 +020051 /* timeout to use for blocking read */
52 unsigned int timeout;
Harald Welte8d858292018-08-15 23:36:46 +020053
Harald Welte77911b02018-08-14 23:47:30 +020054 /* slot number we are representing */
55 struct bank_slot slot;
56
57 /* thread of this worker. */
58 pthread_t thread;
59
60 /* File descriptor of the TCP connection to the remsim-client (modem) */
61 struct {
62 int fd;
63 struct sockaddr_storage peer_addr;
64 socklen_t peer_addr_len;
Harald Welte371d0262018-08-16 15:23:58 +020065 struct client_slot clslot;
Harald Welte77911b02018-08-14 23:47:30 +020066 } client;
67
68 struct {
69 const char *name;
70 union {
71 struct {
72 /* PC/SC context / application handle */
73 SCARDCONTEXT hContext;
74 /* PC/SC card handle */
75 SCARDHANDLE hCard;
76 } pcsc;
77 };
78 } reader;
79};
80
81
82/* global bank deamon */
83struct bankd {
84 struct {
85 uint16_t bank_id;
86 } cfg;
87
Harald Weltef1dd1622018-09-24 14:54:23 +020088 struct app_comp_id comp_id;
Harald Welte707c85a2019-03-09 12:56:35 +010089 /* RSPRO connection to the remsim-server */
90 struct rspro_server_conn srvc;
Harald Weltef1dd1622018-09-24 14:54:23 +020091
Harald Welte77911b02018-08-14 23:47:30 +020092 /* TCP socket at which we are listening */
93 int accept_fd;
94
Harald Weltecbd18962019-03-03 19:02:38 +010095 /* list of slot mappings. only ever modified in main thread! */
96 struct slotmaps *slotmaps;
Harald Welte77911b02018-08-14 23:47:30 +020097
98 /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */
99 struct llist_head workers;
100 pthread_mutex_t workers_mutex;
Harald Welte45c948c2018-09-23 19:26:52 +0200101
102 struct llist_head pcsc_slot_names;
Harald Welte77911b02018-08-14 23:47:30 +0200103};
Harald Welte45c948c2018-09-23 19:26:52 +0200104
105int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file);
106const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot);