AlexeyU
September 23, 2021, 4:58pm
1
Hello.
I’m using C# example from Wiki (Read() method) to read response from RouterOS.
All working fine, but “/user/group” command. Read() method call exception where comment “//Error in packet reception, unknown length”
This exception happens only on custom group. Default groups (“read”, “write”, “full”) readed fine from response.
If I add .props to read only .ids - working fine and i got all groups. But if I add “policy” to .props - I’ll got exception
latest 6.48.4
The command to execute is /user/group/print, but you would have got no such command if that was the problem.
I had no luck reproducing the issue with the MK class in https://wiki.mikrotik.com/wiki/API_in_C_Sharp#Class (without SSL, new login procedure w/ plaintext password). Works as intended.
Custom groups are shown.
var client = new MK(host);
if (client.Login(user, pass))
{
client.Send("/user/group/print");
client.Send("=.proplist=policy", true);
foreach (var line in client.Read())
{
Console.WriteLine(line);
}
}
Can you provide a code example causing the issue?
I’m add ‘/print’ to request, of course
I’m using my own class, with Read() method from example, but use BinaryReader inside
public List<string> Read()
{
List<string> output = new List<string>();
string o = "";
byte[] tmp = new byte[4];
long count;
while (!readCancelationSource.IsCancellationRequested)
{
tmp[3] = (byte)streamReader.Read();
//if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that
//mikrotik send 220 as some kind of "bonus" between words, this fixed things, not sure about it though
if (tmp[3] == 0)
{
output.Add(o);
if (o.Substring(0, 5) == "!done")
{
break;
}
else
{
o = "";
continue;
}
}
else
{
if (tmp[3] < 0x80)
{
count = tmp[3];
}
else
{
if (tmp[3] < 0xC0)
{
int tmpi = BitConverter.ToInt32(new byte[] { (byte)streamReader.Read(), tmp[3], 0, 0 }, 0);
count = tmpi ^ 0x8000;
}
else
{
if (tmp[3] < 0xE0)
{
tmp[2] = (byte)streamReader.Read();
int tmpi = BitConverter.ToInt32(new byte[] { (byte)streamReader.Read(), tmp[2], tmp[3], 0 }, 0);
count = tmpi ^ 0xC00000;
}
else
{
if (tmp[3] < 0xF0)
{
tmp[2] = (byte)streamReader.Read();
tmp[1] = (byte)streamReader.Read();
int tmpi = BitConverter.ToInt32(new byte[] { (byte)streamReader.Read(), tmp[1], tmp[2], tmp[3] }, 0);
count = tmpi ^ 0xE0000000;
}
else
{
if (tmp[3] == 0xF0)
{
tmp[3] = (byte)streamReader.Read();
tmp[2] = (byte)streamReader.Read();
tmp[1] = (byte)streamReader.Read();
tmp[0] = (byte)streamReader.Read();
count = BitConverter.ToInt32(tmp, 0);
}
else
{
//Error in packet reception, unknown length
break;
}
}
}
}
}
}
var b = new byte[count];
for (int i = 0; i < count; i++)
{
b[i] = (byte)streamReader.Read();
}
o += Encoding.Default.GetString(b);
}
return output;
}
No problem with other requests.
Can you reproduce the issue using the code example provided on the wiki?
Have you also considered using tik4net (https://github.com/danikf/tik4net )?
Change from BinaryWriter/Reader to Stream resolved problem.