[go: up one dir, main page]

Menu

[r368]: / main / src / ospxml.c  Maximize  Restore  History

Download this file

485 lines (403 with data), 14.4 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
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
/**************************************************************************
*** COPYRIGHT (c) 2002 by TransNexus, Inc. ***
*** ***
*** This software is property of TransNexus, Inc. ***
*** This software is freely available under license from TransNexus. ***
*** The license terms and conditions for free use of this software by ***
*** third parties are defined in the OSP Toolkit Software License ***
*** Agreement (LICENSE.txt). Any use of this software by third ***
*** parties, which does not comply with the terms and conditions of the ***
*** OSP Toolkit Software License Agreement is prohibited without ***
*** the prior, express, written consent of TransNexus, Inc. ***
*** ***
*** Thank you for using the OSP ToolKit(TM). Please report any bugs, ***
*** suggestions or feedback to support@transnexus.com ***
*** ***
**************************************************************************/
/*
* ospxml.cpp - XML functions
*/
#include "osp/osp.h"
#include "osp/osperrno.h"
#include "osp/ospxml.h"
#include "osp/ospmsg.h"
#include "osp/ospbfr.h"
#include "osp/ospxmldoc.h"
#include "osp/osptokeninfo.h"
#include "osp/ospauthreq.h"
#include "osp/ospauthrsp.h"
#include "osp/ospusagecnf.h"
#include "osp/ospusageind.h"
#include "osp/ospreauthreq.h"
#include "osp/ospreauthrsp.h"
#include "osp/ospcapind.h"
int
OSPPXMLElementProcess(
OSPTXMLELEM *ospvElem, /* In - xmlelement to be processed */
unsigned char **ospvMessage, /* Out - xml doc as unsigned char */
unsigned *ospvSizeOfMessage) /* Out - size of xml doc */
{
OSPTBFR *outbuffer = NULL;
unsigned transferlen = 0;
int errorcode = OSPC_ERR_NO_ERROR;
if(ospvElem == (OSPTXMLELEM *)NULL)
{
errorcode = OSPC_ERR_XML_INVALID_ARGS;
OSPM_DBGERRORLOG(errorcode, "ospvElem == NULL");
}
/* now that we have everything stored into the message element,
* we can
* convert it into an XML.
*/
if(errorcode == OSPC_ERR_NO_ERROR)
{
/* Possible Optimization -
* Instead of allocating buffer for the below mentioned size,
* allocate for OSPC_XMLDOC_DECLLEN byte's.
* This will cut down on some subsequent malloc calls.
*/
if((outbuffer = OSPPBfrNew(0))!=NULL)
{
errorcode = OSPPXMLDocCreate(ospvElem, &outbuffer);
}
else
{
errorcode = OSPC_ERR_XML_BUFR_NEW_FAILED;
OSPM_DBGERRORLOG(errorcode, "bufrnew failed");
}
}
if(errorcode == OSPC_ERR_NO_ERROR)
{
if((transferlen=OSPPBfrSize(outbuffer))>0)
{
OSPM_MALLOC(*ospvMessage, unsigned char, transferlen+1);
}
else
{
errorcode = OSPC_ERR_XML_BFR_SZ_FAIL;
OSPM_DBGERRORLOG(errorcode, "bufrsize failed");
}
}
if(errorcode == OSPC_ERR_NO_ERROR)
{
if(*ospvMessage != (unsigned char *)NULL)
{
OSPM_MEMSET(*ospvMessage, 0, transferlen+1);
/* Read data from OSPTBFR to output message */
if((*ospvSizeOfMessage = OSPPBfrReadBlock(
&outbuffer,
*ospvMessage,
transferlen))!=transferlen)
{
errorcode = OSPC_ERR_XML_BFR_READ_BLOCK_FAIL;
OSPM_DBGERRORLOG(errorcode, "bufrsize failed");
}
}
else
{
errorcode = OSPC_ERR_XML_MALLOC_FAILED;
OSPM_DBGERRORLOG(errorcode, "malloc for ospvMessage failed");
}
}
OSPPBfrDelete(&outbuffer);
if(errorcode != OSPC_ERR_NO_ERROR)
{
/* get rid of any malloced space */
if(*ospvMessage != (unsigned char *)NULL)
{
OSPM_FREE(*ospvMessage);
*ospvMessage = NULL;
}
}
return errorcode;
}
int
OSPPXMLMessageCreate(
OSPE_MSG_DATATYPES ospvDataType, /* In - what data type (AREQ, ARESP...) */
unsigned char **ospvMessage, /* Out - actual xml message */
unsigned *ospvSizeOfMessage, /* Out - size of xml message */
void *ospvInfo, /* In - structure holding data */
OSPTTRANS *trans) /* In - transaction handle */
{
int errorcode = OSPC_ERR_NO_ERROR;
OSPTXMLELEM *xmlelem = NULL;
/* verify input */
if((ospvDataType > OSPC_MSG_UPPER_BOUND) ||
(ospvDataType < OSPC_MSG_LOWER_BOUND) ||
(ospvInfo == NULL))
{
errorcode = OSPC_ERR_XML_INVALID_ARGS;
OSPM_DBGERRORLOG(errorcode, "invalid arg in osppxmlmessagecreate");
}
if(errorcode == OSPC_ERR_NO_ERROR)
{
switch(ospvDataType)
{
case OSPC_MSG_AREQ:
errorcode = OSPPAuthReqToElement((OSPTAUTHREQ *)ospvInfo, &xmlelem, trans);
break;
case OSPC_MSG_UIND:
errorcode = OSPPUsageIndToElement((OSPTLIST *)ospvInfo, &xmlelem, trans);
break;
case OSPC_MSG_REAREQ:
errorcode = OSPPReauthReqToElement((OSPTREAUTHREQ *)ospvInfo, &xmlelem,trans);
break;
case OSPC_MSG_CAPIND:
errorcode = OSPPCapIndToElement((OSPTCAPIND *)ospvInfo, &xmlelem);
break;
#ifndef OSP_SDK
case OSPC_MSG_REARESP:
errorcode = OSPPReauthRspToElement((OSPTREAUTHRSP *)ospvInfo, &xmlelem);
break;
case OSPC_MSG_ARESP:
case OSPC_MSG_AREZP:
errorcode = OSPPAuthRspToElement((OSPTAUTHRSP *)ospvInfo, &xmlelem,ospvDataType);
break;
case OSPC_MSG_UCNF:
errorcode = OSPPUsageCnfToElement((OSPTLIST *)ospvInfo, &xmlelem);
break;
#endif
default:
errorcode = OSPC_ERR_XML_INVALID_TYPE;
}
}
if(errorcode == OSPC_ERR_NO_ERROR)
{
errorcode = OSPPXMLElementProcess(xmlelem, ospvMessage, ospvSizeOfMessage);
}
/* Delete Element */
OSPPXMLElemDelete(&xmlelem);
return errorcode;
}
int
OSPPXMLGetDataType(
OSPTXMLELEM *ospvXMLElem, /* In - xml element */
OSPE_MSG_DATATYPES *ospvDataType) /* Out - datatype for this element */
{
OSPTXMLELEM *parent = OSPC_OSNULL;
char *name = OSPC_OSNULL;
int errorcode = OSPC_ERR_NO_ERROR;
if(ospvXMLElem == OSPC_OSNULL)
{
errorcode = OSPC_ERR_XML_INVALID_ARGS;
OSPM_DBGERRORLOG(errorcode, "ospvXMLElem is NULL");
}
else
{
if(OSPPMsgGetElemPart(OSPPXMLElemGetName(ospvXMLElem)) == ospeElemMessage)
{
parent = (OSPTXMLELEM *)OSPPXMLElemFirstChild(ospvXMLElem);
}
else
{
parent = ospvXMLElem;
}
}
/* make sure we have a parent */
if(errorcode == OSPC_ERR_NO_ERROR)
{
if(parent != OSPC_OSNULL)
{
/* get first element which should hold component name */
name = (char *) OSPPXMLElemGetName(parent);
}
else
{
errorcode = OSPC_ERR_XML_PARENT_NOT_FOUND;
OSPM_DBGERRORLOG(errorcode, "parent OSPTXMLELEM is NULL");
}
}
/* now figure out which one we have */
if((errorcode == OSPC_ERR_NO_ERROR) &&
(name != NULL))
{
/*
** OSP Stds document spelling Authori(z|s)ation
** C. Bokath
*/
if(OSPM_STRSTR(name, "AuthorisationRequest") != NULL)
{
*ospvDataType = OSPC_MSG_AREQ;
}
else if(OSPM_STRSTR(name, "AuthorizationRequest") != NULL)
{
*ospvDataType = OSPC_MSG_AREQ;
}
else if(OSPM_STRSTR(name, "AuthorisationResponse") != NULL)
{
*ospvDataType = OSPC_MSG_ARESP;
}
else if(OSPM_STRSTR(name, "AuthorizationResponse") != NULL)
{
*ospvDataType = OSPC_MSG_ARESP;
}
else if(OSPM_STRSTR(name, "AuthorizationIndication") != NULL)
{
*ospvDataType = OSPC_MSG_AIND;
}
else if(OSPM_STRSTR(name, "AuthorizationConfirmation") != NULL)
{
*ospvDataType = OSPC_MSG_ACNF;
}
else if(OSPM_STRSTR(name, "UsageIndication") != NULL)
{
*ospvDataType = OSPC_MSG_UIND;
}
else if(OSPM_STRSTR(name, "UsageConfirmation") != NULL)
{
*ospvDataType = OSPC_MSG_UCNF;
}
else if(OSPM_STRSTR(name, "TokenInfo") != NULL)
{
*ospvDataType = OSPC_MSG_TOKINFO;
}
else if(OSPM_STRSTR(name, "ReauthorizationRequest") != NULL)
{
*ospvDataType = OSPC_MSG_REAREQ;
}
else if(OSPM_STRSTR(name, "ReauthorizationResponse") != NULL)
{
*ospvDataType = OSPC_MSG_REARESP;
}
else if(OSPM_STRSTR(name, "CapabilitiesConfirmation") != NULL)
{
*ospvDataType = OSPC_MSG_CAPCNF;
}
else
{
errorcode = OSPC_ERR_XML_DATA_TYPE_NOT_FOUND;
OSPM_DBGERRORLOG(errorcode, name);
}
}
return errorcode;
}
int
OSPPXMLMessageParse(
unsigned char *ospvXMLMessage, /* In - xml message */
unsigned ospvSizeOfMessage, /* In - size of message */
void **ospvData, /* Out - pointer to struct
* w/data from message */
OSPE_MSG_DATATYPES *ospvDataType) /* Out - what type struct void pointer is
* pointing to */
{
int errorcode = OSPC_ERR_NO_ERROR;
OSPTXMLELEM *xmlelem = OSPC_OSNULL;
OSPTBFR *xmlbufr = OSPC_OSNULL;
unsigned numbyteswritten = 0;
OSPTXMLELEM *elem1 = OSPC_OSNULL;
OSPTXMLELEM *tempxmlelem = OSPC_OSNULL;
/* check input */
if((ospvXMLMessage == (unsigned char *)NULL) ||
(ospvSizeOfMessage == 0))
{
errorcode = OSPC_ERR_XML_INVALID_ARGS;
OSPM_DBGERRORLOG(errorcode, "invalid arg in osppxmlmessageparse");
}
/* get a new xml buffer */
if(errorcode == OSPC_ERR_NO_ERROR)
{
xmlbufr = OSPPBfrNew(ospvSizeOfMessage);
/* add the message to it */
if(xmlbufr != NULL)
{
numbyteswritten = OSPPBfrWriteBlock(&xmlbufr, ospvXMLMessage, ospvSizeOfMessage);
if(numbyteswritten != ospvSizeOfMessage)
{
errorcode = OSPC_ERR_XML_BFR_WRITE_BLOCK_FAILED;
OSPM_DBGERRORLOG(errorcode, "bufrWriteblock failed for xmlmessageparse");
}
}
else
{
errorcode = OSPC_ERR_XML_BUFR_NEW_FAILED;
OSPM_DBGERRORLOG(errorcode, "bufrNew failed for xmlmessageparse");
}
/* send it to the parser */
if(errorcode == OSPC_ERR_NO_ERROR)
{
errorcode = OSPPXMLDocParse(&xmlbufr, &xmlelem);
}
/* Get data type */
if(errorcode == OSPC_ERR_NO_ERROR)
{
errorcode = OSPPXMLGetDataType(xmlelem, ospvDataType);
}
/* Process the message depending on the data type */
if(errorcode == OSPC_ERR_NO_ERROR)
{
errorcode = OSPPXMLMessageProcess(xmlelem, ospvData, *ospvDataType);
}
/* delete buffer */
if(xmlbufr != OSPC_OSNULL)
{
OSPPBfrDelete(&xmlbufr);
xmlbufr = OSPC_OSNULL;
}
if(xmlelem != OSPC_OSNULL)
{
if((*ospvDataType == OSPC_MSG_UIND) || (*ospvDataType == OSPC_MSG_UCNF))
{
if(OSPPMsgGetElemPart(OSPPXMLElemGetName(xmlelem))==ospeElemMessage)
{
tempxmlelem = (OSPTXMLELEM *)OSPPXMLElemFirstChild(xmlelem);
xmlelem->ospmXMLElemChild = OSPC_OSNULL;
OSPPXMLElemDelete(&xmlelem);
xmlelem = tempxmlelem;
}
while ((elem1 = (OSPTXMLELEM *)OSPPListRemove((OSPTLIST *)&xmlelem)) != OSPC_OSNULL)
{
OSPPXMLElemDelete(&elem1);
elem1 = OSPC_OSNULL;
}
OSPPXMLElemDelete(&xmlelem);
xmlelem = OSPC_OSNULL;
}
else
{
OSPPXMLElemDelete(&xmlelem);
xmlelem = OSPC_OSNULL;
}
}
}
return errorcode;
}
int
OSPPXMLMessageProcess(
OSPTXMLELEM *ospvElem, /* In - xml element for this datatype */
void **ospvStruct, /* Out- pointer to struct to be filled in */
OSPE_MSG_DATATYPES ospvDataType) /* In - datatype for this struct */
{
int errorcode = OSPC_ERR_NO_ERROR;
switch(ospvDataType)
{
case OSPC_MSG_ARESP:
errorcode = OSPPAuthRspFromElement(ospvElem, (OSPTAUTHRSP **)ospvStruct);
break;
case OSPC_MSG_UCNF:
errorcode = OSPPUsageCnfFromElement(ospvElem, (OSPTLIST *)ospvStruct);
break;
case OSPC_MSG_TOKINFO:
errorcode = OSPPTokenInfoFromElement(ospvElem, (OSPTTOKENINFO **)ospvStruct);
break;
case OSPC_MSG_REARESP:
errorcode = OSPPReauthRspFromElement(ospvElem, (OSPTREAUTHRSP **)ospvStruct);
break;
case OSPC_MSG_CAPCNF:
errorcode = OSPPCapCnfFromElement(ospvElem, (OSPTCAPCNF **)ospvStruct);
break;
#ifndef OSP_SDK
case OSPC_MSG_AREQ:
errorcode = OSPPAuthReqFromElement(ospvElem,(OSPTAUTHREQ **)ospvStruct);
break;
case OSPC_MSG_UIND:
errorcode = OSPPUsageIndFromElement(ospvElem, (OSPTLIST *)ospvStruct);
break;
case OSPC_MSG_REAREQ:
errorcode = OSPPReauthReqFromElement(ospvElem, (OSPTREAUTHREQ **)ospvStruct);
break;
#endif
default:
errorcode = OSPC_ERR_DATA_INVALID_TYPE;
}
return errorcode;
}