blob: 3577d15d6d93f7b3445240172cea5fa46395d3fd [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"
16
Harald Welte77911b02018-08-14 23:47:30 +020017struct bankd;
18
19struct bank_slot {
20 uint16_t bank_id;
21 uint16_t slot_nr;
22};
23
24static inline bool bank_slot_equals(const struct bank_slot *a, const struct bank_slot *b)
25{
26 if (a->bank_id == b->bank_id && a->slot_nr == b->slot_nr)
27 return true;
28 else
29 return false;
30}
31
32struct client_slot {
33 uint16_t client_id;
34 uint16_t slot_nr;
35};
36
37static inline bool client_slot_equals(const struct client_slot *a, const struct client_slot *b)
38{
39 if (a->client_id == b->client_id && a->slot_nr == b->slot_nr)
40 return true;
41 else
42 return false;
43}
44
45/* slot mappings are created / removed by the server */
46struct bankd_slot_mapping {
47 /* global lits of bankd slot mappings */
48 struct llist_head list;
49 /* slot on bank side */
50 struct bank_slot bank;
51 /* slot on client side */
52 struct client_slot client;
53};
54
55/* thread-safe lookup of map by client:slot */
56struct bankd_slot_mapping *bankd_slotmap_by_client(struct bankd *bankd,
57 const struct client_slot *client);
58
59/* thread-safe lookup of map by bank:slot */
60struct bankd_slot_mapping *bankd_slotmap_by_bank(struct bankd *bankd, const struct bank_slot *bank);
61
62/* thread-safe creating of a new bank<->client map */
63int bankd_slotmap_add(struct bankd *bankd, const struct bank_slot *bank,
64 const struct client_slot *client);
65
66/* thread-safe removal of a bank<->client map */
67void bankd_slotmap_del(struct bankd *bankd, struct bankd_slot_mapping *map);
68
Harald Welte8d858292018-08-15 23:36:46 +020069enum bankd_worker_state {
70 /* just started*/
71 BW_ST_INIT,
72 /* blocking in the accept() call on the server socket fd */
73 BW_ST_ACCEPTING,
74 /* TCP established, but peer not yet identified itself */
75 BW_ST_CONN_WAIT_ID,
76 /* TCP established, client has identified itself, no mapping */
77 BW_ST_CONN_CLIENT,
Harald Welteaf614732018-08-17 22:10:05 +020078 /* TCP established, client has identified itself, waiting for mapping */
79 BW_ST_CONN_CLIENT_WAIT_MAP,
Harald Welte8d858292018-08-15 23:36:46 +020080 /* TCP established, client has identified itself, mapping exists */
81 BW_ST_CONN_CLIENT_MAPPED,
82 /* TCP established, client identified, mapping exists, card opened */
83 BW_ST_CONN_CLIENT_MAPPED_CARD,
84};
85
Harald Welte77911b02018-08-14 23:47:30 +020086
87/* bankd worker instance; one per card/slot, includes thread */
88struct bankd_worker {
89 /* global list of workers */
90 struct llist_head list;
91 /* back-pointer to bankd */
92 struct bankd *bankd;
93
Harald Welte8d858292018-08-15 23:36:46 +020094 /* thread number */
95 unsigned int num;
96 /* worker thread state */
97 enum bankd_worker_state state;
98
Harald Welte77911b02018-08-14 23:47:30 +020099 /* slot number we are representing */
100 struct bank_slot slot;
101
102 /* thread of this worker. */
103 pthread_t thread;
104
105 /* File descriptor of the TCP connection to the remsim-client (modem) */
106 struct {
107 int fd;
108 struct sockaddr_storage peer_addr;
109 socklen_t peer_addr_len;
Harald Welte371d0262018-08-16 15:23:58 +0200110 struct client_slot clslot;
Harald Welte77911b02018-08-14 23:47:30 +0200111 } client;
112
113 struct {
114 const char *name;
115 union {
116 struct {
117 /* PC/SC context / application handle */
118 SCARDCONTEXT hContext;
119 /* PC/SC card handle */
120 SCARDHANDLE hCard;
121 } pcsc;
122 };
123 } reader;
124};
125
126
127/* global bank deamon */
128struct bankd {
129 struct {
130 uint16_t bank_id;
131 } cfg;
132
Harald Weltef1dd1622018-09-24 14:54:23 +0200133 struct app_comp_id comp_id;
134
Harald Welte77911b02018-08-14 23:47:30 +0200135 /* TCP socket at which we are listening */
136 int accept_fd;
137
138 /* list of slit mappings. only ever modified in main thread! */
139 struct llist_head slot_mappings;
140 pthread_rwlock_t slot_mappings_rwlock;
141
142 /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */
143 struct llist_head workers;
144 pthread_mutex_t workers_mutex;
Harald Welte45c948c2018-09-23 19:26:52 +0200145
146 struct llist_head pcsc_slot_names;
Harald Welte77911b02018-08-14 23:47:30 +0200147};
Harald Welte45c948c2018-09-23 19:26:52 +0200148
149int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file);
150const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot);