Skip to content

Commit

Permalink
🐛 TCP 拆包后数据错误问题 (StevenBaby#48)
Browse files Browse the repository at this point in the history
* 🐛 复现TCP拆包后数据错误

* 🐛 修复tcp拆包后数据错误问题

* 🐛 变量命名,代码格式化
  • Loading branch information
znyinyyniu authored Oct 8, 2023
1 parent 6113876 commit 6f13b29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/net/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ static int tcp_sendmsg(socket_t *s, msghdr_t *msg, u32 flags)
continue;

int len = left < iov->size ? left : iov->size;
tcp_enqueue(pcb, iov->base, left, flags);
tcp_enqueue(pcb, iov->base, len, flags);
left -= len;
}
tcp_output(pcb);
Expand Down
8 changes: 6 additions & 2 deletions src/net/tcp_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ err_t tcp_enqueue(tcp_pcb_t *pcb, void *data, size_t size, int flags)
ip_t *ip;
tcp_t *tcp;
pbuf_t *pbuf;
void *ptr = data;

if (pcb->snd_mss < TCP_DEFAULT_MSS)
pcb->snd_mss = TCP_DEFAULT_MSS;
Expand Down Expand Up @@ -83,8 +84,11 @@ err_t tcp_enqueue(tcp_pcb_t *pcb, void *data, size_t size, int flags)
int seglen = (pcb->snd_mss - pbuf->size);
int len = left < seglen ? left : seglen;

if (left > 0)
memcpy(pbuf->data + pbuf->size, data, len);
if (len > 0)
{
memcpy(pbuf->data + pbuf->size, ptr, len);
ptr += len;
}

pcb->snd_nbb += len;
pbuf->size += len;
Expand Down

0 comments on commit 6f13b29

Please sign in to comment.