Re: How to convert cpp to c?
create a structure called CSSLSocket and put the data objects into it.
Code:
struct CSSLSocket
{
SSL_CTX *m_psslCtx;
SSL *m_pSSL;
int m_sSocket;
};
now just make the methods of that class global functions that use a pointer to an instance of struct CSSLSocket. There will be no automatic call of the current class constructor and destructor, so just make normal functions out of them and call them explicitly in your code.
Re: How to convert cpp to c?
And maybi a few other additional libraries for ...Ex. BOOL .... ( that is if you dont want to modify the existing code and such.
Re: How to convert cpp to c?
use this code
Code:
// function decleration
bool Init();
bool AttachToSocket(int sSocket);
int GetSocket();
bool Accept();
bool Connect();
void Close();
int Read(void *pBuf, int iNum);
int Write(const void *pBuf, int iNum);
bool m_bConnected;
// Variables
SSL_CTX *m_psslCtx;
SSL *m_pSSL;
int m_sSocket;
void main()
{
m_psslCtx=NULL;
m_pSSL=NULL;
SSL_library_init();
m_bConnected=FALSE;
Init();
AttachToSocket(int sSocket);
GetSocket();
Accept();
Connect();
Close();
Read(void *pBuf, int iNum);
Write(const void *pBuf, int iNum);
}
bool Init()
{
m_bConnected=FALSE;
// Initialize the context, exit if fails
m_psslCtx=SSL_CTX_new(SSLv3_method());
if(!m_psslCtx)
return FALSE;
// Initialize the object, exit if fails
m_pSSL=SSL_new(m_psslCtx);
if(!m_pSSL)
return FALSE;
// Success
return TRUE;
}
bool AttachToSocket(int sSocket)
{
m_bConnected=FALSE;
// Fail if not initialized
if(!m_psslCtx || !m_pSSL)
return FALSE;
// Store the socket, and attach it to the ssl object
m_sSocket=sSocket;
if(!SSL_set_fd(m_pSSL, m_sSocket))
return FALSE;
// Success
return TRUE;
}
int GetSocket()
{
return m_sSocket;
}
bool Accept()
{
m_bConnected=FALSE;
// Fail if not initialized
if(!m_psslCtx || !m_pSSL)
return FALSE;
if(SSL_accept(m_pSSL)==1)
{
m_bConnected=TRUE;
return TRUE;
}
else
{
m_bConnected=FALSE;
return FALSE;
}
}
bool Connect()
{
m_bConnected=FALSE;
// Fail if not initialized
if(!m_psslCtx || !m_pSSL)
return FALSE;
if(SSL_connect(m_pSSL)==1)
{
m_bConnected=TRUE;
return TRUE;
}
else
{
m_bConnected=FALSE;
return FALSE;
}
}
void Close()
{
SSL_shutdown(m_pSSL);
m_bConnected=FALSE;
}
int Read(void *pBuf, int iNum)
{
if(!m_psslCtx || !m_pSSL || !m_bConnected)
return 0;
return SSL_read(m_pSSL, pBuf, iNum);
}
int Write(const void *pBuf, int iNum)
{
if(!m_psslCtx || !m_pSSL || !m_bConnected)
return 0;
return SSL_write(m_pSSL, pBuf, iNum);
}
Re: How to convert cpp to c?
Quote:
Originally Posted by Gunaamirthavelu
use this c code
id like to kno how the code you attached is in C :confused:
Re: How to convert cpp to c?
Quote:
Originally Posted by sreehari
id like to kno how the code you attached is in C :confused:
what?i can't understand :cry: :wave: :wave:
Re: How to convert cpp to c?
If you are using sockets then IMHO maybe you should try writing the code using WinSock API, that will be much easier than converting the code to C. But, thats just my opinion.
Re: How to convert cpp to c?
If you are using sockets then IMHO you should use WinSock API rather converting your code to C. But, thats just my opinion.