API Login Process

Hi Forum !!!.

For several days I’m trying to get connected with api using Alaska (xBase Programming language)… after reading and reading the examples, wiki tutorials, api docs and finally several post in forum I don’t have a clue how to be connected ..

Up to now I can send the first /login command… receive the ret challenge but Seems to be that I have a black hole about the answer I have to create using the Password and ret challenge with md5 …mikrotik never answer my last send message (response)

I’d like to show you guys a Little parte of the code I’m using and maybe someone can tell me where is my error …




// Sending Initial Login Command. WORKS FINE
nSend1:= SocketSend( LNSOCKET, chr(6) + “/login”+ chr(0) )
sleep(400)

// Receiving Initial Login Answer
cBuffer := Space( 300 ) ; nBytes := SocketRecv( LNSOCKET, @cBuffer, 300, @LNERROR )
MSGBOX("Answer Initial Login: " + cBuffer)
// Console Output: !done%=ret=25ad12e81cd7b6bd2011d3ae732cd0af - WORKS FINE

cWork := cBuffer ; cFrom:=“=ret=” , nPos:=At(cFrom, cWork)+LEN(cFrom) ; cNew:=SubStr(cWork, nPos, )
msgbox("ret Challegene without =ret= " + cNew ) // Console OutPut: 25ad12e81cd7b6bd2011d3ae732cd0af
msgbox("to string " + HexToStr(cNew) ) // Console OutPut: %­è×¶½ Ó®s,Ð

cSend:=“=response=00”+ Str2Md5Hex( “51092” + HexToStr(cNew) )
msgbox("response to send: ") + cSend
// Console OutPut: =response=00722207153321495723851e6f45a5a976



nSend2:= SocketSend( LNSOCKET, nLength(“/login”) + bData(“/login”) ) // WORKS FINE
nSend3:= SocketSend( LNSOCKET, nLength(“=name=admin”) + bData(“=name=admin”) ) // WORKS FINE
nSend4:= SocketSend( LNSOCKET, nLength(cSend) + bData(cSend) ) // MKTK NEVER ANSWER !


cBuffer := Space( 300 )
nBytes := SocketRecv( LNSOCKET, @cBuffer, 300, @LNERROR )
MSGBOX(“Final Answer " + cBuffer )


// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FUNCTION nLength(cText)
LOCAL cReturn:=”"
cReturn:=AL(STR( LEN(cText) )) + “0” + al(str( LEN(AL(STR(LEN(cText)))) ))
RETURN (cReturn)

FUNCTION bData(cText)
LOCAL cReturn:=“”
cReturn:=ReturnAsciiCode(cText) + “0” + AL(STR( LEN(cText) ))
RETURN (cReturn)

FUNCTION ReturnAsciiCode(cText)
LOCAL cReturn:=“”
FOR A=1 TO LEN(cText)
cReturn:=cReturn+AL(STR(ASC(SubStr(cText, A, 1))))
NEXT A
RETURN (cReturn)

nSend4:= SocketSend( LNSOCKET, nLength(cSend) + bData(cSend) ) // MKTK NEVER ANSWER !

don’t you need to send chr(0) (zero-length word) to complete the sentence?..

p.s. ‘response’ should be ‘00’ + Str2Md5Hex( chr(0) + “51092” + HexToStr(cNew))

Thks Chupaka I’ll try it !!

I’ve finally solved my md5 problem … now I cannot send the =name=admin

First I started sending chr(6) + “/login” + chr(0)

Reading the API docs.. Seems to be that I need to send the “length and the many bytes of content” … from what I understand in this case (=name=admin) I should simply send “11=name=admin” or I should send this “string” converted in Hexa Code, Decimals codes ?

Can someone give me any idea?

the second call to /login should be something like

chr(6) + “/login” + chr(11) + “=name=admin” + chr(43) + “=response=00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” + chr(0)

but it would be much better if you write some wrappers to send ‘words’ to API socket

I tried sending the second login using your comments but I don’t have any answer from mktk…

reading and reading again I noticed now that in wiki docs it’s says “terminated by zero length word” and I assume that this word is chr(0) … that’s ok but in my current programming language the char(0) sentence has a length of 1 !!!

I don’t understand why during the first login mktk answer the call but doesn’t on the second.

yes, chr(0) is 1-byte block of data containing zero length word: 1 byte of encoded length + 0 byte of data

well, you may try to use sniffer to see what data is actually transferred between you and mikrotik…

I’m finally connected and working … the error:= chr(43) … should be chr(44)…

here the piece of code that’s working..

nSend:= SocketSend( LNSOCKET, cwrap(“/login”) + cwrap(“=name=admin”) + cwrap(“=response=00” + md5 ) + chr(0) )



the function cwrap calculate the lenght and returns the length (chr(x)) + the original string…


Thanks Chupaka !.

you are welcome =)