packets.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. //go:generate go tool genxdr -o packets_xdr.go packets.go
  3. package protocol
  4. import (
  5. "fmt"
  6. "net"
  7. "github.com/syncthing/syncthing/lib/protocol"
  8. )
  9. const (
  10. messageTypePing int32 = iota
  11. messageTypePong
  12. messageTypeJoinRelayRequest
  13. messageTypeJoinSessionRequest
  14. messageTypeResponse
  15. messageTypeConnectRequest
  16. messageTypeSessionInvitation
  17. messageTypeRelayFull
  18. )
  19. type header struct {
  20. magic uint32
  21. messageType int32
  22. messageLength int32
  23. }
  24. type (
  25. Ping struct{}
  26. Pong struct{}
  27. RelayFull struct{}
  28. )
  29. type JoinRelayRequest struct {
  30. Token string
  31. }
  32. type JoinSessionRequest struct {
  33. Key []byte // max:32
  34. }
  35. type Response struct {
  36. Code int32
  37. Message string
  38. }
  39. type ConnectRequest struct {
  40. ID []byte // max:32
  41. }
  42. type SessionInvitation struct {
  43. From []byte // max:32
  44. Key []byte // max:32
  45. Address []byte // max:32
  46. Port uint16
  47. ServerSocket bool
  48. }
  49. func (i SessionInvitation) String() string {
  50. device := "<invalid>"
  51. if address, err := protocol.DeviceIDFromBytes(i.From); err == nil {
  52. device = address.String()
  53. }
  54. return fmt.Sprintf("%s@%s:%d", device, net.IP(i.Address), i.Port)
  55. }
  56. func (i SessionInvitation) GoString() string {
  57. return i.String()
  58. }