From 227a871a04ace12f39ddb3b19db9bc892d2ed496 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 13 Jan 2022 15:25:03 +0300 Subject: [PATCH] [#367] session: Support `magic_number` field of `RequestMetaHeader` Signed-off-by: Leonard Lyubich --- session/convert.go | 2 ++ session/grpc/types.go | 16 ++++++++++++++++ session/marshal.go | 11 ++++++++++- session/test/generate.go | 1 + session/types.go | 18 ++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/session/convert.go b/session/convert.go index df78c9eb..5562fffb 100644 --- a/session/convert.go +++ b/session/convert.go @@ -389,6 +389,7 @@ func (r *RequestMetaHeader) ToGRPCMessage() grpc.Message { m.SetEpoch(r.epoch) m.SetTtl(r.ttl) m.SetOrigin(r.origin.ToGRPCMessage().(*session.RequestMetaHeader)) + m.SetNetworkMagic(r.netMagic) } return m @@ -465,6 +466,7 @@ func (r *RequestMetaHeader) FromGRPCMessage(m grpc.Message) error { r.epoch = v.GetEpoch() r.ttl = v.GetTtl() + r.netMagic = v.GetNetworkMagic() return nil } diff --git a/session/grpc/types.go b/session/grpc/types.go index 07d5d13f..484203d4 100644 --- a/session/grpc/types.go +++ b/session/grpc/types.go @@ -185,6 +185,22 @@ func (m *RequestMetaHeader) SetOrigin(v *RequestMetaHeader) { } } +// GetNetworkMagic returns NeoFS network magic. +func (m *RequestMetaHeader) GetNetworkMagic() uint64 { + if m != nil { + return m.MagicNumber + } + + return 0 +} + +// SetNetworkMagic sets NeoFS network magic. +func (m *RequestMetaHeader) SetNetworkMagic(v uint64) { + if m != nil { + m.MagicNumber = v + } +} + // SetVersion sets server protocol version. func (m *ResponseMetaHeader) SetVersion(v *refs.Version) { if m != nil { diff --git a/session/marshal.go b/session/marshal.go index 9513d4c1..f28615c7 100644 --- a/session/marshal.go +++ b/session/marshal.go @@ -41,6 +41,7 @@ const ( reqMetaHeaderSessionTokenField = 5 reqMetaHeaderBearerTokenField = 6 reqMetaHeaderOriginField = 7 + reqMetaHeaderNetMagicField = 8 reqVerifHeaderBodySignatureField = 1 reqVerifHeaderMetaSignatureField = 2 @@ -564,7 +565,14 @@ func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) { offset += n - _, err = proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin) + n, err = proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin) + if err != nil { + return nil, err + } + + offset += n + + _, err = proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf[offset:], r.netMagic) if err != nil { return nil, err } @@ -591,6 +599,7 @@ func (r *RequestMetaHeader) StableSize() (size int) { size += proto.NestedStructureSize(reqMetaHeaderSessionTokenField, r.sessionToken) size += proto.NestedStructureSize(reqMetaHeaderBearerTokenField, r.bearerToken) size += proto.NestedStructureSize(reqMetaHeaderOriginField, r.origin) + size += proto.UInt64Size(reqMetaHeaderNetMagicField, r.netMagic) return size } diff --git a/session/test/generate.go b/session/test/generate.go index 8473d806..d8095925 100644 --- a/session/test/generate.go +++ b/session/test/generate.go @@ -130,6 +130,7 @@ func generateRequestMetaHeader(empty, withOrigin bool) *session.RequestMetaHeade if !empty { m.SetEpoch(13) m.SetTTL(100) + m.SetNetworkMagic(1337) } m.SetXHeaders(GenerateXHeaders(empty)) diff --git a/session/types.go b/session/types.go index 8a978176..26fa26ed 100644 --- a/session/types.go +++ b/session/types.go @@ -88,6 +88,8 @@ type RequestMetaHeader struct { bearerToken *acl.BearerToken origin *RequestMetaHeader + + netMagic uint64 } type ResponseVerificationHeader struct { @@ -443,6 +445,22 @@ func (r *RequestMetaHeader) SetOrigin(v *RequestMetaHeader) { } } +// GetNetworkMagic returns NeoFS network magic. +func (r *RequestMetaHeader) GetNetworkMagic() uint64 { + if r != nil { + return r.netMagic + } + + return 0 +} + +// SetNetworkMagic sets NeoFS network magic. +func (r *RequestMetaHeader) SetNetworkMagic(v uint64) { + if r != nil { + r.netMagic = v + } +} + func (l *TokenLifetime) GetExp() uint64 { if l != nil { return l.exp