Console example - WeOnlyDo Discussion board

Console example (wodSSHServer)

by Hans, Saturday, December 30, 2006, 20:08 (3977 days ago)

I am trying to create a console version of the fastnotification example.
Everything works, but I cannot make a connection to it.

So somewhere I am forgetting something (I am not good in c++)

I am using the standard wodsshd.[hc] and wodsshdnotify.[hc] of the example (without the dialog code) and a simple main function (see underneath)

Any suggestions ?

thx
Hans


[code]
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
VARIANT var;
LPDISPATCH sshd;
IwodSSHDCom *m_SSHD;
ISSHKeyPair *keys;
CwodSSHDNotify *m_SSHDNotify;
char txt[81];


// initialize MFC and print and error on failure
AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
OleInitialize(NULL);
HRESULT hr = CoCreateInstance(__uuidof(CLSID_wodSSHDCom), NULL, CLSCTX_ALL, __uuidof(IID_IwodSSHDCom), (void **)&sshd);
m_SSHD = new IwodSSHDCom(sshd);

m_SSHDNotify = new CwodSSHDNotify();
m_SSHD->SetRefNotification((LPDISPATCH)m_SSHDNotify->GetInterface(&DIID_IwodSSHDNotify)); // initialize events

VariantInit(&var);
// Key loading or generating
puts( Generating key );
keys = new ISSHKeyPair();
keys->AttachDispatch(m_SSHD->GetKeys(), TRUE);
keys->Generate(0 /*RSA*/, var);

puts( Starting server );
m_SSHD->Start(var);
puts( Waiting ... press return to stop server );
gets(txt);

m_SSHD->Stop();
m_SSHD->SetRefNotification(NULL);
m_SSHD->DetachDispatch();
delete m_SSHD;
OleUninitialize();

return 0;
}
[/code]

Re: Console example

by woddrazen, Saturday, December 30, 2006, 20:51 (3977 days ago) @ Hans

Hi Hans,


I'm not C++ guru also. Here is guide what is important for wodSSHServer to work correct.

First it is important that you ones generate private key and load that key before you start server created by wodSSHServer ActiveX component.
You can choose protocol you wish to use using wodSSHServer Protocol Property (SSH2, Telnet).
If you don't want to use default port 22 for SSH2 or 23 for Telnet Protocol you can use Port Property and change port to some other.

In LoginPassword Event who fires when user wants to authenticate to the server, you can determine which user will be able to connect.

Also important is ServiceRequest Event. ServiceRequest Event fires when user requests some service from the server.

More help for Protocol Property you can find here:
http://www.weonlydo.com/SSHServer/Help/wodSSHDLIB~wodSSHD~Protocol.html

More help for Port Property you can find here:
http://www.weonlydo.com/SSHServer/Help/wodSSHDLIB~wodSSHD~Port.html

More help for LoginPassword Event you can find here:
http://www.weonlydo.com/SSHServer/Help/wodSSHDLIB~wodSSHD~LoginPassword_EV.html

and for ServiceRequest Event you can find here:
http://www.weonlydo.com/SSHServer/Help/wodSSHDLIB~wodSSHD~ServiceRequest_EV.html

Hope this helps.


Regards,
Drazen

Re: Console example

by Hans, Sunday, December 31, 2006, 14:52 (3977 days ago) @ woddrazen

Hi Drazen,

First it is important that you ones generate private key and load that key before you start server created by wodSSHServer ActiveX component.

Have done that in the main function, seems to work ok.

You can choose protocol you wish to use using wodSSHServer Protocol Property (SSH2, Telnet).
If you don't want to use default port 22 for SSH2 or 23 for Telnet Protocol you can use Port Property and change port to some other.

The defaults are good enough, I can see that it is listening on port 22, so that is ok.

In LoginPassword Event who fires when user wants to authenticate to the server, you can determine which user will be able to connect.Also important is ServiceRequest Event. ServiceRequest Event fires when user requests some service from the server.

I think the first event that is triggered is the connecting event
I have:
[code]STDMETHODIMP CwodSSHDNotify::XNotify::Connecting(IwodSSHDCom * Owner, ISSHUser * User, SSHActions * Action)
{
METHOD_PROLOGUE(CwodSSHDNotify, Notify);
puts( connecting );
*Action=Allow;
return S_OK;
}[/code]
The same goes for LoginPassword, LoginPubkey and ServiceRequest.

From a linux client I get the following information:
$ ssh -v 192.xx.xx.xx
..
debug1: connecting to 192.xx.xx.xx port 22
debug1: connection establisted
and then the connection hangs

I think that the problem lies that the Notify functions are somehow not correctly linked and therefore not triggered.

If it is easier I could sent you the complete c++ project or perhaps is it possible that you supply a working console version based on the notify sample ?

Hans

Re: Console example

by woddrazen, Sunday, December 31, 2006, 15:04 (3977 days ago) @ Hans

Hans,


You can send us your project and we we try to help you. You can send it to techsupport@weonlydo.com

If you didn't done it please Implement ALL wodSSHServer Events, or at least declare their empty bodies if you don't use it.
Maybe you can do it automatically if you locate 'IwodSSHDNotify' object in the list of all objects existing on your form (combo box in upper left corner of your code view), or just can do it by hand.

More help you can find here:
http://www.weonlydo.com/SSHServer/Help/Notifications.html


Drazen

Re: Console example

by Hans, Wednesday, January 03, 2007, 15:00 (3974 days ago) @ woddrazen

You can send us your project and we we try to help you.

I have sent my project to you.
Thanks for the support.

Hans

Re: Console example

by wodDamir, Wednesday, January 03, 2007, 15:28 (3974 days ago) @ Hans

Hans,

we haven't yet received your sample. Did you send it to the right address? Also, please send it as .zip file.

Regards,
Damba

Re: Console example

by Hans, Wednesday, January 03, 2007, 22:25 (3973 days ago) @ wodDamir

we haven't yet received your sample. Did you send it to the right address? Also, please send it as .zip file.

Sent it again, first time I gor response from your challenge-response mechanisme

Hans

Re: Console example

by wodSupport, Wednesday, January 03, 2007, 23:01 (3973 days ago) @ Hans

Hans,

don't forget that wodSSHServer lives in same thread as your app. At the moment you call gets() you freeze the thread waiting for keyboard input, and you don't allow wodSSHServer to gets its CPU slice to process incoming messages.

Result is that it will accept the connection but will not be able to make any kind of socket communication.

What you need to do is implement something similar to DoEvents. It means you should put message pump like

while (GetMessage())
{
TranslateMessage();
DispatchMessage();
}

so that messages are processed.

Hope this helps!

Kreso

Re: Console example

by Hans, Thursday, January 04, 2007, 17:32 (3972 days ago) @ wodSupport

Kreso,

don't forget that wodSSHServer lives in same thread as your app. At the moment you call gets() you freeze the thread waiting for keyboard input, and you don't allow wodSSHServer to gets its CPU slice to process incoming messages.
Result is that it will accept the connection but will not be able to make any kind of socket communication.

Ok, understood. I can make a loop and use kbhit as non-blocking replacement, but that consumes cpu time.
Since Sleep() also blocks, I searched and found a non-blocking sleep, called Xsleep().
In fact it does the real sleep() in a new thread and waits until the thread is finished while processing the message pump.

The code looks now like:
[code]puts( Starting server );
m_SSHD->Start(var);
puts( Waiting ... press any key to stop the sshserver );
while (!kbhit()) XSleep(300); //check with intervals of 300 millisecs if a key is pressed
m_SSHD->Stop();
[/code]
And it works !! SSH connection with putty works and the application is using almost no cpu time.

Thanks for the support !

Hans

Re: Console example

by wodSupport, Thursday, January 04, 2007, 18:55 (3972 days ago) @ Hans

Xsleep? Where did you find that? I don't see it in MSDN.. It does exactly as I suggested, but I didn't know there's such API/function available. Do you have some URL for it?

Thanks!
Kreso

Re: Console example

by Hans, Thursday, January 04, 2007, 22:08 (3972 days ago) @ wodSupport

Kreso,

You can find it at CodeGuru

Hans

Re: Console example

by Hans, Thursday, January 04, 2007, 22:38 (3972 days ago) @ Hans

Even easier:
[code]void XSleep(DWORD dwWaitInMSecs)
{
DWORD dwStopTime = GetTickCount() + dwWaitInMSecs;
DWORD dwTimeout = dwWaitInMSecs;
MSG msg;

while(1) {
switch(MsgWaitForMultipleObjects(0, NULL, FALSE, dwTimeout, QS_ALLINPUT)) {
case WAIT_TIMEOUT: break;
default:
while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
dwTimeout = dwStopTime - GetTickCount();
if(!dwTimeout || dwTimeout>dwWaitInMSecs) break;
}
}[/code]

Re: Console example

by Hans, Wednesday, January 31, 2007, 13:17 (3946 days ago) @ Hans


Ok, it is finished and working like a charm
You can download it at www.atbas.org/wssh and the source is included.

thx for the support.

Hans

Re: Console example

by Traveller, Saturday, May 25, 2013, 17:53 (1639 days ago) @ Hans

Hi Kreso,
I found the following 2 URL's at least for You:

1- http://www.codeproject.com/Questions/346461/How-to-make-XSleep-function-execute-untill-i-click

2- http://code.google.com/p/c-system-abstraction-component-gui/source/browse/src/sqlite/3.7.16.2/sqlite3.h?spec=svn36993d458f3c452cd107b7a358d805699dc96ec7&r=36993d458f3c452cd107b7a358d805699dc96ec7

But Which Cpp Library Should be used to let them pass Compilation
and Work? for Sleep() & for Xsleep().
My best Regards.

Re: Console example

by wodSupport, Saturday, May 25, 2013, 17:57 (1639 days ago) @ Traveller

Hi. I'm not sure I understand, is this question about wodSSHServer or some code on CodeProject?

Kreso

Re: Console example

by Traveller, Sunday, May 26, 2013, 01:10 (1639 days ago) @ wodSupport

Hi kreso
as we know, in C++ using Libraries Lets
us be able to use functions inside them
to do what we want such as:

#include<iostream.h>
#include<Socket.h>
#include<fstream.h>
#include<math.h>
#include<Tim.h>
# .... etc

int main()
{

}

The question was which Library should be
included in a C++ program to Use sleep()
succesfully to work.
Regards.

Re: Console example

by wodSupport, Sunday, May 26, 2013, 10:05 (1639 days ago) @ Traveller

Hi. Sorry, but we didn't check 3rd party libraries, you must decide this by yourself.

Kreso