From bea1fd210b84e28583da9674ec1b9ee6b5e0f3f6 Mon Sep 17 00:00:00 2001 From: impact-eintr Date: Wed, 7 Dec 2022 21:28:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=BD=E9=9A=BE=20=E5=A5=BD=E9=9A=BE=20?= =?UTF-8?q?=E5=A5=BD=E9=9A=BE=20ListenLoop=E7=9A=84Sleeper=E4=B8=BA?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E6=B2=A1=E6=9C=89=E8=A2=AB=E5=94=A4=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 17 ----------------- sleep/sleep_unsafe.go | 2 ++ tcpip/buffer/prependable.go | 2 +- tcpip/network/ipv4/ipv4.go | 7 +++++-- tcpip/transport/tcp/accept.go | 6 ++++++ tcpip/transport/tcp/connect.go | 17 ++++++++--------- 6 files changed, 22 insertions(+), 29 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index f6d7322..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "intelliSenseMode": "windows-msvc-x64" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/sleep/sleep_unsafe.go b/sleep/sleep_unsafe.go index 0aca751..43478e4 100644 --- a/sleep/sleep_unsafe.go +++ b/sleep/sleep_unsafe.go @@ -233,7 +233,9 @@ func (s *Sleeper) Fetch(block bool) (id int, ok bool) { // Reassociate the waker with the sleeper. If the waker was // still asserted we can return it, otherwise try the next one. old := (*Sleeper)(atomic.SwapPointer(&w.s, usleeper(s))) + log.Println("Sleeper", unsafe.Pointer(s), "old", unsafe.Pointer(old), "&assertSleeper", unsafe.Pointer(&assertedSleeper), "w.id", w.id) if old == &assertedSleeper { + log.Println("成功返回 没有阻塞") return w.id, true } } diff --git a/tcpip/buffer/prependable.go b/tcpip/buffer/prependable.go index 02bdef6..ea1c8f9 100644 --- a/tcpip/buffer/prependable.go +++ b/tcpip/buffer/prependable.go @@ -23,7 +23,7 @@ func (p Prependable) UsedLength() int { return len(p.buf) - p.usedIdx } -// 从内到外暴露报文头的协议 eth|ipv4|tcp +// Prepend 向前扩展size个字节 func (p *Prependable) Prepend(size int) []byte { if size > p.usedIdx { return nil diff --git a/tcpip/network/ipv4/ipv4.go b/tcpip/network/ipv4/ipv4.go index cac91e0..5b0d8e6 100644 --- a/tcpip/network/ipv4/ipv4.go +++ b/tcpip/network/ipv4/ipv4.go @@ -110,8 +110,11 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload b if protocol == header.ICMPv4ProtocolNumber { log.Println("IP 写回ICMP报文", header.IPv4(append(ip, payload.ToView()...))) } else { - //log.Printf("send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol) - log.Println(header.IPv4(append(ip, payload.ToView()...))) + if payload.Size() == 0 { + log.Printf("send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol) + } else { + log.Println(header.IPv4(append(ip, payload.ToView()...))) + } } return e.linkEP.WritePacket(r, hdr, payload, ProtocolNumber) } diff --git a/tcpip/transport/tcp/accept.go b/tcpip/transport/tcp/accept.go index 3640967..88ac2e0 100644 --- a/tcpip/transport/tcp/accept.go +++ b/tcpip/transport/tcp/accept.go @@ -263,6 +263,7 @@ func (e *endpoint) handleSynSegment(ctx *listenContext, s *segment, opts *header // handleListenSegment is called when a listening endpoint receives a segment // and needs to handle it. func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) { + log.Println(s.flags) switch s.flags { case flagSyn: // syn报文处理 // 分析tcp选项 @@ -276,6 +277,8 @@ func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) { // 返回一个syn+ack报文 case flagFin: // fin报文处理 // 三次握手最后一次 ack 报文 + default: + panic(nil) } } @@ -306,6 +309,7 @@ func (e *endpoint) protocolListenLoop(rcvWnd seqnum.Size) *tcpip.Error { for { switch index, _ := s.Fetch(true); index { // Fetch(true) 阻塞获取 case wakerForNewSegment: + log.Println("新数据来咯 欸嘿嘿嘿") mayRequeue := true // 接收和处理tcp报文 for i := 0; i < maxSegmentsPerWake; i++ { @@ -325,6 +329,8 @@ func (e *endpoint) protocolListenLoop(rcvWnd seqnum.Size) *tcpip.Error { case wakerForNotification: // TODO 触发其他事件 log.Println("其他事件?") + default: + panic((nil)) } } } diff --git a/tcpip/transport/tcp/connect.go b/tcpip/transport/tcp/connect.go index d0618ed..30961e0 100644 --- a/tcpip/transport/tcp/connect.go +++ b/tcpip/transport/tcp/connect.go @@ -205,14 +205,14 @@ func (h *handshake) execute() *tcpip.Error { // 如果是客户端当发送 syn 报文,超过一定的时间未收到回包,触发超时重传 // 如果是服务端当发送 syn+ack 报文,超过一定的时间未收到 ack 回包,触发超时重传 // 超时时间变为上次的2倍 - timeOut *= 2 - if timeOut > 60*time.Second { - return tcpip.ErrTimeout - } - rt.Reset(timeOut) - // 重新发送syn报文 - sendSynTCP(&h.ep.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) - + //timeOut *= 2 + //if timeOut > 60*time.Second { + // return tcpip.ErrTimeout + //} + //rt.Reset(timeOut) + //// 重新发送syn报文 + //sendSynTCP(&h.ep.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) + log.Println("超时重发了 xdm") case wakerForNotification: case wakerForNewSegment: @@ -308,7 +308,6 @@ func sendSynTCP(r *stack.Route, id stack.TransportEndpointID, flags byte, // 发送一个tcp段数据,封装 tcp 首部,并写入网路层 func sendTCP(r *stack.Route, id stack.TransportEndpointID, data buffer.VectorisedView, ttl uint8, flags byte, seq, ack seqnum.Value, rcvWnd seqnum.Size, opts []byte) *tcpip.Error { - log.Println("进行一个报文的发送") optLen := len(opts) // Allocate a buffer for the TCP header. hdr := buffer.NewPrependable(header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen)