📄 ospaudit.c
字号:
unsigned OSPPAuditGetMaxTime( OSPTAUDIT *ospvAudit){ if(ospvAudit != OSPC_OSNULL) { return ospvAudit->ospmAuditMaxTime; } else { return 0; }}/* Get start time for auditing */unsigned long OSPPAuditGetStartTime( OSPTAUDIT *ospvAudit){ if(ospvAudit != OSPC_OSNULL) { return ospvAudit->ospmAuditStartTime; } else { return 0; }}/* Get the URL for auditing */const char *OSPPAuditGetURL( OSPTAUDIT *ospvAudit){ if(ospvAudit != OSPC_OSNULL) { return ospvAudit->ospmAuditURL; } else { return OSPC_OSNULL; }}/* Get worker conditional variable */OSPTCONDVAR OSPPAuditGetWorkerCondVar( OSPTAUDIT *ospvAudit){ OSPTCONDVAR condvar; OSPM_MEMSET(&condvar, 0, sizeof(OSPTCONDVAR)); if(ospvAudit != OSPC_OSNULL) { return ospvAudit->ospmAuditWorkerCondVar; } else { return condvar; }}/* Increment used space counter */void OSPPAuditIncrementUsedSpace( OSPTAUDIT *ospvAudit, unsigned ospvIncrement){ if(ospvAudit != OSPC_OSNULL) { ospvAudit->ospmAuditUsedSpace += ospvIncrement; } return;}/* Begin collecting audit data */intOSPPAuditInit( OSPTAUDIT *ospvAudit){ int errorcode = OSPC_ERR_NO_ERROR; if(ospvAudit != OSPC_OSNULL) { /* Set start time */ OSPPAuditSetStartTime(ospvAudit, time(NULL)); OSPPListNew(&(ospvAudit->ospmAuditComponentIdList)); /* Start worker thread */ errorcode = OSPPAuditStartWorker(ospvAudit); if(errorcode != OSPC_ERR_NO_ERROR) { OSPM_DBGERRORLOG(errorcode, "error from OSPPAuditStartWorker."); } } else { errorcode = OSPC_ERR_AUDIT_NOT_FOUND; OSPM_DBGERRORLOG(errorcode, "Audit not found."); } return errorcode;}voidOSPPAuditInitializeBuffer( OSPTAUDIT *ospvAudit){ int numbyteswritten = 0; char random[OSPC_MAX_RANDOM]; OSPM_MEMSET(random, 0, OSPC_MAX_RANDOM); /* add xml header to buffer */ numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), OSPC_AUDIT_XML_HEADER, OSPC_AUDIT_XML_HDR_LEN); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; /* add "Message" header to buffer */ numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), OSPC_AUDIT_MSG_HEADER, OSPC_AUDIT_MSG_HDR_LEN); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; /* add message id string to buffer. use the random generator since we have no TransactionID */ numbyteswritten = OSPPUtilGetRandom(random, 0); if(numbyteswritten > 0) { numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), random, numbyteswritten); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; OSPM_MEMSET(random, 0, OSPC_MAX_RANDOM); } /* add "random" string to buffer */ numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), OSPC_AUDIT_MSG_RANDOM, OSPC_AUDIT_MSG_RANDOM_LEN); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; /* add random attr string to buffer */ numbyteswritten = OSPPUtilGetRandom(random, 0); if(numbyteswritten > 0) { numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), random, numbyteswritten); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; /* add end tag to buffer */ numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), "\"", 1); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, 1); numbyteswritten = 0; } /* add end tag to buffer */ numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), OSPC_AUDIT_TAG_END, OSPC_AUDIT_TAG_END_LEN); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; return;}/* Monitor worker conditional variable in audit and wait for conditions to be met */staticOSPTTHREADRETURN OSPPAuditMonitor( void *ospvArg /* Audit block cast to a void */){ int errorcode = OSPC_ERR_NO_ERROR, tmperror = OSPC_ERR_NO_ERROR; OSPTAUDIT *audit = OSPC_OSNULL; OSPTBOOL do_forever = OSPC_TRUE; audit = (OSPTAUDIT *)ospvArg; while(do_forever) { /* * acquire worker mutex */ OSPM_MUTEX_LOCK(audit->ospmAuditWorkerMutex, errorcode); if(errorcode == OSPC_ERR_NO_ERROR) { /* * wait for conditional variable on the flush flag */ while ((errorcode != OSPC_ERR_OS_CONDVAR_TIMEOUT) && (audit->ospmAuditFlags & OSPC_AUDIT_FLUSH_BUFFER_NOW) == 0) { OSPM_CONDVAR_TIMEDWAIT(audit->ospmAuditWorkerCondVar, audit->ospmAuditWorkerMutex, audit->ospmAuditMaxTime, errorcode); } if((audit->ospmAuditFlags & OSPC_AUDIT_FLUSH_BUFFER_NOW) || (errorcode == OSPC_ERR_OS_CONDVAR_TIMEOUT)) { OSPM_MUTEX_LOCK(audit->ospmAuditAccessMutex, errorcode); if(errorcode == OSPC_ERR_NO_ERROR) { /* * prepare and send the data to the auditurl */ errorcode = OSPPAuditPrepareAndSend(audit); /* If all Usage Indications have been confirmed, clear the buffer */ if((audit->ospmAuditNumMessages == 0) && (errorcode == OSPC_ERR_NO_ERROR)) { OSPPAuditComponentIdDelete(audit); errorcode = OSPPAuditResetDefaults(audit); } if(errorcode == OSPC_ERR_NO_ERROR) { audit->ospmAuditFlags |= OSPC_AUDIT_BUFFER_IS_EMPTY; /* signal in case this is auditdelete */ OSPM_CONDVAR_SIGNAL(audit->ospmAuditAccessCondVar, errorcode); } OSPM_MUTEX_UNLOCK(audit->ospmAuditAccessMutex, errorcode); } /* break;*/ } /* * release the mutex lock */ OSPM_MUTEX_UNLOCK(audit->ospmAuditWorkerMutex, tmperror); } }/* errorcode = OSPPAuditResetDefaults(audit);*/#ifdef _WIN32 return;#else return (OSPTTHREADRETURN)NULL;#endif }/* Create new audit object */OSPTAUDIT *OSPPAuditNew( const char *ospvAuditURL){ OSPTAUDIT *audit = OSPC_OSNULL; int errorcode = OSPC_ERR_NO_ERROR; OSPM_MALLOC(audit, OSPTAUDIT, sizeof(OSPTAUDIT)); if (audit != OSPC_OSNULL) { OSPM_MEMSET(audit, 0, sizeof(OSPTAUDIT)); /* set up the audit url */ if(ospvAuditURL != OSPC_OSNULL) { OSPPAuditSetURL(audit, ospvAuditURL); /* * initialize the audit worker mutex */ OSPM_MUTEX_INIT(audit->ospmAuditWorkerMutex, 0, errorcode); if(errorcode == OSPC_ERR_NO_ERROR) { /* * initialize the audit worker conditional variable */ audit->ospmAuditWorkerHasMutex = OSPC_TRUE; OSPM_CONDVAR_INIT(audit->ospmAuditWorkerCondVar, NULL, errorcode); } /* set up storage */ if(errorcode == OSPC_ERR_NO_ERROR) { audit->ospmAuditStorage = OSPPBfrNew((unsigned)OSPC_AUDIT_MAX_SPACE); if(audit->ospmAuditStorage == OSPC_OSNULL) { errorcode = OSPC_ERR_AUDIT_MALLOC_FAILED; } } if(errorcode == OSPC_ERR_NO_ERROR) { /* * initialize the audit access mutex */ OSPM_MUTEX_INIT(audit->ospmAuditAccessMutex, 0, errorcode); if(errorcode == OSPC_ERR_NO_ERROR) { audit->ospmAuditAccessHasMutex = OSPC_TRUE; /* * initialize the audit access conditional variable */ OSPM_CONDVAR_INIT(audit->ospmAuditAccessCondVar, NULL, errorcode); } } if(errorcode == OSPC_ERR_NO_ERROR) { OSPPAuditSetMaxMessages(audit, OSPC_AUDIT_MAX_MESSAGES); /* adjust space for space needed for * Message end tag. */ OSPPAuditSetMaxSpace(audit, OSPC_AUDIT_MAX_SPACE - (OSPC_AUDIT_BFR_END_LEN + 1)); OSPPAuditSetMaxTime(audit, OSPC_AUDIT_MAX_TIME); } /* Set up xml and Message tag at beginning of buffer */ if(errorcode == OSPC_ERR_NO_ERROR) { OSPPAuditInitializeBuffer(audit); /* Set the "buffer is empty" flag */ audit->ospmAuditFlags |= OSPC_AUDIT_BUFFER_IS_EMPTY; } } else { errorcode = OSPC_ERR_AUDIT_MALLOC_FAILED; } } if(errorcode != OSPC_ERR_NO_ERROR) { OSPPAuditDelete(&audit); } return audit;}/* Prepare audit message and send */int OSPPAuditPrepareAndSend( OSPTAUDIT *ospvAudit){ int errorcode = OSPC_ERR_NO_ERROR; unsigned char *signature = OSPC_OSNULL; unsigned sizeofsignature = 0; unsigned char *auditbuffer = OSPC_OSNULL; unsigned auditbuffersz = 0; unsigned char *outgoingmessage = OSPC_OSNULL; unsigned sizeofoutmsg = 0; OSPTMSGINFO *msginfo = OSPC_OSNULL; int numbyteswritten = 0; if(ospvAudit != OSPC_OSNULL) { if((ospvAudit->ospmAuditSecurity != OSPC_OSNULL) && (ospvAudit->ospmAuditStorage != OSPC_OSNULL)) { /* Add ending "Message" tag */ numbyteswritten = OSPPBfrWriteBlock(&(ospvAudit->ospmAuditStorage), OSPC_AUDIT_BFR_END, OSPC_AUDIT_BFR_END_LEN); /* increment used space */ OSPPAuditIncrementUsedSpace(ospvAudit, numbyteswritten); numbyteswritten = 0; auditbuffer = (unsigned char *)OSPPBfrLinearPtr(ospvAudit->ospmAuditStorage); auditbuffersz = OSPPBfrSize(ospvAudit->ospmAuditStorage); if(auditbuffersz > 0) { errorcode = OSPPSecSignatureCreate( ospvAudit->ospmAuditSecurity, auditbuffer, auditbuffersz, &signature, &sizeofsignature, OSPC_SEC_SIGNATURE_ONLY); if((errorcode == OSPC_ERR_NO_ERROR) && (signature == OSPC_OSNULL)) { OSPM_MALLOC(signature,unsigned char,64); if(signature != OSPC_OSNULL) { OSPM_STRCPY((char *)signature,"Signature placeholder"); sizeofsignature=OSPM_STRLEN((char *)signature); } else { errorcode=OSPC_ERR_SEC_NO_MEMORY; } } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPMimeMessageCreate( auditbuffer, auditbuffersz, signature, sizeofsignature, (unsigned char **)&outgoingmessage, &sizeofoutmsg); if(errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPMsgInfoNew(&msginfo); if (errorcode == OSPC_ERR_NO_ERROR) { /* set the content type depending on the presence * or absence of a signature */ if(signature != OSPC_OSNULL) { OSPM_MALLOC(msginfo->ContentType, unsigned char, strlen(OSPC_COMM_MULTI_MSG)+1); if(msginfo->ContentType != OSPC_OSNULL) { OSPM_MEMSET(msginfo->ContentType, 0, strlen(OSPC_COMM_MULTI_MSG)+1); OSPM_MEMCPY(msginfo->ContentType, OSPC_COMM_MULTI_MSG, strlen(OSPC_COMM_MULTI_MSG)); } } else { OSPM_MALLOC(msginfo->ContentType, unsigned char, strlen(OSPC_COMM_TEXT_MSG)+1); if(msginfo->ContentType != OSPC_OSNULL) { OSPM_MEMSET(msginfo->ContentType, 0, strlen(OSPC_COMM_TEXT_MSG)+1); OSPM_MEMCPY(msginfo->ContentType,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -