packets.go 1.4 KB

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