Community discussions

MikroTik App
 
AlexeyU
just joined
Topic Author
Posts: 5
Joined: Wed Nov 25, 2020 5:50 pm

Error on API /user/group

Thu Sep 23, 2021 7:58 pm

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
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Error on API /user/group

Tue Sep 28, 2021 1:23 pm

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?
 
AlexeyU
just joined
Topic Author
Posts: 5
Joined: Wed Nov 25, 2020 5:50 pm

Re: Error on API /user/group

Thu Oct 21, 2021 9:06 pm

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.
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Error on API /user/group

Fri Oct 22, 2021 11:33 am

Can you reproduce the issue using the code example provided on the wiki?

Have you also considered using tik4net (https://github.com/danikf/tik4net)?
 
AlexeyU
just joined
Topic Author
Posts: 5
Joined: Wed Nov 25, 2020 5:50 pm

Re: Error on API /user/group

Wed Jan 19, 2022 8:51 am

Change from BinaryWriter/Reader to Stream resolved problem.

Who is online

Users browsing this forum: Bing [Bot], GoogleOther [Bot], Huy0880, NetworqAndy, zabloc and 80 guests