-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamknowsDTMAPI.c
More file actions
426 lines (327 loc) · 12.8 KB
/
samknowsDTMAPI.c
File metadata and controls
426 lines (327 loc) · 12.8 KB
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
/*
* Dated : 11/July/2018 , 16:00PM
* Author Amber Pachauri
* Purpose: This is C program module created with concept
* of operations to CURL library requests, to URL to compute
* diagnostics on Network activity - bandwith, lookup, connection etc.
*
*
* This file is library module, interface to user-level
* to sends' response to user program, with data and result from Network.
*
*/
#include <math.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "libINC/common.h"
#include "samknowsDTMAPI.h"
/*****************global decalrations & variables ******************************/
int initsmkwsDTMAPI =0;
int stopsmkwsDTMAPI =0;
static st_samknowsDTMAPI *psamknowsDTMAPI = NULL;
pthread_mutex_t msglock;
pthread_mutex_t lock;
void __samknowsDTMAPI_init(st_usermessage *usrmsg, st_samknowsDTMAPI *psamknowsDTMAPI);
void __samknowsDTMAPI_start(int cperform);
void __samknowsDTMAPI_setbuffer(void *contents, size_t size, size_t nmemb);
size_t __samknowsDTMAPI_callback(void *contents, size_t size, size_t nmemb, void *userp);
/**************** library export declarations ***********************************/
esamknwsDTMAPIStatus init_samknowsDTMAPI (st_usermessage *usermsg, char **header);
esamknwsDTMAPIStatus start_samknowsDTMAPI (int ccount);
/****************** library specific definitions******************************/
/*
* purpose : internal library call to return handle.
* param : void
* return : pointer to library instance.
*/
st_samknowsDTMAPI* __samknowsDTMAPI_getinstance ( void )
{
return psamknowsDTMAPI;
}
/*
* purpose : internal library call to initialize instance.
* param : message - pointer to user message structure.
* : psamknowsDTMAPI - pointer to library structure.
*
* return : void.
*/
void __samknowsDTMAPI_init(st_usermessage *message, st_samknowsDTMAPI *psamknowsDTMAPI)
{
int rescode = CURLE_OK;
if (psamknowsDTMAPI) {
fprintf(stderr,"debug_lib: library instance(%u).\n", psamknowsDTMAPI);
strncpy(psamknowsDTMAPI->url, message->url, (strlen(message->url) +1));
psamknowsDTMAPI->url[strlen(message->url) +1] = 0;
fprintf(stderr,"debug_lib: library url(%s) .\n", psamknowsDTMAPI->url);
//psamknowsDTMAPI->usermessage.esamknowsopt = usrmsg->esamknowsopt;
psamknowsDTMAPI->curlinfo.dspeed = message->dspeed;
psamknowsDTMAPI->curlinfo.dsize = message->dsize;
psamknowsDTMAPI->curlinfo.dtime = message->dtime;
curl_global_init(CURL_GLOBAL_ALL);
// init curl library - block level parameters
psamknowsDTMAPI->pcurl = curl_easy_init();
/*extract user,opt params*/
rescode = curl_easy_setopt(psamknowsDTMAPI->pcurl, CURLOPT_URL, psamknowsDTMAPI->url); //set url
rescode += curl_easy_setopt(psamknowsDTMAPI->pcurl, CURLOPT_WRITEFUNCTION, __samknowsDTMAPI_callback); //set callback
//curl_easy_setopt(psamknowsDTMAPI->curl_handle, CURLOPT_WRITEDATA, (void *)&cdatastor);
if ( psamknowsDTMAPI->headers ) {
/*append headers*/
int ilist=0;
psamknowsDTMAPI->curlheader = NULL;
while (psamknowsDTMAPI->headers[ilist]) {
psamknowsDTMAPI->curlheader = curl_slist_append (psamknowsDTMAPI->curlheader, psamknowsDTMAPI->headers[ilist]);
if (psamknowsDTMAPI->curlheader) {
rescode += curl_easy_setopt(psamknowsDTMAPI->pcurl, CURLOPT_HTTPHEADER, psamknowsDTMAPI->curlheader);
fprintf(stderr, "debug_lib: set curl-header (%s) \n", psamknowsDTMAPI->headers[ilist]);
}
ilist++;
}
}
if ( CURLE_OK == rescode )
fprintf(stderr, "debug_lib: library initialised, status (%d)\n", rescode);
}
return;
}
/*
* purpose : Internal library callback buffer write. Informs user with
* message contents, length and type.
* param : contents - message to request, write from remote.
* : size - length of data received.
* : nmemb - number of elements of data received.
*
* return : void. Informs user with message contents, length and type.
*/
void __samknowsDTMAPI_setbuffer(void *contents, size_t size, size_t nmemb)
{
char *buffer = (char*) malloc(size+1 * nmemb);
memset( buffer, 0, (size*nmemb));
memcpy( buffer, contents, nmemb);
buffer[nmemb] = 0;
int len = strlen(buffer)+1;
//fprintf(stderr,"debug_lib: Rx'd size(%d), nelements(%d) \n", size, nmemb);
st_usermessage user = {0};
if (psamknowsDTMAPI) {
psamknowsDTMAPI->message = (void*) buffer; // malloc'd ??
psamknowsDTMAPI->msglen = len;
user.message =(char*) buffer;
user.msglen = len;
//fprintf(stderr,"debug_lib: rx'd length: (%d) data (%s)\n", len, (char*)psamknowsDTMAPI->message);
userdata_samknowsDTMAPI (eSAMKNWSSTATUS_MSG, (void*) buffer, (void*) &user, len);
/*send user message*/
}
free(buffer);
return;
}
/*
* purpose : internal library callback .
* param : contents - message to request, write from remote.
* : size - length of data received.
* : nmemb - number of elements of data received.
*
* return : length of data received.
*/
size_t __samknowsDTMAPI_callback(void *contents, size_t size, size_t nmemb, void *userp)
{
pthread_mutex_lock(&msglock);
/* set lock to serialize buffer write*/
__samknowsDTMAPI_setbuffer(contents, size, nmemb);
pthread_mutex_unlock(&msglock);
/* set unlock */
return (size *nmemb);
}
/*
* purpose : Internal library callback buffer write. Informs user with
* message contents, length and type.
* param : contents - message to request, write from remote.
* : size - length of data received.
* : nmemb - number of elements of data received.
*
* return : void. Informs user with message contents, length and type.
*/
void __samknowsDTMAPI_start(int cperform)
{
if ( (initsmkwsDTMAPI) /* set initflag */
&& (psamknowsDTMAPI) /* assert pointer?*/ ) {
/* continnue , extract user,opt params ... */
fprintf(stderr,"debug_lib: using instance(%u), url (%s).\n",psamknowsDTMAPI, psamknowsDTMAPI->url);
curl_easy_setopt(psamknowsDTMAPI->pcurl, CURLOPT_WRITEFUNCTION, __samknowsDTMAPI_callback); //set url
/*set error-buffer*/
//memset( (void*) psamknowsDTMAPI->curlbufferr, 0 , CURLOPT_ERRORBUFFER);
//curl_easy_setopt(psamknowsDTMAPI->pcurl, CURLOPT_ERRORBUFFER, psamknowsDTMAPI->curlbufferr); //set error buffer
long response;
int iloop = cperform+1 ;
while( --iloop) { // number of iterations
pthread_mutex_lock(&lock);
/*set lock for curl request*/
psamknowsDTMAPI->curlcode = curl_easy_perform(psamknowsDTMAPI->pcurl);
if ( CURLE_OK == psamknowsDTMAPI->curlcode) { /* url was success! */
while( psamknowsDTMAPI->curlcode != CURLM_OK)
psamknowsDTMAPI->curlcode = curl_easy_getinfo( psamknowsDTMAPI->pcurl, CURLINFO_RESPONSE_CODE, &response);
psamknowsDTMAPI->curlcode = curl_easy_getinfo( psamknowsDTMAPI->pcurl, CURLINFO_RESPONSE_CODE, &response);
/* collect statistics*/
int infocode = CURLM_OK;
CURLInfo cinfo = psamknowsDTMAPI->curlinfo;
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_CONNECT_TIME, &cinfo.dconnect);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_APPCONNECT_TIME, &cinfo.dappconnect);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_NAMELOOKUP_TIME, &cinfo.dnmlookup);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_PRETRANSFER_TIME, &cinfo.dpretransfer);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_REDIRECT_TIME, &cinfo.dredirect);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_STARTTRANSFER_TIME, &cinfo.dtransfer);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_SPEED_DOWNLOAD, &cinfo.dspeed);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_SIZE_DOWNLOAD, &cinfo.dsize);
infocode += curl_easy_getinfo(psamknowsDTMAPI->pcurl, CURLINFO_TOTAL_TIME, &cinfo.dtime);
if ( CURLM_OK == infocode ) { //safe to read all params
psamknowsDTMAPI->curlinfo.dconnect = cinfo.dconnect;
psamknowsDTMAPI->curlinfo.dappconnect = cinfo.dappconnect;
psamknowsDTMAPI->curlinfo.dnmlookup = cinfo.dnmlookup;
psamknowsDTMAPI->curlinfo.dpretransfer = cinfo.dpretransfer;
psamknowsDTMAPI->curlinfo.dredirect = cinfo.dredirect;
psamknowsDTMAPI->curlinfo.dtransfer = cinfo.dtransfer;
psamknowsDTMAPI->curlinfo.dspeed = cinfo.dspeed;
psamknowsDTMAPI->curlinfo.dtime = cinfo.dtime;
psamknowDTMAPI->status = response;
st_usermessage user = {0};
/* curl message done*/
fprintf(stderr,"debug_lib: iterate(%d) infocode(%d) curlcode-%d \n",
iloop, infocode,psamknowsDTMAPI->curlcode);
user.dconnect = cinfo.dconnect;
user.dappconnect = cinfo.dappconnect;
user.dnmlookup = cinfo.dnmlookup;
user.dpretransfer = cinfo.dpretransfer;
user.dtransfer = cinfo.dtransfer;
user.dredirect = cinfo.dredirect;
user.dspeed = cinfo.dspeed;
user.dsize = cinfo.dsize;
user.dtime = cinfo.dtime;
user.status = response;
userdata_samknowsDTMAPI (eSAMKNWSSTATUS_DONE, (void*) &user.message, (void*) &user, 0);
/*send user message*/
}
} else
fprintf(stderr,"debug_lib: url look-up.?\n");
pthread_mutex_unlock(&lock);
/*set lock for curl request*/
}
}
return;
}
/*
* purpose : Internal library routine for allocations.
* params : void.
*
* return : status on library instance. success ( OK)
*/
esamknwsDTMAPIStatus __samknowsDTMAPI_setinstance ( void )
{
int status = eSAMKNWSSTATUS_OK;
psamknowsDTMAPI = (st_samknowsDTMAPI *) malloc (sizeof(st_samknowsDTMAPI) * (sizeof(st_samknowsDTMAPI*)));
if ( NULL == psamknowsDTMAPI ) {
fprintf(stderr,"debug_lib: resource allocation problem\n");
status = eSAMKNWSSTATUS_ALLOC;
}
return status;
}
.
/*
* purpose : Internal library routine for release allocations.
* for structure, headers (if any) , global block level (curl)
* params : void.
*
* return : void.
*/
void __samknowsDTMAPI_release ( void )
{
st_samknowsDTMAPI *psmknwsDTMAPI = NULL;
psmknwsDTMAPI = __samknowsDTMAPI_getinstance();
fprintf(stderr, "debug_lib: resource release ... (%u)\n", psmknwsDTMAPI);
if ( psmknwsDTMAPI ) { /*sanity check for instance*/
curl_easy_cleanup(psmknwsDTMAPI->pcurl);
if (psamknowsDTMAPI->headers)
free (psamknowsDTMAPI->headers);
if (psmknwsDTMAPI->curlheader)
curl_slist_free_all(psmknwsDTMAPI->curlheader);
curl_global_cleanup();
/*release curl block*/
free( psmknwsDTMAPI );
/*release library instance*/
}
}
/*
* purpose : Internal library routine for release allocations.
* for structure, headers (if any) , global block level (curl)
* params : void.
*
* return : void.
*/
esamknwsDTMAPIStatus __samknowsDTMAPI_alloc ( char **headers )
{
int status = eSAMKNWSSTATUS_OK;
if ( (psamknowsDTMAPI) ) {
if ( headers ) {
psamknowsDTMAPI->headers = (char**) malloc (sizeof(char*) * MAX_HEADERS);
int i=0;
while (headers[i] != 0) {
psamknowsDTMAPI->headers[i] = strdup(headers[i]);
fprintf(stderr,"debug_lib: Rx'd header (%s)\n", psamknowsDTMAPI->headers[i]);
i++;
}
}
/* allow options ... */
}
return status;
}
/*
* purpose : exported library routine to user to initialize.
* params : usermsg - pointer to user structure.
* headers - pointer to headers in argv format.
*
* return : status of library , esamknwsDTMAPIStatus
*/
esamknwsDTMAPIStatus init_samknowsDTMAPI (st_usermessage *usermsg, char **headers)
{
int status = eSAMKNWSSTATUS_OK;
if ( !psamknowsDTMAPI ) {
status = __samknowsDTMAPI_setinstance();
if ( ( eSAMKNWSSTATUS_OK == status) &&
( headers ) ) {
status = __samknowsDTMAPI_alloc(headers);
if (eSAMKNWSSTATUS_OK == status)
__samknowsDTMAPI_init(usermsg, psamknowsDTMAPI);
}
} else
status = eSAMKNWSSTATUS_ERR;
return status;
}
/*
* purpose : exported routine to user to start library.
* sets flag to control library feature.
* params : count - iterations to curl requests.
*
* return : status of library , esamknwsDTMAPIStatus
*/
esamknwsDTMAPIStatus start_samknowsDTMAPI (int count)
{
int status = eSAMKNWSSTATUS_OK;
if ( (count > 0) && (psamknowsDTMAPI) ) {
fprintf(stderr,"debug_lib: start test-metrics(%u) ...\n", psamknowsDTMAPI);
initsmkwsDTMAPI = 1; // allow metrics, diagnostics ?
__samknowsDTMAPI_start(count);
} else
status = eSAMKNWSSTATUS_ERR;
return status;
}
/*
* purpose : exported routine to user to start library.
* sets flag to control library feature.
* params : count - iterations to curl requests.
*
* return : status of library , esamknwsDTMAPIStatus
*/
void stop_samknowsDTMAPI (void)
{
fprintf(stderr, "debug_lib: stop test-metrics(%u) ...\n", psamknowsDTMAPI );
__samknowsDTMAPI_release ();
return;
}