blob: cd977723d4976603dd2995c2145b43dd230941ab [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 Weltea4e0a232018-10-14 17:44:25 +020017#include "debug.h"
Harald Weltef94b9ee2018-09-25 15:04:21 +020018
Harald Welte77911b02018-08-14 23:47:30 +020019struct bankd;
20
Harald Welte8d858292018-08-15 23:36:46 +020021enum 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 Welteaf614732018-08-17 22:10:05 +020030 /* TCP established, client has identified itself, waiting for mapping */
31 BW_ST_CONN_CLIENT_WAIT_MAP,
Harald Welte8d858292018-08-15 23:36:46 +020032 /* 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 Welte77911b02018-08-14 23:47:30 +020038
39/* bankd worker instance; one per card/slot, includes thread */
40struct bankd_worker {
41 /* global list of workers */
42 struct llist_head list;
43 /* back-pointer to bankd */
44 struct bankd *bankd;
45
Harald Welte8d858292018-08-15 23:36:46 +020046 /* thread number */
47 unsigned int num;
48 /* worker thread state */
49 enum bankd_worker_state state;
Harald Welte694df832018-10-03 22:47:52 +020050 /* timeout to use for blocking read */
51 unsigned int timeout;
Harald Welte8d858292018-08-15 23:36:46 +020052
Harald Welte77911b02018-08-14 23:47:30 +020053 /* 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 Welte371d0262018-08-16 15:23:58 +020064 struct client_slot clslot;
Harald Welte77911b02018-08-14 23:47:30 +020065 } 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 */
82struct bankd {
83 struct {
84 uint16_t bank_id;
85 } cfg;
86
Harald Weltef1dd1622018-09-24 14:54:23 +020087 struct app_comp_id comp_id;
88
Harald Welte77911b02018-08-14 23:47:30 +020089 /* TCP socket at which we are listening */
90 int accept_fd;
91
Harald Weltecbd18962019-03-03 19:02:38 +010092 /* list of slot mappings. only ever modified in main thread! */
93 struct slotmaps *slotmaps;
Harald Welte77911b02018-08-14 23:47:30 +020094
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 Welte45c948c2018-09-23 19:26:52 +020098
99 struct llist_head pcsc_slot_names;
Harald Welte77911b02018-08-14 23:47:30 +0200100};
Harald Welte45c948c2018-09-23 19:26:52 +0200101
102int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file);
103const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot);