packets.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. //go:generate -command genxdr go run github.com/calmh/xdr/cmd/genxdr
  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 (
  26. Ping struct{}
  27. Pong struct{}
  28. RelayFull struct{}
  29. )
  30. type JoinRelayRequest struct {
  31. Token string
  32. }
  33. type JoinSessionRequest struct {
  34. Key []byte // max:32
  35. }
  36. type Response struct {
  37. Code int32
  38. Message string
  39. }
  40. type ConnectRequest struct {
  41. ID []byte // max:32
  42. }
  43. type SessionInvitation struct {
  44. From []byte // max:32
  45. Key []byte // max:32
  46. Address []byte // max:32
  47. Port uint16
  48. ServerSocket bool
  49. }
  50. func (i SessionInvitation) String() string {
  51. device := "<invalid>"
  52. if address, err := syncthingprotocol.DeviceIDFromBytes(i.From); err == nil {
  53. device = address.String()
  54. }
  55. return fmt.Sprintf("%s@%s:%d", device, net.IP(i.Address), i.Port)
  56. }
  57. func (i SessionInvitation) GoString() string {
  58. return i.String()
  59. }