This is the first time I post here.
I need to write a Win32 application to interact with a RouterOS CPE... a kind of "very simplified" WinBox, with just some basic fuctions.
I've been asked to develop this application in pure C under the Win32 API, so to make a small, portable and high performant application which can start quite immediately on every Windows platform.
My project is to make a quite simple application, but I obviously need it to be multithreaded and, most of all, I need it to make use of sockets to interact with the RouterOS API!
I've made a testing app just to try the connection and communication between my C code and the RouterOS API... but unfortunally something seems to go wrong!
I don't know exactly what's happening, because if I try to communicate via socket with other applications, I don't seem to have problems... but I have big problems when I try to communicate with the CPE!
If I exchange messages via socket with a testing application I run on my PC (Serv/Term 1.0 from PC-Tools.net) as a server, I can perfectly send and receive messages in plain text.
With the CPE I can connect the socket without any problem... and I don't seem to have any problem in sending messages (even if I don't know what the CPE receives!!!), but I have problems in receiving messages back!
If I connect with the API port, the 8728, I simply don't receive ANYTHING back!!!
Nothing back after the connect(), nothing back after sending the "/login" string!!!
Just for testing, I've tried to "talk" with the CPE via the port 23, the Telnet standard... which SHOULD talk plain text... and after the connect() what I get back is this:
ÿýÿý ÿý#ÿý'
As you can imagine, if I try to send something to the CPE I can't do much... since I don't know what the CPE is answering me! And even if I try to send the username and then the password... nothing useful seems to happen!
The strange thing is that even the Serv/Term 1.0 application, started as a client, receives EXACTLY the same message from the CPE immediately after the connection!
It's not that good, but alas the problem seems to be not just on my code!
Can anybody help me?
Here is some snippet of the code I'm using for this tests:
Code: Select all
/* some includes and declarations */
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include <winsock.h>
#include "resource.h"
SOCKET clientSocket;
...
/* the code used to connect the socket... it seems to work well... */
SOCKADDR_IN addr;
short port;
char ipAddr[15]="192.168.168.112"; // the ip of the CPE I'm using for the tests
port=8728; // to test the API
//port=23; // to test the telnet connection
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=inet_addr(ipAddr);
addr.sin_port=htons(port);
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(connect(clientSocket, (LPSOCKADDR)&addr, sizeof(addr))<0)
{
MessageBox(hwndDlg, "Error connecting the socket!", "Connection error!", MB_OK | MB_ICONERROR);
WSACleanup();
}
break;
...
/* the code used to send a message and receive an answer */
/*
since at the present moment this code isn't multithreaded,
it hangs the application if not receiving anything...
I'll check it after being capable of communicating good!
*/
char* msg; // this will contain the API command line... "/login" for example...
char buf[256];
int bytesreceived=SOCKET_ERROR;
// these two lines get the command line from a control on the application GUI...
msg = (char*)GlobalAlloc(GPTR, len+1);
GetDlgItemText(hwndDlg, ID_COMMANDLINE, msg, len+1);
ZeroMemory(buf, sizeof(buf)); // I clear the buffer...
send(clientSocket, msg, len+1,0); // and send my message on the socket!
while(bytesreceived==SOCKET_ERROR)
{
// this line hangs the application if trying to communicate using the API because of no response
// and gets that bunch of strange characters posted before using the port 23
bytesreceived = recv(clientSocket, buf, sizeof(buf)-1, 0);
if (bytesreceived == 0 || bytesreceived == WSAECONNRESET)
{
MessageBox(hwndDlg, "Client closed the connection", "Connection closed!", MB_OK);
break;
}
buf[bytesreceived] = '\0'; // I add the terminator to the received message...
MessageBox(hwndDlg, buf, "Message received from socket:", MB_OK);
}
GlobalFree(msg);
The API service is obviously started in RouterOS, and it accepts connections from 0.0.0.0/0 (which should be ANYTHING, right?).
Any kind of hint that can help me to solve this problem is welcome!!!