aboutsummaryrefslogtreecommitdiff
path: root/net/net.c
diff options
context:
space:
mode:
authorZhang Chen <zhangchen.fnst@cn.fujitsu.com>2016-05-13 15:35:19 +0800
committerJason Wang <jasowang@redhat.com>2016-06-01 09:25:29 +0800
commit16a3df403b10c4ac347159e39005fd520b2648bb (patch)
tree50ade2b78e5758951e34c077ae207393fe95813f /net/net.c
parentd30300f771bffc3964e86472e0c21607521a2e24 (diff)
net/net: Add SocketReadState for reuse codes
This function is from net/socket.c, move it to net.c and net.h. Add SocketReadState to make others reuse net_fill_rstate(). suggestion from jason. v4: - move 'rs->finalize = finalize' to rs_init() v3: - remove SocketReadState init callback - put finalize callback to net_fill_rstate() v2: - rename ReadState to SocketReadState - add SocketReadState init and finalize callback v1: - init patch Signed-off-by: Zhang Chen <zhangchen.fnst@cn.fujitsu.com> Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com> Signed-off-by: Wen Congyang <wency@cn.fujitsu.com> Signed-off-by: Jason Wang <jasowang@redhat.com>
Diffstat (limited to 'net/net.c')
-rw-r--r--net/net.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/net/net.c b/net/net.c
index 1680b680e1..5f3e5a9ff5 100644
--- a/net/net.c
+++ b/net/net.c
@@ -1550,3 +1550,73 @@ QemuOptsList qemu_net_opts = {
{ /* end of list */ }
},
};
+
+void net_socket_rs_init(SocketReadState *rs,
+ SocketReadStateFinalize *finalize)
+{
+ rs->state = 0;
+ rs->index = 0;
+ rs->packet_len = 0;
+ memset(rs->buf, 0, sizeof(rs->buf));
+ rs->finalize = finalize;
+}
+
+/*
+ * Returns
+ * 0: SocketReadState is not ready
+ * 1: SocketReadState is ready
+ * otherwise error occurs
+ */
+int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
+{
+ unsigned int l;
+
+ while (size > 0) {
+ /* reassemble a packet from the network */
+ switch (rs->state) { /* 0 = getting length, 1 = getting data */
+ case 0:
+ l = 4 - rs->index;
+ if (l > size) {
+ l = size;
+ }
+ memcpy(rs->buf + rs->index, buf, l);
+ buf += l;
+ size -= l;
+ rs->index += l;
+ if (rs->index == 4) {
+ /* got length */
+ rs->packet_len = ntohl(*(uint32_t *)rs->buf);
+ rs->index = 0;
+ rs->state = 1;
+ }
+ break;
+ case 1:
+ l = rs->packet_len - rs->index;
+ if (l > size) {
+ l = size;
+ }
+ if (rs->index + l <= sizeof(rs->buf)) {
+ memcpy(rs->buf + rs->index, buf, l);
+ } else {
+ fprintf(stderr, "serious error: oversized packet received,"
+ "connection terminated.\n");
+ rs->index = rs->state = 0;
+ return -1;
+ }
+
+ rs->index += l;
+ buf += l;
+ size -= l;
+ if (rs->index >= rs->packet_len) {
+ rs->index = 0;
+ rs->state = 0;
+ if (rs->finalize) {
+ rs->finalize(rs);
+ }
+ return 1;
+ }
+ break;
+ }
+ }
+ return 0;
+}