blob: eabf1329c5632007263d9451073a0c277af346b1 [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
67
68/* bankd worker instance; one per card/slot, includes thread */
69struct bankd_worker {
70 /* global list of workers */
71 struct llist_head list;
72 /* back-pointer to bankd */
73 struct bankd *bankd;
74
75 /* slot number we are representing */
76 struct bank_slot slot;
77
78 /* thread of this worker. */
79 pthread_t thread;
80
81 /* File descriptor of the TCP connection to the remsim-client (modem) */
82 struct {
83 int fd;
84 struct sockaddr_storage peer_addr;
85 socklen_t peer_addr_len;
86 } client;
87
88 struct {
89 const char *name;
90 union {
91 struct {
92 /* PC/SC context / application handle */
93 SCARDCONTEXT hContext;
94 /* PC/SC card handle */
95 SCARDHANDLE hCard;
96 } pcsc;
97 };
98 } reader;
99};
100
101
102/* global bank deamon */
103struct bankd {
104 struct {
105 uint16_t bank_id;
106 } cfg;
107
108 /* TCP socket at which we are listening */
109 int accept_fd;
110
111 /* list of slit mappings. only ever modified in main thread! */
112 struct llist_head slot_mappings;
113 pthread_rwlock_t slot_mappings_rwlock;
114
115 /* list of bankd_workers. accessed/modified by multiple threads; protected by mutex */
116 struct llist_head workers;
117 pthread_mutex_t workers_mutex;
118};