CRAS: rclient - Own buffer to message conversion

Make rclient handle the conversion of a raw client buffer to a client
message. Moving this in to rclient allows it to be tested more easily.

In particular a new fuzz tester will replace cras_server and throw
random data at cras_rclient's interface.

Change-Id: I399c339b2cb308875f093eba390a9520c518276a
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/679818
diff --git a/cras/src/server/cras_rclient.c b/cras/src/server/cras_rclient.c
index 6a68351..eb5966c 100644
--- a/cras/src/server/cras_rclient.c
+++ b/cras/src/server/cras_rclient.c
@@ -369,6 +369,22 @@
 
 /* Entry point for handling a message from the client.  Called from the main
  * server context. */
+int cras_rclient_buffer_from_client(struct cras_rclient *client,
+                                    const uint8_t *buf,
+                                    size_t buf_len,
+                                    int fd) {
+        struct cras_server_message *msg = (struct cras_server_message *)buf;
+
+        if (buf_len < sizeof(msg->length))
+                return -EINVAL;
+        if (msg->length != buf_len)
+                return -EINVAL;
+        cras_rclient_message_from_client(client, msg, fd);
+        return 0;
+}
+
+/* Entry point for handling a message from the client.  Called from the main
+ * server context. */
 int cras_rclient_message_from_client(struct cras_rclient *client,
 				     const struct cras_server_message *msg,
 				     int fd) {
diff --git a/cras/src/server/cras_rclient.h b/cras/src/server/cras_rclient.h
index e3733a6..af0457f 100644
--- a/cras/src/server/cras_rclient.h
+++ b/cras/src/server/cras_rclient.h
@@ -42,6 +42,22 @@
 				     const struct cras_server_message *msg,
 				     int fd);
 
+/* Handles a received buffer from the client.
+ * Args:
+ *    client - The client that received this message.
+ *    buf - The raw byte buffer the client sent. It should contain a valid
+ *      cras_server_message.
+ *    buf_len - The length of |buf|.
+ *    fd - The file descriptor that was sent by the remote client (or -1 if no
+ *         file descriptor was sent).
+ * Returns:
+ *    0 on success, otherwise a negative error code.
+ */
+int cras_rclient_buffer_from_client(struct cras_rclient *client,
+				    const uint8_t *buf,
+                                    size_t buf_len,
+                                    int fd);
+
 /* Sends a message to the client.
  * Args:
  *    client - The client to send the message to.
diff --git a/cras/src/server/cras_server.c b/cras/src/server/cras_server.c
index 4d2df29..12f0f1d 100644
--- a/cras/src/server/cras_server.c
+++ b/cras/src/server/cras_server.c
@@ -115,18 +115,15 @@
 static void handle_message_from_client(struct attached_client *client)
 {
 	uint8_t buf[CRAS_SERV_MAX_MSG_SIZE];
-	struct cras_server_message *msg;
 	int nread;
 	int fd;
 	unsigned int num_fds = 1;
 
-	msg = (struct cras_server_message *)buf;
 	nread = cras_recv_with_fds(client->fd, buf, sizeof(buf), &fd, &num_fds);
-	if (nread < sizeof(msg->length))
+        if (nread < 0)
+                goto read_error;
+        if (cras_rclient_buffer_from_client(client->client, buf, nread, fd) < 0)
 		goto read_error;
-	if (msg->length != nread)
-		goto read_error;
-	cras_rclient_message_from_client(client->client, msg, fd);
 	return;
 
 read_error: