blob: a2b293fbb87d8ebed7f32465e11630d54771eef3 [file] [log] [blame]
Harald Welte77911b02018-08-14 23:47:30 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <unistd.h>
5
6#include <pthread.h>
7
8#include <wintypes.h>
9#include <winscard.h>
10#include <pcsclite.h>
11
12#include <osmocom/core/linuxlist.h>
13
14#include <osmocom/gsm/ipa.h>
15#include <osmocom/gsm/protocol/ipaccess.h>
16
17#include <asn1c/asn_application.h>
18#include <osmocom/rspro/RsproPDU.h>
19
20#include "bankd.h"
21
22__thread void *talloc_asn1_ctx;
23
24static void *worker_main(void *arg);
25
26/***********************************************************************
27* bankd core / main thread
28***********************************************************************/
29
30static void bankd_init(struct bankd *bankd)
31{
32 /* intialize members of 'bankd' */
33 INIT_LLIST_HEAD(&bankd->slot_mappings);
34 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
35 INIT_LLIST_HEAD(&bankd->workers);
36 pthread_mutex_init(&bankd->workers_mutex, NULL);
37}
38
39/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020040static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020041{
42 struct bankd_worker *worker;
43 int rc;
44
45 worker = talloc_zero(bankd, struct bankd_worker);
46 if (!worker)
47 return NULL;
48
49 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020050 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020051
52 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
53
54 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
55 if (rc != 0) {
56 talloc_free(worker);
57 return NULL;
58 }
59
60 pthread_mutex_lock(&bankd->workers_mutex);
61 llist_add_tail(&worker->list, &bankd->workers);
62 pthread_mutex_unlock(&bankd->workers_mutex);
63
64 return worker;
65}
66
67static bool terminate = false;
68
69int main(int argc, char **argv)
70{
71 struct bankd *bankd = talloc_zero(NULL, struct bankd);
72 int i, rc;
73
74 OSMO_ASSERT(bankd);
75 bankd_init(bankd);
76
77 for (i = 0; i < 10; i++) {
78 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +020079 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +020080 if (!w)
81 exit(21);
82 }
83
84 while (1) {
85 if (terminate)
86 break;
87 }
88
89 talloc_free(bankd);
90 exit(0);
91}
92
93
94
95/***********************************************************************
96 * bankd worker thread
97 ***********************************************************************/
98
99#define PCSC_ERROR(rv, text) \
100if (rv != SCARD_S_SUCCESS) { \
101 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
102 goto end; \
103} else { \
104 printf(text ": OK\n\n"); \
105}
106
Harald Welte8d858292018-08-15 23:36:46 +0200107#define LOGW(w, fmt, args...) \
108 printf("[%u] " fmt, (w)->num, args)
109
110struct value_string worker_state_names[] = {
111 { BW_ST_INIT, "INIT" },
112 { BW_ST_ACCEPTING, "ACCEPTING" },
113 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
114 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
115 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
116 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
117 { 0, NULL }
118};
119
120static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
121{
122 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
123 worker->state = new_state;
124}
Harald Welte77911b02018-08-14 23:47:30 +0200125
126static void worker_cleanup(void *arg)
127{
128 struct bankd_worker *worker = (struct bankd_worker *) arg;
129 struct bankd *bankd = worker->bankd;
130
131 /* FIXME: should we still do this? in the thread ?!? */
132 pthread_mutex_lock(&bankd->workers_mutex);
133 llist_del(&worker->list);
134 talloc_free(worker); /* FIXME: is this safe? */
135 pthread_mutex_unlock(&bankd->workers_mutex);
136}
137
138
139#if 0
140/* function running inside a worker thread; doing some initialization */
141static void worker_init(struct bankd_worker *worker)
142{
143 int rc;
144
145 /* push cleanup helper */
146 pthread_cleanup_push(&worker_cleanup, worker);
147
148 /* The PC/SC context must be created inside the thread where we'll later use it */
149 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
150 PCSC_ERROR(rc, "SCardEstablishContext")
151
152 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
153 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
154 PCSC_ERROR(rc, "SCardConnect")
155
156 return;
157end:
158 pthread_exit(NULL);
159}
160#endif
161
162
163static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
164{
165 struct ipaccess_head *hh;
166 uint16_t len;
167 int needed, rc;
168
169 if (buf_size < sizeof(*hh))
170 return -1;
171
172 hh = (struct ipaccess_head *) buf;
173
174 /* 1) blocking read from the socket (IPA header) */
175 rc = read(fd, buf, sizeof(*hh));
176 if (rc < sizeof(*hh))
177 return -2;
178
179 len = ntohs(hh->len);
180 needed = len; //- sizeof(*hh);
181
182 /* 2) blocking read from the socket (payload) */
183 rc = read(fd, buf+sizeof(*hh), needed);
184 if (rc < needed)
185 return -3;
186
187 return len;
188}
189
190/* handle one incoming RSPRO message from a client inside a worker thread */
191static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
192{
193 switch (pdu->msg.present) {
194 case RsproPDUchoice_PR_connectClientReq:
195 /* FIXME */
196 break;
197 case RsproPDUchoice_PR_tpduModemToCard:
198 /* FIXME */
199 break;
200 case RsproPDUchoice_PR_clientSlotStatusInd:
201 /* FIXME */
202 break;
203 default:
204 return -100;
205 }
206
207 return 0;
208}
209
210/* body of the main transceive loop */
211static int worker_transceive_loop(struct bankd_worker *worker)
212{
213 struct ipaccess_head *hh;
214 struct ipaccess_head_ext *hh_ext;
215 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
216 asn_dec_rval_t rval;
217 int data_len, rc;
218 RsproPDU_t *pdu;
219
220 /* 1) blocking read of entire IPA message from the socket */
221 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
222 if (rc < 0)
223 return rc;
224 data_len = rc;
225
226 hh = (struct ipaccess_head *) buf;
227 if (hh->proto != IPAC_PROTO_OSMO)
228 return -4;
229
230 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
231 if (data_len < sizeof(*hh_ext))
232 return -5;
233 data_len -= sizeof(*hh_ext);
234 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
235 return -6;
236
237 /* 2) ASN1 BER decode of the message */
238 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
239 if (rval.code != RC_OK)
240 return -7;
241
242 /* 3) handling of the message, possibly resulting in PCSC commands */
243 rc = worker_handle_rspro(worker, pdu);
244 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
245 if (rc < 0)
246 return rc;
247
248 /* everything OK if we reach here */
249 return 0;
250}
251
252/* worker thread main function */
253static void *worker_main(void *arg)
254{
255 struct bankd_worker *worker = (struct bankd_worker *) arg;
256 void *top_ctx;
257 int rc;
258
Harald Welte8d858292018-08-15 23:36:46 +0200259 worker_set_state(worker, BW_ST_INIT);
260
Harald Welte77911b02018-08-14 23:47:30 +0200261 /* not permitted in multithreaded environment */
262 talloc_disable_null_tracking();
263 top_ctx = talloc_named_const(NULL, 0, "top");
264 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
265
266 /* push cleanup helper */
267 pthread_cleanup_push(&worker_cleanup, worker);
268
269 /* we continuously perform the same loop here, recycling the worker thread
270 * once the client connection is gone or we have some trouble with the card/reader */
271 while (1) {
272 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
273
Harald Welte8d858292018-08-15 23:36:46 +0200274 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200275 /* first wait for an incoming TCP connection */
276 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
277 &worker->client.peer_addr_len);
278 if (rc < 0) {
279 continue;
280 }
281 worker->client.fd = rc;
Harald Welte8d858292018-08-15 23:36:46 +0200282 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200283
284 /* run the main worker transceive loop body until there was some error */
285 while (1) {
286 rc = worker_transceive_loop(worker);
287 if (rc < 0)
288 break;
289 }
290
291 /* clean-up: reset to sane state */
292 if (worker->reader.pcsc.hCard) {
293 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
294 worker->reader.pcsc.hCard = 0;
295 }
296 if (worker->reader.pcsc.hContext) {
297 SCardReleaseContext(worker->reader.pcsc.hContext);
298 worker->reader.pcsc.hContext = 0;
299 }
300 if (worker->client.fd >= 0)
301 close(worker->client.fd);
302 worker->client.fd = -1;
303 }
304
305 pthread_cleanup_pop(1);
306 talloc_free(top_ctx);
307 pthread_exit(NULL);
308}