blob: 3545d686a8a9bea4d2a1bc38529fb4900ed5e6b9 [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 Welte77911b02018-08-14 23:47:30 +020019
20#include <osmocom/gsm/ipa.h>
21#include <osmocom/gsm/protocol/ipaccess.h>
22
23#include <asn1c/asn_application.h>
24#include <osmocom/rspro/RsproPDU.h>
25
26#include "bankd.h"
Harald Welte796a7492018-09-23 19:31:28 +020027#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020028
29__thread void *talloc_asn1_ctx;
30
31static void *worker_main(void *arg);
32
33/***********************************************************************
34* bankd core / main thread
35***********************************************************************/
36
Harald Weltef94b9ee2018-09-25 15:04:21 +020037static const struct log_info_cat default_categories[] = {
38 [DMAIN] = {
39 .name = "DMAIN",
40 .loglevel = LOGL_DEBUG,
41 .enabled = 1,
42 },
43};
44
45static const struct log_info log_info = {
46 .cat = default_categories,
47 .num_cat = ARRAY_SIZE(default_categories),
48};
49
Harald Welte77911b02018-08-14 23:47:30 +020050static void bankd_init(struct bankd *bankd)
51{
Harald Weltef94b9ee2018-09-25 15:04:21 +020052 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
53 osmo_init_logging2(g_tall_ctx, &log_info);
54
Harald Welte77911b02018-08-14 23:47:30 +020055 /* intialize members of 'bankd' */
56 INIT_LLIST_HEAD(&bankd->slot_mappings);
57 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
58 INIT_LLIST_HEAD(&bankd->workers);
59 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020060
Harald Weltef1dd1622018-09-24 14:54:23 +020061 bankd->comp_id.type = ComponentType_remsimBankd;
62 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
63 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
64 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
65 /* FIXME: other members of app_comp_id */
66
Harald Welte45c948c2018-09-23 19:26:52 +020067 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
68 * read once during bankd initialization, when the worker threads haven't
69 * started yet */
70 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
71 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020072
73 /* HACK HACK HACK */
74 {
75 struct bank_slot bs = { .bank_id = 1, };
76 struct client_slot cs = { .client_id = 23, };
77 int i;
78 for (i = 0; i < 5; i++) {
79 bs.slot_nr = cs.slot_nr = i;
80 bankd_slotmap_add(bankd, &bs, &cs);
81 }
82 }
Harald Welte77911b02018-08-14 23:47:30 +020083}
84
85/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020086static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020087{
88 struct bankd_worker *worker;
89 int rc;
90
91 worker = talloc_zero(bankd, struct bankd_worker);
92 if (!worker)
93 return NULL;
94
95 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020096 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020097
98 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
99
100 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
101 if (rc != 0) {
102 talloc_free(worker);
103 return NULL;
104 }
105
106 pthread_mutex_lock(&bankd->workers_mutex);
107 llist_add_tail(&worker->list, &bankd->workers);
108 pthread_mutex_unlock(&bankd->workers_mutex);
109
110 return worker;
111}
112
113static bool terminate = false;
114
115int main(int argc, char **argv)
116{
117 struct bankd *bankd = talloc_zero(NULL, struct bankd);
118 int i, rc;
119
120 OSMO_ASSERT(bankd);
121 bankd_init(bankd);
122
Harald Welte12534e72018-08-15 23:37:29 +0200123 /* create listening socket */
124 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
125 if (rc < 0)
126 exit(1);
127 bankd->accept_fd = rc;
128
129 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200130 for (i = 0; i < 10; i++) {
131 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200132 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200133 if (!w)
134 exit(21);
135 }
136
137 while (1) {
138 if (terminate)
139 break;
Harald Welte7f684a02018-08-16 14:43:50 +0200140 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200141 }
142
143 talloc_free(bankd);
144 exit(0);
145}
146
147
148
149/***********************************************************************
150 * bankd worker thread
151 ***********************************************************************/
152
Harald Welte8d858292018-08-15 23:36:46 +0200153struct value_string worker_state_names[] = {
154 { BW_ST_INIT, "INIT" },
155 { BW_ST_ACCEPTING, "ACCEPTING" },
156 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
157 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200158 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200159 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
160 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
161 { 0, NULL }
162};
163
Harald Welteceb3e682018-08-16 14:47:11 +0200164#define LOGW(w, fmt, args...) \
165 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
166 __FILE__, __LINE__, ## args)
167
Harald Welteaf614732018-08-17 22:10:05 +0200168#define PCSC_ERROR(w, rv, text) \
169if (rv != SCARD_S_SUCCESS) { \
170 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
171 goto end; \
172} else { \
173 LOGW((w), ": OK\n\n"); \
174}
175
Harald Welte8d858292018-08-15 23:36:46 +0200176static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
177{
178 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
179 worker->state = new_state;
180}
Harald Welte77911b02018-08-14 23:47:30 +0200181
182static void worker_cleanup(void *arg)
183{
184 struct bankd_worker *worker = (struct bankd_worker *) arg;
185 struct bankd *bankd = worker->bankd;
186
187 /* FIXME: should we still do this? in the thread ?!? */
188 pthread_mutex_lock(&bankd->workers_mutex);
189 llist_del(&worker->list);
190 talloc_free(worker); /* FIXME: is this safe? */
191 pthread_mutex_unlock(&bankd->workers_mutex);
192}
193
194
Harald Welteaf614732018-08-17 22:10:05 +0200195static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200196{
Harald Welteaf614732018-08-17 22:10:05 +0200197 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200198
Harald Welte45c948c2018-09-23 19:26:52 +0200199 /* resolve PC/SC reader name from slot_id -> name map */
200 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
201 OSMO_ASSERT(worker->reader.name);
202
203 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
204
Harald Welte77911b02018-08-14 23:47:30 +0200205 /* The PC/SC context must be created inside the thread where we'll later use it */
206 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
Harald Welteaf614732018-08-17 22:10:05 +0200207 PCSC_ERROR(worker, rc, "SCardEstablishContext")
Harald Welte77911b02018-08-14 23:47:30 +0200208
Harald Welte48865c22018-09-23 19:30:07 +0200209 DWORD dwActiveProtocol;
Harald Welte77911b02018-08-14 23:47:30 +0200210 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
Harald Welte48865c22018-09-23 19:30:07 +0200211 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
Harald Welteaf614732018-08-17 22:10:05 +0200212 PCSC_ERROR(worker, rc, "SCardConnect")
Harald Welte77911b02018-08-14 23:47:30 +0200213
Harald Welte57593f02018-09-23 19:30:31 +0200214 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
215
Harald Welteaf614732018-08-17 22:10:05 +0200216 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200217end:
Harald Welteaf614732018-08-17 22:10:05 +0200218 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200219}
Harald Welte77911b02018-08-14 23:47:30 +0200220
221
222static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
223{
224 struct ipaccess_head *hh;
225 uint16_t len;
226 int needed, rc;
227
228 if (buf_size < sizeof(*hh))
229 return -1;
230
231 hh = (struct ipaccess_head *) buf;
232
233 /* 1) blocking read from the socket (IPA header) */
234 rc = read(fd, buf, sizeof(*hh));
235 if (rc < sizeof(*hh))
236 return -2;
237
238 len = ntohs(hh->len);
239 needed = len; //- sizeof(*hh);
240
241 /* 2) blocking read from the socket (payload) */
242 rc = read(fd, buf+sizeof(*hh), needed);
243 if (rc < needed)
244 return -3;
245
246 return len;
247}
248
Harald Welte796a7492018-09-23 19:31:28 +0200249static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
250{
251 struct msgb *msg = rspro_enc_msg(pdu);
252 int rc;
253
254 if (!msg) {
255 LOGW(worker, "error encoding RSPRO\n");
256 return -1;
257 }
258
Harald Weltefd471192018-09-24 14:51:14 +0200259 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200260 /* prepend the header */
261 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200262 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200263
264 /* actually send it through the socket */
265 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
266 if (rc == msgb_length(msg))
267 rc = 0;
268 else {
269 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
270 rc = -1;
271 }
272
273 msgb_free(msg);
274
275 return rc;
276}
277
Harald Weltecce2aad2018-08-16 14:44:37 +0200278static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
279{
Harald Welteaf614732018-08-17 22:10:05 +0200280 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
281 struct bankd_slot_mapping *slmap;
Harald Welte3e689872018-09-24 14:52:56 +0200282 e_ResultCode res;
283 RsproPDU_t *resp;
284 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200285
Harald Weltecce2aad2018-08-16 14:44:37 +0200286 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
287
Harald Weltecce2aad2018-08-16 14:44:37 +0200288 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
289 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
290 /* FIXME: store somewhere? */
291
292 if (worker->state != BW_ST_CONN_WAIT_ID) {
293 LOGW(worker, "Unexpected connectClientReq\n");
294 return -102;
295 }
296
Harald Welte371d0262018-08-16 15:23:58 +0200297 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200298 LOGW(worker, "missing clientID, aborting\n");
299 return -103;
300 }
Harald Welte371d0262018-08-16 15:23:58 +0200301 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
302 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200303 worker_set_state(worker, BW_ST_CONN_CLIENT);
304
Harald Welteaf614732018-08-17 22:10:05 +0200305 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
306 if (!slmap) {
307 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
308 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
309 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
310 /* FIXME: how to update the map in case a map is installed later */
Harald Welte3e689872018-09-24 14:52:56 +0200311 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200312 } else {
313 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
314 slmap->client.client_id, slmap->client.slot_nr,
315 slmap->bank.bank_id, slmap->bank.slot_nr);
Harald Welte796a7492018-09-23 19:31:28 +0200316 worker->slot = slmap->bank;
Harald Welteaf614732018-08-17 22:10:05 +0200317 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
Harald Welte796a7492018-09-23 19:31:28 +0200318 /* actually open the mapped reader/card/slot */
Harald Welte3e689872018-09-24 14:52:56 +0200319 rc = worker_open_card(worker);
320 if (rc == 0)
321 res = ResultCode_ok;
322 else
323 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200324 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200325
Harald Welte3e689872018-09-24 14:52:56 +0200326 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
327 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200328}
329
Harald Welte796a7492018-09-23 19:31:28 +0200330static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
331{
332 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
333 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
334 SCARD_IO_REQUEST pioRecvPci;
335 uint8_t rx_buf[1024];
336 DWORD rx_buf_len = sizeof(rx_buf);
337 RsproPDU_t *pdu_resp;
338 long rc;
339
340 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
341
342 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
343 LOGW(worker, "Unexpected tpduModemToCaard\n");
344 return -104;
345 }
346
347 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
348
349 rc = SCardTransmit(worker->reader.pcsc.hCard,
350 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
351 &pioRecvPci, rx_buf, &rx_buf_len);
352 PCSC_ERROR(worker, rc, "SCardTransmit");
353
354 /* encode response PDU and send it */
355 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
356 rx_buf, rx_buf_len);
357 worker_send_rspro(worker, pdu_resp);
358
359 return 0;
360end:
361 return rc;
362}
363
Harald Welte77911b02018-08-14 23:47:30 +0200364/* handle one incoming RSPRO message from a client inside a worker thread */
365static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
366{
Harald Weltecce2aad2018-08-16 14:44:37 +0200367 int rc = -100;
368
Harald Welte77911b02018-08-14 23:47:30 +0200369 switch (pdu->msg.present) {
370 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200371 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200372 break;
373 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200374 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200375 break;
376 case RsproPDUchoice_PR_clientSlotStatusInd:
377 /* FIXME */
378 break;
379 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200380 rc = -101;
381 break;
Harald Welte77911b02018-08-14 23:47:30 +0200382 }
383
Harald Weltecce2aad2018-08-16 14:44:37 +0200384 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200385}
386
387/* body of the main transceive loop */
388static int worker_transceive_loop(struct bankd_worker *worker)
389{
390 struct ipaccess_head *hh;
391 struct ipaccess_head_ext *hh_ext;
392 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
393 asn_dec_rval_t rval;
394 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200395 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200396
397 /* 1) blocking read of entire IPA message from the socket */
398 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
399 if (rc < 0)
400 return rc;
401 data_len = rc;
402
403 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200404 if (hh->proto != IPAC_PROTO_OSMO) {
405 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200406 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200407 }
Harald Welte77911b02018-08-14 23:47:30 +0200408
409 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200410 if (data_len < sizeof(*hh_ext)) {
411 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200412 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200413 }
Harald Welte77911b02018-08-14 23:47:30 +0200414 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200415 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
416 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200417 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200418 }
Harald Welte77911b02018-08-14 23:47:30 +0200419
420 /* 2) ASN1 BER decode of the message */
421 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200422 if (rval.code != RC_OK) {
423 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200424 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200425 }
Harald Welte77911b02018-08-14 23:47:30 +0200426
427 /* 3) handling of the message, possibly resulting in PCSC commands */
428 rc = worker_handle_rspro(worker, pdu);
429 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200430 if (rc < 0) {
431 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200432 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200433 }
Harald Welte77911b02018-08-14 23:47:30 +0200434
435 /* everything OK if we reach here */
436 return 0;
437}
438
Harald Welted6dfb8c2018-08-16 14:46:53 +0200439/* obtain an ascii representation of the client IP/port */
440static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
441{
442 char hostbuf[32], portbuf[32];
443 int rc;
444
445 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
446 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
447 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
448 if (rc != 0) {
449 out[0] = '\0';
450 return -1;
451 }
452 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
453 return 0;
454}
455
Harald Welte77911b02018-08-14 23:47:30 +0200456/* worker thread main function */
457static void *worker_main(void *arg)
458{
459 struct bankd_worker *worker = (struct bankd_worker *) arg;
460 void *top_ctx;
461 int rc;
462
Harald Welte8d858292018-08-15 23:36:46 +0200463 worker_set_state(worker, BW_ST_INIT);
464
Harald Welte77911b02018-08-14 23:47:30 +0200465 /* not permitted in multithreaded environment */
466 talloc_disable_null_tracking();
467 top_ctx = talloc_named_const(NULL, 0, "top");
468 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
469
470 /* push cleanup helper */
471 pthread_cleanup_push(&worker_cleanup, worker);
472
473 /* we continuously perform the same loop here, recycling the worker thread
474 * once the client connection is gone or we have some trouble with the card/reader */
475 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200476 char buf[128];
477
Harald Welte77911b02018-08-14 23:47:30 +0200478 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
479
Harald Welte8d858292018-08-15 23:36:46 +0200480 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200481 /* first wait for an incoming TCP connection */
482 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
483 &worker->client.peer_addr_len);
484 if (rc < 0) {
485 continue;
486 }
487 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200488 worker_client_addrstr(buf, sizeof(buf), worker);
489 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200490 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200491
492 /* run the main worker transceive loop body until there was some error */
493 while (1) {
494 rc = worker_transceive_loop(worker);
495 if (rc < 0)
496 break;
497 }
498
Harald Welted6dfb8c2018-08-16 14:46:53 +0200499 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
500
Harald Welte77911b02018-08-14 23:47:30 +0200501 /* clean-up: reset to sane state */
502 if (worker->reader.pcsc.hCard) {
503 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
504 worker->reader.pcsc.hCard = 0;
505 }
506 if (worker->reader.pcsc.hContext) {
507 SCardReleaseContext(worker->reader.pcsc.hContext);
508 worker->reader.pcsc.hContext = 0;
509 }
510 if (worker->client.fd >= 0)
511 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200512 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200513 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200514 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200515 }
516
517 pthread_cleanup_pop(1);
518 talloc_free(top_ctx);
519 pthread_exit(NULL);
520}