blob: 5f1844aadbd1a04454256498479ae60d7e29b81b [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
Harald Welted6dfb8c2018-08-16 14:46:53 +020012#include <sys/socket.h>
13#include <netdb.h>
14
Harald Welte12534e72018-08-15 23:37:29 +020015#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020016#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020017#include <osmocom/core/logging.h>
18#include <osmocom/core/application.h>
Harald Welted08d5052018-10-03 20:43:31 +020019#include <osmocom/core/fsm.h>
Harald Welte77911b02018-08-14 23:47:30 +020020
21#include <osmocom/gsm/ipa.h>
22#include <osmocom/gsm/protocol/ipaccess.h>
23
24#include <asn1c/asn_application.h>
25#include <osmocom/rspro/RsproPDU.h>
26
27#include "bankd.h"
Harald Welte796a7492018-09-23 19:31:28 +020028#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020029
30__thread void *talloc_asn1_ctx;
31
32static void *worker_main(void *arg);
33
34/***********************************************************************
35* bankd core / main thread
36***********************************************************************/
37
Harald Weltef94b9ee2018-09-25 15:04:21 +020038static const struct log_info_cat default_categories[] = {
39 [DMAIN] = {
40 .name = "DMAIN",
41 .loglevel = LOGL_DEBUG,
42 .enabled = 1,
43 },
44};
45
46static const struct log_info log_info = {
47 .cat = default_categories,
48 .num_cat = ARRAY_SIZE(default_categories),
49};
50
Harald Welte77911b02018-08-14 23:47:30 +020051static void bankd_init(struct bankd *bankd)
52{
Harald Weltef94b9ee2018-09-25 15:04:21 +020053 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
54 osmo_init_logging2(g_tall_ctx, &log_info);
55
Harald Welte77911b02018-08-14 23:47:30 +020056 /* intialize members of 'bankd' */
57 INIT_LLIST_HEAD(&bankd->slot_mappings);
58 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
59 INIT_LLIST_HEAD(&bankd->workers);
60 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020061
Harald Weltef1dd1622018-09-24 14:54:23 +020062 bankd->comp_id.type = ComponentType_remsimBankd;
63 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
64 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
65 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
66 /* FIXME: other members of app_comp_id */
67
Harald Welte45c948c2018-09-23 19:26:52 +020068 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
69 * read once during bankd initialization, when the worker threads haven't
70 * started yet */
71 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
72 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020073
74 /* HACK HACK HACK */
75 {
76 struct bank_slot bs = { .bank_id = 1, };
77 struct client_slot cs = { .client_id = 23, };
78 int i;
79 for (i = 0; i < 5; i++) {
80 bs.slot_nr = cs.slot_nr = i;
81 bankd_slotmap_add(bankd, &bs, &cs);
82 }
83 }
Harald Welte77911b02018-08-14 23:47:30 +020084}
85
86/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020087static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020088{
89 struct bankd_worker *worker;
90 int rc;
91
92 worker = talloc_zero(bankd, struct bankd_worker);
93 if (!worker)
94 return NULL;
95
96 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020097 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020098
99 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
100
101 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
102 if (rc != 0) {
103 talloc_free(worker);
104 return NULL;
105 }
106
107 pthread_mutex_lock(&bankd->workers_mutex);
108 llist_add_tail(&worker->list, &bankd->workers);
109 pthread_mutex_unlock(&bankd->workers_mutex);
110
111 return worker;
112}
113
114static bool terminate = false;
115
116int main(int argc, char **argv)
117{
118 struct bankd *bankd = talloc_zero(NULL, struct bankd);
119 int i, rc;
120
121 OSMO_ASSERT(bankd);
122 bankd_init(bankd);
123
Harald Welte12534e72018-08-15 23:37:29 +0200124 /* create listening socket */
125 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
126 if (rc < 0)
127 exit(1);
128 bankd->accept_fd = rc;
129
130 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200131 for (i = 0; i < 10; i++) {
132 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200133 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200134 if (!w)
135 exit(21);
136 }
137
138 while (1) {
139 if (terminate)
140 break;
Harald Welte7f684a02018-08-16 14:43:50 +0200141 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200142 }
143
144 talloc_free(bankd);
145 exit(0);
146}
147
148
149
150/***********************************************************************
151 * bankd worker thread
152 ***********************************************************************/
153
Harald Welte8d858292018-08-15 23:36:46 +0200154struct value_string worker_state_names[] = {
155 { BW_ST_INIT, "INIT" },
156 { BW_ST_ACCEPTING, "ACCEPTING" },
157 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
158 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200159 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200160 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
161 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
162 { 0, NULL }
163};
164
Harald Welteceb3e682018-08-16 14:47:11 +0200165#define LOGW(w, fmt, args...) \
166 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
167 __FILE__, __LINE__, ## args)
168
Harald Welteaf614732018-08-17 22:10:05 +0200169#define PCSC_ERROR(w, rv, text) \
170if (rv != SCARD_S_SUCCESS) { \
171 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
172 goto end; \
173} else { \
174 LOGW((w), ": OK\n\n"); \
175}
176
Harald Welte8d858292018-08-15 23:36:46 +0200177static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
178{
179 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
180 worker->state = new_state;
181}
Harald Welte77911b02018-08-14 23:47:30 +0200182
183static void worker_cleanup(void *arg)
184{
185 struct bankd_worker *worker = (struct bankd_worker *) arg;
186 struct bankd *bankd = worker->bankd;
187
188 /* FIXME: should we still do this? in the thread ?!? */
189 pthread_mutex_lock(&bankd->workers_mutex);
190 llist_del(&worker->list);
191 talloc_free(worker); /* FIXME: is this safe? */
192 pthread_mutex_unlock(&bankd->workers_mutex);
193}
194
195
Harald Welteaf614732018-08-17 22:10:05 +0200196static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200197{
Harald Welteaf614732018-08-17 22:10:05 +0200198 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200199
Harald Welte45c948c2018-09-23 19:26:52 +0200200 /* resolve PC/SC reader name from slot_id -> name map */
201 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
202 OSMO_ASSERT(worker->reader.name);
203
204 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
205
Harald Welte77911b02018-08-14 23:47:30 +0200206 /* The PC/SC context must be created inside the thread where we'll later use it */
207 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
Harald Welteaf614732018-08-17 22:10:05 +0200208 PCSC_ERROR(worker, rc, "SCardEstablishContext")
Harald Welte77911b02018-08-14 23:47:30 +0200209
Harald Welted08d5052018-10-03 20:43:31 +0200210 worker->reader.fi = sc_fsm_alloc(worker);
Harald Welte77911b02018-08-14 23:47:30 +0200211
Harald Welte57593f02018-09-23 19:30:31 +0200212 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
213
Harald Welteaf614732018-08-17 22:10:05 +0200214 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200215end:
Harald Welteaf614732018-08-17 22:10:05 +0200216 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200217}
Harald Welte77911b02018-08-14 23:47:30 +0200218
219
220static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
221{
222 struct ipaccess_head *hh;
223 uint16_t len;
224 int needed, rc;
225
226 if (buf_size < sizeof(*hh))
227 return -1;
228
229 hh = (struct ipaccess_head *) buf;
230
231 /* 1) blocking read from the socket (IPA header) */
232 rc = read(fd, buf, sizeof(*hh));
233 if (rc < sizeof(*hh))
234 return -2;
235
236 len = ntohs(hh->len);
237 needed = len; //- sizeof(*hh);
238
239 /* 2) blocking read from the socket (payload) */
240 rc = read(fd, buf+sizeof(*hh), needed);
241 if (rc < needed)
242 return -3;
243
244 return len;
245}
246
Harald Welted08d5052018-10-03 20:43:31 +0200247int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
Harald Welte796a7492018-09-23 19:31:28 +0200248{
249 struct msgb *msg = rspro_enc_msg(pdu);
250 int rc;
251
252 if (!msg) {
253 LOGW(worker, "error encoding RSPRO\n");
254 return -1;
255 }
256
Harald Weltefd471192018-09-24 14:51:14 +0200257 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200258 /* prepend the header */
259 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200260 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200261
Harald Welted08d5052018-10-03 20:43:31 +0200262 printf("tx: %s\n", msgb_hexdump(msg));
Harald Welte796a7492018-09-23 19:31:28 +0200263 /* actually send it through the socket */
264 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
265 if (rc == msgb_length(msg))
266 rc = 0;
267 else {
268 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
269 rc = -1;
270 }
271
272 msgb_free(msg);
273
274 return rc;
275}
276
Harald Weltecce2aad2018-08-16 14:44:37 +0200277static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
278{
Harald Welteaf614732018-08-17 22:10:05 +0200279 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
280 struct bankd_slot_mapping *slmap;
Harald Welte3e689872018-09-24 14:52:56 +0200281 e_ResultCode res;
282 RsproPDU_t *resp;
283 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200284
Harald Weltecce2aad2018-08-16 14:44:37 +0200285 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
286
Harald Weltecce2aad2018-08-16 14:44:37 +0200287 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
288 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
289 /* FIXME: store somewhere? */
290
291 if (worker->state != BW_ST_CONN_WAIT_ID) {
292 LOGW(worker, "Unexpected connectClientReq\n");
293 return -102;
294 }
295
Harald Welte371d0262018-08-16 15:23:58 +0200296 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200297 LOGW(worker, "missing clientID, aborting\n");
298 return -103;
299 }
Harald Welte371d0262018-08-16 15:23:58 +0200300 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
301 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200302 worker_set_state(worker, BW_ST_CONN_CLIENT);
303
Harald Welteaf614732018-08-17 22:10:05 +0200304 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
305 if (!slmap) {
306 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
307 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
308 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
309 /* FIXME: how to update the map in case a map is installed later */
Harald Welte3e689872018-09-24 14:52:56 +0200310 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200311 } else {
312 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
313 slmap->client.client_id, slmap->client.slot_nr,
314 slmap->bank.bank_id, slmap->bank.slot_nr);
Harald Welte796a7492018-09-23 19:31:28 +0200315 worker->slot = slmap->bank;
Harald Welteaf614732018-08-17 22:10:05 +0200316 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
Harald Welte796a7492018-09-23 19:31:28 +0200317 /* actually open the mapped reader/card/slot */
Harald Welte3e689872018-09-24 14:52:56 +0200318 rc = worker_open_card(worker);
319 if (rc == 0)
320 res = ResultCode_ok;
321 else
322 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200323 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200324
Harald Welte3e689872018-09-24 14:52:56 +0200325 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
326 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200327}
328
Harald Welted08d5052018-10-03 20:43:31 +0200329int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
Harald Welte796a7492018-09-23 19:31:28 +0200330{
331 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
332 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
333 SCARD_IO_REQUEST pioRecvPci;
334 uint8_t rx_buf[1024];
335 DWORD rx_buf_len = sizeof(rx_buf);
336 RsproPDU_t *pdu_resp;
337 long rc;
338
339 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
340
Harald Welte796a7492018-09-23 19:31:28 +0200341 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
342
343 rc = SCardTransmit(worker->reader.pcsc.hCard,
344 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
345 &pioRecvPci, rx_buf, &rx_buf_len);
346 PCSC_ERROR(worker, rc, "SCardTransmit");
347
348 /* encode response PDU and send it */
349 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
350 rx_buf, rx_buf_len);
351 worker_send_rspro(worker, pdu_resp);
352
353 return 0;
354end:
355 return rc;
356}
357
Harald Welte77911b02018-08-14 23:47:30 +0200358/* handle one incoming RSPRO message from a client inside a worker thread */
359static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
360{
Harald Weltecce2aad2018-08-16 14:44:37 +0200361 int rc = -100;
362
Harald Welte77911b02018-08-14 23:47:30 +0200363 switch (pdu->msg.present) {
364 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200365 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200366 break;
367 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welted08d5052018-10-03 20:43:31 +0200368 osmo_fsm_inst_dispatch(worker->reader.fi, SC_E_TPDU_CMD, (void *)pdu);
369 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200370 break;
371 case RsproPDUchoice_PR_clientSlotStatusInd:
372 /* FIXME */
373 break;
374 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200375 rc = -101;
376 break;
Harald Welte77911b02018-08-14 23:47:30 +0200377 }
378
Harald Weltecce2aad2018-08-16 14:44:37 +0200379 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200380}
381
382/* body of the main transceive loop */
383static int worker_transceive_loop(struct bankd_worker *worker)
384{
385 struct ipaccess_head *hh;
386 struct ipaccess_head_ext *hh_ext;
387 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
388 asn_dec_rval_t rval;
389 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200390 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200391
392 /* 1) blocking read of entire IPA message from the socket */
393 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
394 if (rc < 0)
395 return rc;
396 data_len = rc;
397
398 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200399 if (hh->proto != IPAC_PROTO_OSMO) {
400 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200401 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200402 }
Harald Welte77911b02018-08-14 23:47:30 +0200403
404 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200405 if (data_len < sizeof(*hh_ext)) {
406 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200407 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200408 }
Harald Welte77911b02018-08-14 23:47:30 +0200409 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200410 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
411 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200412 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200413 }
Harald Welte77911b02018-08-14 23:47:30 +0200414
415 /* 2) ASN1 BER decode of the message */
416 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200417 if (rval.code != RC_OK) {
418 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200419 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200420 }
Harald Welte77911b02018-08-14 23:47:30 +0200421
422 /* 3) handling of the message, possibly resulting in PCSC commands */
423 rc = worker_handle_rspro(worker, pdu);
424 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200425 if (rc < 0) {
426 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200427 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200428 }
Harald Welte77911b02018-08-14 23:47:30 +0200429
430 /* everything OK if we reach here */
431 return 0;
432}
433
Harald Welted6dfb8c2018-08-16 14:46:53 +0200434/* obtain an ascii representation of the client IP/port */
435static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
436{
437 char hostbuf[32], portbuf[32];
438 int rc;
439
440 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
441 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
442 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
443 if (rc != 0) {
444 out[0] = '\0';
445 return -1;
446 }
447 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
448 return 0;
449}
450
Harald Welte77911b02018-08-14 23:47:30 +0200451/* worker thread main function */
452static void *worker_main(void *arg)
453{
454 struct bankd_worker *worker = (struct bankd_worker *) arg;
455 void *top_ctx;
456 int rc;
457
Harald Welte8d858292018-08-15 23:36:46 +0200458 worker_set_state(worker, BW_ST_INIT);
459
Harald Welte77911b02018-08-14 23:47:30 +0200460 /* not permitted in multithreaded environment */
461 talloc_disable_null_tracking();
462 top_ctx = talloc_named_const(NULL, 0, "top");
463 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
464
465 /* push cleanup helper */
466 pthread_cleanup_push(&worker_cleanup, worker);
467
468 /* we continuously perform the same loop here, recycling the worker thread
469 * once the client connection is gone or we have some trouble with the card/reader */
470 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200471 char buf[128];
472
Harald Welte77911b02018-08-14 23:47:30 +0200473 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
474
Harald Welte8d858292018-08-15 23:36:46 +0200475 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200476 /* first wait for an incoming TCP connection */
477 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
478 &worker->client.peer_addr_len);
479 if (rc < 0) {
480 continue;
481 }
482 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200483 worker_client_addrstr(buf, sizeof(buf), worker);
484 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200485 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200486
487 /* run the main worker transceive loop body until there was some error */
488 while (1) {
489 rc = worker_transceive_loop(worker);
490 if (rc < 0)
491 break;
492 }
493
Harald Welted6dfb8c2018-08-16 14:46:53 +0200494 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
495
Harald Welte77911b02018-08-14 23:47:30 +0200496 /* clean-up: reset to sane state */
497 if (worker->reader.pcsc.hCard) {
498 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
499 worker->reader.pcsc.hCard = 0;
500 }
501 if (worker->reader.pcsc.hContext) {
502 SCardReleaseContext(worker->reader.pcsc.hContext);
503 worker->reader.pcsc.hContext = 0;
504 }
505 if (worker->client.fd >= 0)
506 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200507 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200508 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200509 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200510 }
511
512 pthread_cleanup_pop(1);
513 talloc_free(top_ctx);
514 pthread_exit(NULL);
515}