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