|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/encodeous/nylon/polyamide/conn" |
| 5 | + "github.com/encodeous/nylon/polyamide/device" |
| 6 | + "github.com/encodeous/nylon/protocol" |
| 7 | + "github.com/encodeous/nylon/state" |
| 8 | + "google.golang.org/protobuf/proto" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + NyProtoId = 8 |
| 13 | + NyPriority = 10 |
| 14 | +) |
| 15 | + |
| 16 | +// polyamide traffic control for nylon |
| 17 | + |
| 18 | +func (n *Nylon) InstallTC() { |
| 19 | + // bounce back packets if using system routing |
| 20 | + if n.env.UseSystemRouting { |
| 21 | + n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) { |
| 22 | + if packet.Incoming() { |
| 23 | + // bounce incoming packets |
| 24 | + return device.TcBounce, nil |
| 25 | + } |
| 26 | + return device.TcPass, nil |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + // bounce back packets destined for the current node |
| 31 | + n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) { |
| 32 | + if n.env.GetNode(n.env.Id).Address == packet.GetDst() { |
| 33 | + //dev.Log.Verbosef("BounceCur packet: %v -> %v", packet.GetSrc(), packet.GetDst()) |
| 34 | + return device.TcBounce, nil |
| 35 | + } |
| 36 | + return device.TcPass, nil |
| 37 | + }) |
| 38 | + |
| 39 | + // handle incoming nylon packets |
| 40 | + n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) { |
| 41 | + if packet.Incoming() && packet.GetIPVersion() == NyProtoId { |
| 42 | + n.handleNylonPacket(packet.Payload(), packet.FromEp, packet.FromPeer) |
| 43 | + return device.TcDrop, nil |
| 44 | + } |
| 45 | + return device.TcPass, nil |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func (n *Nylon) SendNylon(pkt proto.Message, endpoint conn.Endpoint, peer *device.Peer) error { |
| 50 | + tce := n.Device.NewTCElement() |
| 51 | + offset := device.MessageTransportOffsetContent + device.PolyHeaderSize |
| 52 | + buf, err := proto.MarshalOptions{ |
| 53 | + Deterministic: true, |
| 54 | + }.MarshalAppend(tce.Buffer[offset:offset], pkt) |
| 55 | + if err != nil { |
| 56 | + n.Device.PutMessageBuffer(tce.Buffer) |
| 57 | + n.Device.PutTCElement(tce) |
| 58 | + return err |
| 59 | + } |
| 60 | + tce.InitPacket(NyProtoId, uint16(len(buf)+device.PolyHeaderSize)) |
| 61 | + tce.Priority = device.TcHighPriority |
| 62 | + |
| 63 | + tce.ToEp = endpoint |
| 64 | + tce.ToPeer = peer |
| 65 | + |
| 66 | + // TODO: Optimize? is it worth it? |
| 67 | + |
| 68 | + tcs := device.NewTCState() |
| 69 | + |
| 70 | + n.Device.TCBatch([]*device.TCElement{tce}, tcs) |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func (n *Nylon) handleNylonPacket(packet []byte, endpoint conn.Endpoint, peer *device.Peer) { |
| 75 | + pkt := &protocol.Ny{} |
| 76 | + err := proto.Unmarshal(packet, pkt) |
| 77 | + if err != nil { |
| 78 | + // log skipped message |
| 79 | + n.env.Log.Debug("Failed to unmarshal packet", "err", err) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + e := n.env |
| 84 | + |
| 85 | + neigh := e.FindNodeBy(state.NyPublicKey(peer.GetPublicKey())) |
| 86 | + if neigh == nil { |
| 87 | + // this should not be possible |
| 88 | + panic("impossible state, peer added, but not a node in the network") |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + switch pkt.Type.(type) { |
| 93 | + case *protocol.Ny_SeqnoRequestOp: |
| 94 | + e.Dispatch(func(s *state.State) error { |
| 95 | + return routerHandleSeqnoRequest(s, *neigh, pkt.GetSeqnoRequestOp()) |
| 96 | + }) |
| 97 | + case *protocol.Ny_RouteOp: |
| 98 | + e.Dispatch(func(s *state.State) error { |
| 99 | + return routerHandleRouteUpdate(s, *neigh, pkt.GetRouteOp()) |
| 100 | + }) |
| 101 | + case *protocol.Ny_ProbeOp: |
| 102 | + handleProbe(n, pkt.GetProbeOp(), endpoint, peer, *neigh) |
| 103 | + } |
| 104 | + defer func() { |
| 105 | + err := recover() |
| 106 | + if err != nil { |
| 107 | + n.env.Log.Error("panic while handling poly socket: %v", err) |
| 108 | + } |
| 109 | + }() |
| 110 | +} |
0 commit comments