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