blob: 063b4753b607230399511315863b5a63fcfd4f4c [file] [log] [blame]
Harald Welte31c9eca2018-10-03 21:03:34 +02001#define _GNU_SOURCE
2
Harald Welte77911b02018-08-14 23:47:30 +02003#include <stdio.h>
4#include <stdlib.h>
5#include <stdint.h>
6#include <unistd.h>
7
8#include <pthread.h>
9
10#include <wintypes.h>
11#include <winscard.h>
12#include <pcsclite.h>
13
Harald Welted6dfb8c2018-08-16 14:46:53 +020014#include <sys/socket.h>
15#include <netdb.h>
16
Harald Welte12534e72018-08-15 23:37:29 +020017#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020018#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020019#include <osmocom/core/logging.h>
20#include <osmocom/core/application.h>
Harald Welte77911b02018-08-14 23:47:30 +020021
22#include <osmocom/gsm/ipa.h>
23#include <osmocom/gsm/protocol/ipaccess.h>
24
25#include <asn1c/asn_application.h>
26#include <osmocom/rspro/RsproPDU.h>
27
28#include "bankd.h"
Harald Welte796a7492018-09-23 19:31:28 +020029#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020030
31__thread void *talloc_asn1_ctx;
32
33static void *worker_main(void *arg);
34
35/***********************************************************************
36* bankd core / main thread
37***********************************************************************/
38
Harald Weltef94b9ee2018-09-25 15:04:21 +020039static const struct log_info_cat default_categories[] = {
40 [DMAIN] = {
41 .name = "DMAIN",
42 .loglevel = LOGL_DEBUG,
43 .enabled = 1,
44 },
45};
46
47static const struct log_info log_info = {
48 .cat = default_categories,
49 .num_cat = ARRAY_SIZE(default_categories),
50};
51
Harald Welte77911b02018-08-14 23:47:30 +020052static void bankd_init(struct bankd *bankd)
53{
Harald Weltef94b9ee2018-09-25 15:04:21 +020054 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
55 osmo_init_logging2(g_tall_ctx, &log_info);
56
Harald Welte77911b02018-08-14 23:47:30 +020057 /* intialize members of 'bankd' */
58 INIT_LLIST_HEAD(&bankd->slot_mappings);
59 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
60 INIT_LLIST_HEAD(&bankd->workers);
61 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020062
Harald Weltef1dd1622018-09-24 14:54:23 +020063 bankd->comp_id.type = ComponentType_remsimBankd;
64 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
65 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
66 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
67 /* FIXME: other members of app_comp_id */
68
Harald Welte45c948c2018-09-23 19:26:52 +020069 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
70 * read once during bankd initialization, when the worker threads haven't
71 * started yet */
72 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
73 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020074
75 /* HACK HACK HACK */
76 {
77 struct bank_slot bs = { .bank_id = 1, };
78 struct client_slot cs = { .client_id = 23, };
79 int i;
80 for (i = 0; i < 5; i++) {
81 bs.slot_nr = cs.slot_nr = i;
82 bankd_slotmap_add(bankd, &bs, &cs);
83 }
84 }
Harald Welte77911b02018-08-14 23:47:30 +020085}
86
87/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020088static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020089{
90 struct bankd_worker *worker;
91 int rc;
92
93 worker = talloc_zero(bankd, struct bankd_worker);
94 if (!worker)
95 return NULL;
96
97 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020098 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020099
100 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
101
102 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
103 if (rc != 0) {
104 talloc_free(worker);
105 return NULL;
106 }
107
108 pthread_mutex_lock(&bankd->workers_mutex);
109 llist_add_tail(&worker->list, &bankd->workers);
110 pthread_mutex_unlock(&bankd->workers_mutex);
111
112 return worker;
113}
114
115static bool terminate = false;
116
117int main(int argc, char **argv)
118{
119 struct bankd *bankd = talloc_zero(NULL, struct bankd);
120 int i, rc;
121
122 OSMO_ASSERT(bankd);
123 bankd_init(bankd);
124
Harald Welte12534e72018-08-15 23:37:29 +0200125 /* create listening socket */
126 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
127 if (rc < 0)
128 exit(1);
129 bankd->accept_fd = rc;
130
131 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200132 for (i = 0; i < 10; i++) {
133 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200134 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200135 if (!w)
136 exit(21);
137 }
138
139 while (1) {
140 if (terminate)
141 break;
Harald Welte7f684a02018-08-16 14:43:50 +0200142 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200143 }
144
145 talloc_free(bankd);
146 exit(0);
147}
148
149
150
151/***********************************************************************
152 * bankd worker thread
153 ***********************************************************************/
154
Harald Welte8d858292018-08-15 23:36:46 +0200155struct value_string worker_state_names[] = {
156 { BW_ST_INIT, "INIT" },
157 { BW_ST_ACCEPTING, "ACCEPTING" },
158 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
159 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200160 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200161 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
162 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
163 { 0, NULL }
164};
165
Harald Welteceb3e682018-08-16 14:47:11 +0200166#define LOGW(w, fmt, args...) \
167 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
168 __FILE__, __LINE__, ## args)
169
Harald Welteaf614732018-08-17 22:10:05 +0200170#define PCSC_ERROR(w, rv, text) \
171if (rv != SCARD_S_SUCCESS) { \
172 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
173 goto end; \
174} else { \
175 LOGW((w), ": OK\n\n"); \
176}
177
Harald Welte8d858292018-08-15 23:36:46 +0200178static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
179{
180 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
181 worker->state = new_state;
182}
Harald Welte77911b02018-08-14 23:47:30 +0200183
184static void worker_cleanup(void *arg)
185{
186 struct bankd_worker *worker = (struct bankd_worker *) arg;
187 struct bankd *bankd = worker->bankd;
188
189 /* FIXME: should we still do this? in the thread ?!? */
190 pthread_mutex_lock(&bankd->workers_mutex);
191 llist_del(&worker->list);
192 talloc_free(worker); /* FIXME: is this safe? */
193 pthread_mutex_unlock(&bankd->workers_mutex);
194}
195
196
Harald Welteaf614732018-08-17 22:10:05 +0200197static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200198{
Harald Welteaf614732018-08-17 22:10:05 +0200199 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200200
Harald Welte45c948c2018-09-23 19:26:52 +0200201 /* resolve PC/SC reader name from slot_id -> name map */
202 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
203 OSMO_ASSERT(worker->reader.name);
204
205 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
206
Harald Welte77911b02018-08-14 23:47:30 +0200207 /* The PC/SC context must be created inside the thread where we'll later use it */
208 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
Harald Welteaf614732018-08-17 22:10:05 +0200209 PCSC_ERROR(worker, rc, "SCardEstablishContext")
Harald Welte77911b02018-08-14 23:47:30 +0200210
Harald Welte48865c22018-09-23 19:30:07 +0200211 DWORD dwActiveProtocol;
Harald Welte77911b02018-08-14 23:47:30 +0200212 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
Harald Welte48865c22018-09-23 19:30:07 +0200213 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
Harald Welteaf614732018-08-17 22:10:05 +0200214 PCSC_ERROR(worker, rc, "SCardConnect")
Harald Welte77911b02018-08-14 23:47:30 +0200215
Harald Welte57593f02018-09-23 19:30:31 +0200216 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
217
Harald Welteaf614732018-08-17 22:10:05 +0200218 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200219end:
Harald Welteaf614732018-08-17 22:10:05 +0200220 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200221}
Harald Welte77911b02018-08-14 23:47:30 +0200222
223
224static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
225{
226 struct ipaccess_head *hh;
227 uint16_t len;
228 int needed, rc;
229
230 if (buf_size < sizeof(*hh))
231 return -1;
232
233 hh = (struct ipaccess_head *) buf;
234
235 /* 1) blocking read from the socket (IPA header) */
236 rc = read(fd, buf, sizeof(*hh));
237 if (rc < sizeof(*hh))
238 return -2;
239
240 len = ntohs(hh->len);
241 needed = len; //- sizeof(*hh);
242
243 /* 2) blocking read from the socket (payload) */
244 rc = read(fd, buf+sizeof(*hh), needed);
245 if (rc < needed)
246 return -3;
247
248 return len;
249}
250
Harald Welte796a7492018-09-23 19:31:28 +0200251static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
252{
253 struct msgb *msg = rspro_enc_msg(pdu);
254 int rc;
255
256 if (!msg) {
257 LOGW(worker, "error encoding RSPRO\n");
258 return -1;
259 }
260
Harald Weltefd471192018-09-24 14:51:14 +0200261 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200262 /* prepend the header */
263 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200264 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200265
266 /* actually send it through the socket */
267 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
268 if (rc == msgb_length(msg))
269 rc = 0;
270 else {
271 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
272 rc = -1;
273 }
274
275 msgb_free(msg);
276
277 return rc;
278}
279
Harald Weltecce2aad2018-08-16 14:44:37 +0200280static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
281{
Harald Welteaf614732018-08-17 22:10:05 +0200282 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
283 struct bankd_slot_mapping *slmap;
Harald Welte3e689872018-09-24 14:52:56 +0200284 e_ResultCode res;
285 RsproPDU_t *resp;
286 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200287
Harald Weltecce2aad2018-08-16 14:44:37 +0200288 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
289
Harald Weltecce2aad2018-08-16 14:44:37 +0200290 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
291 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
292 /* FIXME: store somewhere? */
293
294 if (worker->state != BW_ST_CONN_WAIT_ID) {
295 LOGW(worker, "Unexpected connectClientReq\n");
296 return -102;
297 }
298
Harald Welte371d0262018-08-16 15:23:58 +0200299 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200300 LOGW(worker, "missing clientID, aborting\n");
301 return -103;
302 }
Harald Welte371d0262018-08-16 15:23:58 +0200303 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
304 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200305 worker_set_state(worker, BW_ST_CONN_CLIENT);
306
Harald Welteaf614732018-08-17 22:10:05 +0200307 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
308 if (!slmap) {
309 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
310 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
311 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
312 /* FIXME: how to update the map in case a map is installed later */
Harald Welte3e689872018-09-24 14:52:56 +0200313 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200314 } else {
315 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
316 slmap->client.client_id, slmap->client.slot_nr,
317 slmap->bank.bank_id, slmap->bank.slot_nr);
Harald Welte796a7492018-09-23 19:31:28 +0200318 worker->slot = slmap->bank;
Harald Welteaf614732018-08-17 22:10:05 +0200319 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
Harald Welte796a7492018-09-23 19:31:28 +0200320 /* actually open the mapped reader/card/slot */
Harald Welte3e689872018-09-24 14:52:56 +0200321 rc = worker_open_card(worker);
322 if (rc == 0)
323 res = ResultCode_ok;
324 else
325 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200326 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200327
Harald Welte3e689872018-09-24 14:52:56 +0200328 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
329 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200330}
331
Harald Welte796a7492018-09-23 19:31:28 +0200332static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
333{
334 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
335 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
336 SCARD_IO_REQUEST pioRecvPci;
337 uint8_t rx_buf[1024];
338 DWORD rx_buf_len = sizeof(rx_buf);
339 RsproPDU_t *pdu_resp;
340 long rc;
341
342 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
343
344 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
345 LOGW(worker, "Unexpected tpduModemToCaard\n");
346 return -104;
347 }
348
349 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
350
351 rc = SCardTransmit(worker->reader.pcsc.hCard,
352 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
353 &pioRecvPci, rx_buf, &rx_buf_len);
354 PCSC_ERROR(worker, rc, "SCardTransmit");
355
356 /* encode response PDU and send it */
357 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
358 rx_buf, rx_buf_len);
359 worker_send_rspro(worker, pdu_resp);
360
361 return 0;
362end:
363 return rc;
364}
365
Harald Welte77911b02018-08-14 23:47:30 +0200366/* handle one incoming RSPRO message from a client inside a worker thread */
367static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
368{
Harald Weltecce2aad2018-08-16 14:44:37 +0200369 int rc = -100;
370
Harald Welte77911b02018-08-14 23:47:30 +0200371 switch (pdu->msg.present) {
372 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200373 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200374 break;
375 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200376 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200377 break;
378 case RsproPDUchoice_PR_clientSlotStatusInd:
379 /* FIXME */
380 break;
381 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200382 rc = -101;
383 break;
Harald Welte77911b02018-08-14 23:47:30 +0200384 }
385
Harald Weltecce2aad2018-08-16 14:44:37 +0200386 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200387}
388
389/* body of the main transceive loop */
390static int worker_transceive_loop(struct bankd_worker *worker)
391{
392 struct ipaccess_head *hh;
393 struct ipaccess_head_ext *hh_ext;
394 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
395 asn_dec_rval_t rval;
396 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200397 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200398
399 /* 1) blocking read of entire IPA message from the socket */
400 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
401 if (rc < 0)
402 return rc;
403 data_len = rc;
404
405 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200406 if (hh->proto != IPAC_PROTO_OSMO) {
407 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200408 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200409 }
Harald Welte77911b02018-08-14 23:47:30 +0200410
411 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200412 if (data_len < sizeof(*hh_ext)) {
413 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200414 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200415 }
Harald Welte77911b02018-08-14 23:47:30 +0200416 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200417 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
418 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200419 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200420 }
Harald Welte77911b02018-08-14 23:47:30 +0200421
422 /* 2) ASN1 BER decode of the message */
423 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200424 if (rval.code != RC_OK) {
425 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200426 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200427 }
Harald Welte77911b02018-08-14 23:47:30 +0200428
429 /* 3) handling of the message, possibly resulting in PCSC commands */
430 rc = worker_handle_rspro(worker, pdu);
431 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200432 if (rc < 0) {
433 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200434 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200435 }
Harald Welte77911b02018-08-14 23:47:30 +0200436
437 /* everything OK if we reach here */
438 return 0;
439}
440
Harald Welted6dfb8c2018-08-16 14:46:53 +0200441/* obtain an ascii representation of the client IP/port */
442static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
443{
444 char hostbuf[32], portbuf[32];
445 int rc;
446
447 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
448 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
449 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
450 if (rc != 0) {
451 out[0] = '\0';
452 return -1;
453 }
454 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
455 return 0;
456}
457
Harald Welte77911b02018-08-14 23:47:30 +0200458/* worker thread main function */
459static void *worker_main(void *arg)
460{
461 struct bankd_worker *worker = (struct bankd_worker *) arg;
462 void *top_ctx;
463 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200464 char worker_name[32];
465
466 /* set the thread name */
467 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
468 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200469
Harald Welte8d858292018-08-15 23:36:46 +0200470 worker_set_state(worker, BW_ST_INIT);
471
Harald Welte77911b02018-08-14 23:47:30 +0200472 /* not permitted in multithreaded environment */
473 talloc_disable_null_tracking();
474 top_ctx = talloc_named_const(NULL, 0, "top");
475 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
476
477 /* push cleanup helper */
478 pthread_cleanup_push(&worker_cleanup, worker);
479
480 /* we continuously perform the same loop here, recycling the worker thread
481 * once the client connection is gone or we have some trouble with the card/reader */
482 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200483 char buf[128];
484
Harald Welte77911b02018-08-14 23:47:30 +0200485 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
486
Harald Welte8d858292018-08-15 23:36:46 +0200487 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200488 /* first wait for an incoming TCP connection */
489 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
490 &worker->client.peer_addr_len);
491 if (rc < 0) {
492 continue;
493 }
494 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200495 worker_client_addrstr(buf, sizeof(buf), worker);
496 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200497 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200498
499 /* run the main worker transceive loop body until there was some error */
500 while (1) {
501 rc = worker_transceive_loop(worker);
502 if (rc < 0)
503 break;
504 }
505
Harald Welted6dfb8c2018-08-16 14:46:53 +0200506 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
507
Harald Welte77911b02018-08-14 23:47:30 +0200508 /* clean-up: reset to sane state */
509 if (worker->reader.pcsc.hCard) {
510 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
511 worker->reader.pcsc.hCard = 0;
512 }
513 if (worker->reader.pcsc.hContext) {
514 SCardReleaseContext(worker->reader.pcsc.hContext);
515 worker->reader.pcsc.hContext = 0;
516 }
517 if (worker->client.fd >= 0)
518 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200519 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200520 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200521 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200522 }
523
524 pthread_cleanup_pop(1);
525 talloc_free(top_ctx);
526 pthread_exit(NULL);
527}