return (0);
} /* }}} int reply_add_keyval */
+static void reply_dump (const mt_reply_t *r) /* {{{ */
+{
+ if (r == NULL)
+ return;
+
+ printf ("=== BEGIN REPLY ===\n"
+ "Address: %p\n"
+ "Status: %s\n",
+ (void *) r, r->status);
+ if (r->params_num > 0)
+ {
+ unsigned int i;
+
+ printf ("Arguments:\n");
+ for (i = 0; i < r->params_num; i++)
+ printf (" %3u: %s = %s\n", i, r->keys[i], r->values[i]);
+ }
+ if (r->next != NULL)
+ printf ("Next: %p\n", (void *) r->next);
+ printf ("=== END REPLY ===\n");
+
+ reply_dump (r->next);
+} /* }}} void reply_dump */
+
static void reply_free (mt_reply_t *r) /* {{{ */
{
mt_reply_t *next;
if (status != 0)
return (status);
+ mt_debug ("send_command: command = %s;\n", command);
status = buffer_add (&buffer_ptr, &buffer_size, command);
if (status != 0)
return (status);
if (args[i] == NULL)
return (EINVAL);
+ mt_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
if (status != 0)
return (status);
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];
}
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)
{
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] == '=') /* {{{ */
{
*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 */
return (EINVAL);
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 */
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)