blob: f755b8f2b957f0c3750648680a0078db4a7af2d2 [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 Welte694df832018-10-03 22:47:52 +0200201 if (!worker->reader.name) {
202 /* resolve PC/SC reader name from slot_id -> name map */
203 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
204 if (!worker->reader.name) {
205 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
206 worker->slot.bank_id, worker->slot.slot_nr);
207 rc = -1;
208 goto end;
209 }
210 }
Harald Welte45c948c2018-09-23 19:26:52 +0200211 OSMO_ASSERT(worker->reader.name);
212
Harald Welte694df832018-10-03 22:47:52 +0200213 if (!worker->reader.pcsc.hContext) {
214 LOGW(worker, "Attempting to open PC/SC context\n");
215 /* The PC/SC context must be created inside the thread where we'll later use it */
216 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
217 PCSC_ERROR(worker, rc, "SCardEstablishContext")
218 }
Harald Welte45c948c2018-09-23 19:26:52 +0200219
Harald Welte694df832018-10-03 22:47:52 +0200220 if (!worker->reader.pcsc.hCard) {
221 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
222 DWORD dwActiveProtocol;
223 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
224 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
225 PCSC_ERROR(worker, rc, "SCardConnect")
226 }
Harald Welte77911b02018-08-14 23:47:30 +0200227
Harald Welte57593f02018-09-23 19:30:31 +0200228 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
229
Harald Welteaf614732018-08-17 22:10:05 +0200230 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200231end:
Harald Welte694df832018-10-03 22:47:52 +0200232 /* retry in 10s */
233 worker->timeout = 10;
Harald Welteaf614732018-08-17 22:10:05 +0200234 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200235}
Harald Welte77911b02018-08-14 23:47:30 +0200236
237
238static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
239{
240 struct ipaccess_head *hh;
241 uint16_t len;
242 int needed, rc;
243
244 if (buf_size < sizeof(*hh))
245 return -1;
246
247 hh = (struct ipaccess_head *) buf;
248
249 /* 1) blocking read from the socket (IPA header) */
250 rc = read(fd, buf, sizeof(*hh));
251 if (rc < sizeof(*hh))
252 return -2;
253
254 len = ntohs(hh->len);
255 needed = len; //- sizeof(*hh);
256
257 /* 2) blocking read from the socket (payload) */
258 rc = read(fd, buf+sizeof(*hh), needed);
259 if (rc < needed)
260 return -3;
261
262 return len;
263}
264
Harald Welte796a7492018-09-23 19:31:28 +0200265static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
266{
267 struct msgb *msg = rspro_enc_msg(pdu);
268 int rc;
269
270 if (!msg) {
271 LOGW(worker, "error encoding RSPRO\n");
272 return -1;
273 }
274
Harald Weltefd471192018-09-24 14:51:14 +0200275 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200276 /* prepend the header */
277 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200278 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200279
280 /* actually send it through the socket */
281 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
282 if (rc == msgb_length(msg))
283 rc = 0;
284 else {
285 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
286 rc = -1;
287 }
288
289 msgb_free(msg);
290
291 return rc;
292}
293
Harald Weltecce2aad2018-08-16 14:44:37 +0200294static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
295{
Harald Welteaf614732018-08-17 22:10:05 +0200296 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
297 struct bankd_slot_mapping *slmap;
Harald Welte3e689872018-09-24 14:52:56 +0200298 e_ResultCode res;
299 RsproPDU_t *resp;
300 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200301
Harald Weltecce2aad2018-08-16 14:44:37 +0200302 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
303
Harald Weltecce2aad2018-08-16 14:44:37 +0200304 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
305 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
306 /* FIXME: store somewhere? */
307
308 if (worker->state != BW_ST_CONN_WAIT_ID) {
309 LOGW(worker, "Unexpected connectClientReq\n");
310 return -102;
311 }
312
Harald Welte371d0262018-08-16 15:23:58 +0200313 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200314 LOGW(worker, "missing clientID, aborting\n");
315 return -103;
316 }
Harald Welte371d0262018-08-16 15:23:58 +0200317 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
318 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200319 worker_set_state(worker, BW_ST_CONN_CLIENT);
320
Harald Welteaf614732018-08-17 22:10:05 +0200321 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
322 if (!slmap) {
323 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
324 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
325 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
326 /* FIXME: how to update the map in case a map is installed later */
Harald Welte3e689872018-09-24 14:52:56 +0200327 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200328 } else {
329 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
330 slmap->client.client_id, slmap->client.slot_nr,
331 slmap->bank.bank_id, slmap->bank.slot_nr);
Harald Welte796a7492018-09-23 19:31:28 +0200332 worker->slot = slmap->bank;
Harald Welteaf614732018-08-17 22:10:05 +0200333 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
Harald Welte796a7492018-09-23 19:31:28 +0200334 /* actually open the mapped reader/card/slot */
Harald Welte3e689872018-09-24 14:52:56 +0200335 rc = worker_open_card(worker);
336 if (rc == 0)
337 res = ResultCode_ok;
338 else
339 res = ResultCode_cardNotPresent;
Harald Welteaf614732018-08-17 22:10:05 +0200340 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200341
Harald Welte3e689872018-09-24 14:52:56 +0200342 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
343 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200344}
345
Harald Welte796a7492018-09-23 19:31:28 +0200346static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
347{
348 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
349 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
350 SCARD_IO_REQUEST pioRecvPci;
351 uint8_t rx_buf[1024];
352 DWORD rx_buf_len = sizeof(rx_buf);
353 RsproPDU_t *pdu_resp;
354 long rc;
355
356 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
357
358 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
359 LOGW(worker, "Unexpected tpduModemToCaard\n");
360 return -104;
361 }
362
363 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
364
365 rc = SCardTransmit(worker->reader.pcsc.hCard,
366 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
367 &pioRecvPci, rx_buf, &rx_buf_len);
368 PCSC_ERROR(worker, rc, "SCardTransmit");
369
370 /* encode response PDU and send it */
371 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
372 rx_buf, rx_buf_len);
373 worker_send_rspro(worker, pdu_resp);
374
375 return 0;
376end:
377 return rc;
378}
379
Harald Welte77911b02018-08-14 23:47:30 +0200380/* handle one incoming RSPRO message from a client inside a worker thread */
381static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
382{
Harald Weltecce2aad2018-08-16 14:44:37 +0200383 int rc = -100;
384
Harald Welte77911b02018-08-14 23:47:30 +0200385 switch (pdu->msg.present) {
386 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200387 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200388 break;
389 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200390 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200391 break;
392 case RsproPDUchoice_PR_clientSlotStatusInd:
393 /* FIXME */
394 break;
395 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200396 rc = -101;
397 break;
Harald Welte77911b02018-08-14 23:47:30 +0200398 }
399
Harald Weltecce2aad2018-08-16 14:44:37 +0200400 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200401}
402
Harald Welte694df832018-10-03 22:47:52 +0200403static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
404{
405 struct timeval tout = { timeout_secs, 0 };
406 fd_set readset;
407
408 FD_ZERO(&readset);
409 FD_SET(fd, &readset);
410 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
411}
412
Harald Welte77911b02018-08-14 23:47:30 +0200413/* body of the main transceive loop */
414static int worker_transceive_loop(struct bankd_worker *worker)
415{
416 struct ipaccess_head *hh;
417 struct ipaccess_head_ext *hh_ext;
418 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
419 asn_dec_rval_t rval;
420 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200421 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200422
Harald Welte694df832018-10-03 22:47:52 +0200423 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
424 if (rc == 0) {
425 /* TIMEOUT case */
426 worker_open_card(worker);
427 return 0;
428 };
429
Harald Welte77911b02018-08-14 23:47:30 +0200430 /* 1) blocking read of entire IPA message from the socket */
431 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
432 if (rc < 0)
433 return rc;
434 data_len = rc;
435
436 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200437 if (hh->proto != IPAC_PROTO_OSMO) {
438 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200439 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200440 }
Harald Welte77911b02018-08-14 23:47:30 +0200441
442 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200443 if (data_len < sizeof(*hh_ext)) {
444 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200445 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200446 }
Harald Welte77911b02018-08-14 23:47:30 +0200447 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200448 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
449 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200450 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200451 }
Harald Welte77911b02018-08-14 23:47:30 +0200452
453 /* 2) ASN1 BER decode of the message */
454 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200455 if (rval.code != RC_OK) {
456 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200457 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200458 }
Harald Welte77911b02018-08-14 23:47:30 +0200459
460 /* 3) handling of the message, possibly resulting in PCSC commands */
461 rc = worker_handle_rspro(worker, pdu);
462 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200463 if (rc < 0) {
464 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200465 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200466 }
Harald Welte77911b02018-08-14 23:47:30 +0200467
468 /* everything OK if we reach here */
469 return 0;
470}
471
Harald Welted6dfb8c2018-08-16 14:46:53 +0200472/* obtain an ascii representation of the client IP/port */
473static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
474{
475 char hostbuf[32], portbuf[32];
476 int rc;
477
478 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
479 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
480 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
481 if (rc != 0) {
482 out[0] = '\0';
483 return -1;
484 }
485 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
486 return 0;
487}
488
Harald Welte77911b02018-08-14 23:47:30 +0200489/* worker thread main function */
490static void *worker_main(void *arg)
491{
492 struct bankd_worker *worker = (struct bankd_worker *) arg;
493 void *top_ctx;
494 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200495 char worker_name[32];
496
497 /* set the thread name */
498 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
499 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200500
Harald Welte8d858292018-08-15 23:36:46 +0200501 worker_set_state(worker, BW_ST_INIT);
502
Harald Welte77911b02018-08-14 23:47:30 +0200503 /* not permitted in multithreaded environment */
504 talloc_disable_null_tracking();
505 top_ctx = talloc_named_const(NULL, 0, "top");
506 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
507
508 /* push cleanup helper */
509 pthread_cleanup_push(&worker_cleanup, worker);
510
511 /* we continuously perform the same loop here, recycling the worker thread
512 * once the client connection is gone or we have some trouble with the card/reader */
513 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200514 char buf[128];
515
Harald Welte77911b02018-08-14 23:47:30 +0200516 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
517
Harald Welte8d858292018-08-15 23:36:46 +0200518 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200519 /* first wait for an incoming TCP connection */
520 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
521 &worker->client.peer_addr_len);
522 if (rc < 0) {
523 continue;
524 }
525 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200526 worker_client_addrstr(buf, sizeof(buf), worker);
527 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200528 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200529
530 /* run the main worker transceive loop body until there was some error */
531 while (1) {
532 rc = worker_transceive_loop(worker);
533 if (rc < 0)
534 break;
535 }
536
Harald Welted6dfb8c2018-08-16 14:46:53 +0200537 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
538
Harald Welte77911b02018-08-14 23:47:30 +0200539 /* clean-up: reset to sane state */
540 if (worker->reader.pcsc.hCard) {
541 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
542 worker->reader.pcsc.hCard = 0;
543 }
544 if (worker->reader.pcsc.hContext) {
545 SCardReleaseContext(worker->reader.pcsc.hContext);
546 worker->reader.pcsc.hContext = 0;
547 }
Harald Welte694df832018-10-03 22:47:52 +0200548 if (worker->reader.name)
549 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200550 if (worker->client.fd >= 0)
551 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200552 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200553 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200554 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200555 }
556
557 pthread_cleanup_pop(1);
558 talloc_free(top_ctx);
559 pthread_exit(NULL);
560}