1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
|
/*
This file is part of TALER
(C) 2014-2023 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file taler-merchant-httpd_exchanges.c
* @brief logic this HTTPD keeps for each exchange we interact with
* @author Marcello Stanisci
* @author Christian Grothoff
*/
#include "platform.h"
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd_exchanges.h"
#include "taler-merchant-httpd.h"
#include <regex.h>
/**
* How often do we retry DB transactions with soft errors?
*/
#define MAX_RETRIES 3
/**
* Minimum delay after which we'll re-fetch key information from the exchange.
*/
#define MIN_RELOAD_DELAY GNUNET_TIME_relative_multiply ( \
GNUNET_TIME_UNIT_MINUTES, 2)
/**
* Delay after which we'll allow clients to force us to re-fetch key
* information from the exchange if we don't know the denomination key.
*/
#define FORCED_RELOAD_DELAY GNUNET_TIME_relative_multiply ( \
GNUNET_TIME_UNIT_MINUTES, 15)
/**
* Threshold after which exponential backoff should not increase.
*/
#define RETRY_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply ( \
GNUNET_TIME_UNIT_SECONDS, 60)
/**
* Perform our exponential back-off calculation, starting at 1 ms
* and then going by a factor of 2 up unto a maximum of RETRY_BACKOFF_THRESHOLD.
*
* @param r current backoff time, initially zero
*/
#define RETRY_BACKOFF(r) GNUNET_TIME_relative_min (RETRY_BACKOFF_THRESHOLD, \
GNUNET_TIME_relative_multiply ( \
GNUNET_TIME_relative_max ( \
GNUNET_TIME_UNIT_MILLISECONDS, \
(r)), 2));
/**
* Information we keep for a pending #MMH_EXCHANGES_find_exchange() operation.
*/
struct TMH_EXCHANGES_FindOperation
{
/**
* Kept in a DLL.
*/
struct TMH_EXCHANGES_FindOperation *next;
/**
* Kept in a DLL.
*/
struct TMH_EXCHANGES_FindOperation *prev;
/**
* Function to call with the result.
*/
TMH_EXCHANGES_FindContinuation fc;
/**
* Closure for @e fc.
*/
void *fc_cls;
/**
* Exchange we wait for the /keys for.
*/
struct TMH_Exchange *my_exchange;
/**
* Task scheduled to asynchronously return the result to
* the find continuation.
*/
struct GNUNET_SCHEDULER_Task *at;
};
/**
* Information about wire transfer fees of an exchange, by wire method.
*/
struct FeesByWireMethod
{
/**
* Kept in a DLL.
*/
struct FeesByWireMethod *next;
/**
* Kept in a DLL.
*/
struct FeesByWireMethod *prev;
/**
* Wire method these fees are for.
*/
char *wire_method;
/**
* Applicable fees, NULL if unknown/error.
*/
struct TALER_EXCHANGE_WireAggregateFees *af;
};
/**
* Restriction that applies to an exchange account.
*/
struct Restriction
{
/**
* Kept in a DLL.
*/
struct Restriction *next;
/**
* Kept in a DLL.
*/
struct Restriction *prev;
/**
* Type of restriction imposed on the account.
*/
enum TALER_EXCHANGE_AccountRestrictionType type;
/**
* Details depending on @e type.
*/
union
{
/**
* Accounts must match the given regular expression.
*/
struct
{
/**
* Pre-compiled regex.
*/
regex_t ex;
} regex;
} details;
};
/**
* Information about a bank account of the exchange.
*/
struct ExchangeAccount
{
/**
* Kept in a DLL.
*/
struct ExchangeAccount *next;
/**
* Kept in a DLL.
*/
struct ExchangeAccount *prev;
/**
* Wire method of this exchange account.
*/
char *wire_method;
/**
* Currency conversion that applies to this account,
* NULL if none.
*/
char *conversion_url;
/**
* Head of DLL of debit restrictions of this account.
*/
struct Restriction *d_head;
/**
* Tail of DLL of debit restrictions of this account.
*/
struct Restriction *d_tail;
};
/**
* Exchange
*/
struct TMH_Exchange
{
/**
* Kept in a DLL.
*/
struct TMH_Exchange *next;
/**
* Kept in a DLL.
*/
struct TMH_Exchange *prev;
/**
* Head of FOs pending for this exchange.
*/
struct TMH_EXCHANGES_FindOperation *fo_head;
/**
* Tail of FOs pending for this exchange.
*/
struct TMH_EXCHANGES_FindOperation *fo_tail;
/**
* Head of accounts of this exchange.
*/
struct ExchangeAccount *acc_head;
/**
* Tail of accounts of this exchange.
*/
struct ExchangeAccount *acc_tail;
/**
* (base) URL of the exchange.
*/
char *url;
/**
* A connection to this exchange
*/
struct TALER_EXCHANGE_Handle *conn;
/**
* Active /wire request to the exchange, or NULL.
*/
struct TALER_EXCHANGE_WireHandle *wire_request;
/**
* Task to re-run /wire after some delay.
*/
struct GNUNET_SCHEDULER_Task *wire_task;
/**
* Head of wire fees from /wire request.
*/
struct FeesByWireMethod *wire_fees_head;
/**
* Tail of wire fees from /wire request.
*/
struct FeesByWireMethod *wire_fees_tail;
/**
* Master public key, guaranteed to be set ONLY for
* trusted exchanges.
*/
struct TALER_MasterPublicKeyP master_pub;
/**
* How soon can may we, at the earliest, re-download /keys?
*/
struct GNUNET_TIME_Absolute first_retry;
/**
* How soon should we re-download /keys?
*/
struct GNUNET_TIME_Timestamp keys_expiration;
/**
* How long should we wait between the next retry?
*/
struct GNUNET_TIME_Relative retry_delay;
/**
* How long should we wait between the next retry for /wire?
*/
struct GNUNET_TIME_Relative wire_retry_delay;
/**
* Task where we retry fetching /keys from the exchange.
*/
struct GNUNET_SCHEDULER_Task *retry_task;
/**
* Falsoe to indicate that there is a /keys request
* we are waiting for.
* True to indicate that /keys data is up-to-date.
*/
bool have_keys;
/**
* true if this exchange is from our configuration and
* explicitly trusted, false if we need to check each
* key to be sure it is trusted.
*/
bool trusted;
/**
* true if this exchange did return to use the
* response from /wire.
*/
bool have_wire;
};
/**
* Context for all exchange operations (useful to the event loop)
*/
static struct GNUNET_CURL_Context *merchant_curl_ctx;
/**
* Context for integrating #merchant_curl_ctx with the
* GNUnet event loop.
*/
static struct GNUNET_CURL_RescheduleContext *merchant_curl_rc;
/**
* Head of exchanges we know about.
*/
static struct TMH_Exchange *exchange_head;
/**
* Tail of exchanges we know about.
*/
static struct TMH_Exchange *exchange_tail;
/**
* How many exchanges do we trust (for our configured
* currency) as per our configuration? Used for a
* sanity-check on startup.
*/
static int trusted_exchange_count;
/**
* Function called with information about who is auditing
* a particular exchange and what key the exchange is using.
*
* @param cls closure, will be `struct TMH_Exchange` so that
* when this function gets called, it will change the flag 'have_keys'
* to 'true'. Note: 'keys' is automatically saved inside the exchange's
* handle, which is contained inside 'struct TMH_Exchange', when
* this callback is called. Thus, once 'have_keys' turns 'true',
* it is safe to call 'TALER_EXCHANGE_get_keys()' on the exchange's handle,
* in order to get the "good" keys.
* @param kr response details
*/
static void
keys_mgmt_cb (void *cls,
const struct TALER_EXCHANGE_KeysResponse *kr);
/**
* Free data structures within @a ea, but not @a ea
* pointer itself.
*
* @param[in] ea data structure to free
*/
static void
free_exchange_account (struct ExchangeAccount *ea)
{
struct Restriction *r;
while (NULL != (r = ea->d_head))
{
GNUNET_CONTAINER_DLL_remove (ea->d_head,
ea->d_tail,
r);
switch (r->type)
{
case TALER_EXCHANGE_AR_INVALID:
GNUNET_assert (0);
break;
case TALER_EXCHANGE_AR_DENY:
break;
case TALER_EXCHANGE_AR_REGEX:
regfree (&r->details.regex.ex);
break;
}
GNUNET_free (r);
}
GNUNET_free (ea->wire_method);
GNUNET_free (ea->conversion_url);
}
/**
* Free list of all accounts in @a exchange.
*
* @param[in,out] exchange entry to free accounts for
*/
static void
purge_exchange_accounts (struct TMH_Exchange *exchange)
{
struct ExchangeAccount *acc;
while (NULL != (acc = exchange->acc_head))
{
GNUNET_CONTAINER_DLL_remove (exchange->acc_head,
exchange->acc_tail,
acc);
free_exchange_account (acc);
GNUNET_free (acc);
}
}
/**
* Set the list of accounts of @a exchange.
*
* @param[in,out] exchange exchange to initialize or update
* @param accounts_len length of @a accounts array
* @param accounts array of accounts to convert
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
set_exchange_accounts (struct TMH_Exchange *exchange,
unsigned int accounts_len,
const struct TALER_EXCHANGE_WireAccount *accounts)
{
purge_exchange_accounts (exchange);
for (unsigned int i = 0; i<accounts_len; i++)
{
const struct TALER_EXCHANGE_WireAccount *account = &accounts[i];
struct ExchangeAccount *acc;
acc = GNUNET_new (struct ExchangeAccount);
acc->wire_method = TALER_payto_get_method (account->payto_uri);
if (NULL != account->conversion_url)
acc->conversion_url = GNUNET_strdup (account->conversion_url);
GNUNET_CONTAINER_DLL_insert (exchange->acc_head,
exchange->acc_tail,
acc);
for (unsigned int j = 0; j<account->debit_restrictions_length; j++)
{
const struct TALER_EXCHANGE_AccountRestriction *ar =
&account->debit_restrictions[j];
struct Restriction *r;
r = GNUNET_new (struct Restriction);
r->type = ar->type;
switch (ar->type)
{
case TALER_EXCHANGE_AR_INVALID:
GNUNET_assert (0);
break;
case TALER_EXCHANGE_AR_DENY:
break;
case TALER_EXCHANGE_AR_REGEX:
if (0 != regcomp (&r->details.regex.ex,
ar->details.regex.posix_egrep,
REG_NOSUB | REG_EXTENDED))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
break;
}
GNUNET_CONTAINER_DLL_insert (acc->d_head,
acc->d_tail,
r);
}
}
return GNUNET_OK;
}
/**
* Retry getting information from the given exchange in
* the closure.
*
* @param cls the exchange
*/
static void
retry_exchange (void *cls)
{
struct TMH_Exchange *exchange = cls;
/* might be a scheduled reload and not our first attempt */
exchange->retry_task = NULL;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Connecting to exchange %s in retry_exchange()\n",
exchange->url);
if (NULL == exchange->conn)
{
exchange->conn = TALER_EXCHANGE_connect (merchant_curl_ctx,
exchange->url,
&keys_mgmt_cb,
exchange,
TALER_EXCHANGE_OPTION_END);
}
else
{
struct GNUNET_TIME_Timestamp next;
next = TALER_EXCHANGE_check_keys_current (exchange->conn,
TALER_EXCHANGE_CKF_NONE);
if (! GNUNET_TIME_absolute_is_zero (next.abs_time))
{
exchange->retry_task = GNUNET_SCHEDULER_add_at (next.abs_time,
&retry_exchange,
exchange);
}
}
/* Note: while the API spec says 'returns NULL on error', the implementation
actually never returns NULL. */
GNUNET_break (NULL != exchange->conn);
}
/**
* Function called with information about the wire fees
* for each wire method. Stores the wire fees with the
* exchange for later use.
*
* @param exchange connection to the exchange
* @param master_pub public key of the exchange
* @param num_methods number of wire methods supported
* @param fbm wire fees by method
* @return #TALER_EC_NONE on success
*/
static enum TALER_ErrorCode
process_wire_fees (struct TMH_Exchange *exchange,
const struct TALER_MasterPublicKeyP *master_pub,
unsigned int num_methods,
const struct TALER_EXCHANGE_WireFeesByMethod *fbm)
{
for (unsigned int i = 0; i<num_methods; i++)
{
const char *wire_method = fbm[i].method;
const struct TALER_EXCHANGE_WireAggregateFees *fees = fbm[i].fees_head;
struct FeesByWireMethod *f;
struct TALER_EXCHANGE_WireAggregateFees *endp;
struct TALER_EXCHANGE_WireAggregateFees *af;
for (f = exchange->wire_fees_head; NULL != f; f = f->next)
if (0 == strcasecmp (wire_method,
f->wire_method))
break;
if (NULL == f)
{
f = GNUNET_new (struct FeesByWireMethod);
f->wire_method = GNUNET_strdup (wire_method);
GNUNET_CONTAINER_DLL_insert (exchange->wire_fees_head,
exchange->wire_fees_tail,
f);
}
endp = f->af;
while ( (NULL != endp) &&
(NULL != endp->next) )
endp = endp->next;
while ( (NULL != endp) &&
(NULL != fees) &&
(GNUNET_TIME_timestamp_cmp (fees->start_date,
<,
endp->end_date)) )
fees = fees->next;
if ( (NULL != endp) &&
(NULL != fees) &&
(GNUNET_TIME_timestamp_cmp (fees->start_date,
!=,
endp->end_date)) )
{
/* Hole in the fee structure, not allowed! */
GNUNET_break_op (0);
return TALER_EC_MERCHANT_GENERIC_HOLE_IN_WIRE_FEE_STRUCTURE;
}
while (NULL != fees)
{
struct GNUNET_HashCode h_wire_method;
enum GNUNET_DB_QueryStatus qs;
af = GNUNET_new (struct TALER_EXCHANGE_WireAggregateFees);
*af = *fees;
GNUNET_CRYPTO_hash (wire_method,
strlen (wire_method) + 1,
&h_wire_method);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Storing wire fee for `%s' and method `%s' at %s in DB; the fee is %s\n",
TALER_B2S (master_pub),
wire_method,
GNUNET_TIME_timestamp2s (af->start_date),
TALER_amount2s (&af->fees.wire));
TMH_db->preflight (TMH_db->cls);
if (GNUNET_OK !=
TMH_db->start (TMH_db->cls,
"store wire fee"))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to start database transaction to store exchange wire fees (will try to continue anyway)!\n");
GNUNET_free (af);
fees = fees->next;
continue;
}
qs = TMH_db->store_wire_fee_by_exchange (TMH_db->cls,
master_pub,
&h_wire_method,
&af->fees,
af->start_date,
af->end_date,
&af->master_sig);
if (0 > qs)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to persist exchange wire fees in merchant DB (will try to continue anyway)!\n");
GNUNET_free (af);
fees = fees->next;
TMH_db->rollback (TMH_db->cls);
continue;
}
if (0 == qs)
{
/* Entry was already in DB, fine, continue as if we had succeeded */
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Fees already in DB, rolling back transaction attempt!\n");
TMH_db->rollback (TMH_db->cls);
}
if (0 < qs)
{
/* Inserted into DB, make sure transaction completes */
qs = TMH_db->commit (TMH_db->cls);
if (0 > qs)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to persist exchange wire fees in merchant DB (will try to continue anyway)!\n");
GNUNET_free (af);
fees = fees->next;
continue;
}
}
af->next = NULL;
if (NULL == endp)
f->af = af;
else
endp->next = af;
endp = af;
fees = fees->next;
} /* all fees for this method */
} /* for all methods (i) */
return TALER_EC_NONE;
}
/**
* Add account restriction @a a to array of @a restrictions.
*
* @param[in,out] restrictions JSON array to build
* @param r restriction to add to @a restrictions
* @return #GNUNET_SYSERR if @a r is malformed
*/
static enum GNUNET_GenericReturnValue
add_restriction (json_t *restrictions,
const struct TALER_EXCHANGE_AccountRestriction *r)
{
json_t *jr;
jr = NULL;
switch (r->type)
{
case TALER_EXCHANGE_AR_INVALID:
GNUNET_break_op (0);
return GNUNET_SYSERR;
case TALER_EXCHANGE_AR_DENY:
jr = GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("type",
"deny")
);
break;
case TALER_EXCHANGE_AR_REGEX:
jr = GNUNET_JSON_PACK (
GNUNET_JSON_pack_string (
"type",
"regex"),
GNUNET_JSON_pack_string (
"regex",
r->details.regex.posix_egrep),
GNUNET_JSON_pack_string (
"human_hint",
r->details.regex.human_hint),
GNUNET_JSON_pack_object_incref (
"human_hint_i18n",
(json_t *) r->details.regex.human_hint_i18n)
);
break;
}
if (NULL == jr)
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
GNUNET_assert (0 ==
json_array_append_new (restrictions,
jr));
return GNUNET_OK;
}
/**
* Function called with information about the wire accounts of the exchange.
*
* @param exchange the exchange
* @param master_pub public key of the exchange
* @param accounts_len length of the @a accounts array
* @param accounts list of wire accounts of the exchange
* @return #TALER_EC_NONE on success
*/
static enum TALER_ErrorCode
process_wire_accounts (struct TMH_Exchange *exchange,
const struct TALER_MasterPublicKeyP *master_pub,
unsigned int accounts_len,
const struct TALER_EXCHANGE_WireAccount *accounts)
{
for (unsigned int r = 0; r<MAX_RETRIES; r++)
{
enum GNUNET_DB_QueryStatus qs;
if (GNUNET_OK !=
TMH_db->start (TMH_db->cls,
"update_exchange_accounts"))
{
TMH_db->rollback (TMH_db->cls);
GNUNET_break (0);
return TALER_EC_GENERIC_DB_START_FAILED;
}
qs = TMH_db->delete_exchange_accounts (TMH_db->cls,
master_pub);
if (qs < 0)
{
TMH_db->rollback (TMH_db->cls);
if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
continue;
GNUNET_break (0);
return TALER_EC_GENERIC_DB_STORE_FAILED;
}
for (unsigned int i = 0; i<accounts_len; i++)
{
const struct TALER_EXCHANGE_WireAccount *account = &accounts[i];
json_t *debit_restrictions;
json_t *credit_restrictions;
debit_restrictions = json_array ();
GNUNET_assert (NULL != debit_restrictions);
credit_restrictions = json_array ();
GNUNET_assert (NULL != credit_restrictions);
for (unsigned int j = 0; j<account->debit_restrictions_length; j++)
{
if (GNUNET_OK !=
add_restriction (debit_restrictions,
&account->debit_restrictions[j]))
{
TMH_db->rollback (TMH_db->cls);
GNUNET_break (0);
json_decref (debit_restrictions);
json_decref (credit_restrictions);
return TALER_EC_MERCHANT_GENERIC_EXCHANGE_WIRE_REQUEST_FAILED;
}
}
for (unsigned int j = 0; j<account->credit_restrictions_length; j++)
{
if (GNUNET_OK !=
add_restriction (credit_restrictions,
&account->credit_restrictions[j]))
{
TMH_db->rollback (TMH_db->cls);
GNUNET_break (0);
json_decref (debit_restrictions);
json_decref (credit_restrictions);
return TALER_EC_MERCHANT_GENERIC_EXCHANGE_WIRE_REQUEST_FAILED;
}
}
qs = TMH_db->insert_exchange_account (TMH_db->cls,
master_pub,
account->payto_uri,
account->conversion_url,
debit_restrictions,
credit_restrictions,
&account->master_sig);
json_decref (debit_restrictions);
json_decref (credit_restrictions);
if (qs < 0)
{
TMH_db->rollback (TMH_db->cls);
if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
goto outer;
GNUNET_break (0);
return TALER_EC_GENERIC_DB_STORE_FAILED;
}
}
qs = TMH_db->commit (TMH_db->cls);
if (qs < 0)
{
TMH_db->rollback (TMH_db->cls);
if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
continue;
GNUNET_break (0);
return TALER_EC_GENERIC_DB_COMMIT_FAILED;
}
set_exchange_accounts (exchange,
accounts_len,
accounts);
return TALER_EC_NONE;
outer:;
}
return TALER_EC_GENERIC_DB_SOFT_FAILURE;
}
/**
* Obtain applicable fees for @a exchange and @a wire_method.
*
* @param exchange the exchange to query
* @param now current time
* @param wire_method the wire method we want the fees for
* @return NULL if we do not have fees for this method yet
*/
static const struct FeesByWireMethod *
get_wire_fees (struct TMH_Exchange *exchange,
struct GNUNET_TIME_Timestamp now,
const char *wire_method)
{
for (struct FeesByWireMethod *fbw = exchange->wire_fees_head;
NULL != fbw;
fbw = fbw->next)
{
if (0 == strcasecmp (fbw->wire_method,
wire_method) )
{
struct TALER_EXCHANGE_WireAggregateFees *af;
/* Advance through list up to current time */
while ( (NULL != (af = fbw->af)) &&
(GNUNET_TIME_timestamp_cmp (now,
>=,
af->end_date)) )
{
fbw->af = af->next;
GNUNET_free (af);
}
return fbw;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Exchange supports `%s' as a wire method (but we do not use that one)\n",
fbw->wire_method);
}
return NULL;
}
/**
* Check if we have any remaining pending requests for the
* given @a exchange, and if we have the required data, call
* the callback.
*
* @param exchange the exchange to check for pending find operations
* @return true if we need /wire data from @a exchange
*/
static bool
process_find_operations (struct TMH_Exchange *exchange)
{
struct TMH_EXCHANGES_FindOperation *fn;
struct GNUNET_TIME_Timestamp now;
if (! exchange->have_wire)
return true;
now = GNUNET_TIME_timestamp_get ();
for (struct FeesByWireMethod *fbw = exchange->wire_fees_head;
NULL != fbw;
fbw = fbw->next)
{
bool removed = false;
while ( (NULL != fbw->af) &&
(GNUNET_TIME_timestamp_cmp (fbw->af->end_date,
<,
now)) )
{
struct TALER_EXCHANGE_WireAggregateFees *af = fbw->af;
fbw->af = af->next;
GNUNET_free (af);
removed = true;
}
if (NULL == fbw->af)
{
/* Disagreement on the current time */
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Exchange has no wire fees configured for `%s' wire method\n",
fbw->wire_method);
if (removed)
{
exchange->have_wire = false;
return true; /* We just removed previous fees, try fetching update */
}
}
if (GNUNET_TIME_timestamp_cmp (fbw->af->start_date,
>,
now))
{
/* Disagreement on the current time */
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Exchange's earliest fee is %s ahead of our time. Clock skew issue?\n",
GNUNET_TIME_relative2s (
GNUNET_TIME_absolute_get_remaining (
fbw->af->start_date.abs_time),
true));
}
}
if (! exchange->have_wire)
return true; /* need /wire response to continue */
fn = NULL;
for (struct TMH_EXCHANGES_FindOperation *fo = exchange->fo_head;
NULL != fo;
fo = fn)
{
struct TALER_EXCHANGE_HttpResponse hr = {
.http_status = MHD_HTTP_OK,
};
fo->fc (fo->fc_cls,
&hr,
exchange->conn,
exchange);
fn = fo->next;
TMH_EXCHANGES_find_exchange_cancel (fo);
}
return false;
}
static void
wire_task_cb (void *cls);
/**
* Callbacks of this type are used to serve the result of submitting a
* wire format inquiry request to a exchange.
*
* If the request fails to generate a valid response from the
* exchange, @a http_status will also be zero.
*
* Must only be called if 'exchange->have_keys' is true.
* that is #TALER_EXCHANGE_get_keys() will succeed.
*
* @param cls closure, a `struct TMH_Exchange`
* @param wr response details
*/
static void
handle_wire_data (void *cls,
const struct TALER_EXCHANGE_WireResponse *wr)
{
struct TMH_Exchange *exchange = cls;
const struct TALER_EXCHANGE_Keys *keys;
enum TALER_ErrorCode ecx;
exchange->wire_request = NULL;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Received /wire response\n");
if (MHD_HTTP_OK != wr->hr.http_status)
{
struct TMH_EXCHANGES_FindOperation *fo;
exchange->have_wire = false;
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Failed to obtain /wire details from `%s': %u/%d\n",
exchange->url,
wr->hr.http_status,
wr->hr.ec);
while (NULL != (fo = exchange->fo_head))
{
fo->fc (fo->fc_cls,
&wr->hr,
exchange->conn,
false);
TMH_EXCHANGES_find_exchange_cancel (fo);
}
return;
}
keys = TALER_EXCHANGE_get_keys (exchange->conn);
GNUNET_assert (NULL != keys);
ecx = process_wire_accounts (exchange,
&keys->master_pub,
wr->details.ok.accounts_len,
wr->details.ok.accounts);
if (TALER_EC_NONE == ecx)
ecx = process_wire_fees (exchange,
&keys->master_pub,
wr->details.ok.fees_len,
wr->details.ok.fees);
if (TALER_EC_NONE != ecx)
{
/* Report hard failure to all callbacks! */
struct TMH_EXCHANGES_FindOperation *fo;
struct TALER_EXCHANGE_HttpResponse hrx = {
.ec = ecx,
.http_status = 0,
.reply = wr->hr.reply
};
GNUNET_break_op (0);
exchange->have_wire = false;
while (NULL != (fo = exchange->fo_head))
{
fo->fc (fo->fc_cls,
&hrx,
NULL,
false);
TMH_EXCHANGES_find_exchange_cancel (fo);
}
return;
}
exchange->have_wire = true;
if ( (process_find_operations (exchange)) &&
(NULL == exchange->wire_task) &&
(NULL == exchange->wire_request) )
{
/* need to run /wire again. But as we DID get a successful reply,
and as the exchange is unlikely to offer new wire methods very
frequently, start with some significant delay */
exchange->wire_retry_delay
= GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MINUTES,
exchange->wire_retry_delay);
exchange->wire_retry_delay = RETRY_BACKOFF (exchange->wire_retry_delay);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Need %s/wire, next download attempt in %s\n",
exchange->url,
GNUNET_STRINGS_relative_time_to_string (
exchange->wire_retry_delay,
true));
exchange->wire_task
= GNUNET_SCHEDULER_add_delayed (exchange->wire_retry_delay,
&wire_task_cb,
exchange);
}
}
/**
* Check if we have any remaining pending requests for the
* given @a exchange, and if we have the required data, call
* the callback. If requests without /wire data remain,
* retry the /wire request after some delay.
*
* Must only be called if 'exchange->have_keys' is true,
* that is #TALER_EXCHANGE_get_keys() will succeed.
*
* @param cls a `struct TMH_Exchange` to check
*/
static void
wire_task_cb (void *cls)
{
struct TMH_Exchange *exchange = cls;
exchange->wire_task = NULL;
GNUNET_assert (exchange->have_keys);
if (! process_find_operations (exchange))
return; /* no more need */
GNUNET_assert (NULL == exchange->wire_request);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Initiating /wire download\n");
exchange->wire_request = TALER_EXCHANGE_wire (exchange->conn,
&handle_wire_data,
exchange);
}
/**
* Free @a exchange.
*
* @param[in] exchange entry to free
*/
static void
free_exchange_entry (struct TMH_Exchange *exchange)
{
struct FeesByWireMethod *f;
GNUNET_CONTAINER_DLL_remove (exchange_head,
exchange_tail,
exchange);
purge_exchange_accounts (exchange);
while (NULL != (f = exchange->wire_fees_head))
{
struct TALER_EXCHANGE_WireAggregateFees *af;
GNUNET_CONTAINER_DLL_remove (exchange->wire_fees_head,
exchange->wire_fees_tail,
f);
while (NULL != (af = f->af))
{
f->af = af->next;
GNUNET_free (af);
}
GNUNET_free (f->wire_method);
GNUNET_free (f);
}
if (NULL != exchange->wire_request)
{
TALER_EXCHANGE_wire_cancel (exchange->wire_request);
exchange->wire_request = NULL;
}
if (NULL != exchange->wire_task)
{
GNUNET_SCHEDULER_cancel (exchange->wire_task);
exchange->wire_task = NULL;
}
if (NULL != exchange->conn)
{
TALER_EXCHANGE_disconnect (exchange->conn);
exchange->conn = NULL;
}
if (NULL != exchange->retry_task)
{
GNUNET_SCHEDULER_cancel (exchange->retry_task);
exchange->retry_task = NULL;
}
GNUNET_assert (NULL == exchange->fo_head);
GNUNET_assert (NULL == exchange->fo_tail);
GNUNET_free (exchange->url);
GNUNET_free (exchange);
}
/**
* We failed downloading /keys from @a exchange. Tell clients
* about our failure, abort pending operations and retry later.
*
* @param exchange exchange that failed
* @param hr details about the HTTP reply
*/
static void
fail_and_retry (struct TMH_Exchange *exchange,
const struct TALER_EXCHANGE_HttpResponse *hr)
{
struct TMH_EXCHANGES_FindOperation *fo;
exchange->have_keys = false;
if (NULL != exchange->wire_request)
{
TALER_EXCHANGE_wire_cancel (exchange->wire_request);
exchange->wire_request = NULL;
}
if (NULL != exchange->wire_task)
{
GNUNET_SCHEDULER_cancel (exchange->wire_task);
exchange->wire_task = NULL;
}
while (NULL != (fo = exchange->fo_head))
{
fo->fc (fo->fc_cls,
hr,
NULL,
false);
TMH_EXCHANGES_find_exchange_cancel (fo);
}
if ( (NULL == exchange->fo_head) &&
(TALER_EC_GENERIC_CONFIGURATION_INVALID == hr->ec) )
{
/* This can NEVER work, so don't retry */
free_exchange_entry (exchange);
return;
}
exchange->retry_delay = RETRY_BACKOFF (exchange->retry_delay);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Failed to fetch /keys from `%s': %d/%u, retrying in %s\n",
exchange->url,
(int) hr->ec,
hr->http_status,
GNUNET_STRINGS_relative_time_to_string (exchange->retry_delay,
true));
if (NULL != exchange->retry_task)
GNUNET_SCHEDULER_cancel (exchange->retry_task);
exchange->first_retry = GNUNET_TIME_relative_to_absolute (
exchange->retry_delay);
exchange->retry_task = GNUNET_SCHEDULER_add_delayed (exchange->retry_delay,
&retry_exchange,
exchange);
}
static void
keys_mgmt_cb (void *cls,
const struct TALER_EXCHANGE_KeysResponse *kr)
{
struct TMH_Exchange *exchange = cls;
struct GNUNET_TIME_Relative delay;
const struct TALER_EXCHANGE_Keys *keys;
if (MHD_HTTP_OK != kr->hr.http_status)
{
fail_and_retry (exchange,
&kr->hr);
return;
}
keys = kr->details.ok.keys;
if ( (exchange->trusted) &&
(0 != GNUNET_memcmp (&exchange->master_pub,
&keys->master_pub)) )
{
/* master pub differs => do not trust the exchange (without auditor) */
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Master public key of exchange `%s' differs from our configuration. Not trusting exchange.\n",
exchange->url);
exchange->trusted = false;
}
if (! exchange->trusted)
{
exchange->master_pub = keys->master_pub;
for (struct TMH_Exchange *e = exchange_head;
NULL != e;
e = e->next)
{
if (e == exchange)
continue;
if (! e->trusted)
continue;
if (0 ==
GNUNET_memcmp (&e->master_pub,
&exchange->master_pub))
exchange->trusted = true; /* same exchange, different URL => trust applies */
}
}
/* store exchange online signing keys in our DB */
for (unsigned int i = 0; i<keys->num_sign_keys; i++)
{
struct TALER_EXCHANGE_SigningPublicKey *sign_key = &keys->sign_keys[i];
enum GNUNET_DB_QueryStatus qs;
TMH_db->preflight (TMH_db->cls);
qs = TMH_db->insert_exchange_signkey (TMH_db->cls,
&keys->master_pub,
&sign_key->key,
sign_key->valid_from,
sign_key->valid_until,
sign_key->valid_legal,
&sign_key->master_sig);
/* 0 is OK, we may already have the key in the DB! */
if (0 > qs)
{
GNUNET_break (0);
fail_and_retry (exchange,
&kr->hr);
return;
}
}
exchange->first_retry
= GNUNET_TIME_relative_to_absolute (MIN_RELOAD_DELAY);
exchange->keys_expiration
= TALER_EXCHANGE_check_keys_current (exchange->conn,
TALER_EXCHANGE_CKF_NONE);
delay = GNUNET_TIME_absolute_get_remaining (
exchange->keys_expiration.abs_time);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"/keys response from expires at %s! Retrying at that time!\n",
GNUNET_TIME_absolute2s (
exchange->keys_expiration.abs_time));
delay = GNUNET_TIME_relative_max (delay,
MIN_RELOAD_DELAY);
exchange->retry_delay = GNUNET_TIME_UNIT_ZERO;
if (NULL != exchange->retry_task)
GNUNET_SCHEDULER_cancel (exchange->retry_task);
exchange->retry_task
= GNUNET_SCHEDULER_add_delayed (delay,
&retry_exchange,
exchange);
exchange->have_keys = true;
if ( (process_find_operations (exchange)) &&
(NULL == exchange->wire_request) &&
(NULL == exchange->wire_task) )
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Got key data, but also need wire data. Will request /wire now\n");
exchange->wire_request
= TALER_EXCHANGE_wire (exchange->conn,
&handle_wire_data,
exchange);
}
}
/**
* Task to return find operation result asynchronously to caller.
*
* @param cls a `struct TMH_EXCHANGES_FindOperation`
*/
static void
return_result (void *cls)
{
struct TMH_EXCHANGES_FindOperation *fo = cls;
struct TMH_Exchange *exchange = fo->my_exchange;
fo->at = NULL;
if ( (process_find_operations (exchange)) &&
(NULL == exchange->wire_request) &&
(exchange->have_keys) &&
(NULL != exchange->wire_task) )
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Do not have current wire data. Will re-request /wire in %s\n",
GNUNET_STRINGS_relative_time_to_string (
exchange->wire_retry_delay,
true));
exchange->wire_task
= GNUNET_SCHEDULER_add_delayed (exchange->wire_retry_delay,
&wire_task_cb,
exchange);
}
}
/**
* Lookup exchange by @a exchange_url.
*
* @param exchange_url base URL to match against
* @return NULL if exchange is not yet known
*/
static struct TMH_Exchange *
lookup_exchange (const char *exchange_url)
{
for (struct TMH_Exchange *exchange = exchange_head;
NULL != exchange;
exchange = exchange->next)
if (0 == strcmp (exchange->url,
exchange_url))
return exchange;
return NULL;
}
struct TMH_EXCHANGES_FindOperation *
TMH_EXCHANGES_find_exchange (const char *chosen_exchange,
bool force_reload,
TMH_EXCHANGES_FindContinuation fc,
void *fc_cls)
{
struct TMH_Exchange *exchange;
struct TMH_EXCHANGES_FindOperation *fo;
if (NULL == merchant_curl_ctx)
{
GNUNET_break (0);
return NULL;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Trying to find chosen exchange `%s'\n",
chosen_exchange);
/* Check if the exchange is known */
exchange = lookup_exchange (chosen_exchange);
if (NULL == exchange)
{
/* This is a new exchange */
exchange = GNUNET_new (struct TMH_Exchange);
exchange->url = GNUNET_strdup (chosen_exchange);
GNUNET_CONTAINER_DLL_insert (exchange_head,
exchange_tail,
exchange);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"The exchange `%s' is new\n",
chosen_exchange);
}
fo = GNUNET_new (struct TMH_EXCHANGES_FindOperation);
fo->fc = fc;
fo->fc_cls = fc_cls;
fo->my_exchange = exchange;
GNUNET_CONTAINER_DLL_insert (exchange->fo_head,
exchange->fo_tail,
fo);
if ( (GNUNET_TIME_absolute_is_past (exchange->first_retry)) &&
(force_reload ||
(GNUNET_TIME_absolute_is_past (
exchange->keys_expiration.abs_time))) )
{
/* increment exponential-backoff */
exchange->retry_delay = RETRY_BACKOFF (exchange->retry_delay);
/* do not allow forced check until both backoff and #FORCED_RELOAD_DELAY
are satisfied again */
exchange->first_retry
= GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_max (
exchange->retry_delay,
FORCED_RELOAD_DELAY));
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"/keys retry forced, waiting until %s\n",
GNUNET_TIME_absolute2s (exchange->first_retry));
if (NULL == exchange->conn)
{
/* Not connected at all yet */
exchange->retry_task
= GNUNET_SCHEDULER_add_now (&retry_exchange,
exchange);
}
else
{
/* Use existing connection, but update /keys */
exchange->keys_expiration
= TALER_EXCHANGE_check_keys_current (exchange->conn,
TALER_EXCHANGE_CKF_FORCE_DOWNLOAD);
}
return fo;
}
if ( (exchange->have_keys) &&
(exchange->have_wire) )
{
/* We are not currently waiting for a reply, immediately
return result */
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"The exchange `%s' is ready\n",
exchange->url);
GNUNET_assert (NULL == fo->at);
fo->at = GNUNET_SCHEDULER_add_now (&return_result,
fo);
return fo;
}
/* If new or resumed, (re)try fetching /keys */
if ( (NULL == exchange->conn) &&
(NULL == exchange->retry_task) &&
(! exchange->have_keys) )
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Do not have current /keys data for `%s'. Will request /keys now\n",
exchange->url);
exchange->retry_task = GNUNET_SCHEDULER_add_now (&retry_exchange,
exchange);
return fo;
}
if ( (exchange->have_keys) &&
(! exchange->have_wire) &&
(NULL == exchange->wire_task) &&
(NULL == exchange->wire_request) )
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Do not have required /wire data. Will re-request %s/wire now\n",
exchange->url);
exchange->wire_task = GNUNET_SCHEDULER_add_now (&wire_task_cb,
exchange);
return fo;
}
/* No activity to launch, we are already doing so */
return fo;
}
void
TMH_EXCHANGES_find_exchange_cancel (struct TMH_EXCHANGES_FindOperation *fo)
{
struct TMH_Exchange *exchange = fo->my_exchange;
if (NULL != fo->at)
{
GNUNET_SCHEDULER_cancel (fo->at);
fo->at = NULL;
}
GNUNET_CONTAINER_DLL_remove (exchange->fo_head,
exchange->fo_tail,
fo);
GNUNET_free (fo);
}
/**
* Function called on each configuration section. Finds sections
* about exchanges, parses the entries and tries to connect to
* it in order to fetch /keys.
*
* @param cls closure, with a `const struct GNUNET_CONFIGURATION_Handle *`
* @param section name of the section
*/
static void
accept_exchanges (void *cls,
const char *section)
{
const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
char *url;
char *mks;
struct TMH_Exchange *exchange;
char *currency;
if (0 != strncasecmp (section,
"merchant-exchange-",
strlen ("merchant-exchange-")))
return;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg,
section,
"CURRENCY",
¤cy))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
section,
"CURRENCY");
return;
}
if (0 != strcasecmp (currency,
TMH_currency))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Exchange given in section `%s' is for another currency. Skipping.\n",
section);
GNUNET_free (currency);
return;
}
GNUNET_free (currency);
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg,
section,
"EXCHANGE_BASE_URL",
&url))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
section,
"EXCHANGE_BASE_URL");
return;
}
exchange = GNUNET_new (struct TMH_Exchange);
exchange->url = url;
if (GNUNET_OK ==
GNUNET_CONFIGURATION_get_value_string (cfg,
section,
"MASTER_KEY",
&mks))
{
if (GNUNET_OK ==
GNUNET_CRYPTO_eddsa_public_key_from_string (mks,
strlen (mks),
&exchange->master_pub.
eddsa_pub))
{
exchange->trusted = true;
trusted_exchange_count++;
}
else
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"MASTER_KEY",
_ ("ill-formed EdDSA key"));
}
GNUNET_free (mks);
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"MASTER_KEY missing in section '%s', not trusting exchange\n",
section);
}
GNUNET_CONTAINER_DLL_insert (exchange_head,
exchange_tail,
exchange);
GNUNET_assert (NULL == exchange->retry_task);
exchange->retry_task = GNUNET_SCHEDULER_add_now (&retry_exchange,
exchange);
}
enum GNUNET_GenericReturnValue
TMH_EXCHANGES_lookup_wire_fee (const char *exchange_url,
const char *wire_method,
struct TALER_Amount *wire_fee)
{
struct TMH_Exchange *exchange;
const struct FeesByWireMethod *fbm;
const struct TALER_EXCHANGE_WireAggregateFees *af;
exchange = lookup_exchange (exchange_url);
if (NULL == exchange)
{
return GNUNET_SYSERR;
}
if (! exchange->have_wire)
{
return GNUNET_SYSERR;
}
fbm = get_wire_fees (exchange,
GNUNET_TIME_timestamp_get (),
wire_method);
if (NULL == fbm)
return GNUNET_NO;
af = fbm->af;
*wire_fee = af->fees.wire;
return GNUNET_OK;
}
enum GNUNET_GenericReturnValue
TMH_EXCHANGES_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
merchant_curl_ctx
= GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
&merchant_curl_rc);
if (NULL == merchant_curl_ctx)
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
merchant_curl_rc = GNUNET_CURL_gnunet_rc_create (merchant_curl_ctx);
GNUNET_CURL_enable_async_scope_header (merchant_curl_ctx,
"Taler-Correlation-Id");
/* get exchanges from the merchant configuration and try to connect to them */
GNUNET_CONFIGURATION_iterate_sections (cfg,
&accept_exchanges,
(void *) cfg);
/* build JSON with list of trusted exchanges (will be included in contracts) */
return trusted_exchange_count;
}
void
TMH_EXCHANGES_done ()
{
while (NULL != exchange_head)
free_exchange_entry (exchange_head);
if (NULL != merchant_curl_ctx)
{
GNUNET_CURL_fini (merchant_curl_ctx);
merchant_curl_ctx = NULL;
}
if (NULL != merchant_curl_rc)
{
GNUNET_CURL_gnunet_rc_destroy (merchant_curl_rc);
merchant_curl_rc = NULL;
}
}
enum GNUNET_GenericReturnValue
TMH_exchange_check_debit (struct TMH_Exchange *ex,
const struct TMH_WireMethod *wm)
{
for (struct ExchangeAccount *acc = ex->acc_head;
NULL != acc;
acc = acc->next)
{
bool ok = true;
if (0 != strcmp (acc->wire_method,
wm->wire_method))
continue;
if (NULL != acc->conversion_url)
continue; /* never use accounts with conversion */
for (struct Restriction *r = acc->d_head;
NULL != r;
r = r->next)
{
switch (r->type)
{
case TALER_EXCHANGE_AR_INVALID:
GNUNET_break (0);
ok = false;
break;
case TALER_EXCHANGE_AR_DENY:
ok = false;
break;
case TALER_EXCHANGE_AR_REGEX:
if (0 != regexec (&r->details.regex.ex,
wm->payto_uri,
0, NULL, 0))
ok = false;
break;
}
if (! ok)
break;
}
if (ok)
return GNUNET_OK;
}
return GNUNET_NO;
}
json_t *
TMH_exchange_get_acceptable (const struct TMH_WireMethod *wm)
{
json_t *te = json_array ();
GNUNET_assert (NULL != te);
for (struct TMH_Exchange *exchange = exchange_head;
NULL != exchange;
exchange = exchange->next)
{
json_t *j_exchange;
unsigned int priority;
if (! exchange->trusted)
continue;
priority = 512; /* medium */
if (exchange->have_wire)
{
if (GNUNET_OK ==
TMH_exchange_check_debit (exchange,
wm))
priority = 1024; /* high */
else
priority = 0; /* negative response */
}
j_exchange = GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("url",
exchange->url),
GNUNET_JSON_pack_uint64 ("priority",
priority),
GNUNET_JSON_pack_data_auto ("master_pub",
&exchange->master_pub));
GNUNET_assert (NULL != j_exchange);
GNUNET_assert (0 ==
json_array_append_new (te,
j_exchange));
}
return te;
}
/* end of taler-merchant-httpd_exchanges.c */
|