packet.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package udptransfer
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. )
  6. const (
  7. _F_NIL = 0
  8. _F_SYN = 1
  9. _F_ACK = 1 << 1
  10. _F_SACK = 1 << 2
  11. _F_TIME = 1 << 3
  12. _F_DATA = 1 << 4
  13. // reserved = 1 << 5
  14. _F_RESET = 1 << 6
  15. _F_FIN = 1 << 7
  16. )
  17. var packetTypeNames = map[byte]string{
  18. 0: "NOOP",
  19. 1: "SYN",
  20. 2: "ACK",
  21. 3: "SYN+ACK",
  22. 4: "SACK",
  23. 8: "TIME",
  24. 12: "SACK+TIME",
  25. 16: "DATA",
  26. 64: "RESET",
  27. 128: "FIN",
  28. 192: "FIN+RESET",
  29. }
  30. const (
  31. _S_FIN = iota
  32. _S_FIN0
  33. _S_FIN1
  34. _S_SYN0
  35. _S_SYN1
  36. _S_EST0
  37. _S_EST1
  38. )
  39. // Magic-6 | TH-10 | CH-10 | payload
  40. const (
  41. _MAGIC_SIZE = 6
  42. _TH_SIZE = 10 + _MAGIC_SIZE
  43. _CH_SIZE = 10
  44. _AH_SIZE = _TH_SIZE + _CH_SIZE
  45. )
  46. const (
  47. // Max UDP payload: 1500 MTU - 20 IP hdr - 8 UDP hdr = 1472 bytes
  48. // Then: MSS = 1472-26 = 1446
  49. // And For ADSL: 1446-8 = 1438
  50. _MSS = 1438
  51. )
  52. const (
  53. _SENT_OK = 0xff
  54. )
  55. type packet struct {
  56. seq uint32
  57. ack uint32
  58. flag uint8
  59. scnt uint8
  60. payload []byte
  61. buffer []byte
  62. }
  63. func (p *packet) marshall(id connID) []byte {
  64. buf := p.buffer
  65. if buf == nil {
  66. buf = make([]byte, _AH_SIZE+len(p.payload))
  67. copy(buf[_TH_SIZE+10:], p.payload)
  68. }
  69. binary.BigEndian.PutUint16(buf[_MAGIC_SIZE:], uint16(len(buf)))
  70. binary.BigEndian.PutUint32(buf[_MAGIC_SIZE+2:], id.rid)
  71. binary.BigEndian.PutUint32(buf[_MAGIC_SIZE+6:], id.lid)
  72. binary.BigEndian.PutUint32(buf[_TH_SIZE:], p.seq)
  73. binary.BigEndian.PutUint32(buf[_TH_SIZE+4:], p.ack)
  74. buf[_TH_SIZE+8] = p.flag
  75. buf[_TH_SIZE+9] = p.scnt
  76. return buf
  77. }
  78. func unmarshall(pk *packet, buf []byte) {
  79. if len(buf) >= _CH_SIZE {
  80. pk.seq = binary.BigEndian.Uint32(buf)
  81. pk.ack = binary.BigEndian.Uint32(buf[4:])
  82. pk.flag = buf[8]
  83. pk.scnt = buf[9]
  84. pk.payload = buf[10:]
  85. }
  86. }
  87. func (n *qNode) String() string {
  88. now := Now()
  89. return fmt.Sprintf("type=%s seq=%d scnt=%d sndtime~=%d,%d miss=%d",
  90. packetTypeNames[n.flag], n.seq, n.scnt, n.sent-now, n.sent_1-now, n.miss)
  91. }
  92. func maxI64(a, b int64) int64 {
  93. if a >= b {
  94. return a
  95. } else {
  96. return b
  97. }
  98. }
  99. func maxU32(a, b uint32) uint32 {
  100. if a >= b {
  101. return a
  102. } else {
  103. return b
  104. }
  105. }
  106. func minI64(a, b int64) int64 {
  107. if a <= b {
  108. return a
  109. } else {
  110. return b
  111. }
  112. }
  113. func maxI32(a, b int32) int32 {
  114. if a >= b {
  115. return a
  116. } else {
  117. return b
  118. }
  119. }
  120. func minI32(a, b int32) int32 {
  121. if a <= b {
  122. return a
  123. } else {
  124. return b
  125. }
  126. }