diff --git a/notify.go b/notify.go index 6d9f62d..d9f8b34 100644 --- a/notify.go +++ b/notify.go @@ -51,4 +51,19 @@ func (n *RelayNotifiee) Connected(s inet.Network, c inet.Conn) { }(c.RemotePeer()) } -func (n *RelayNotifiee) Disconnected(s inet.Network, c inet.Conn) {} +func (n *RelayNotifiee) Disconnected(s inet.Network, c inet.Conn) { + r := n.Relay() + p := c.RemotePeer() + + if r.host.Network().Connectedness(p) == inet.Connected { + // we have multiple connections + return + } + + r.mx.Lock() + defer r.mx.Unlock() + if _, ok := r.rsvp[p]; ok { + delete(r.rsvp, p) + r.host.ConnManager().UntagPeer(p, "relay") + } +} diff --git a/pb/relay.pb.go b/pb/relay.pb.go index ca165ee..6c7a565 100644 --- a/pb/relay.pb.go +++ b/pb/relay.pb.go @@ -3,13 +3,13 @@ package relay_pb -import ( - fmt "fmt" - github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" - proto "github.com/gogo/protobuf/proto" - io "io" - math "math" -) +import proto "github.com/gogo/protobuf/proto" +import fmt "fmt" +import math "math" + +import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" + +import io "io" // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal @@ -41,6 +41,7 @@ const ( CircuitRelay_STOP_DST_MULTIADDR_INVALID CircuitRelay_Status = 351 CircuitRelay_STOP_RELAY_REFUSED CircuitRelay_Status = 390 CircuitRelay_MALFORMED_MESSAGE CircuitRelay_Status = 400 + CircuitRelay_RESERVATION_REFUSED CircuitRelay_Status = 500 ) var CircuitRelay_Status_name = map[int32]string{ @@ -60,8 +61,8 @@ var CircuitRelay_Status_name = map[int32]string{ 351: "STOP_DST_MULTIADDR_INVALID", 390: "STOP_RELAY_REFUSED", 400: "MALFORMED_MESSAGE", + 500: "RESERVATION_REFUSED", } - var CircuitRelay_Status_value = map[string]int32{ "SUCCESS": 100, "HOP_SRC_ADDR_TOO_LONG": 220, @@ -79,6 +80,7 @@ var CircuitRelay_Status_value = map[string]int32{ "STOP_DST_MULTIADDR_INVALID": 351, "STOP_RELAY_REFUSED": 390, "MALFORMED_MESSAGE": 400, + "RESERVATION_REFUSED": 500, } func (x CircuitRelay_Status) Enum() *CircuitRelay_Status { @@ -86,11 +88,9 @@ func (x CircuitRelay_Status) Enum() *CircuitRelay_Status { *p = x return p } - func (x CircuitRelay_Status) String() string { return proto.EnumName(CircuitRelay_Status_name, int32(x)) } - func (x *CircuitRelay_Status) UnmarshalJSON(data []byte) error { value, err := proto.UnmarshalJSONEnum(CircuitRelay_Status_value, data, "CircuitRelay_Status") if err != nil { @@ -99,9 +99,8 @@ func (x *CircuitRelay_Status) UnmarshalJSON(data []byte) error { *x = CircuitRelay_Status(value) return nil } - func (CircuitRelay_Status) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_9f69a7d5a802d584, []int{0, 0} + return fileDescriptor_relay_bffd91b997cb3c4f, []int{0, 0} } type CircuitRelay_Type int32 @@ -111,6 +110,7 @@ const ( CircuitRelay_STOP CircuitRelay_Type = 2 CircuitRelay_STATUS CircuitRelay_Type = 3 CircuitRelay_CAN_HOP CircuitRelay_Type = 4 + CircuitRelay_RESERVE CircuitRelay_Type = 5 ) var CircuitRelay_Type_name = map[int32]string{ @@ -118,13 +118,14 @@ var CircuitRelay_Type_name = map[int32]string{ 2: "STOP", 3: "STATUS", 4: "CAN_HOP", + 5: "RESERVE", } - var CircuitRelay_Type_value = map[string]int32{ "HOP": 1, "STOP": 2, "STATUS": 3, "CAN_HOP": 4, + "RESERVE": 5, } func (x CircuitRelay_Type) Enum() *CircuitRelay_Type { @@ -132,11 +133,9 @@ func (x CircuitRelay_Type) Enum() *CircuitRelay_Type { *p = x return p } - func (x CircuitRelay_Type) String() string { return proto.EnumName(CircuitRelay_Type_name, int32(x)) } - func (x *CircuitRelay_Type) UnmarshalJSON(data []byte) error { value, err := proto.UnmarshalJSONEnum(CircuitRelay_Type_value, data, "CircuitRelay_Type") if err != nil { @@ -145,9 +144,8 @@ func (x *CircuitRelay_Type) UnmarshalJSON(data []byte) error { *x = CircuitRelay_Type(value) return nil } - func (CircuitRelay_Type) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_9f69a7d5a802d584, []int{0, 1} + return fileDescriptor_relay_bffd91b997cb3c4f, []int{0, 1} } type CircuitRelay struct { @@ -164,7 +162,7 @@ func (m *CircuitRelay) Reset() { *m = CircuitRelay{} } func (m *CircuitRelay) String() string { return proto.CompactTextString(m) } func (*CircuitRelay) ProtoMessage() {} func (*CircuitRelay) Descriptor() ([]byte, []int) { - return fileDescriptor_9f69a7d5a802d584, []int{0} + return fileDescriptor_relay_bffd91b997cb3c4f, []int{0} } func (m *CircuitRelay) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -181,8 +179,8 @@ func (m *CircuitRelay) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) return b[:n], nil } } -func (m *CircuitRelay) XXX_Merge(src proto.Message) { - xxx_messageInfo_CircuitRelay.Merge(m, src) +func (dst *CircuitRelay) XXX_Merge(src proto.Message) { + xxx_messageInfo_CircuitRelay.Merge(dst, src) } func (m *CircuitRelay) XXX_Size() int { return m.Size() @@ -233,7 +231,7 @@ func (m *CircuitRelay_Peer) Reset() { *m = CircuitRelay_Peer{} } func (m *CircuitRelay_Peer) String() string { return proto.CompactTextString(m) } func (*CircuitRelay_Peer) ProtoMessage() {} func (*CircuitRelay_Peer) Descriptor() ([]byte, []int) { - return fileDescriptor_9f69a7d5a802d584, []int{0, 0} + return fileDescriptor_relay_bffd91b997cb3c4f, []int{0, 0} } func (m *CircuitRelay_Peer) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -250,8 +248,8 @@ func (m *CircuitRelay_Peer) XXX_Marshal(b []byte, deterministic bool) ([]byte, e return b[:n], nil } } -func (m *CircuitRelay_Peer) XXX_Merge(src proto.Message) { - xxx_messageInfo_CircuitRelay_Peer.Merge(m, src) +func (dst *CircuitRelay_Peer) XXX_Merge(src proto.Message) { + xxx_messageInfo_CircuitRelay_Peer.Merge(dst, src) } func (m *CircuitRelay_Peer) XXX_Size() int { return m.Size() @@ -277,48 +275,11 @@ func (m *CircuitRelay_Peer) GetAddrs() [][]byte { } func init() { - proto.RegisterEnum("relay.pb.CircuitRelay_Status", CircuitRelay_Status_name, CircuitRelay_Status_value) - proto.RegisterEnum("relay.pb.CircuitRelay_Type", CircuitRelay_Type_name, CircuitRelay_Type_value) proto.RegisterType((*CircuitRelay)(nil), "relay.pb.CircuitRelay") proto.RegisterType((*CircuitRelay_Peer)(nil), "relay.pb.CircuitRelay.Peer") + proto.RegisterEnum("relay.pb.CircuitRelay_Status", CircuitRelay_Status_name, CircuitRelay_Status_value) + proto.RegisterEnum("relay.pb.CircuitRelay_Type", CircuitRelay_Type_name, CircuitRelay_Type_value) } - -func init() { proto.RegisterFile("relay.proto", fileDescriptor_9f69a7d5a802d584) } - -var fileDescriptor_9f69a7d5a802d584 = []byte{ - // 473 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0x4f, 0x6f, 0xd3, 0x3e, - 0x18, 0xc7, 0x65, 0x27, 0xbf, 0x76, 0x7a, 0x5a, 0x4d, 0xfe, 0x59, 0x63, 0x64, 0x9d, 0x56, 0xaa, - 0x9e, 0x7a, 0x40, 0x45, 0x4c, 0xe2, 0x05, 0x98, 0xc4, 0xdd, 0x2a, 0xd2, 0x38, 0xb2, 0x5d, 0x24, - 0x4e, 0x56, 0x69, 0x72, 0xa8, 0x84, 0xd4, 0x2a, 0xcd, 0x0e, 0xbd, 0xc3, 0xb8, 0x21, 0x8e, 0xbc, - 0x1c, 0xe0, 0xc4, 0x91, 0x17, 0xc0, 0x3f, 0xf5, 0x65, 0xc0, 0x05, 0xd9, 0x5d, 0x33, 0x44, 0x37, - 0x89, 0xa3, 0x9f, 0xef, 0xe7, 0xe3, 0x3c, 0xf9, 0x26, 0xd0, 0x28, 0xf2, 0x17, 0x93, 0x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, 0x1b, 0xfd, 0x0e, 0x00, 0x00, 0xff, - 0xff, 0x6b, 0x22, 0x33, 0xbb, 0x2f, 0x03, 0x00, 0x00, -} - func (m *CircuitRelay) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -492,7 +453,7 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - wire |= uint64(b&0x7F) << shift + wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } @@ -520,7 +481,7 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - v |= CircuitRelay_Type(b&0x7F) << shift + v |= (CircuitRelay_Type(b) & 0x7F) << shift if b < 0x80 { break } @@ -540,7 +501,7 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } @@ -549,9 +510,6 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { return ErrInvalidLengthRelay } postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthRelay - } if postIndex > l { return io.ErrUnexpectedEOF } @@ -576,7 +534,7 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } @@ -585,9 +543,6 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { return ErrInvalidLengthRelay } postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthRelay - } if postIndex > l { return io.ErrUnexpectedEOF } @@ -612,7 +567,7 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - v |= CircuitRelay_Status(b&0x7F) << shift + v |= (CircuitRelay_Status(b) & 0x7F) << shift if b < 0x80 { break } @@ -627,9 +582,6 @@ func (m *CircuitRelay) Unmarshal(dAtA []byte) error { if skippy < 0 { return ErrInvalidLengthRelay } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthRelay - } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } @@ -659,7 +611,7 @@ func (m *CircuitRelay_Peer) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - wire |= uint64(b&0x7F) << shift + wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } @@ -687,7 +639,7 @@ func (m *CircuitRelay_Peer) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + byteLen |= (int(b) & 0x7F) << shift if b < 0x80 { break } @@ -696,9 +648,6 @@ func (m *CircuitRelay_Peer) Unmarshal(dAtA []byte) error { return ErrInvalidLengthRelay } postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthRelay - } if postIndex > l { return io.ErrUnexpectedEOF } @@ -722,7 +671,7 @@ func (m *CircuitRelay_Peer) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + byteLen |= (int(b) & 0x7F) << shift if b < 0x80 { break } @@ -731,9 +680,6 @@ func (m *CircuitRelay_Peer) Unmarshal(dAtA []byte) error { return ErrInvalidLengthRelay } postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthRelay - } if postIndex > l { return io.ErrUnexpectedEOF } @@ -749,9 +695,6 @@ func (m *CircuitRelay_Peer) Unmarshal(dAtA []byte) error { if skippy < 0 { return ErrInvalidLengthRelay } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthRelay - } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } @@ -822,11 +765,8 @@ func skipRelay(dAtA []byte) (n int, err error) { break } } - if length < 0 { - return 0, ErrInvalidLengthRelay - } iNdEx += length - if iNdEx < 0 { + if length < 0 { return 0, ErrInvalidLengthRelay } return iNdEx, nil @@ -857,9 +797,6 @@ func skipRelay(dAtA []byte) (n int, err error) { return 0, err } iNdEx = start + next - if iNdEx < 0 { - return 0, ErrInvalidLengthRelay - } } return iNdEx, nil case 4: @@ -878,3 +815,40 @@ var ( ErrInvalidLengthRelay = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowRelay = fmt.Errorf("proto: integer overflow") ) + +func init() { proto.RegisterFile("relay.proto", fileDescriptor_relay_bffd91b997cb3c4f) } + +var fileDescriptor_relay_bffd91b997cb3c4f = []byte{ + // 496 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0x4f, 0x6f, 0xd3, 0x3e, + 0x18, 0xc7, 0x65, 0x27, 0x6b, 0xa7, 0xa7, 0xd5, 0xe4, 0x9f, 0x7f, 0x63, 0x64, 0x9d, 0x56, 0xaa, + 0x9e, 0x7a, 0x40, 0x45, 0x20, 0x71, 0x46, 0x26, 0x71, 0xb7, 0x8a, 0x34, 0xae, 0x6c, 0x77, 0x12, + 0x27, 0xab, 0x34, 0x39, 0x54, 0x42, 0x6a, 0x95, 0x66, 0x87, 0xde, 0x61, 0x47, 0xc4, 0x91, 0x57, + 0xc1, 0x6b, 0x80, 0x1b, 0x47, 0x5e, 0xc0, 0xf8, 0xa3, 0xbe, 0x06, 0x4e, 0x70, 0x41, 0x76, 0xd7, + 0x0c, 0xd1, 0x4d, 0xe2, 0xe8, 0xe7, 0xfb, 0xf9, 0x3c, 0x71, 0xbe, 0x09, 0xd4, 0xf2, 0xec, 0xe5, + 0x78, 0xd9, 0x9d, 0xe7, 0xb3, 0x62, 0x46, 0x77, 0xaf, 0x0e, 0x2f, 0xda, 0xef, 0x2b, 0x50, 0x0f, + 0xa7, 0xf9, 0xe4, 0x7c, 0x5a, 0x48, 0x3b, 0xa3, 0x0f, 0xc0, 0x2f, 0x96, 0xf3, 0x2c, 0x40, 0x2d, + 0xd4, 0xd9, 0x7b, 0x74, 0xd4, 0xdd, 0x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x98, 0x1e, 0x43, + 0x50, 0xce, 0xc5, 0x90, 0x27, 0x6e, 0xb5, 0xd2, 0x92, 0xb3, 0x01, 0xb9, 0xc0, 0xf4, 0x10, 0xf6, + 0xcb, 0x58, 0x0d, 0x39, 0x7b, 0x66, 0x24, 0x8f, 0xd9, 0x73, 0xf2, 0x06, 0xd3, 0x23, 0x38, 0x28, + 0x23, 0x37, 0xb4, 0x4f, 0x53, 0x3c, 0xee, 0x91, 0x77, 0x2e, 0x54, 0xfa, 0xc6, 0x02, 0x3e, 0x5c, + 0x87, 0xdb, 0x0d, 0x7c, 0xc4, 0xf4, 0x1e, 0x34, 0x4a, 0x73, 0xfb, 0x15, 0xbf, 0x5e, 0x03, 0x37, + 0x77, 0xf0, 0x0d, 0xdb, 0x0e, 0x1c, 0xb0, 0xbe, 0x94, 0xe4, 0xbd, 0x91, 0xe2, 0x11, 0xb9, 0xf0, + 0x6c, 0x07, 0x03, 0x16, 0xf7, 0x84, 0x1c, 0xf0, 0xc8, 0x0c, 0xb8, 0x52, 0xec, 0x84, 0x93, 0xb7, + 0x1e, 0x0d, 0xe0, 0x7f, 0xc9, 0x15, 0x97, 0x67, 0x4c, 0xf7, 0x45, 0x52, 0x1a, 0x3f, 0xbc, 0xf6, + 0x13, 0xf0, 0xed, 0xbf, 0x4b, 0xab, 0xe0, 0x9d, 0x8a, 0x21, 0x41, 0x74, 0x17, 0x7c, 0xbb, 0x9b, + 0x60, 0x0a, 0x50, 0x51, 0x9a, 0xe9, 0x91, 0x22, 0x9e, 0xfd, 0xf4, 0x21, 0x4b, 0x8c, 0x45, 0x7c, + 0x7b, 0x58, 0x6f, 0xe3, 0x64, 0xe7, 0x69, 0xfd, 0xd3, 0xaa, 0x89, 0x3e, 0xaf, 0x9a, 0xe8, 0xfb, + 0xaa, 0x89, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x87, 0xbf, 0xe1, 0x14, 0x56, 0x03, 0x00, 0x00, +} diff --git a/pb/relay.proto b/pb/relay.proto index de3e637..a8a4886 100644 --- a/pb/relay.proto +++ b/pb/relay.proto @@ -21,6 +21,7 @@ message CircuitRelay { STOP_DST_MULTIADDR_INVALID = 351; STOP_RELAY_REFUSED = 390; MALFORMED_MESSAGE = 400; + RESERVATION_REFUSED = 500; } enum Type { // RPC identifier, either HOP, STOP or STATUS @@ -28,6 +29,7 @@ message CircuitRelay { STOP = 2; STATUS = 3; CAN_HOP = 4; + RESERVE = 5; } message Peer { diff --git a/relay.go b/relay.go index 8fa10c4..3c9b1e7 100644 --- a/relay.go +++ b/relay.go @@ -40,12 +40,13 @@ type Relay struct { incoming chan *Conn - relays map[peer.ID]struct{} mx sync.Mutex + relays map[peer.ID]struct{} + rsvp map[peer.ID]struct{} + lhLk sync.Mutex liveHops map[peer.ID]map[peer.ID]int lhCount uint64 - lhLk sync.Mutex } // RelayOpts are options for configuring the relay transport. @@ -72,7 +73,17 @@ type RelayError struct { } func (e RelayError) Error() string { - return fmt.Sprintf("error opening relay circuit: %s (%d)", pb.CircuitRelay_Status_name[int32(e.Code)], e.Code) + return fmt.Sprintf("relay error: %s (%d)", pb.CircuitRelay_Status_name[int32(e.Code)], e.Code) +} + +func IsMalformedMessageError(err error) bool { + re, ok := err.(RelayError) + return ok && re.Code == pb.CircuitRelay_MALFORMED_MESSAGE +} + +func IsHopRefusedError(err error) bool { + re, ok := err.(RelayError) + return ok && re.Code == pb.CircuitRelay_HOP_CANT_SPEAK_RELAY } // NewRelay constructs a new relay. @@ -264,6 +275,8 @@ func (r *Relay) handleNewStream(s inet.Stream) { r.handleStopStream(s, &msg) case pb.CircuitRelay_CAN_HOP: r.handleCanHop(s, &msg) + case pb.CircuitRelay_RESERVE: + r.handleReserve(s, &msg) default: log.Warningf("unexpected relay handshake: %d", msg.GetType()) r.handleError(s, pb.CircuitRelay_MALFORMED_MESSAGE) @@ -447,6 +460,84 @@ func (r *Relay) handleCanHop(s inet.Stream, msg *pb.CircuitRelay) { } } +func (r *Relay) handleReserve(s inet.Stream, msg *pb.CircuitRelay) { + var err error + + if r.hop { + // TODO this should check against some user-specified resource limit + // for now it just tags the peer in the connection manager + // so the reservation is never refused + p := s.Conn().RemotePeer() + r.mx.Lock() + r.rsvp[p] = struct{}{} + r.host.ConnManager().TagPeer(p, "relay", 1) + r.mx.Unlock() + err = r.writeResponse(s, pb.CircuitRelay_SUCCESS) + if err != nil { + // undo the reservation + r.mx.Lock() + delete(r.rsvp, p) + r.host.ConnManager().UntagPeer(p, "relay") + r.mx.Unlock() + } + } else { + // we are not a hop relay, the reservation is meaningless + err = r.writeResponse(s, pb.CircuitRelay_HOP_CANT_SPEAK_RELAY) + } + + if err != nil { + s.Reset() + log.Debugf("error writing relay response: %s", err.Error()) + } else { + inet.FullClose(s) + } +} + +// Reserve asks a hop relay to reserve the connection. +func Reserve(ctx context.Context, h host.Host, p peer.ID) (bool, error) { + s, err := h.NewStream(ctx, p, ProtoID) + if err != nil { + return false, err + } + + rd := newDelimitedReader(s, maxMessageSize) + wr := newDelimitedWriter(s) + + var msg pb.CircuitRelay + + msg.Type = pb.CircuitRelay_RESERVE.Enum() + + if err := wr.WriteMsg(&msg); err != nil { + s.Reset() + return false, err + } + + msg.Reset() + + if err := rd.ReadMsg(&msg); err != nil { + s.Reset() + return false, err + } + if err := inet.FullClose(s); err != nil { + return false, err + } + + if msg.GetType() != pb.CircuitRelay_STATUS { + return false, fmt.Errorf("unexpected relay response; not a status message (%d)", msg.GetType()) + } + + switch msg.GetCode() { + case pb.CircuitRelay_SUCCESS: + return true, nil + + case pb.CircuitRelay_RESERVATION_REFUSED: + return false, nil + + default: + return false, RelayError{Code: msg.GetCode()} + } +} + func (r *Relay) handleError(s inet.Stream, code pb.CircuitRelay_Status) { log.Warningf("relay error: %s (%d)", pb.CircuitRelay_Status_name[int32(code)], code) err := r.writeResponse(s, code)