packets.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. //go:generate -command genxdr go run ../../../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. 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. return fmt.Sprintf("%s@%s", syncthingprotocol.DeviceIDFromBytes(i.From), i.AddressString())
  48. }
  49. func (i SessionInvitation) GoString() string {
  50. return i.String()
  51. }
  52. func (i SessionInvitation) AddressString() string {
  53. return fmt.Sprintf("%s:%d", net.IP(i.Address), i.Port)
  54. }