blob: f84d0d0a29a596d13044cedf9ebf30ac49497a9e [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>
17
18#include <osmocom/gsm/ipa.h>
19#include <osmocom/gsm/protocol/ipaccess.h>
20
21#include <asn1c/asn_application.h>
22#include <osmocom/rspro/RsproPDU.h>
23
24#include "bankd.h"
Harald Welte796a7492018-09-23 19:31:28 +020025#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020026
27__thread void *talloc_asn1_ctx;
28
29static void *worker_main(void *arg);
30
31/***********************************************************************
32* bankd core / main thread
33***********************************************************************/
34
35static void bankd_init(struct bankd *bankd)
36{
37 /* intialize members of 'bankd' */
38 INIT_LLIST_HEAD(&bankd->slot_mappings);
39 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
40 INIT_LLIST_HEAD(&bankd->workers);
41 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020042
Harald Weltef1dd1622018-09-24 14:54:23 +020043 bankd->comp_id.type = ComponentType_remsimBankd;
44 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
45 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
46 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
47 /* FIXME: other members of app_comp_id */
48
Harald Welte45c948c2018-09-23 19:26:52 +020049 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
50 * read once during bankd initialization, when the worker threads haven't
51 * started yet */
52 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
53 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020054
55 /* HACK HACK HACK */
56 {
57 struct bank_slot bs = { .bank_id = 1, };
58 struct client_slot cs = { .client_id = 23, };
59 int i;
60 for (i = 0; i < 5; i++) {
61 bs.slot_nr = cs.slot_nr = i;
62 bankd_slotmap_add(bankd, &bs, &cs);
63 }
64 }
Harald Welte77911b02018-08-14 23:47:30 +020065}
66
67/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020068static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020069{
70 struct bankd_worker *worker;
71 int rc;
72
73 worker = talloc_zero(bankd, struct bankd_worker);
74 if (!worker)
75 return NULL;
76
77 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020078 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020079
80 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
81
82 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
83 if (rc != 0) {
84 talloc_free(worker);
85 return NULL;
86 }
87
88 pthread_mutex_lock(&bankd->workers_mutex);
89 llist_add_tail(&worker->list, &bankd->workers);
90 pthread_mutex_unlock(&bankd->workers_mutex);
91
92 return worker;
93}
94
95static bool terminate = false;
96
97int main(int argc, char **argv)
98{
99 struct bankd *bankd = talloc_zero(NULL, struct bankd);
100 int i, rc;
101
102 OSMO_ASSERT(bankd);
103 bankd_init(bankd);
104
Harald Welte12534e72018-08-15 23:37:29 +0200105 /* create listening socket */
106 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
107 if (rc < 0)
108 exit(1);
109 bankd->accept_fd = rc;
110
111 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200112 for (i = 0; i < 10; i++) {
113 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200114 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200115 if (!w)
116 exit(21);
117 }
118
119 while (1) {
120 if (terminate)
121 break;
Harald Welte7f684a02018-08-16 14:43:50 +0200122 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200123 }
124
125 talloc_free(bankd);
126 exit(0);
127}
128
129
130
131/***********************************************************************
132 * bankd worker thread
133 ***********************************************************************/
134
Harald Welte8d858292018-08-15 23:36:46 +0200135struct value_string worker_state_names[] = {
136 { BW_ST_INIT, "INIT" },
137 { BW_ST_ACCEPTING, "ACCEPTING" },
138 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
139 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200140 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200141 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
142 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
143 { 0, NULL }
144};
145
Harald Welteceb3e682018-08-16 14:47:11 +0200146#define LOGW(w, fmt, args...) \
147 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
148 __FILE__, __LINE__, ## args)
149
Harald Welteaf614732018-08-17 22:10:05 +0200150#define PCSC_ERROR(w, rv, text) \
151if (rv != SCARD_S_SUCCESS) { \
152 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
153 goto end; \
154} else { \
155 LOGW((w), ": OK\n\n"); \
156}
157
Harald Welte8d858292018-08-15 23:36:46 +0200158static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
159{
160 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
161 worker->state = new_state;
162}
Harald Welte77911b02018-08-14 23:47:30 +0200163
164static void worker_cleanup(void *arg)
165{
166 struct bankd_worker *worker = (struct bankd_worker *) arg;
167 struct bankd *bankd = worker->bankd;
168
169 /* FIXME: should we still do this? in the thread ?!? */
170 pthread_mutex_lock(&bankd->workers_mutex);
171 llist_del(&worker->list);
172 talloc_free(worker); /* FIXME: is this safe? */
173 pthread_mutex_unlock(&bankd->workers_mutex);
174}
175
176
Harald Welteaf614732018-08-17 22:10:05 +0200177static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200178{
Harald Welteaf614732018-08-17 22:10:05 +0200179 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200180
Harald Welte45c948c2018-09-23 19:26:52 +0200181 /* resolve PC/SC reader name from slot_id -> name map */
182 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
183 OSMO_ASSERT(worker->reader.name);
184
185 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
186
Harald Welte77911b02018-08-14 23:47:30 +0200187 /* The PC/SC context must be created inside the thread where we'll later use it */
188 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
Harald Welteaf614732018-08-17 22:10:05 +0200189 PCSC_ERROR(worker, rc, "SCardEstablishContext")
Harald Welte77911b02018-08-14 23:47:30 +0200190
Harald Welte48865c22018-09-23 19:30:07 +0200191 DWORD dwActiveProtocol;
Harald Welte77911b02018-08-14 23:47:30 +0200192 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
Harald Welte48865c22018-09-23 19:30:07 +0200193 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
Harald Welteaf614732018-08-17 22:10:05 +0200194 PCSC_ERROR(worker, rc, "SCardConnect")
Harald Welte77911b02018-08-14 23:47:30 +0200195
Harald Welte57593f02018-09-23 19:30:31 +0200196 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
197
Harald Welteaf614732018-08-17 22:10:05 +0200198 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200199end:
Harald Welteaf614732018-08-17 22:10:05 +0200200 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200201}
Harald Welte77911b02018-08-14 23:47:30 +0200202
203
204static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
205{
206 struct ipaccess_head *hh;
207 uint16_t len;
208 int needed, rc;
209
210 if (buf_size < sizeof(*hh))
211 return -1;
212
213 hh = (struct ipaccess_head *) buf;
214
215 /* 1) blocking read from the socket (IPA header) */
216 rc = read(fd, buf, sizeof(*hh));
217 if (rc < sizeof(*hh))
218 return -2;
219
220 len = ntohs(hh->len);
221 needed = len; //- sizeof(*hh);
222
223 /* 2) blocking read from the socket (payload) */
224 rc = read(fd, buf+sizeof(*hh), needed);
225 if (rc < needed)
226 return -3;
227
228 return len;
229}
230
Harald Welte796a7492018-09-23 19:31:28 +0200231static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
232{
233 struct msgb *msg = rspro_enc_msg(pdu);
234 int rc;
235
236 if (!msg) {
237 LOGW(worker, "error encoding RSPRO\n");
238 return -1;
239 }
240
Harald Weltefd471192018-09-24 14:51:14 +0200241 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200242 /* prepend the header */
243 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200244 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200245
246 /* actually send it through the socket */
247 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
248 if (rc == msgb_length(msg))
249 rc = 0;
250 else {
251 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
252 rc = -1;
253 }
254
255 msgb_free(msg);
256
257 return rc;
258}
259
Harald Weltecce2aad2018-08-16 14:44:37 +0200260static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
261{
Harald Welteaf614732018-08-17 22:10:05 +0200262 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
263 struct bankd_slot_mapping *slmap;
Harald Welte3e689872018-09-24 14:52:56 +0200264 e_ResultCode res;
265 RsproPDU_t *resp;
266 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200267
Harald Weltecce2aad2018-08-16 14:44:37 +0200268 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
269
Harald Weltecce2aad2018-08-16 14:44:37 +0200270 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
271 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
272 /* FIXME: store somewhere? */
273
274 if (worker->state != BW_ST_CONN_WAIT_ID) {
275 LOGW(worker, "Unexpected connectClientReq\n");
276 return -102;
277 }
278
Harald Welte371d0262018-08-16 15:23:58 +0200279 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200280 LOGW(worker, "missing clientID, aborting\n");
281 return -103;
282 }
Harald Welte371d0262018-08-16 15:23:58 +0200283 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
284 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200285 worker_set_state(worker, BW_ST_CONN_CLIENT);
286
Harald Welteaf614732018-08-17 22:10:05 +0200287 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
288 if (!slmap) {
289 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
290 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
291 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
292 /* FIXME: how to update the map in case a map is installed later */
Harald Welte3e689872018-09-24 14:52:56 +0200293 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200294 } else {
295 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
296 slmap->client.client_id, slmap->client.slot_nr,
297 slmap->bank.bank_id, slmap->bank.slot_nr);
Harald Welte796a7492018-09-23 19:31:28 +0200298 worker->slot = slmap->bank;
Harald Welteaf614732018-08-17 22:10:05 +0200299 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
Harald Welte796a7492018-09-23 19:31:28 +0200300 /* actually open the mapped reader/card/slot */
Harald Welte3e689872018-09-24 14:52:56 +0200301 rc = worker_open_card(worker);
302 if (rc == 0)
303 res = ResultCode_ok;
304 else
305 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200306 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200307
Harald Welte3e689872018-09-24 14:52:56 +0200308 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
309 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200310}
311
Harald Welte796a7492018-09-23 19:31:28 +0200312static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
313{
314 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
315 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
316 SCARD_IO_REQUEST pioRecvPci;
317 uint8_t rx_buf[1024];
318 DWORD rx_buf_len = sizeof(rx_buf);
319 RsproPDU_t *pdu_resp;
320 long rc;
321
322 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
323
324 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
325 LOGW(worker, "Unexpected tpduModemToCaard\n");
326 return -104;
327 }
328
329 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
330
331 rc = SCardTransmit(worker->reader.pcsc.hCard,
332 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
333 &pioRecvPci, rx_buf, &rx_buf_len);
334 PCSC_ERROR(worker, rc, "SCardTransmit");
335
336 /* encode response PDU and send it */
337 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
338 rx_buf, rx_buf_len);
339 worker_send_rspro(worker, pdu_resp);
340
341 return 0;
342end:
343 return rc;
344}
345
Harald Welte77911b02018-08-14 23:47:30 +0200346/* handle one incoming RSPRO message from a client inside a worker thread */
347static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
348{
Harald Weltecce2aad2018-08-16 14:44:37 +0200349 int rc = -100;
350
Harald Welte77911b02018-08-14 23:47:30 +0200351 switch (pdu->msg.present) {
352 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200353 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200354 break;
355 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200356 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200357 break;
358 case RsproPDUchoice_PR_clientSlotStatusInd:
359 /* FIXME */
360 break;
361 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200362 rc = -101;
363 break;
Harald Welte77911b02018-08-14 23:47:30 +0200364 }
365
Harald Weltecce2aad2018-08-16 14:44:37 +0200366 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200367}
368
369/* body of the main transceive loop */
370static int worker_transceive_loop(struct bankd_worker *worker)
371{
372 struct ipaccess_head *hh;
373 struct ipaccess_head_ext *hh_ext;
374 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
375 asn_dec_rval_t rval;
376 int data_len, rc;
377 RsproPDU_t *pdu;
378
379 /* 1) blocking read of entire IPA message from the socket */
380 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
381 if (rc < 0)
382 return rc;
383 data_len = rc;
384
385 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200386 if (hh->proto != IPAC_PROTO_OSMO) {
387 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200388 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200389 }
Harald Welte77911b02018-08-14 23:47:30 +0200390
391 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200392 if (data_len < sizeof(*hh_ext)) {
393 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200394 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200395 }
Harald Welte77911b02018-08-14 23:47:30 +0200396 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200397 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
398 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200399 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200400 }
Harald Welte77911b02018-08-14 23:47:30 +0200401
402 /* 2) ASN1 BER decode of the message */
403 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200404 if (rval.code != RC_OK) {
405 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200406 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200407 }
Harald Welte77911b02018-08-14 23:47:30 +0200408
409 /* 3) handling of the message, possibly resulting in PCSC commands */
410 rc = worker_handle_rspro(worker, pdu);
411 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200412 if (rc < 0) {
413 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200414 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200415 }
Harald Welte77911b02018-08-14 23:47:30 +0200416
417 /* everything OK if we reach here */
418 return 0;
419}
420
Harald Welted6dfb8c2018-08-16 14:46:53 +0200421/* obtain an ascii representation of the client IP/port */
422static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
423{
424 char hostbuf[32], portbuf[32];
425 int rc;
426
427 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
428 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
429 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
430 if (rc != 0) {
431 out[0] = '\0';
432 return -1;
433 }
434 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
435 return 0;
436}
437
Harald Welte77911b02018-08-14 23:47:30 +0200438/* worker thread main function */
439static void *worker_main(void *arg)
440{
441 struct bankd_worker *worker = (struct bankd_worker *) arg;
442 void *top_ctx;
443 int rc;
444
Harald Welte8d858292018-08-15 23:36:46 +0200445 worker_set_state(worker, BW_ST_INIT);
446
Harald Welte77911b02018-08-14 23:47:30 +0200447 /* not permitted in multithreaded environment */
448 talloc_disable_null_tracking();
449 top_ctx = talloc_named_const(NULL, 0, "top");
450 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
451
452 /* push cleanup helper */
453 pthread_cleanup_push(&worker_cleanup, worker);
454
455 /* we continuously perform the same loop here, recycling the worker thread
456 * once the client connection is gone or we have some trouble with the card/reader */
457 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200458 char buf[128];
459
Harald Welte77911b02018-08-14 23:47:30 +0200460 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
461
Harald Welte8d858292018-08-15 23:36:46 +0200462 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200463 /* first wait for an incoming TCP connection */
464 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
465 &worker->client.peer_addr_len);
466 if (rc < 0) {
467 continue;
468 }
469 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200470 worker_client_addrstr(buf, sizeof(buf), worker);
471 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200472 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200473
474 /* run the main worker transceive loop body until there was some error */
475 while (1) {
476 rc = worker_transceive_loop(worker);
477 if (rc < 0)
478 break;
479 }
480
Harald Welted6dfb8c2018-08-16 14:46:53 +0200481 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
482
Harald Welte77911b02018-08-14 23:47:30 +0200483 /* clean-up: reset to sane state */
484 if (worker->reader.pcsc.hCard) {
485 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
486 worker->reader.pcsc.hCard = 0;
487 }
488 if (worker->reader.pcsc.hContext) {
489 SCardReleaseContext(worker->reader.pcsc.hContext);
490 worker->reader.pcsc.hContext = 0;
491 }
492 if (worker->client.fd >= 0)
493 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200494 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200495 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200496 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200497 }
498
499 pthread_cleanup_pop(1);
500 talloc_free(top_ctx);
501 pthread_exit(NULL);
502}