DINAMO Networks is now aDINAMO cyber security company!
API C/C++
HSM DINAMO
Loading...
Searching...
No entry found
pix_handle_request.c

Example of standard POST, PUT, GET, and DELETE requests PIX using a handle PIX (with caching), where a single entry point handles all methods.

See Note on examples.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "127.0.0.1"
#define USER_ID "master"
#define USER_PWD "12345678"
int main()
{
int nRet = 0;
int i = 0;
struct AUTH_PWD authPwd;
HPIXSESSIONCTX hPIXSession = NULL;
const char *pszHeaderList[] = { "Content-Type: application/xml; charset=utf-8" };
DWORD dwHeaderListCount = 1; //Número de linhas em pszHeaderList
BYTE pbTestData[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><xml>sdfsdf</xml>";
const char szKeyId[] = "key";
const char szKeyCert[] = "key_cert";
const char szPIXChain[] = "pix_chain";
const char szUrl[] = "https://127.0.0.1:8000";
//Um único ponto de entrada atende todos os métodos: só dwMethod muda.
const char *apszMethodNames[] = { "POST", "PUT", "GET", "DELETE" };
PIX_REQUEST stRequest;
PIX_RESPONSE stResponse;
//Inicializa as bibliotecas do Dinamo
nRet = DInitialize(0);
if (nRet) {
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Bibliotecas inicializadas.\n");
//Inicializa a estrutura para conexão com o HSM
memset(&authPwd, 0, sizeof(authPwd));
strncpy(authPwd.szAddr, HOST_ADDR, sizeof(authPwd.szAddr));
authPwd.nPort = DEFAULT_PORT;
strncpy(authPwd.szUserId, USER_ID, sizeof(authPwd.szUserId));
strncpy(authPwd.szPassword, USER_PWD, sizeof(authPwd.szPassword));
/*
Não há DOpenSession aqui. O handle guarda as credenciais e a identificação dos
objetos; a sessão com o HSM é aberta apenas durante o handshake TLS, e liberada
em seguida. Sem DN_PIX_NO_CACHE o handle é uma referência leve: a conexão HTTP
vem do cache da biblioteca a cada requisição.
*/
nRet = DPIXOpenHandle(&hPIXSession,
szKeyId,
szKeyCert,
szPIXChain,
(BYTE *)&authPwd,
sizeof(authPwd),
0);
if (nRet) {
printf("Falha na funcao: DPIXOpenHandle \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Handle PIX criado.\n");
//A resposta é preenchida pela biblioteca; dwVersion identifica o layout.
memset(&stResponse, 0, sizeof(stResponse));
for (i = 0; i < 4; i++) {
memset(&stRequest, 0, sizeof(stRequest));
stRequest.dwMethod = adwMethods[i];
stRequest.szURL = szUrl;
stRequest.pszHeaderList = pszHeaderList;
stRequest.dwHeaderCount = dwHeaderListCount;
stRequest.dwTimeOut = 30000;
stRequest.dwParam = 0;
if ((adwMethods[i] == PIX_HTTP_POST) || (adwMethods[i] == PIX_HTTP_PUT)) {
stRequest.pbBody = pbTestData;
stRequest.dwBodyLen = sizeof(pbTestData) - 1;
}
else {
/*
GET e DELETE aceitam corpo se um for informado, mas aqui não enviamos
nenhum. O corpo é zerado a cada volta para que o da requisição anterior
não seja reaproveitado sem querer.
*/
stRequest.pbBody = NULL;
stRequest.dwBodyLen = 0;
}
nRet = DPIXRequest(hPIXSession, &stRequest, &stResponse, 0);
if (nRet) {
printf("Falha na funcao: DPIXRequest (%s) \nCodigo de erro: %d\n",
apszMethodNames[i], nRet);
goto clean;
}
printf("PIX %s executado com sucesso. HTTP %llu, %u bytes de corpo.\n",
apszMethodNames[i],
stResponse.nHttpCode,
stResponse.dwBodyLen);
/*
Libera o cabeçalho e o corpo e zera os demais campos. dwVersion não
é alterada, por isso a mesma estrutura serve para a próxima
requisição sem precisar ser preenchida de novo.
*/
DPIXFreeResponse(&stResponse);
}
clean:
if (hPIXSession) {
DPIXCloseHandle(&hPIXSession, 0);
printf("Handle PIX encerrado.\n");
}
printf("Bibliotecas finalizada.\n");
return nRet;
}
Application Programming Interface (API) do HSM Dinamo.
#define PIX_RESPONSE_VERSION
Definição dinamo.h:2803
#define DEFAULT_PORT
Definição dinamo.h:2091
#define PIX_HTTP_PUT
Definição dinamo.h:2798
unsigned char BYTE
Definição dinamo.h:44
#define PIX_HTTP_POST
Definição dinamo.h:2797
unsigned int DWORD
Definição dinamo.h:45
void * HPIXSESSIONCTX
Definição dinamo.h:68
#define PIX_REQUEST_VERSION
Definição dinamo.h:2802
#define ENCRYPTED_CONN
Definição dinamo.h:586
#define SS_USER_PWD
Definição dinamo.h:577
#define PIX_HTTP_GET
Definição dinamo.h:2799
#define PIX_HTTP_DELETE
Definição dinamo.h:2800
int AAP_API DPIXCloseHandle(HPIXSESSIONCTX *phPIXSession, DWORD dwReserved)
Destrói um handle PIX criado por DPIXOpenHandle(), apagando da memória as credenciais nele armazenada...
int AAP_API DPIXRequest(HPIXSESSIONCTX hPIXSession, const PIX_REQUEST *pstRequest, PIX_RESPONSE *pstResponse, DWORD dwReserved)
Executa uma requisição HTTP com um handle PIX, para qualquer método.
int AAP_API DPIXOpenHandle(HPIXSESSIONCTX *phPIXSession, const char *szKeyId, const char *szCertId, const char *szPIXCertChainId, DWORD dwParam, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags, DWORD dwPixFlags)
Cria um handle PIX, que encapsula uma conexão HTTP persistente e o material TLS (certificado,...
int AAP_API DPIXFreeResponse(PIX_RESPONSE *pstResponse)
Libera os buffers de uma PIX_RESPONSE preenchida por DPIXRequest().
int AAP_API DInitialize(DWORD dwReserved)
int AAP_API DFinalize()
Definição dinamo.h:3545
int nPort
Definição dinamo.h:3547
char szUserId[MAX_USR_LEN]
Definição dinamo.h:3548
char szAddr[MAX_ADDR_LEN]
Definição dinamo.h:3546
char szPassword[MAX_USR_PWD]
Definição dinamo.h:3549
Definição dinamo.h:2817
const char * szURL
Definição dinamo.h:2821
DWORD dwTimeOut
Definição dinamo.h:2829
DWORD dwBodyLen
Definição dinamo.h:2827
DWORD dwParam
Definição dinamo.h:2830
DWORD dwHeaderCount
Definição dinamo.h:2824
DWORD dwMethod
Definição dinamo.h:2820
const char ** pszHeaderList
Definição dinamo.h:2823
DWORD dwVersion
Definição dinamo.h:2818
BYTE * pbBody
Definição dinamo.h:2826
Definição dinamo.h:2841
DWORD dwBodyLen
Definição dinamo.h:2848
ND_uint64_t nHttpCode
Definição dinamo.h:2850
DWORD dwVersion
Definição dinamo.h:2842