| // Copyright 2018 The Netstack Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // Package channel provides the implemention of channel-based data-link layer |
| // endpoints. Such endpoints allow injection of inbound packets and store |
| // outbound packets in a channel. |
| package bufwritingchannel |
| |
| import ( |
| "github.com/google/netstack/tcpip" |
| "github.com/google/netstack/tcpip/buffer" |
| "github.com/google/netstack/tcpip/link/channel" |
| "github.com/google/netstack/tcpip/stack" |
| ) |
| |
| // Endpoint is link layer endpoint that stores outbound packets in a channel |
| // and allows injection of inbound packets. |
| type Endpoint struct { |
| channel.Endpoint |
| } |
| |
| // New creates a new channel endpoint. |
| func New(size int, mtu uint32, linkAddr tcpip.LinkAddress) (tcpip.LinkEndpointID, *Endpoint) { |
| _, ce := channel.New(size, mtu, linkAddr) |
| |
| e := &Endpoint{ |
| Endpoint: *ce, |
| } |
| |
| return stack.RegisterLinkEndpoint(e), e |
| } |
| |
| func (e *Endpoint) WriteBuffer(_ *stack.Route, payload *buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error { |
| p := channel.PacketInfo{ |
| Proto: protocol, |
| } |
| |
| if payload != nil { |
| p.Payload = payload.ToView() |
| } |
| |
| select { |
| case e.C <- p: |
| default: |
| } |
| |
| return nil |
| } |