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