core40Example_main.c

Example TFS accepting only connections using SSL encryption. This example needs a compiled schema, core40Example.sdl.

#include "rdm.h"
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "core40Example_cat.h"
#include "privatekey.h"
#include "certificate.h"
#include "rdmstartupapi.h"
#include <stdio.h>
#include <stdlib.h>
RDM_EXPORT int32_t main_sslTFSTutorial(int32_t argc, const char* const* argv)
{
RDM_TFS tfs = NULL;
RDM_DB db = NULL;
RDM_UNREF(argc);
RDM_UNREF(argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS(&tfs);
if (rc == sOKAY)
{
/* Setup TFS to use ONLY SSL encrypted connections.
* If this is not set, the TFS will allow SSL and non-SSL
* connections.
*/
rc = rdm_tfsSetOption (tfs, "force_ssl", "1");
/* Set an SSL private key for this RDM_TFS instance.
* The pointer to the privateKeyBuffer must remain active
* for the duration of this RDM_TFS instance.
*/
if (rc == sOKAY)
{
}
/* Set an SSL certificate for this RDM_TFS instance.
* The pointer to the certificateBuffer must remain active
* for the duration of this RDM_TFS instance.
*/
if (rc == sOKAY)
{
}
/* Setup TFS to listen for connection on startup.
* If not set, the rdm_tfsEnableListener() function can be used
* start the listener.
*/
if (rc == sOKAY)
{
rc = rdm_tfsSetOption (tfs, "listen", "1");
}
/* Setup TFS to port/name to listen on (default is 21553) */
if (rc == sOKAY)
{
rc = rdm_tfsSetOption (tfs, "name", "21560");
}
/* Initialize the TFS handle */
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (tfs);
}
/*
* The RaimaDB TFS is accepting connections when we reach this point.
* We can connect to the running TFS instance within this application
* or we can wait until we're signalled to shutdown.
*/
/* Allocate a database handle */
if (rc == sOKAY)
{
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
{
rc = rdm_dbSetCatalog (db, core40Example_cat);
}
/* Setup DB to use SSL encrypted connection.
* If not set, the rdm_dbOpen will attempt a non-SSL connection.
*/
rc = rdm_dbSetOption (db, "use_ssl", "1");
if (rc == sOKAY)
{
/* Open the database using a TCP/IP URI specification */
rc = rdm_dbOpen (
db, "tfs-tcp://localhost:21560/core40", RDM_OPEN_EXCLUSIVE);
if (rc == sOKAY)
{
size_t outSize;
size_t outSize1;
char *buffer;
printf ("Connected to the remote TFS using tcp/ip "
"(tfs-tcp://localhost:21560/core40)\n");
/*
** Retrieve the certificate from the TFS
*
* The first call to rdm_dbGetCertificate below retrieves the
*size buffer needed to hold the entire certificate.
*/
rc = rdm_dbGetCertificate (db, NULL, 0, &outSize);
if (rc == sOKAY)
{
/* Allocate a buffer to receive the certificate */
buffer = malloc (outSize);
db, buffer, outSize, &outSize1);
puts (buffer);
free (buffer);
}
/* Close the database */
}
else if (rc == eTX_CONNECT)
{
printf ("Could not connect to the remote TFS, make sure "
"one has "
"been started.\n");
}
}
rdm_tfsFree (tfs); /* The will shutdown the listening TFS */
}
}
printf (
"Exit Status (%s -%s)\n", rdm_retcodeGetName (rc),
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE(sslTFSTutorial)
@ eTX_CONNECT
Definition: rdmretcodetypes.h:679
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
RDM_RETCODE rdm_tfsSetKey(RDM_TFS tfs, const char *private_key)
Set an SSL private key for an RDM_TFS instance.
Header for the native RaimaDB Runtime API.
RDM_RETCODE rdm_rdmAllocTFS(RDM_TFS *phTFS)
Allocate a TFS handle.
Header for the RaimaDB Core API.
const char * privateKeyBuffer
Definition: privatekey.h:9
@ RDM_OPEN_EXCLUSIVE
Definition: rdmtypes.h:290
RDM_RETCODE rdm_tfsSetCertificate(RDM_TFS tfs, const char *certificate)
Set an SSL certificate for an RDM_TFS instance.
RDM_RETCODE rdm_dbClose(RDM_DB db)
Close the database associated with a database handle.
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
@ sOKAY
Definition: rdmretcodetypes.h:100
RDM_RETCODE rdm_dbFree(RDM_DB db)
Free a database handle.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RaimaDB database using the specified database handle.
RDM_RETCODE rdm_dbGetCertificate(RDM_DB db, char *certificate_info, size_t sizeIn, size_t *sizeOut)
Retrieve the information about the SSL certificate.
RDM_RETCODE rdm_dbSetOption(RDM_DB db, const char *keyword, const char *strValue)
Set a single RaimaDB option from a string.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RaimaDB db.
Header for the Transactional File Server (TFS) API.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:81
enum RDM_RETCODE_E RDM_RETCODE
RaimaDB status and error return codes.
RDM_RETCODE rdm_tfsSetOption(RDM_TFS tfs, const char *keyword, const char *strValue)
Set a single TFS option from a string.
const char * rdm_retcodeGetDescription(RDM_RETCODE retcode)
Invoke RaimaDB error handler.
#define RDM_UNREF(a)
Definition: psptypes.h:86
struct RDM_TFS_S * RDM_TFS
RaimaDB TFS Handle.
Definition: rdmtfstypes.h:21
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:346
RDM_RETCODE rdm_tfsFree(RDM_TFS hTFS)
Terminate a TFS service.
const char * certificateBuffer
Definition: certificate.h:9
@ eNOTIMPLEMENTED_TRANSPORT_SSL
Definition: rdmretcodetypes.h:814
Internal RaimaDB Startup API used by startup macros.
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.