diff --git a/router/BUILD.bazel b/router/BUILD.bazel index e0c562ce8e..f7fee76cd7 100644 --- a/router/BUILD.bazel +++ b/router/BUILD.bazel @@ -5,9 +5,9 @@ go_library( srcs = [ "connector.go", "dataplane.go", - "serializeProxy.go", "fnv1aCheap.go", "metrics.go", + "serializeProxy.go", "svc.go", ], importpath = "github.com/scionproto/scion/router", diff --git a/router/dataplane.go b/router/dataplane.go index 2ce5a18740..57cb9ee108 100644 --- a/router/dataplane.go +++ b/router/dataplane.go @@ -2288,15 +2288,15 @@ func decodeSCMP(scmp *slayers.SCMP) ([]gopacket.SerializableLayer, error) { } // updateSCIONLayer rewrites the SCION header at the start of the given raw packet buffer; replacing -// it with the serialization of the given new SCION header. This works only if the new header -// is of the same size as the old one. This function has no knowledge of the actual size of the -// headers; it only ensures that the new one ends exactly where the old one did. It is possible to -// use this function to replace a header with a smaller one; but the preceding headers, and the -// start of the slice must be corrected afterwards. +// it with the serialization of the given new SCION header. This works only if the new header is of +// the same size as the old one. This function has no knowledge of the actual size of the headers; +// it only ensures that the new one ends exactly where the old one did. It is possible to use this +// function to replace a header with a smaller one; but the rawPacket's slice must be fixed +// afterwards (and the preceding headers, if any). func updateSCIONLayer(rawPkt []byte, s slayers.SCION) error { payloadOffset := len(rawPkt) - len(s.LayerPayload()) serBuf := newSerializeProxy(rawPkt) - serBuf.clear(payloadOffset) // So, prepending just in front of the payload. Better not append! + serBuf.clear(payloadOffset) // Prepends will go just before payload. (Appends will wreck it) return s.SerializeTo(&serBuf, gopacket.SerializeOptions{}) } @@ -2403,7 +2403,7 @@ func (b *bfdSend) Send(bfd *layers.BFD) error { return err } - // The usefull part of the buffer is given by Bytes. We don't copy the bytes; just the slice's + // The useful part of the buffer is given by Bytes. We don't copy the bytes; just the slice's // metadata. p.rawPacket = serBuf.Bytes() if count < 10 { diff --git a/router/serializeProxy.go b/router/serializeProxy.go index e146d03298..96a355301d 100644 --- a/router/serializeProxy.go +++ b/router/serializeProxy.go @@ -27,7 +27,7 @@ import ( type serializeProxy struct { initStart int // Initial starting point (where the first prepend or append occurs) data []byte - start int // data[0] == buf[0] (bc changing it is one way), so we keep track of the real start. + start int // The slice's offset can't change (irreversible). So keep track separately. layers []gopacket.LayerType } @@ -45,15 +45,15 @@ func newSerializeProxy(buf []byte) serializeProxy { // Resets the buffer to empty and sets the initial prepend/append point to the given position. func (s *serializeProxy) clear(newStart int) { s.initStart = newStart - s.Clear() + s.start = newStart + s.data = s.data[:newStart] + s.layers = s.layers[:0] } -// Implements serializeBuffer.Clear(). Never returns an error. +// Implements serializeBuffer.Clear(). This implementation never returns an error. // The initial prepend/append point is reset to that which was set by the last call to clear(). func (s *serializeProxy) Clear() error { - s.start = s.initStart - s.data = s.data[:s.start] - s.layers = s.layers[:0] + s.clear(s.initStart) return nil }