common.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 (
  20. "encoding/binary"
  21. "errors"
  22. "fmt"
  23. )
  24. const (
  25. // GcmTagSize is the GCM tag size is the difference in length between
  26. // plaintext and ciphertext. From crypto/cipher/gcm.go in Go crypto
  27. // library.
  28. GcmTagSize = 16
  29. )
  30. // ErrAuth occurs on authentication failure.
  31. var ErrAuth = errors.New("message authentication failed")
  32. // SliceForAppend takes a slice and a requested number of bytes. It returns a
  33. // slice with the contents of the given slice followed by that many bytes and a
  34. // second slice that aliases into it and contains only the extra bytes. If the
  35. // original slice has sufficient capacity then no allocation is performed.
  36. func SliceForAppend(in []byte, n int) (head, tail []byte) {
  37. if total := len(in) + n; cap(in) >= total {
  38. head = in[:total]
  39. } else {
  40. head = make([]byte, total)
  41. copy(head, in)
  42. }
  43. tail = head[len(in):]
  44. return head, tail
  45. }
  46. // ParseFramedMsg parse the provided buffer and returns a frame of the format
  47. // msgLength+msg and any remaining bytes in that buffer.
  48. func ParseFramedMsg(b []byte, maxLen uint32) ([]byte, []byte, error) {
  49. // If the size field is not complete, return the provided buffer as
  50. // remaining buffer.
  51. if len(b) < MsgLenFieldSize {
  52. return nil, b, nil
  53. }
  54. msgLenField := b[:MsgLenFieldSize]
  55. length := binary.LittleEndian.Uint32(msgLenField)
  56. if length > maxLen {
  57. return nil, nil, fmt.Errorf("received the frame length %d larger than the limit %d", length, maxLen)
  58. }
  59. if len(b) < int(length)+4 { // account for the first 4 msg length bytes.
  60. // Frame is not complete yet.
  61. return nil, b, nil
  62. }
  63. return b[:MsgLenFieldSize+length], b[MsgLenFieldSize+length:], nil
  64. }