packets.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Ping struct{}
  26. type Pong struct{}
  27. type RelayFull struct{}
  28. type JoinRelayRequest struct {
  29. Token string
  30. }
  31. type JoinSessionRequest struct {
  32. Key []byte // max:32
  33. }
  34. type Response struct {
  35. Code int32
  36. Message string
  37. }
  38. type ConnectRequest struct {
  39. ID []byte // max:32
  40. }
  41. type SessionInvitation struct {
  42. From []byte // max:32
  43. Key []byte // max:32
  44. Address []byte // max:32
  45. Port uint16
  46. ServerSocket bool
  47. }
  48. func (i SessionInvitation) String() string {
  49. device := "<invalid>"
  50. if address, err := syncthingprotocol.DeviceIDFromBytes(i.From); err == nil {
  51. device = address.String()
  52. }
  53. return fmt.Sprintf("%s@%s:%d", device, net.IP(i.Address), i.Port)
  54. }
  55. func (i SessionInvitation) GoString() string {
  56. return i.String()
  57. }