gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/credentials/alts/internal/conn/utils.go (about) 1 /* 2 * 3 * Copyright 2018 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package conn 20 21 import core "gitee.com/ks-custle/core-gm/grpc/credentials/alts/internal" 22 23 // NewOutCounter returns an outgoing counter initialized to the starting sequence 24 // number for the client/server side of a connection. 25 func NewOutCounter(s core.Side, overflowLen int) (c Counter) { 26 c.overflowLen = overflowLen 27 if s == core.ServerSide { 28 // Server counters in ALTS record have the little-endian high bit 29 // set. 30 c.value[counterLen-1] = 0x80 31 } 32 return 33 } 34 35 // NewInCounter returns an incoming counter initialized to the starting sequence 36 // number for the client/server side of a connection. This is used in ALTS record 37 // to check that incoming counters are as expected, since ALTS record guarantees 38 // that messages are unwrapped in the same order that the peer wrapped them. 39 func NewInCounter(s core.Side, overflowLen int) (c Counter) { 40 c.overflowLen = overflowLen 41 if s == core.ClientSide { 42 // Server counters in ALTS record have the little-endian high bit 43 // set. 44 c.value[counterLen-1] = 0x80 45 } 46 return 47 } 48 49 // CounterFromValue creates a new counter given an initial value. 50 func CounterFromValue(value []byte, overflowLen int) (c Counter) { 51 c.overflowLen = overflowLen 52 copy(c.value[:], value) 53 return 54 } 55 56 // CounterSide returns the connection side (client/server) a sequence counter is 57 // associated with. 58 func CounterSide(c []byte) core.Side { 59 if c[counterLen-1]&0x80 == 0x80 { 60 return core.ServerSide 61 } 62 return core.ClientSide 63 }