blob: 491958f8d09e0fdda45acfe0b276b5aa473e878a [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
199/* handle one incoming RSPRO message from a client inside a worker thread */
200static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
201{
202 switch (pdu->msg.present) {
203 case RsproPDUchoice_PR_connectClientReq:
204 /* FIXME */
205 break;
206 case RsproPDUchoice_PR_tpduModemToCard:
207 /* FIXME */
208 break;
209 case RsproPDUchoice_PR_clientSlotStatusInd:
210 /* FIXME */
211 break;
212 default:
213 return -100;
214 }
215
216 return 0;
217}
218
219/* body of the main transceive loop */
220static int worker_transceive_loop(struct bankd_worker *worker)
221{
222 struct ipaccess_head *hh;
223 struct ipaccess_head_ext *hh_ext;
224 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
225 asn_dec_rval_t rval;
226 int data_len, rc;
227 RsproPDU_t *pdu;
228
229 /* 1) blocking read of entire IPA message from the socket */
230 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
231 if (rc < 0)
232 return rc;
233 data_len = rc;
234
235 hh = (struct ipaccess_head *) buf;
236 if (hh->proto != IPAC_PROTO_OSMO)
237 return -4;
238
239 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
240 if (data_len < sizeof(*hh_ext))
241 return -5;
242 data_len -= sizeof(*hh_ext);
243 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
244 return -6;
245
246 /* 2) ASN1 BER decode of the message */
247 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
248 if (rval.code != RC_OK)
249 return -7;
250
251 /* 3) handling of the message, possibly resulting in PCSC commands */
252 rc = worker_handle_rspro(worker, pdu);
253 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
254 if (rc < 0)
255 return rc;
256
257 /* everything OK if we reach here */
258 return 0;
259}
260
261/* worker thread main function */
262static void *worker_main(void *arg)
263{
264 struct bankd_worker *worker = (struct bankd_worker *) arg;
265 void *top_ctx;
266 int rc;
267
Harald Welte8d858292018-08-15 23:36:46 +0200268 worker_set_state(worker, BW_ST_INIT);
269
Harald Welte77911b02018-08-14 23:47:30 +0200270 /* not permitted in multithreaded environment */
271 talloc_disable_null_tracking();
272 top_ctx = talloc_named_const(NULL, 0, "top");
273 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
274
275 /* push cleanup helper */
276 pthread_cleanup_push(&worker_cleanup, worker);
277
278 /* we continuously perform the same loop here, recycling the worker thread
279 * once the client connection is gone or we have some trouble with the card/reader */
280 while (1) {
281 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
282
Harald Welte8d858292018-08-15 23:36:46 +0200283 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200284 /* first wait for an incoming TCP connection */
285 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
286 &worker->client.peer_addr_len);
287 if (rc < 0) {
288 continue;
289 }
290 worker->client.fd = rc;
Harald Welte8d858292018-08-15 23:36:46 +0200291 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200292
293 /* run the main worker transceive loop body until there was some error */
294 while (1) {
295 rc = worker_transceive_loop(worker);
296 if (rc < 0)
297 break;
298 }
299
300 /* clean-up: reset to sane state */
301 if (worker->reader.pcsc.hCard) {
302 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
303 worker->reader.pcsc.hCard = 0;
304 }
305 if (worker->reader.pcsc.hContext) {
306 SCardReleaseContext(worker->reader.pcsc.hContext);
307 worker->reader.pcsc.hContext = 0;
308 }
309 if (worker->client.fd >= 0)
310 close(worker->client.fd);
311 worker->client.fd = -1;
312 }
313
314 pthread_cleanup_pop(1);
315 talloc_free(top_ctx);
316 pthread_exit(NULL);
317}