You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

64 lines
2.0 KiB

  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. package conn
  19. import core "google.golang.org/grpc/credentials/alts/internal"
  20. // NewOutCounter returns an outgoing counter initialized to the starting sequence
  21. // number for the client/server side of a connection.
  22. func NewOutCounter(s core.Side, overflowLen int) (c Counter) {
  23. c.overflowLen = overflowLen
  24. if s == core.ServerSide {
  25. // Server counters in ALTS record have the little-endian high bit
  26. // set.
  27. c.value[counterLen-1] = 0x80
  28. }
  29. return
  30. }
  31. // NewInCounter returns an incoming counter initialized to the starting sequence
  32. // number for the client/server side of a connection. This is used in ALTS record
  33. // to check that incoming counters are as expected, since ALTS record guarantees
  34. // that messages are unwrapped in the same order that the peer wrapped them.
  35. func NewInCounter(s core.Side, overflowLen int) (c Counter) {
  36. c.overflowLen = overflowLen
  37. if s == core.ClientSide {
  38. // Server counters in ALTS record have the little-endian high bit
  39. // set.
  40. c.value[counterLen-1] = 0x80
  41. }
  42. return
  43. }
  44. // CounterFromValue creates a new counter given an initial value.
  45. func CounterFromValue(value []byte, overflowLen int) (c Counter) {
  46. c.overflowLen = overflowLen
  47. copy(c.value[:], value)
  48. return
  49. }
  50. // CounterSide returns the connection side (client/server) a sequence counter is
  51. // associated with.
  52. func CounterSide(c []byte) core.Side {
  53. if c[counterLen-1]&0x80 == 0x80 {
  54. return core.ServerSide
  55. }
  56. return core.ClientSide
  57. }