mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-30 22:03:54 -06:00
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>
This commit is contained in:
parent
d30300f771
commit
16a3df403b
4 changed files with 119 additions and 103 deletions
70
net/net.c
70
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue