blob: f6eb64f6c928761adaca622029ed07042afde1f3 [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
12#include <osmocom/core/linuxlist.h>
13
14#include <osmocom/gsm/ipa.h>
15#include <osmocom/gsm/protocol/ipaccess.h>
16
17#include <asn1c/asn_application.h>
18#include <osmocom/rspro/RsproPDU.h>
19
20#include "bankd.h"
21
22__thread void *talloc_asn1_ctx;
23
24static void *worker_main(void *arg);
25
26/***********************************************************************
27* bankd core / main thread
28***********************************************************************/
29
30static void bankd_init(struct bankd *bankd)
31{
32 /* intialize members of 'bankd' */
33 INIT_LLIST_HEAD(&bankd->slot_mappings);
34 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
35 INIT_LLIST_HEAD(&bankd->workers);
36 pthread_mutex_init(&bankd->workers_mutex, NULL);
37}
38
39/* create + start a new bankd_worker thread */
40static struct bankd_worker *bankd_create_worker(struct bankd *bankd)
41{
42 struct bankd_worker *worker;
43 int rc;
44
45 worker = talloc_zero(bankd, struct bankd_worker);
46 if (!worker)
47 return NULL;
48
49 worker->bankd = bankd;
50
51 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
52
53 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
54 if (rc != 0) {
55 talloc_free(worker);
56 return NULL;
57 }
58
59 pthread_mutex_lock(&bankd->workers_mutex);
60 llist_add_tail(&worker->list, &bankd->workers);
61 pthread_mutex_unlock(&bankd->workers_mutex);
62
63 return worker;
64}
65
66static bool terminate = false;
67
68int main(int argc, char **argv)
69{
70 struct bankd *bankd = talloc_zero(NULL, struct bankd);
71 int i, rc;
72
73 OSMO_ASSERT(bankd);
74 bankd_init(bankd);
75
76 for (i = 0; i < 10; i++) {
77 struct bankd_worker *w;
78 w = bankd_create_worker(bankd);
79 if (!w)
80 exit(21);
81 }
82
83 while (1) {
84 if (terminate)
85 break;
86 }
87
88 talloc_free(bankd);
89 exit(0);
90}
91
92
93
94/***********************************************************************
95 * bankd worker thread
96 ***********************************************************************/
97
98#define PCSC_ERROR(rv, text) \
99if (rv != SCARD_S_SUCCESS) { \
100 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
101 goto end; \
102} else { \
103 printf(text ": OK\n\n"); \
104}
105
106
107static void worker_cleanup(void *arg)
108{
109 struct bankd_worker *worker = (struct bankd_worker *) arg;
110 struct bankd *bankd = worker->bankd;
111
112 /* FIXME: should we still do this? in the thread ?!? */
113 pthread_mutex_lock(&bankd->workers_mutex);
114 llist_del(&worker->list);
115 talloc_free(worker); /* FIXME: is this safe? */
116 pthread_mutex_unlock(&bankd->workers_mutex);
117}
118
119
120#if 0
121/* function running inside a worker thread; doing some initialization */
122static void worker_init(struct bankd_worker *worker)
123{
124 int rc;
125
126 /* push cleanup helper */
127 pthread_cleanup_push(&worker_cleanup, worker);
128
129 /* The PC/SC context must be created inside the thread where we'll later use it */
130 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
131 PCSC_ERROR(rc, "SCardEstablishContext")
132
133 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
134 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
135 PCSC_ERROR(rc, "SCardConnect")
136
137 return;
138end:
139 pthread_exit(NULL);
140}
141#endif
142
143
144static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
145{
146 struct ipaccess_head *hh;
147 uint16_t len;
148 int needed, rc;
149
150 if (buf_size < sizeof(*hh))
151 return -1;
152
153 hh = (struct ipaccess_head *) buf;
154
155 /* 1) blocking read from the socket (IPA header) */
156 rc = read(fd, buf, sizeof(*hh));
157 if (rc < sizeof(*hh))
158 return -2;
159
160 len = ntohs(hh->len);
161 needed = len; //- sizeof(*hh);
162
163 /* 2) blocking read from the socket (payload) */
164 rc = read(fd, buf+sizeof(*hh), needed);
165 if (rc < needed)
166 return -3;
167
168 return len;
169}
170
171/* handle one incoming RSPRO message from a client inside a worker thread */
172static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
173{
174 switch (pdu->msg.present) {
175 case RsproPDUchoice_PR_connectClientReq:
176 /* FIXME */
177 break;
178 case RsproPDUchoice_PR_tpduModemToCard:
179 /* FIXME */
180 break;
181 case RsproPDUchoice_PR_clientSlotStatusInd:
182 /* FIXME */
183 break;
184 default:
185 return -100;
186 }
187
188 return 0;
189}
190
191/* body of the main transceive loop */
192static int worker_transceive_loop(struct bankd_worker *worker)
193{
194 struct ipaccess_head *hh;
195 struct ipaccess_head_ext *hh_ext;
196 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
197 asn_dec_rval_t rval;
198 int data_len, rc;
199 RsproPDU_t *pdu;
200
201 /* 1) blocking read of entire IPA message from the socket */
202 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
203 if (rc < 0)
204 return rc;
205 data_len = rc;
206
207 hh = (struct ipaccess_head *) buf;
208 if (hh->proto != IPAC_PROTO_OSMO)
209 return -4;
210
211 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
212 if (data_len < sizeof(*hh_ext))
213 return -5;
214 data_len -= sizeof(*hh_ext);
215 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
216 return -6;
217
218 /* 2) ASN1 BER decode of the message */
219 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
220 if (rval.code != RC_OK)
221 return -7;
222
223 /* 3) handling of the message, possibly resulting in PCSC commands */
224 rc = worker_handle_rspro(worker, pdu);
225 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
226 if (rc < 0)
227 return rc;
228
229 /* everything OK if we reach here */
230 return 0;
231}
232
233/* worker thread main function */
234static void *worker_main(void *arg)
235{
236 struct bankd_worker *worker = (struct bankd_worker *) arg;
237 void *top_ctx;
238 int rc;
239
240 /* not permitted in multithreaded environment */
241 talloc_disable_null_tracking();
242 top_ctx = talloc_named_const(NULL, 0, "top");
243 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
244
245 /* push cleanup helper */
246 pthread_cleanup_push(&worker_cleanup, worker);
247
248 /* we continuously perform the same loop here, recycling the worker thread
249 * once the client connection is gone or we have some trouble with the card/reader */
250 while (1) {
251 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
252
253 /* first wait for an incoming TCP connection */
254 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
255 &worker->client.peer_addr_len);
256 if (rc < 0) {
257 continue;
258 }
259 worker->client.fd = rc;
260
261 /* run the main worker transceive loop body until there was some error */
262 while (1) {
263 rc = worker_transceive_loop(worker);
264 if (rc < 0)
265 break;
266 }
267
268 /* clean-up: reset to sane state */
269 if (worker->reader.pcsc.hCard) {
270 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
271 worker->reader.pcsc.hCard = 0;
272 }
273 if (worker->reader.pcsc.hContext) {
274 SCardReleaseContext(worker->reader.pcsc.hContext);
275 worker->reader.pcsc.hContext = 0;
276 }
277 if (worker->client.fd >= 0)
278 close(worker->client.fd);
279 worker->client.fd = -1;
280 }
281
282 pthread_cleanup_pop(1);
283 talloc_free(top_ctx);
284 pthread_exit(NULL);
285}