packets.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. //go:generate -command genxdr go run ../../syncthing/Godeps/_workspace/src/github.com/calmh/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. )
  19. type header struct {
  20. magic uint32
  21. messageType int32
  22. messageLength int32
  23. }
  24. type Ping struct{}
  25. type Pong struct{}
  26. type JoinRelayRequest struct{}
  27. type JoinSessionRequest struct {
  28. Key []byte // max:32
  29. }
  30. type Response struct {
  31. Code int32
  32. Message string
  33. }
  34. type ConnectRequest struct {
  35. ID []byte // max:32
  36. }
  37. type SessionInvitation struct {
  38. From []byte // max:32
  39. Key []byte // max:32
  40. Address []byte // max:32
  41. Port uint16
  42. ServerSocket bool
  43. }
  44. func (i SessionInvitation) String() string {
  45. return fmt.Sprintf("%s@%s", syncthingprotocol.DeviceIDFromBytes(i.From), i.AddressString())
  46. }
  47. func (i SessionInvitation) GoString() string {
  48. return i.String()
  49. }
  50. func (i SessionInvitation) AddressString() string {
  51. return fmt.Sprintf("%s:%d", net.IP(i.Address), i.Port)
  52. }