Fixed a nasty typo when calculating word length.
[routeros-api.git] / src / main.c
index 2b59cbf..9232fd9 100644 (file)
@@ -408,48 +408,48 @@ static int read_word (mt_connection_t *c, /* {{{ */
                return (status);
 
        /* Calculate `req_size' */
-       if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
+       if (((unsigned char) word_length[0]) == 0xF0) /* {{{ */
        {
                status = read_exact (c->fd, &word_length[1], 4);
                if (status != 0)
                        return (status);
 
-               req_size = (buffer[1] << 24)
-                       | (buffer[2] << 16)
-                       | (buffer[3] << 8)
-                       | buffer[4];
+               req_size = (word_length[1] << 24)
+                       | (word_length[2] << 16)
+                       | (word_length[3] << 8)
+                       | word_length[4];
        }
-       else if ((buffer[0] & 0xE0) == 0xE0)
+       else if ((word_length[0] & 0xE0) == 0xE0)
        {
                status = read_exact (c->fd, &word_length[1], 3);
                if (status != 0)
                        return (status);
 
-               req_size = ((buffer[0] & 0x1F) << 24)
-                       | (buffer[1] << 16)
-                       | (buffer[2] << 8)
-                       | buffer[3];
+               req_size = ((word_length[0] & 0x1F) << 24)
+                       | (word_length[1] << 16)
+                       | (word_length[2] << 8)
+                       | word_length[3];
        }
-       else if ((buffer[0] & 0xC0) == 0xC0)
+       else if ((word_length[0] & 0xC0) == 0xC0)
        {
                status = read_exact (c->fd, &word_length[1], 2);
                if (status != 0)
                        return (status);
 
-               req_size = ((buffer[0] & 0x3F) << 16)
-                       | (buffer[1] << 8)
-                       | buffer[2];
+               req_size = ((word_length[0] & 0x3F) << 16)
+                       | (word_length[1] << 8)
+                       | word_length[2];
        }
-       else if ((buffer[0] & 0x80) == 0x80)
+       else if ((word_length[0] & 0x80) == 0x80)
        {
                status = read_exact (c->fd, &word_length[1], 1);
                if (status != 0)
                        return (status);
 
-               req_size = ((buffer[0] & 0x7F) << 8)
-                       | buffer[1];
+               req_size = ((word_length[0] & 0x7F) << 8)
+                       | word_length[1];
        }
-       else if ((buffer[0] & 0x80) == 0)
+       else if ((word_length[0] & 0x80) == 0)
        {
                req_size = (size_t) word_length[0];
        }
@@ -478,17 +478,17 @@ static int read_word (mt_connection_t *c, /* {{{ */
        return (0);
 } /* }}} int buffer_decode_next */
 
-static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
+static mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
 {
        char buffer[4096];
        size_t buffer_size;
        int status;
 
-       mt_reply_t *head;
-       mt_reply_t *tail;
+       mt_reply_t *r;
 
-       head = NULL;
-       tail = NULL;
+       r = reply_alloc ();
+       if (r == NULL)
+               return (NULL);
 
        while (42)
        {
@@ -505,33 +505,11 @@ static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
 
                if (buffer[0] == '!') /* {{{ */
                {
-                       mt_reply_t *tmp;
-
-                       tmp = reply_alloc ();
-                       if (tmp == NULL)
-                       {
-                               status = ENOMEM;
-                               break;
-                       }
-
-                       tmp->status = strdup (&buffer[1]);
-                       if (tmp->status == NULL)
-                       {
-                               reply_free (tmp);
-                               status = ENOMEM;
+                       if (r->status != NULL)
+                               free (r->status);
+                       r->status = strdup (&buffer[1]);
+                       if (r->status == NULL)
                                break;
-                       }
-
-                       if (tail == NULL)
-                       {
-                               head = tmp;
-                               tail = tmp;
-                       }
-                       else
-                       {
-                               tail->next = tmp;
-                               tail = tmp;
-                       }
                } /* }}} if (buffer[0] == '!') */
                else if (buffer[0] == '=') /* {{{ */
                {
@@ -548,20 +526,54 @@ static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
                        *val = 0;
                        val++;
 
-                       reply_add_keyval (tail, key, val);
+                       reply_add_keyval (r, key, val);
                } /* }}} if (buffer[0] == '=') */
                else
                {
-                       printf ("Ignoring unknown word: %s\n", buffer);
+                       mt_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
                }
        } /* while (42) */
        
-       if (status != 0)
+       if (r->status == NULL)
        {
-               reply_free (head);
+               reply_free (r);
                return (NULL);
        }
 
+       return (r);
+} /* }}} mt_reply_t *receive_sentence */
+
+static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
+{
+       mt_reply_t *head;
+       mt_reply_t *tail;
+
+       head = NULL;
+       tail = NULL;
+
+       while (42)
+       {
+               mt_reply_t *tmp;
+
+               tmp = receive_sentence (c);
+               if (tmp == NULL)
+                       break;
+
+               if (tail == NULL)
+               {
+                       head = tmp;
+                       tail = tmp;
+               }
+               else
+               {
+                       tail->next = tmp;
+                       tail = tmp;
+               }
+
+               if (strcmp ("done", tmp->status) == 0)
+                       break;
+       } /* while (42) */
+       
        return (head);
 } /* }}} mt_reply_t *receive_reply */
 
@@ -626,6 +638,12 @@ static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
        printf ("login2_handler has been called.\n");
        reply_dump (r);
 
+       if (strcmp (r->status, "done") != 0)
+       {
+               mt_debug ("login2_handler: Unexpected status: %s.\n", r->status);
+               return (EPROTO);
+       }
+
        return (0);
 } /* }}} int login2_handler */
 
@@ -697,9 +715,22 @@ static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
        if (r == NULL)
                return (EINVAL);
 
+       /* The expected result looks like this:
+        * -- 8< --
+        *  !done 
+        *  =ret=ebddd18303a54111e2dea05a92ab46b4
+        * -- >8 --
+        */
+
        printf ("login_handler has been called.\n");
        reply_dump (r);
 
+       if (strcmp (r->status, "done") != 0)
+       {
+               mt_debug ("login_handler: Unexpected status: %s.\n", r->status);
+               return (EPROTO);
+       }
+
        login_data = user_data;
        if (login_data == NULL)
                return (EINVAL);