| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package pipe |
| 18 |
|
| 19 |
import ( |
| 20 |
"inet.af/netstack/tcpip" |
| 21 |
"inet.af/netstack/tcpip/buffer" |
| 22 |
"inet.af/netstack/tcpip/header" |
| 23 |
"inet.af/netstack/tcpip/stack" |
| 24 |
) |
| 25 |
|
| 26 |
var _ stack.LinkEndpoint = (*Endpoint)(nil) |
| 27 |
|
| 28 |
|
| 29 |
func New(linkAddr1, linkAddr2 tcpip.LinkAddress) (*Endpoint, *Endpoint) { |
| 30 |
ep1 := &Endpoint{ |
| 31 |
linkAddr: linkAddr1, |
| 32 |
} |
| 33 |
ep2 := &Endpoint{ |
| 34 |
linkAddr: linkAddr2, |
| 35 |
} |
| 36 |
ep1.linked = ep2 |
| 37 |
ep2.linked = ep1 |
| 38 |
return ep1, ep2 |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
type Endpoint struct { |
| 43 |
dispatcher stack.NetworkDispatcher |
| 44 |
linked *Endpoint |
| 45 |
linkAddr tcpip.LinkAddress |
| 46 |
} |
| 47 |
|
| 48 |
func (e *Endpoint) deliverPackets(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkts stack.PacketBufferList) { |
| 49 |
if !e.linked.IsAttached() { |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() { |
| 62 |
newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 63 |
Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()), |
| 64 |
}) |
| 65 |
e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress , r.RemoteLinkAddress , proto, newPkt) |
| 66 |
newPkt.DecRef() |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (e *Endpoint) WritePacket(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { |
| 72 |
var pkts stack.PacketBufferList |
| 73 |
pkts.PushBack(pkt) |
| 74 |
e.deliverPackets(r, proto, pkts) |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) { |
| 80 |
n := pkts.Len() |
| 81 |
e.deliverPackets(r, proto, pkts) |
| 82 |
return n, nil |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (e *Endpoint) Attach(dispatcher stack.NetworkDispatcher) { |
| 87 |
e.dispatcher = dispatcher |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (e *Endpoint) IsAttached() bool { |
| 92 |
return e.dispatcher != nil |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (*Endpoint) Wait() {} |
| 97 |
|
| 98 |
|
| 99 |
func (*Endpoint) MTU() uint32 { |
| 100 |
return header.IPv6MinimumMTU |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func (*Endpoint) Capabilities() stack.LinkEndpointCapabilities { |
| 105 |
return 0 |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func (*Endpoint) MaxHeaderLength() uint16 { |
| 110 |
return 0 |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func (e *Endpoint) LinkAddress() tcpip.LinkAddress { |
| 115 |
return e.linkAddr |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (*Endpoint) ARPHardwareType() header.ARPHardwareType { |
| 120 |
return header.ARPHardwareNone |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (*Endpoint) AddHeader(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) { |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error { |
| 129 |
return e.WritePacket(stack.RouteInfo{}, 0, pkt) |
| 130 |
} |