blob: 61bf34acf40a7e4a69a39dcfcfcc1b7cc8f61fed [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,
Harald Welteaf614732018-08-17 22:10:05 +020076 /* TCP established, client has identified itself, waiting for mapping */
77 BW_ST_CONN_CLIENT_WAIT_MAP,
Harald Welte8d858292018-08-15 23:36:46 +020078 /* TCP established, client has identified itself, mapping exists */
79 BW_ST_CONN_CLIENT_MAPPED,
80 /* TCP established, client identified, mapping exists, card opened */
81 BW_ST_CONN_CLIENT_MAPPED_CARD,
82};
83
Harald Welte77911b02018-08-14 23:47:30 +020084
85/* bankd worker instance; one per card/slot, includes thread */
86struct bankd_worker {
87 /* global list of workers */
88 struct llist_head list;
89 /* back-pointer to bankd */
90 struct bankd *bankd;
91
Harald Welte8d858292018-08-15 23:36:46 +020092 /* thread number */
93 unsigned int num;
94 /* worker thread state */
95 enum bankd_worker_state state;
96
Harald Welte77911b02018-08-14 23:47:30 +020097 /* slot number we are representing */
98 struct bank_slot slot;
99
100 /* thread of this worker. */
101 pthread_t thread;
102
103 /* File descriptor of the TCP connection to the remsim-client (modem) */
104 struct {
105 int fd;
106 struct sockaddr_storage peer_addr;
107 socklen_t peer_addr_len;
Harald Welte371d0262018-08-16 15:23:58 +0200108 struct client_slot clslot;
Harald Welte77911b02018-08-14 23:47:30 +0200109 } client;
110
111 struct {
112 const char *name;
113 union {
114 struct {
115 /* PC/SC context / application handle */
116 SCARDCONTEXT hContext;
117 /* PC/SC card handle */
118 SCARDHANDLE hCard;
119 } pcsc;
120 };
121 } reader;
122};
123
124
125/* global bank deamon */
126struct bankd {
127 struct {
128 uint16_t bank_id;
129 } cfg;
130
131 /* TCP socket at which we are listening */
132 int accept_fd;
133
134 /* list of slit mappings. only ever modified in main thread! */
135 struct llist_head slot_mappings;
136 pthread_rwlock_t slot_mappings_rwlock;
137
138 /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */
139 struct llist_head workers;
140 pthread_mutex_t workers_mutex;
141};