diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2021-01-23 10:45:11 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2021-01-23 21:43:01 +0100 |
commit | dd357236f156b3be8cc8e4de2787e4ba08f3c68c (patch) | |
tree | ec6ef6458910064d4d9ddfae553d14cc084afd2d /pkg/tcpip/sample | |
parent | 9acc1a73ccb686afeaf02dec521311235745ad26 (diff) |
fix indent
Diffstat (limited to 'pkg/tcpip/sample')
-rw-r--r-- | pkg/tcpip/sample/tun_tcp_connect/main.go | 184 |
1 files changed, 92 insertions, 92 deletions
diff --git a/pkg/tcpip/sample/tun_tcp_connect/main.go b/pkg/tcpip/sample/tun_tcp_connect/main.go index 06ee5110b..762c6dc29 100644 --- a/pkg/tcpip/sample/tun_tcp_connect/main.go +++ b/pkg/tcpip/sample/tun_tcp_connect/main.go @@ -41,7 +41,7 @@ package main import ( - "context" + "context" "bufio" "fmt" "log" @@ -99,71 +99,71 @@ func writer(ch chan struct{}, ep tcpip.Endpoint) { } type GrePacketInfo struct { - pi *channel.PacketInfo + pi *channel.PacketInfo } func (g GrePacketInfo) FullPayload() ([]byte, *tcpip.Error){ - log.Println("FullPayload") - pkt := g.pi.Pkt - size := pkt.Size() - vv := buffer.NewVectorisedView(size, pkt.Views()) - var buf = make([]byte, size, size) - pos := 0 - for { - copied, err := vv.Read(buf[pos:]) - log.Printf("VectorisedView Read: %d %d %d %v", size, pos, copied, err) - if err != nil { - return nil, tcpip.ErrBadBuffer - } - pos = pos + copied - if pos == size { - break - } - } - - log.Printf("FullPayload return: %d %v", len(buf), buf) - return buf, nil + log.Println("FullPayload") + pkt := g.pi.Pkt + size := pkt.Size() + vv := buffer.NewVectorisedView(size, pkt.Views()) + var buf = make([]byte, size, size) + pos := 0 + for { + copied, err := vv.Read(buf[pos:]) + log.Printf("VectorisedView Read: %d %d %d %v", size, pos, copied, err) + if err != nil { + return nil, tcpip.ErrBadBuffer + } + pos = pos + copied + if pos == size { + break + } + } + + log.Printf("FullPayload return: %d %v", len(buf), buf) + return buf, nil } func (g GrePacketInfo) Payload(size int) ([]byte, *tcpip.Error){ - log.Println("Payload") - return nil, tcpip.ErrNotSupported + log.Println("Payload") + return nil, tcpip.ErrNotSupported } type GreHandlerInfo struct { - ChEp *channel.Endpoint - Raw tcpip.Endpoint - Raddr tcpip.FullAddress + ChEp *channel.Endpoint + Raw tcpip.Endpoint + Raddr tcpip.FullAddress } func (info *GreHandlerInfo) greHandler(req *gre.ForwarderRequest) { - pkt := req.Pkt - log.Println("greHandler: ", req.Pkt.Size(), req.Pkt.Views()) - greHdr := header.GRE(pkt.TransportHeader().View()) - proto := greHdr.ProtocolType() - views := pkt.Data.Views() - size := pkt.Data.Size() - data := buffer.NewVectorisedView(size, views) + pkt := req.Pkt + log.Println("greHandler: ", req.Pkt.Size(), req.Pkt.Views()) + greHdr := header.GRE(pkt.TransportHeader().View()) + proto := greHdr.ProtocolType() + views := pkt.Data.Views() + size := pkt.Data.Size() + data := buffer.NewVectorisedView(size, views) newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ Data: data, }) - log.Printf("greHandler proto: %d cloned: %v", proto, newPkt.Views()) + log.Printf("greHandler proto: %d cloned: %v", proto, newPkt.Views()) - info.ChEp.InjectInbound(proto, newPkt) + info.ChEp.InjectInbound(proto, newPkt) } func (info *GreHandlerInfo) greRead(ep *channel.Endpoint) { - for { - pi, err := ep.ReadContext(context.Background()); - linkHdr := pi.Pkt.LinkHeader() - greHdr := header.GRE(linkHdr.Push(header.GREHeaderSize)) - greHdr.SetProtocolType(pi.Proto) - log.Printf("greRead %d %v %v %v", pi.Proto, pi, err, greHdr) - opts := tcpip.WriteOptions{ - //To: &info.Raddr - } - info.Raw.Write(GrePacketInfo{&pi}, opts) - } + for { + pi, err := ep.ReadContext(context.Background()); + linkHdr := pi.Pkt.LinkHeader() + greHdr := header.GRE(linkHdr.Push(header.GREHeaderSize)) + greHdr.SetProtocolType(pi.Proto) + log.Printf("greRead %d %v %v %v", pi.Proto, pi, err, greHdr) + opts := tcpip.WriteOptions{ + //To: &info.Raddr + } + info.Raw.Write(GrePacketInfo{&pi}, opts) + } } func main() { @@ -176,8 +176,8 @@ func main() { portName := os.Args[3] remoteAddrName := os.Args[4] remotePortName := os.Args[5] - greAddrName := os.Args[6] - greRemoteAddrName := os.Args[7] + greAddrName := os.Args[6] + greRemoteAddrName := os.Args[7] rand.Seed(time.Now().UnixNano()) @@ -189,7 +189,7 @@ func main() { Addr: tcpip.Address(net.ParseIP(remoteAddrName).To4()), } - log.Printf("local:%v remote:%v", addr, remote) + log.Printf("local:%v remote:%v", addr, remote) var localPort uint16 if v, err := strconv.Atoi(portName); err != nil { @@ -239,48 +239,48 @@ func main() { if tcperr != nil { log.Fatal(tcperr) } - log.Println("EP: %s", rawEp) - - raddr := greRemoteAddr - fraddr := tcpip.FullAddress{NIC: 1, Addr: raddr} - fgreAddr := tcpip.FullAddress{NIC: 1, Addr: greAddr} - tcperr = rawEp.Bind(fgreAddr) - if tcperr != nil { - log.Fatal(tcperr) - } - log.Printf("Remote: %v %v", raddr, fraddr) - - // Create GRE - greEP := grelink.New(mtu - 24) - chEP := greEP.GetChannel() - // TODO detect IPv4/IPv6 - greEP.NMaxHeaderLength = header.IPv6FixedHeaderSize + header.GREHeaderSize - - greInfo := GreHandlerInfo{ -// Ep: loEP, - ChEp: chEP, - Raw: rawEp, - Raddr: fraddr, - } - greFwd := gre.NewForwarder(s, greInfo.greHandler) - s.SetTransportProtocolHandler(header.GREProtocolNumber, greFwd.HandlePacket) - - go greInfo.greRead(chEP) - //greEP.Bind(s, greIP) + log.Println("EP: %s", rawEp) + + raddr := greRemoteAddr + fraddr := tcpip.FullAddress{NIC: 1, Addr: raddr} + fgreAddr := tcpip.FullAddress{NIC: 1, Addr: greAddr} + tcperr = rawEp.Bind(fgreAddr) + if tcperr != nil { + log.Fatal(tcperr) + } + log.Printf("Remote: %v %v", raddr, fraddr) + + // Create GRE + greEP := grelink.New(mtu - 24) + chEP := greEP.GetChannel() + // TODO detect IPv4/IPv6 + greEP.NMaxHeaderLength = header.IPv6FixedHeaderSize + header.GREHeaderSize + + greInfo := GreHandlerInfo{ +// Ep: loEP, + ChEp: chEP, + Raw: rawEp, + Raddr: fraddr, + } + greFwd := gre.NewForwarder(s, greInfo.greHandler) + s.SetTransportProtocolHandler(header.GREProtocolNumber, greFwd.HandlePacket) + + go greInfo.greRead(chEP) + //greEP.Bind(s, greIP) if err := s.CreateNIC(2, sniffer.New(greEP)); err != nil { log.Fatal(err) - } + } if err := s.AddAddress(2, ipv4.ProtocolNumber, addr); err != nil { log.Fatal(err) } // Add default route. - subnet, err := tcpip.NewSubnet( - tcpip.Address(net.ParseIP("10.0.0.0").To4()), - tcpip.AddressMask(net.ParseIP("255.255.255.0").To4())) - if err != nil { - panic(err) - } + subnet, err := tcpip.NewSubnet( + tcpip.Address(net.ParseIP("10.0.0.0").To4()), + tcpip.AddressMask(net.ParseIP("255.255.255.0").To4())) + if err != nil { + panic(err) + } s.SetRouteTable([]tcpip.Route{ { Destination: subnet, @@ -291,12 +291,12 @@ func main() { NIC: 2, }, }) - log.Printf("Nics enabled 1:%v 2:%v 3:%v", s.CheckNIC(1), s.CheckNIC(2), s.CheckNIC(3)) + log.Printf("Nics enabled 1:%v 2:%v 3:%v", s.CheckNIC(1), s.CheckNIC(2), s.CheckNIC(3)) - tcperr = rawEp.Connect(fraddr) - if tcperr != nil { - log.Fatal(tcperr) - } + tcperr = rawEp.Connect(fraddr) + if tcperr != nil { + log.Fatal(tcperr) + } // Create TCP endpoint. var wq waiter.Queue @@ -331,8 +331,8 @@ func main() { fmt.Println("Connected") - payload := tcpip.SlicePayload([]byte("GET / HTTP/1.0\r\nHost: www.m7n.se\r\n\r\n")) - ep.Write(payload, tcpip.WriteOptions{}) + payload := tcpip.SlicePayload([]byte("GET / HTTP/1.0\r\nHost: www.m7n.se\r\n\r\n")) + ep.Write(payload, tcpip.WriteOptions{}) // Start the writer in its own goroutine. writerCompletedCh := make(chan struct{}) |