|
-
March 23rd, 2006, 07:56 PM
#1
How to convert cpp to c?
I have this cpp code that i need to convert to c
can anyone help or give advice on converting cpp to c?
is it possible to convert to c in ms
header:
Code:
class CSSLSocket
{
public:
CSSLSocket();
virtual ~CSSLSocket();
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;
protected:
SSL_CTX *m_psslCtx;
SSL *m_pSSL;
int m_sSocket;
};
cpp code:
Code:
CSSLSocket::CSSLSocket()
{
m_psslCtx=NULL;
m_pSSL=NULL;
SSL_library_init();
m_bConnected=FALSE;
}
CSSLSocket::~CSSLSocket()
{ // Cleanup
if(m_psslCtx)
SSL_CTX_free(m_psslCtx);
if(m_pSSL)
{
SSL_shutdown(m_pSSL);
SSL_free(m_pSSL);
}
m_bConnected=FALSE;
}
bool CSSLSocket::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 CSSLSocket::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 CSSLSocket::GetSocket() {
return m_sSocket;
}
bool CSSLSocket::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 CSSLSocket::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 CSSLSocket::Close()
{
SSL_shutdown(m_pSSL);
m_bConnected=FALSE;
}
int CSSLSocket::Read(void *pBuf, int iNum)
{
if(!m_psslCtx || !m_pSSL || !m_bConnected)
return 0;
return SSL_read(m_pSSL, pBuf, iNum);
}
int CSSLSocket::Write(const void *pBuf, int iNum)
{
if(!m_psslCtx || !m_pSSL || !m_bConnected)
return 0;
return SSL_write(m_pSSL, pBuf, iNum);
}
Mouse 
-
March 23rd, 2006, 08:34 PM
#2
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.
-
March 24th, 2006, 12:17 AM
#3
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.
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
March 24th, 2006, 04:26 AM
#4
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);
}
Last edited by Gunaamirthavelu; March 24th, 2006 at 06:55 AM.
If I Helped You, "Rate This Post"
Thanks
Guna
-
March 24th, 2006, 06:50 AM
#5
Re: How to convert cpp to c?
 Originally Posted by Gunaamirthavelu
use this c code
id like to kno how the code you attached is in C
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
March 24th, 2006, 06:54 AM
#6
Re: How to convert cpp to c?
If I Helped You, "Rate This Post"
Thanks
Guna
-
March 24th, 2006, 09:59 AM
#7
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.
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!
-
March 24th, 2006, 10:01 AM
#8
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.
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|