blob: fe961862c0631eb1c6af273821411ffd74aea333 [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;
95 }
96
97 talloc_free(bankd);
98 exit(0);
99}
100
101
102
103/***********************************************************************
104 * bankd worker thread
105 ***********************************************************************/
106
107#define PCSC_ERROR(rv, text) \
108if (rv != SCARD_S_SUCCESS) { \
109 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
110 goto end; \
111} else { \
112 printf(text ": OK\n\n"); \
113}
114
Harald Welte8d858292018-08-15 23:36:46 +0200115#define LOGW(w, fmt, args...) \
116 printf("[%u] " fmt, (w)->num, args)
117
118struct value_string worker_state_names[] = {
119 { BW_ST_INIT, "INIT" },
120 { BW_ST_ACCEPTING, "ACCEPTING" },
121 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
122 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
123 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
124 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
125 { 0, NULL }
126};
127
128static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
129{
130 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
131 worker->state = new_state;
132}
Harald Welte77911b02018-08-14 23:47:30 +0200133
134static void worker_cleanup(void *arg)
135{
136 struct bankd_worker *worker = (struct bankd_worker *) arg;
137 struct bankd *bankd = worker->bankd;
138
139 /* FIXME: should we still do this? in the thread ?!? */
140 pthread_mutex_lock(&bankd->workers_mutex);
141 llist_del(&worker->list);
142 talloc_free(worker); /* FIXME: is this safe? */
143 pthread_mutex_unlock(&bankd->workers_mutex);
144}
145
146
147#if 0
148/* function running inside a worker thread; doing some initialization */
149static void worker_init(struct bankd_worker *worker)
150{
151 int rc;
152
153 /* push cleanup helper */
154 pthread_cleanup_push(&worker_cleanup, worker);
155
156 /* The PC/SC context must be created inside the thread where we'll later use it */
157 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
158 PCSC_ERROR(rc, "SCardEstablishContext")
159
160 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
161 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
162 PCSC_ERROR(rc, "SCardConnect")
163
164 return;
165end:
166 pthread_exit(NULL);
167}
168#endif
169
170
171static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
172{
173 struct ipaccess_head *hh;
174 uint16_t len;
175 int needed, rc;
176
177 if (buf_size < sizeof(*hh))
178 return -1;
179
180 hh = (struct ipaccess_head *) buf;
181
182 /* 1) blocking read from the socket (IPA header) */
183 rc = read(fd, buf, sizeof(*hh));
184 if (rc < sizeof(*hh))
185 return -2;
186
187 len = ntohs(hh->len);
188 needed = len; //- sizeof(*hh);
189
190 /* 2) blocking read from the socket (payload) */
191 rc = read(fd, buf+sizeof(*hh), needed);
192 if (rc < needed)
193 return -3;
194
195 return len;
196}
197
198/* handle one incoming RSPRO message from a client inside a worker thread */
199static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
200{
201 switch (pdu->msg.present) {
202 case RsproPDUchoice_PR_connectClientReq:
203 /* FIXME */
204 break;
205 case RsproPDUchoice_PR_tpduModemToCard:
206 /* FIXME */
207 break;
208 case RsproPDUchoice_PR_clientSlotStatusInd:
209 /* FIXME */
210 break;
211 default:
212 return -100;
213 }
214
215 return 0;
216}
217
218/* body of the main transceive loop */
219static int worker_transceive_loop(struct bankd_worker *worker)
220{
221 struct ipaccess_head *hh;
222 struct ipaccess_head_ext *hh_ext;
223 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
224 asn_dec_rval_t rval;
225 int data_len, rc;
226 RsproPDU_t *pdu;
227
228 /* 1) blocking read of entire IPA message from the socket */
229 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
230 if (rc < 0)
231 return rc;
232 data_len = rc;
233
234 hh = (struct ipaccess_head *) buf;
235 if (hh->proto != IPAC_PROTO_OSMO)
236 return -4;
237
238 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
239 if (data_len < sizeof(*hh_ext))
240 return -5;
241 data_len -= sizeof(*hh_ext);
242 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
243 return -6;
244
245 /* 2) ASN1 BER decode of the message */
246 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
247 if (rval.code != RC_OK)
248 return -7;
249
250 /* 3) handling of the message, possibly resulting in PCSC commands */
251 rc = worker_handle_rspro(worker, pdu);
252 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
253 if (rc < 0)
254 return rc;
255
256 /* everything OK if we reach here */
257 return 0;
258}
259
260/* worker thread main function */
261static void *worker_main(void *arg)
262{
263 struct bankd_worker *worker = (struct bankd_worker *) arg;
264 void *top_ctx;
265 int rc;
266
Harald Welte8d858292018-08-15 23:36:46 +0200267 worker_set_state(worker, BW_ST_INIT);
268
Harald Welte77911b02018-08-14 23:47:30 +0200269 /* not permitted in multithreaded environment */
270 talloc_disable_null_tracking();
271 top_ctx = talloc_named_const(NULL, 0, "top");
272 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
273
274 /* push cleanup helper */
275 pthread_cleanup_push(&worker_cleanup, worker);
276
277 /* we continuously perform the same loop here, recycling the worker thread
278 * once the client connection is gone or we have some trouble with the card/reader */
279 while (1) {
280 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
281
Harald Welte8d858292018-08-15 23:36:46 +0200282 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200283 /* first wait for an incoming TCP connection */
284 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
285 &worker->client.peer_addr_len);
286 if (rc < 0) {
287 continue;
288 }
289 worker->client.fd = rc;
Harald Welte8d858292018-08-15 23:36:46 +0200290 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200291
292 /* run the main worker transceive loop body until there was some error */
293 while (1) {
294 rc = worker_transceive_loop(worker);
295 if (rc < 0)
296 break;
297 }
298
299 /* clean-up: reset to sane state */
300 if (worker->reader.pcsc.hCard) {
301 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
302 worker->reader.pcsc.hCard = 0;
303 }
304 if (worker->reader.pcsc.hContext) {
305 SCardReleaseContext(worker->reader.pcsc.hContext);
306 worker->reader.pcsc.hContext = 0;
307 }
308 if (worker->client.fd >= 0)
309 close(worker->client.fd);
310 worker->client.fd = -1;
311 }
312
313 pthread_cleanup_pop(1);
314 talloc_free(top_ctx);
315 pthread_exit(NULL);
316}