API in C using winsock
57
API in C using winsock
This is an implementation of RouterOS API written on C that using winsock2 for compatibility with Windows
operation systems. It's based on API in C
[1]
(You should use all sources from here, except mikrotik-api.c, that
incompatible with Windows). Use compatible mikrotik-api.c source from below.
If your compiler does not support "#pragma comment", add ws2_32.lib to your linker manually.
RouterOS API Source file (mikrotik-api.c)
/********************************************************************
* Some definitions
* Word = piece of API code
* Sentence = multiple words
* Block = multiple sentences (usually in response to a sentence request)
*
int fdSock;
int iLoginResult;
struct Sentence stSentence;
struct Block stBlock;
fdSock = apiConnect("10.0.0.1", 8728);
// attempt login
iLoginResult = login(fdSock, "admin", "adminPassword");
if (!iLoginResult)
{
apiDisconnect(fdSock);
printf("Invalid username or password.\n");
exit(1);
}
// initialize, fill and send sentence to the API
initializeSentence(&stSentence);
addWordToSentence(&stSentence, "/interface/getall");
writeSentence(fdSock, &stSentence);
// receive and print block from the API
stBlock = readBlock(fdSock);
printBlock(&stBlock);
apiDisconnect(fdSock);
********************************************************************/
// C implementation of Mikrotik's API rewritten for Winsock2 (for windows)
Comments to this Manuals