blob: 55d7155ce76305b1e152fe21fe447e4ae36066b3 [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 Welte12534e72018-08-15 23:37:29 +020012#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020013#include <osmocom/core/linuxlist.h>
14
15#include <osmocom/gsm/ipa.h>
16#include <osmocom/gsm/protocol/ipaccess.h>
17
18#include <asn1c/asn_application.h>
19#include <osmocom/rspro/RsproPDU.h>
20
21#include "bankd.h"
22
23__thread void *talloc_asn1_ctx;
24
25static void *worker_main(void *arg);
26
27/***********************************************************************
28* bankd core / main thread
29***********************************************************************/
30
31static void bankd_init(struct bankd *bankd)
32{
33 /* intialize members of 'bankd' */
34 INIT_LLIST_HEAD(&bankd->slot_mappings);
35 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
36 INIT_LLIST_HEAD(&bankd->workers);
37 pthread_mutex_init(&bankd->workers_mutex, NULL);
38}
39
40/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020041static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020042{
43 struct bankd_worker *worker;
44 int rc;
45
46 worker = talloc_zero(bankd, struct bankd_worker);
47 if (!worker)
48 return NULL;
49
50 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020051 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020052
53 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
54
55 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
56 if (rc != 0) {
57 talloc_free(worker);
58 return NULL;
59 }
60
61 pthread_mutex_lock(&bankd->workers_mutex);
62 llist_add_tail(&worker->list, &bankd->workers);
63 pthread_mutex_unlock(&bankd->workers_mutex);
64
65 return worker;
66}
67
68static bool terminate = false;
69
70int main(int argc, char **argv)
71{
72 struct bankd *bankd = talloc_zero(NULL, struct bankd);
73 int i, rc;
74
75 OSMO_ASSERT(bankd);
76 bankd_init(bankd);
77
Harald Welte12534e72018-08-15 23:37:29 +020078 /* create listening socket */
79 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
80 if (rc < 0)
81 exit(1);
82 bankd->accept_fd = rc;
83
84 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +020085 for (i = 0; i < 10; i++) {
86 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +020087 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +020088 if (!w)
89 exit(21);
90 }
91
92 while (1) {
93 if (terminate)
94 break;
Harald Welte7f684a02018-08-16 14:43:50 +020095 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +020096 }
97
98 talloc_free(bankd);
99 exit(0);
100}
101
102
103
104/***********************************************************************
105 * bankd worker thread
106 ***********************************************************************/
107
108#define PCSC_ERROR(rv, text) \
109if (rv != SCARD_S_SUCCESS) { \
110 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
111 goto end; \
112} else { \
113 printf(text ": OK\n\n"); \
114}
115
Harald Welte8d858292018-08-15 23:36:46 +0200116#define LOGW(w, fmt, args...) \
117 printf("[%u] " fmt, (w)->num, args)
118
119struct value_string worker_state_names[] = {
120 { BW_ST_INIT, "INIT" },
121 { BW_ST_ACCEPTING, "ACCEPTING" },
122 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
123 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
124 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
125 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
126 { 0, NULL }
127};
128
129static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
130{
131 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
132 worker->state = new_state;
133}
Harald Welte77911b02018-08-14 23:47:30 +0200134
135static void worker_cleanup(void *arg)
136{
137 struct bankd_worker *worker = (struct bankd_worker *) arg;
138 struct bankd *bankd = worker->bankd;
139
140 /* FIXME: should we still do this? in the thread ?!? */
141 pthread_mutex_lock(&bankd->workers_mutex);
142 llist_del(&worker->list);
143 talloc_free(worker); /* FIXME: is this safe? */
144 pthread_mutex_unlock(&bankd->workers_mutex);
145}
146
147
148#if 0
149/* function running inside a worker thread; doing some initialization */
150static void worker_init(struct bankd_worker *worker)
151{
152 int rc;
153
154 /* push cleanup helper */
155 pthread_cleanup_push(&worker_cleanup, worker);
156
157 /* The PC/SC context must be created inside the thread where we'll later use it */
158 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
159 PCSC_ERROR(rc, "SCardEstablishContext")
160
161 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
162 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
163 PCSC_ERROR(rc, "SCardConnect")
164
165 return;
166end:
167 pthread_exit(NULL);
168}
169#endif
170
171
172static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
173{
174 struct ipaccess_head *hh;
175 uint16_t len;
176 int needed, rc;
177
178 if (buf_size < sizeof(*hh))
179 return -1;
180
181 hh = (struct ipaccess_head *) buf;
182
183 /* 1) blocking read from the socket (IPA header) */
184 rc = read(fd, buf, sizeof(*hh));
185 if (rc < sizeof(*hh))
186 return -2;
187
188 len = ntohs(hh->len);
189 needed = len; //- sizeof(*hh);
190
191 /* 2) blocking read from the socket (payload) */
192 rc = read(fd, buf+sizeof(*hh), needed);
193 if (rc < needed)
194 return -3;
195
196 return len;
197}
198
Harald Weltecce2aad2018-08-16 14:44:37 +0200199static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
200{
201 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
202
203 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
204
205 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
206 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
207 /* FIXME: store somewhere? */
208
209 if (worker->state != BW_ST_CONN_WAIT_ID) {
210 LOGW(worker, "Unexpected connectClientReq\n");
211 return -102;
212 }
213
214 if (!pdu->msg.choice.connectClientReq.clientId) {
215 LOGW(worker, "missing clientID, aborting\n");
216 return -103;
217 }
218 worker->client.id = *pdu->msg.choice.connectClientReq.clientId;
219 worker_set_state(worker, BW_ST_CONN_CLIENT);
220
221 /* FIXME: resolve mapping */
222
223 return 0;
224}
225
Harald Welte77911b02018-08-14 23:47:30 +0200226/* handle one incoming RSPRO message from a client inside a worker thread */
227static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
228{
Harald Weltecce2aad2018-08-16 14:44:37 +0200229 int rc = -100;
230
Harald Welte77911b02018-08-14 23:47:30 +0200231 switch (pdu->msg.present) {
232 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200233 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200234 break;
235 case RsproPDUchoice_PR_tpduModemToCard:
236 /* FIXME */
237 break;
238 case RsproPDUchoice_PR_clientSlotStatusInd:
239 /* FIXME */
240 break;
241 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200242 rc = -101;
243 break;
Harald Welte77911b02018-08-14 23:47:30 +0200244 }
245
Harald Weltecce2aad2018-08-16 14:44:37 +0200246 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200247}
248
249/* body of the main transceive loop */
250static int worker_transceive_loop(struct bankd_worker *worker)
251{
252 struct ipaccess_head *hh;
253 struct ipaccess_head_ext *hh_ext;
254 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
255 asn_dec_rval_t rval;
256 int data_len, rc;
257 RsproPDU_t *pdu;
258
259 /* 1) blocking read of entire IPA message from the socket */
260 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
261 if (rc < 0)
262 return rc;
263 data_len = rc;
264
265 hh = (struct ipaccess_head *) buf;
266 if (hh->proto != IPAC_PROTO_OSMO)
267 return -4;
268
269 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
270 if (data_len < sizeof(*hh_ext))
271 return -5;
272 data_len -= sizeof(*hh_ext);
273 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
274 return -6;
275
276 /* 2) ASN1 BER decode of the message */
277 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
278 if (rval.code != RC_OK)
279 return -7;
280
281 /* 3) handling of the message, possibly resulting in PCSC commands */
282 rc = worker_handle_rspro(worker, pdu);
283 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
284 if (rc < 0)
285 return rc;
286
287 /* everything OK if we reach here */
288 return 0;
289}
290
291/* worker thread main function */
292static void *worker_main(void *arg)
293{
294 struct bankd_worker *worker = (struct bankd_worker *) arg;
295 void *top_ctx;
296 int rc;
297
Harald Welte8d858292018-08-15 23:36:46 +0200298 worker_set_state(worker, BW_ST_INIT);
299
Harald Welte77911b02018-08-14 23:47:30 +0200300 /* not permitted in multithreaded environment */
301 talloc_disable_null_tracking();
302 top_ctx = talloc_named_const(NULL, 0, "top");
303 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
304
305 /* push cleanup helper */
306 pthread_cleanup_push(&worker_cleanup, worker);
307
308 /* we continuously perform the same loop here, recycling the worker thread
309 * once the client connection is gone or we have some trouble with the card/reader */
310 while (1) {
311 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
312
Harald Welte8d858292018-08-15 23:36:46 +0200313 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200314 /* first wait for an incoming TCP connection */
315 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
316 &worker->client.peer_addr_len);
317 if (rc < 0) {
318 continue;
319 }
320 worker->client.fd = rc;
Harald Welte8d858292018-08-15 23:36:46 +0200321 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200322
323 /* run the main worker transceive loop body until there was some error */
324 while (1) {
325 rc = worker_transceive_loop(worker);
326 if (rc < 0)
327 break;
328 }
329
330 /* clean-up: reset to sane state */
331 if (worker->reader.pcsc.hCard) {
332 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
333 worker->reader.pcsc.hCard = 0;
334 }
335 if (worker->reader.pcsc.hContext) {
336 SCardReleaseContext(worker->reader.pcsc.hContext);
337 worker->reader.pcsc.hContext = 0;
338 }
339 if (worker->client.fd >= 0)
340 close(worker->client.fd);
341 worker->client.fd = -1;
342 }
343
344 pthread_cleanup_pop(1);
345 talloc_free(top_ctx);
346 pthread_exit(NULL);
347}