net.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2016, Cong Ding. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: Cong Ding <dinggnu@gmail.com>
  16. package stun
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "errors"
  21. "net"
  22. "time"
  23. )
  24. const (
  25. numRetransmit = 9
  26. defaultTimeout = 100
  27. maxTimeout = 1600
  28. maxPacketSize = 1024
  29. )
  30. func (c *Client) sendBindingReq(conn net.PacketConn, addr net.Addr, changeIP bool, changePort bool) (*response, error) {
  31. // Construct packet.
  32. pkt, err := newPacket()
  33. if err != nil {
  34. return nil, err
  35. }
  36. pkt.types = typeBindingRequest
  37. attribute := newSoftwareAttribute(c.softwareName)
  38. pkt.addAttribute(*attribute)
  39. if changeIP || changePort {
  40. attribute = newChangeReqAttribute(changeIP, changePort)
  41. pkt.addAttribute(*attribute)
  42. }
  43. // length of fingerprint attribute must be included into crc,
  44. // so we add it before calculating crc, then subtract it after calculating crc.
  45. pkt.length += 8
  46. attribute = newFingerprintAttribute(pkt)
  47. pkt.length -= 8
  48. pkt.addAttribute(*attribute)
  49. // Send packet.
  50. return c.send(pkt, conn, addr)
  51. }
  52. // RFC 3489: Clients SHOULD retransmit the request starting with an interval
  53. // of 100ms, doubling every retransmit until the interval reaches 1.6s.
  54. // Retransmissions continue with intervals of 1.6s until a response is
  55. // received, or a total of 9 requests have been sent.
  56. func (c *Client) send(pkt *packet, conn net.PacketConn, addr net.Addr) (*response, error) {
  57. c.logger.Info("\n" + hex.Dump(pkt.bytes()))
  58. timeout := defaultTimeout
  59. packetBytes := make([]byte, maxPacketSize)
  60. for i := 0; i < numRetransmit; i++ {
  61. // Send packet to the server.
  62. length, err := conn.WriteTo(pkt.bytes(), addr)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if length != len(pkt.bytes()) {
  67. return nil, errors.New("Error in sending data.")
  68. }
  69. err = conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Millisecond))
  70. if err != nil {
  71. return nil, err
  72. }
  73. if timeout < maxTimeout {
  74. timeout *= 2
  75. }
  76. for {
  77. // Read from the port.
  78. length, raddr, err := conn.ReadFrom(packetBytes)
  79. if err != nil {
  80. if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
  81. break
  82. }
  83. return nil, err
  84. }
  85. p, err := newPacketFromBytes(packetBytes[0:length])
  86. if err != nil {
  87. return nil, err
  88. }
  89. // If transId mismatches, keep reading until get a
  90. // matched packet or timeout.
  91. if !bytes.Equal(pkt.transID, p.transID) {
  92. continue
  93. }
  94. c.logger.Info("\n" + hex.Dump(packetBytes[0:length]))
  95. resp := newResponse(p, conn)
  96. resp.serverAddr = newHostFromStr(raddr.String())
  97. return resp, err
  98. }
  99. }
  100. return nil, nil
  101. }