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
|
/*
This file is part of TALER
Copyright (C) 2019 GNUnet e.V.
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU 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 communication.c
* @brief functions to communicate with taler backend
* @author BOSS Marco
*/
#include <string.h>
#include <stdbool.h>
#include "communication.h"
#include "nfc-wallet/wallet.h"
#include "configuration.h"
int taler_init(CURL **curl)
{
if( *curl != NULL ){
printf( "taler_init: curl handle already initialized\n" );
return EXIT_FAILURE;
}
// initialize curl
CURLcode result = curl_global_init( CURL_GLOBAL_ALL );
*curl = curl_easy_init();
if( !(*curl) || result != CURLE_OK ){
printf( "taler_init: could not inizialize curl handle\n" );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void taler_exit( CURL **curl )
{
curl_easy_cleanup(*curl);
curl_global_cleanup();
}
int taler_alloc_string( char **string, size_t size )
{
*string = malloc( size );
if( !(*string) ){
printf( "taler_alloc: unable to allocate string" );
exit( EXIT_FAILURE );
}
return EXIT_SUCCESS;
}
static size_t _taler_write_response( void *contents, size_t size, size_t nmemb, void *userp )
{
size_t realSize = size * nmemb;
struct TalerResponse *response = (struct TalerResponse *)userp;
char *tempString = realloc( response->string, response->size + realSize + 1 );
if( !tempString ) {
printf( "Allocation failure" );
return EXIT_FAILURE;
}
response->string = tempString;
memcpy(&( response->string[response->size] ), contents, realSize );
response->size += realSize;
response->string[response->size] = 0;
return realSize;
}
int taler_create_order_req( ProductOrder *product )
{
char buffer[256];
sprintf( buffer, JSON_ORDER, product->product, product->currency, product->amount, product->product );
char* temp = realloc( product->orderRequest, strlen(buffer) + 1 );
if( !temp ){
printf( "could not allocate order\n" );
return EXIT_FAILURE;
}
product->orderRequest = temp;
strcpy( product->orderRequest, buffer );
printf( "Created order Request: \n%s\n\n", product->orderRequest );
return EXIT_SUCCESS;
}
int taler_create_order( CURL *curl, ProductOrder *product )
{
// reset the response size
product->response->size = 0;
// set the url
char url[ strlen(BACKEND_BASE_URL) + strlen( ORDER ) + 1 ];
sprintf( url, "%s%s", BACKEND_BASE_URL, ORDER );
curl_easy_setopt( curl, CURLOPT_URL, url );
// set the authentication headers
struct curl_slist *list = NULL;
list = curl_slist_append( list, AUTH_HEADER );
curl_easy_setopt( curl, CURLOPT_HTTPHEADER, list );
// curl option "post"
curl_easy_setopt( curl, CURLOPT_HTTPPOST, true );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, product->orderRequest );
// pass the write function and the struct to write to
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, _taler_write_response );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)product->response );
// perform the call
CURLcode result = curl_easy_perform( curl );
if( result != CURLE_OK ){
printf( "could not communicate with \"%s\"\n", url );
return EXIT_FAILURE;
}
// reset the curl options
curl_easy_reset( curl );
// clean up
curl_slist_free_all( list );
return EXIT_SUCCESS;
}
int taler_check_payment_status( CURL *curl, ProductOrder *product )
{
// reset the response size
product->response->size = 0;
// set the url
curl_easy_setopt( curl, CURLOPT_URL, product->checkUrl );
// set the authentication headers
struct curl_slist *list = NULL;
list = curl_slist_append( list, AUTH_HEADER );
curl_easy_setopt( curl, CURLOPT_HTTPHEADER, list );
// curl option "get"
curl_easy_setopt( curl, CURLOPT_HTTPGET, true );
// pass the write function and the struct to write to
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, _taler_write_response );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)product->response );
// perform the call
CURLcode result = curl_easy_perform(curl);
if( result != CURLE_OK ){
printf( "could not communicate with \"%s\"\n", product->checkUrl );
return EXIT_FAILURE;
}
// reset the curl options
curl_easy_reset( curl );
// clean up
curl_slist_free_all( list );
return EXIT_SUCCESS;
}
int taler_parse_json( const TalerResponse *response, const char *memberName, char **returnStr, bool isBoolean )
{
if( !(*returnStr) )
taler_alloc_string( returnStr, 1 );
char *result = NULL;
char *temp = NULL;
char mbr[64];
if( isBoolean ){
// If the wanted member is of type boolean
sprintf( mbr, "\"%s\": true", memberName );
if( (temp = strstr( response->string, mbr)) != NULL )
result = "true";
else
result = "false";
}else{
// String type members
sprintf( mbr, "\"%s\":", memberName );
if( (temp = strstr( response->string, mbr )) != NULL ){
if( (temp = strstr( response->string, ": ")) != NULL ){
result = strtok( temp, "\"");
result = strtok( NULL, "\"");
}
}
}
if( !result ){
printf( "taler_parse_json: no member named \"%s\" found!\n\n", memberName );
return EXIT_FAILURE;
}
temp = realloc( *returnStr, strlen(result) + 1 );
if( !temp ){
printf( "taler_parse_json: could not allocate memory for member\n" );
return EXIT_FAILURE;
}
*returnStr = temp;
strcpy( *returnStr, result );
return EXIT_SUCCESS;
}
|