diff --git a/conn.go b/conn.go new file mode 100644 index 0000000..ed5b6c6 --- /dev/null +++ b/conn.go @@ -0,0 +1,178 @@ +package p2pd + +import ( + "context" + "errors" + "io" + "net" + "time" + + pb "github.com/libp2p/go-libp2p-daemon/pb" + + ggio "github.com/gogo/protobuf/io" + inet "github.com/libp2p/go-libp2p-net" + peer "github.com/libp2p/go-libp2p-peer" + pstore "github.com/libp2p/go-libp2p-peerstore" + proto "github.com/libp2p/go-libp2p-protocol" + ma "github.com/multiformats/go-multiaddr" +) + +const DefaultTimeout = 60 * time.Second + +func (d *Daemon) handleConn(c net.Conn) { + defer c.Close() + + r := ggio.NewDelimitedReader(c, inet.MessageSizeMax) + w := ggio.NewDelimitedWriter(c) + + for { + var req pb.Request + + err := r.ReadMsg(&req) + if err != nil { + if err != io.EOF { + log.Debugf("Error reading message: %s", err.Error()) + } + return + } + + switch *req.Type { + case pb.Request_CONNECT: + res := d.doConnect(&req) + err := w.WriteMsg(res) + if err != nil { + log.Debugf("Error writing response: %s", err.Error()) + return + } + + case pb.Request_STREAM_OPEN: + res, s := d.doStreamOpen(&req) + err := w.WriteMsg(res) + if err != nil { + log.Debugf("Error writing response: %s", err.Error()) + if s != nil { + s.Reset() + } + return + } + + if s != nil { + d.doStreamPipe(c, s) + return + } + + case pb.Request_STREAM_HANDLER: + res := d.doStreamHandler(&req) + err := w.WriteMsg(res) + if err != nil { + log.Debugf("Error writing response: %s", err.Error()) + return + } + + default: + log.Debugf("Unexpected request type: %s", req.Type) + return + } + } +} + +func (d *Daemon) doConnect(req *pb.Request) *pb.Response { + ctx, cancel := context.WithTimeout(d.ctx, DefaultTimeout) + defer cancel() + + if req.Connect == nil { + return errorResponse(errors.New("Malformed request; missing parameters")) + } + + pid, err := peer.IDFromBytes(req.Connect.Peer) + if err != nil { + log.Debugf("Error parsing peer ID: %s", err.Error()) + return errorResponse(err) + } + + var addrs []ma.Multiaddr + addrs = make([]ma.Multiaddr, len(req.Connect.Addrs)) + for x, bs := range req.Connect.Addrs { + addr, err := ma.NewMultiaddrBytes(bs) + if err != nil { + log.Debugf("Error parsing multiaddr: %s", err.Error()) + return errorResponse(err) + } + addrs[x] = addr + } + + pi := pstore.PeerInfo{ID: pid, Addrs: addrs} + + log.Debugf("connecting to %s", pid.Pretty()) + err = d.host.Connect(ctx, pi) + if err != nil { + log.Debugf("error opening connection to %s: %s", pid.Pretty(), err.Error()) + return errorResponse(err) + } + + return okResponse() +} + +func (d *Daemon) doStreamOpen(req *pb.Request) (*pb.Response, inet.Stream) { + ctx, cancel := context.WithTimeout(d.ctx, DefaultTimeout) + defer cancel() + + if req.StreamOpen == nil { + return errorResponse(errors.New("Malformed request; missing parameters")), nil + } + + pid, err := peer.IDFromBytes(req.StreamOpen.Peer) + if err != nil { + log.Debugf("Error parsing peer ID: %s", err.Error()) + return errorResponse(err), nil + } + + protos := make([]proto.ID, len(req.StreamOpen.Proto)) + for x, str := range req.StreamOpen.Proto { + protos[x] = proto.ID(str) + } + + log.Debugf("opening stream to %s", pid.Pretty()) + s, err := d.host.NewStream(ctx, pid, protos...) + if err != nil { + log.Debugf("Error opening stream to %s: %s", pid.Pretty(), err.Error()) + return errorResponse(err), nil + } + + return okResponse(), s +} + +func (d *Daemon) doStreamHandler(req *pb.Request) *pb.Response { + if req.StreamHandler == nil { + return errorResponse(errors.New("Malformed request; missing parameters")) + } + + p := proto.ID(*req.StreamHandler.Proto) + + d.mx.Lock() + defer d.mx.Unlock() + + log.Debugf("set stream handler: %s -> %s", p, *req.StreamHandler.Path) + + _, ok := d.handlers[p] + if !ok { + d.host.SetStreamHandler(p, d.handleStream) + } + d.handlers[p] = *req.StreamHandler.Path + + return okResponse() +} + +func okResponse() *pb.Response { + return &pb.Response{ + Type: pb.Response_OK.Enum(), + } +} + +func errorResponse(err error) *pb.Response { + errstr := err.Error() + return &pb.Response{ + Type: pb.Response_ERROR.Enum(), + Error: &pb.ErrorResponse{Msg: &errstr}, + } +} diff --git a/daemon.go b/daemon.go new file mode 100644 index 0000000..811b597 --- /dev/null +++ b/daemon.go @@ -0,0 +1,60 @@ +package p2pd + +import ( + "context" + "net" + "sync" + + logging "github.com/ipfs/go-log" + libp2p "github.com/libp2p/go-libp2p" + host "github.com/libp2p/go-libp2p-host" + proto "github.com/libp2p/go-libp2p-protocol" +) + +var log = logging.Logger("p2pd") + +type Daemon struct { + ctx context.Context + host host.Host + listener net.Listener + + mx sync.Mutex + // stream handlers: map of protocol.ID to unix socket path + handlers map[proto.ID]string +} + +func NewDaemon(ctx context.Context, path string, opts ...libp2p.Option) (*Daemon, error) { + h, err := libp2p.New(ctx, opts...) + if err != nil { + return nil, err + } + + l, err := net.Listen("unix", path) + if err != nil { + h.Close() + return nil, err + } + + d := &Daemon{ + ctx: ctx, + host: h, + listener: l, + handlers: make(map[proto.ID]string), + } + + go d.listen() + + return d, nil +} + +func (d *Daemon) listen() { + for { + c, err := d.listener.Accept() + if err != nil { + log.Errorf("error accepting connection: %s", err.Error()) + } + + log.Debug("incoming connection") + go d.handleConn(c) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..65c0ed8 --- /dev/null +++ b/package.json @@ -0,0 +1,69 @@ +{ + "author": "vyzo", + "bugs": {}, + "gx": { + "dvcsimport": "github.com/libp2p/go-libp2p-daemon" + }, + "gxDependencies": [ + { + "author": "whyrusleeping", + "hash": "QmUEqyXr97aUbNmQADHYNknjwjjdVpJXEt1UZXmSG81EV4", + "name": "go-libp2p", + "version": "6.0.12" + }, + { + "author": "whyrusleeping", + "hash": "QmeMYW7Nj8jnnEfs9qhm7SxKkoDPUWXu3MsxX6BFwz34tf", + "name": "go-libp2p-host", + "version": "3.0.9" + }, + { + "author": "whyrusleeping", + "hash": "QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN", + "name": "go-libp2p-protocol", + "version": "1.0.0" + }, + { + "hash": "Qmbq7kGxgcpALGLPaWDyTa6KUq5kBUKdEvkvPZcBkJoLex", + "name": "go-log", + "version": "1.5.6" + }, + { + "author": "whyrusleeping", + "hash": "QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W", + "name": "go-libp2p-peer", + "version": "2.3.7" + }, + { + "author": "whyrusleeping", + "hash": "QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve", + "name": "go-libp2p-net", + "version": "3.0.9" + }, + { + "author": "multiformats", + "hash": "QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7", + "name": "go-multiaddr", + "version": "1.3.0" + }, + { + "author": "whyrusleeping", + "hash": "QmdxUuburamoF6zF9qjeQC4WYcWGbWuRmdLacMEsW8ioD8", + "name": "gogo-protobuf", + "version": "0.0.0" + }, + { + "author": "whyrusleeping", + "hash": "Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6", + "name": "go-libp2p-peerstore", + "version": "2.0.0" + } + ], + "gxVersion": "0.12.1", + "language": "go", + "license": "", + "name": "go-libp2p-daemon", + "releaseCmd": "git commit -a -m \"gx publish $VERSION\"", + "version": "0.0.0" +} + diff --git a/pb/p2pd.pb.go b/pb/p2pd.pb.go new file mode 100644 index 0000000..3454002 --- /dev/null +++ b/pb/p2pd.pb.go @@ -0,0 +1,1909 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: p2pd.proto + +package p2pd_pb + +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 +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package + +type Request_Type int32 + +const ( + Request_CONNECT Request_Type = 1 + Request_STREAM_OPEN Request_Type = 2 + Request_STREAM_HANDLER Request_Type = 3 +) + +var Request_Type_name = map[int32]string{ + 1: "CONNECT", + 2: "STREAM_OPEN", + 3: "STREAM_HANDLER", +} +var Request_Type_value = map[string]int32{ + "CONNECT": 1, + "STREAM_OPEN": 2, + "STREAM_HANDLER": 3, +} + +func (x Request_Type) Enum() *Request_Type { + p := new(Request_Type) + *p = x + return p +} +func (x Request_Type) String() string { + return proto.EnumName(Request_Type_name, int32(x)) +} +func (x *Request_Type) UnmarshalJSON(data []byte) error { + value, err := proto.UnmarshalJSONEnum(Request_Type_value, data, "Request_Type") + if err != nil { + return err + } + *x = Request_Type(value) + return nil +} +func (Request_Type) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{0, 0} +} + +type Response_Type int32 + +const ( + Response_OK Response_Type = 1 + Response_ERROR Response_Type = 2 +) + +var Response_Type_name = map[int32]string{ + 1: "OK", + 2: "ERROR", +} +var Response_Type_value = map[string]int32{ + "OK": 1, + "ERROR": 2, +} + +func (x Response_Type) Enum() *Response_Type { + p := new(Response_Type) + *p = x + return p +} +func (x Response_Type) String() string { + return proto.EnumName(Response_Type_name, int32(x)) +} +func (x *Response_Type) UnmarshalJSON(data []byte) error { + value, err := proto.UnmarshalJSONEnum(Response_Type_value, data, "Response_Type") + if err != nil { + return err + } + *x = Response_Type(value) + return nil +} +func (Response_Type) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{1, 0} +} + +type Request struct { + Type *Request_Type `protobuf:"varint,1,req,name=type,enum=p2pd.pb.Request_Type" json:"type,omitempty"` + Connect *ConnectRequest `protobuf:"bytes,2,opt,name=connect" json:"connect,omitempty"` + StreamOpen *StreamOpenRequest `protobuf:"bytes,3,opt,name=streamOpen" json:"streamOpen,omitempty"` + StreamHandler *StreamHandlerRequest `protobuf:"bytes,4,opt,name=streamHandler" json:"streamHandler,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} +func (*Request) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{0} +} +func (m *Request) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Request.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *Request) XXX_Merge(src proto.Message) { + xxx_messageInfo_Request.Merge(dst, src) +} +func (m *Request) XXX_Size() int { + return m.Size() +} +func (m *Request) XXX_DiscardUnknown() { + xxx_messageInfo_Request.DiscardUnknown(m) +} + +var xxx_messageInfo_Request proto.InternalMessageInfo + +func (m *Request) GetType() Request_Type { + if m != nil && m.Type != nil { + return *m.Type + } + return Request_CONNECT +} + +func (m *Request) GetConnect() *ConnectRequest { + if m != nil { + return m.Connect + } + return nil +} + +func (m *Request) GetStreamOpen() *StreamOpenRequest { + if m != nil { + return m.StreamOpen + } + return nil +} + +func (m *Request) GetStreamHandler() *StreamHandlerRequest { + if m != nil { + return m.StreamHandler + } + return nil +} + +type Response struct { + Type *Response_Type `protobuf:"varint,1,req,name=type,enum=p2pd.pb.Response_Type" json:"type,omitempty"` + Error *ErrorResponse `protobuf:"bytes,2,opt,name=error" json:"error,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} +func (*Response) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{1} +} +func (m *Response) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Response.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *Response) XXX_Merge(src proto.Message) { + xxx_messageInfo_Response.Merge(dst, src) +} +func (m *Response) XXX_Size() int { + return m.Size() +} +func (m *Response) XXX_DiscardUnknown() { + xxx_messageInfo_Response.DiscardUnknown(m) +} + +var xxx_messageInfo_Response proto.InternalMessageInfo + +func (m *Response) GetType() Response_Type { + if m != nil && m.Type != nil { + return *m.Type + } + return Response_OK +} + +func (m *Response) GetError() *ErrorResponse { + if m != nil { + return m.Error + } + return nil +} + +type ConnectRequest struct { + Peer []byte `protobuf:"bytes,1,req,name=peer" json:"peer,omitempty"` + Addrs [][]byte `protobuf:"bytes,2,rep,name=addrs" json:"addrs,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *ConnectRequest) Reset() { *m = ConnectRequest{} } +func (m *ConnectRequest) String() string { return proto.CompactTextString(m) } +func (*ConnectRequest) ProtoMessage() {} +func (*ConnectRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{2} +} +func (m *ConnectRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ConnectRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ConnectRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *ConnectRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_ConnectRequest.Merge(dst, src) +} +func (m *ConnectRequest) XXX_Size() int { + return m.Size() +} +func (m *ConnectRequest) XXX_DiscardUnknown() { + xxx_messageInfo_ConnectRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_ConnectRequest proto.InternalMessageInfo + +func (m *ConnectRequest) GetPeer() []byte { + if m != nil { + return m.Peer + } + return nil +} + +func (m *ConnectRequest) GetAddrs() [][]byte { + if m != nil { + return m.Addrs + } + return nil +} + +type StreamOpenRequest struct { + Peer []byte `protobuf:"bytes,1,req,name=peer" json:"peer,omitempty"` + Proto []string `protobuf:"bytes,2,rep,name=proto" json:"proto,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *StreamOpenRequest) Reset() { *m = StreamOpenRequest{} } +func (m *StreamOpenRequest) String() string { return proto.CompactTextString(m) } +func (*StreamOpenRequest) ProtoMessage() {} +func (*StreamOpenRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{3} +} +func (m *StreamOpenRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *StreamOpenRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_StreamOpenRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *StreamOpenRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_StreamOpenRequest.Merge(dst, src) +} +func (m *StreamOpenRequest) XXX_Size() int { + return m.Size() +} +func (m *StreamOpenRequest) XXX_DiscardUnknown() { + xxx_messageInfo_StreamOpenRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_StreamOpenRequest proto.InternalMessageInfo + +func (m *StreamOpenRequest) GetPeer() []byte { + if m != nil { + return m.Peer + } + return nil +} + +func (m *StreamOpenRequest) GetProto() []string { + if m != nil { + return m.Proto + } + return nil +} + +type StreamHandlerRequest struct { + Proto *string `protobuf:"bytes,1,req,name=proto" json:"proto,omitempty"` + Path *string `protobuf:"bytes,2,req,name=path" json:"path,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *StreamHandlerRequest) Reset() { *m = StreamHandlerRequest{} } +func (m *StreamHandlerRequest) String() string { return proto.CompactTextString(m) } +func (*StreamHandlerRequest) ProtoMessage() {} +func (*StreamHandlerRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{4} +} +func (m *StreamHandlerRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *StreamHandlerRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_StreamHandlerRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *StreamHandlerRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_StreamHandlerRequest.Merge(dst, src) +} +func (m *StreamHandlerRequest) XXX_Size() int { + return m.Size() +} +func (m *StreamHandlerRequest) XXX_DiscardUnknown() { + xxx_messageInfo_StreamHandlerRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_StreamHandlerRequest proto.InternalMessageInfo + +func (m *StreamHandlerRequest) GetProto() string { + if m != nil && m.Proto != nil { + return *m.Proto + } + return "" +} + +func (m *StreamHandlerRequest) GetPath() string { + if m != nil && m.Path != nil { + return *m.Path + } + return "" +} + +type ErrorResponse struct { + Msg *string `protobuf:"bytes,1,req,name=msg" json:"msg,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *ErrorResponse) Reset() { *m = ErrorResponse{} } +func (m *ErrorResponse) String() string { return proto.CompactTextString(m) } +func (*ErrorResponse) ProtoMessage() {} +func (*ErrorResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{5} +} +func (m *ErrorResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ErrorResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ErrorResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *ErrorResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_ErrorResponse.Merge(dst, src) +} +func (m *ErrorResponse) XXX_Size() int { + return m.Size() +} +func (m *ErrorResponse) XXX_DiscardUnknown() { + xxx_messageInfo_ErrorResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_ErrorResponse proto.InternalMessageInfo + +func (m *ErrorResponse) GetMsg() string { + if m != nil && m.Msg != nil { + return *m.Msg + } + return "" +} + +type StreamAccept struct { + Peer []byte `protobuf:"bytes,1,req,name=peer" json:"peer,omitempty"` + Addr []byte `protobuf:"bytes,2,req,name=addr" json:"addr,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *StreamAccept) Reset() { *m = StreamAccept{} } +func (m *StreamAccept) String() string { return proto.CompactTextString(m) } +func (*StreamAccept) ProtoMessage() {} +func (*StreamAccept) Descriptor() ([]byte, []int) { + return fileDescriptor_p2pd_42789a4761ca500d, []int{6} +} +func (m *StreamAccept) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *StreamAccept) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_StreamAccept.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (dst *StreamAccept) XXX_Merge(src proto.Message) { + xxx_messageInfo_StreamAccept.Merge(dst, src) +} +func (m *StreamAccept) XXX_Size() int { + return m.Size() +} +func (m *StreamAccept) XXX_DiscardUnknown() { + xxx_messageInfo_StreamAccept.DiscardUnknown(m) +} + +var xxx_messageInfo_StreamAccept proto.InternalMessageInfo + +func (m *StreamAccept) GetPeer() []byte { + if m != nil { + return m.Peer + } + return nil +} + +func (m *StreamAccept) GetAddr() []byte { + if m != nil { + return m.Addr + } + return nil +} + +func init() { + proto.RegisterType((*Request)(nil), "p2pd.pb.Request") + proto.RegisterType((*Response)(nil), "p2pd.pb.Response") + proto.RegisterType((*ConnectRequest)(nil), "p2pd.pb.ConnectRequest") + proto.RegisterType((*StreamOpenRequest)(nil), "p2pd.pb.StreamOpenRequest") + proto.RegisterType((*StreamHandlerRequest)(nil), "p2pd.pb.StreamHandlerRequest") + proto.RegisterType((*ErrorResponse)(nil), "p2pd.pb.ErrorResponse") + proto.RegisterType((*StreamAccept)(nil), "p2pd.pb.StreamAccept") + proto.RegisterEnum("p2pd.pb.Request_Type", Request_Type_name, Request_Type_value) + proto.RegisterEnum("p2pd.pb.Response_Type", Response_Type_name, Response_Type_value) +} +func (m *Request) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Request) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Type == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("type") + } else { + dAtA[i] = 0x8 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(*m.Type)) + } + if m.Connect != nil { + dAtA[i] = 0x12 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(m.Connect.Size())) + n1, err := m.Connect.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n1 + } + if m.StreamOpen != nil { + dAtA[i] = 0x1a + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(m.StreamOpen.Size())) + n2, err := m.StreamOpen.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n2 + } + if m.StreamHandler != nil { + dAtA[i] = 0x22 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(m.StreamHandler.Size())) + n3, err := m.StreamHandler.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n3 + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *Response) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Response) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Type == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("type") + } else { + dAtA[i] = 0x8 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(*m.Type)) + } + if m.Error != nil { + dAtA[i] = 0x12 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(m.Error.Size())) + n4, err := m.Error.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n4 + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *ConnectRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ConnectRequest) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Peer == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("peer") + } else { + dAtA[i] = 0xa + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(m.Peer))) + i += copy(dAtA[i:], m.Peer) + } + if len(m.Addrs) > 0 { + for _, b := range m.Addrs { + dAtA[i] = 0x12 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(b))) + i += copy(dAtA[i:], b) + } + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *StreamOpenRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *StreamOpenRequest) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Peer == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("peer") + } else { + dAtA[i] = 0xa + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(m.Peer))) + i += copy(dAtA[i:], m.Peer) + } + if len(m.Proto) > 0 { + for _, s := range m.Proto { + dAtA[i] = 0x12 + i++ + l = len(s) + for l >= 1<<7 { + dAtA[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + dAtA[i] = uint8(l) + i++ + i += copy(dAtA[i:], s) + } + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *StreamHandlerRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *StreamHandlerRequest) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Proto == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("proto") + } else { + dAtA[i] = 0xa + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(*m.Proto))) + i += copy(dAtA[i:], *m.Proto) + } + if m.Path == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("path") + } else { + dAtA[i] = 0x12 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(*m.Path))) + i += copy(dAtA[i:], *m.Path) + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *ErrorResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ErrorResponse) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Msg == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("msg") + } else { + dAtA[i] = 0xa + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(*m.Msg))) + i += copy(dAtA[i:], *m.Msg) + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *StreamAccept) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *StreamAccept) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Peer == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("peer") + } else { + dAtA[i] = 0xa + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(m.Peer))) + i += copy(dAtA[i:], m.Peer) + } + if m.Addr == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("addr") + } else { + dAtA[i] = 0x12 + i++ + i = encodeVarintP2Pd(dAtA, i, uint64(len(m.Addr))) + i += copy(dAtA[i:], m.Addr) + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func encodeVarintP2Pd(dAtA []byte, offset int, v uint64) int { + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return offset + 1 +} +func (m *Request) Size() (n int) { + var l int + _ = l + if m.Type != nil { + n += 1 + sovP2Pd(uint64(*m.Type)) + } + if m.Connect != nil { + l = m.Connect.Size() + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.StreamOpen != nil { + l = m.StreamOpen.Size() + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.StreamHandler != nil { + l = m.StreamHandler.Size() + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *Response) Size() (n int) { + var l int + _ = l + if m.Type != nil { + n += 1 + sovP2Pd(uint64(*m.Type)) + } + if m.Error != nil { + l = m.Error.Size() + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *ConnectRequest) Size() (n int) { + var l int + _ = l + if m.Peer != nil { + l = len(m.Peer) + n += 1 + l + sovP2Pd(uint64(l)) + } + if len(m.Addrs) > 0 { + for _, b := range m.Addrs { + l = len(b) + n += 1 + l + sovP2Pd(uint64(l)) + } + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *StreamOpenRequest) Size() (n int) { + var l int + _ = l + if m.Peer != nil { + l = len(m.Peer) + n += 1 + l + sovP2Pd(uint64(l)) + } + if len(m.Proto) > 0 { + for _, s := range m.Proto { + l = len(s) + n += 1 + l + sovP2Pd(uint64(l)) + } + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *StreamHandlerRequest) Size() (n int) { + var l int + _ = l + if m.Proto != nil { + l = len(*m.Proto) + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.Path != nil { + l = len(*m.Path) + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *ErrorResponse) Size() (n int) { + var l int + _ = l + if m.Msg != nil { + l = len(*m.Msg) + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *StreamAccept) Size() (n int) { + var l int + _ = l + if m.Peer != nil { + l = len(m.Peer) + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.Addr != nil { + l = len(m.Addr) + n += 1 + l + sovP2Pd(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func sovP2Pd(x uint64) (n int) { + for { + n++ + x >>= 7 + if x == 0 { + break + } + } + return n +} +func sozP2Pd(x uint64) (n int) { + return sovP2Pd(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *Request) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Request: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Request: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) + } + var v Request_Type + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= (Request_Type(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + m.Type = &v + hasFields[0] |= uint64(0x00000001) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Connect", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + msglen + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Connect == nil { + m.Connect = &ConnectRequest{} + } + if err := m.Connect.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field StreamOpen", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + msglen + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.StreamOpen == nil { + m.StreamOpen = &StreamOpenRequest{} + } + if err := m.StreamOpen.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field StreamHandler", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + msglen + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.StreamHandler == nil { + m.StreamHandler = &StreamHandlerRequest{} + } + if err := m.StreamHandler.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("type") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *Response) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Response: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Response: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) + } + var v Response_Type + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= (Response_Type(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + m.Type = &v + hasFields[0] |= uint64(0x00000001) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Error", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + msglen + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Error == nil { + m.Error = &ErrorResponse{} + } + if err := m.Error.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("type") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ConnectRequest) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ConnectRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ConnectRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Peer", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Peer = append(m.Peer[:0], dAtA[iNdEx:postIndex]...) + if m.Peer == nil { + m.Peer = []byte{} + } + iNdEx = postIndex + hasFields[0] |= uint64(0x00000001) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Addrs", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Addrs = append(m.Addrs, make([]byte, postIndex-iNdEx)) + copy(m.Addrs[len(m.Addrs)-1], dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("peer") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *StreamOpenRequest) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: StreamOpenRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: StreamOpenRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Peer", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Peer = append(m.Peer[:0], dAtA[iNdEx:postIndex]...) + if m.Peer == nil { + m.Peer = []byte{} + } + iNdEx = postIndex + hasFields[0] |= uint64(0x00000001) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Proto", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + intStringLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Proto = append(m.Proto, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("peer") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *StreamHandlerRequest) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: StreamHandlerRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: StreamHandlerRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Proto", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + intStringLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + s := string(dAtA[iNdEx:postIndex]) + m.Proto = &s + iNdEx = postIndex + hasFields[0] |= uint64(0x00000001) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Path", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + intStringLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + s := string(dAtA[iNdEx:postIndex]) + m.Path = &s + iNdEx = postIndex + hasFields[0] |= uint64(0x00000002) + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("proto") + } + if hasFields[0]&uint64(0x00000002) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("path") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ErrorResponse) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ErrorResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ErrorResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Msg", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + intStringLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + s := string(dAtA[iNdEx:postIndex]) + m.Msg = &s + iNdEx = postIndex + hasFields[0] |= uint64(0x00000001) + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("msg") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *StreamAccept) Unmarshal(dAtA []byte) error { + var hasFields [1]uint64 + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: StreamAccept: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: StreamAccept: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Peer", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Peer = append(m.Peer[:0], dAtA[iNdEx:postIndex]...) + if m.Peer == nil { + m.Peer = []byte{} + } + iNdEx = postIndex + hasFields[0] |= uint64(0x00000001) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Addr", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowP2Pd + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthP2Pd + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Addr = append(m.Addr[:0], dAtA[iNdEx:postIndex]...) + if m.Addr == nil { + m.Addr = []byte{} + } + iNdEx = postIndex + hasFields[0] |= uint64(0x00000002) + default: + iNdEx = preIndex + skippy, err := skipP2Pd(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthP2Pd + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + if hasFields[0]&uint64(0x00000001) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("peer") + } + if hasFields[0]&uint64(0x00000002) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("addr") + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipP2Pd(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowP2Pd + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowP2Pd + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + return iNdEx, nil + case 1: + iNdEx += 8 + return iNdEx, nil + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowP2Pd + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + iNdEx += length + if length < 0 { + return 0, ErrInvalidLengthP2Pd + } + return iNdEx, nil + case 3: + for { + var innerWire uint64 + var start int = iNdEx + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowP2Pd + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + innerWire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + innerWireType := int(innerWire & 0x7) + if innerWireType == 4 { + break + } + next, err := skipP2Pd(dAtA[start:]) + if err != nil { + return 0, err + } + iNdEx = start + next + } + return iNdEx, nil + case 4: + return iNdEx, nil + case 5: + iNdEx += 4 + return iNdEx, nil + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + } + panic("unreachable") +} + +var ( + ErrInvalidLengthP2Pd = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowP2Pd = fmt.Errorf("proto: integer overflow") +) + +func init() { proto.RegisterFile("p2pd.proto", fileDescriptor_p2pd_42789a4761ca500d) } + +var fileDescriptor_p2pd_42789a4761ca500d = []byte{ + // 398 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x92, 0xc1, 0xea, 0xd3, 0x40, + 0x10, 0xc6, 0xc9, 0x26, 0x35, 0xfe, 0xa7, 0x69, 0x8d, 0x43, 0xd5, 0x28, 0x58, 0xea, 0x9e, 0xaa, + 0x48, 0xc0, 0x1e, 0x44, 0x0a, 0x82, 0x35, 0x06, 0x0a, 0x6a, 0x22, 0xdb, 0xde, 0x25, 0x26, 0x8b, + 0x1e, 0x6c, 0xb2, 0x6e, 0xe2, 0xa1, 0x27, 0xdf, 0xc5, 0xa7, 0xf1, 0xe8, 0x23, 0x48, 0x9f, 0x44, + 0xb2, 0xbb, 0x69, 0x1b, 0xed, 0xff, 0xf6, 0xed, 0xcc, 0xf7, 0x9b, 0xcc, 0x7c, 0x04, 0x40, 0x2c, + 0x44, 0x11, 0x0a, 0x59, 0x35, 0x15, 0xba, 0x5a, 0x7f, 0xa2, 0x3f, 0x09, 0xb8, 0x8c, 0x7f, 0xfb, + 0xce, 0xeb, 0x06, 0x1f, 0x83, 0xd3, 0xec, 0x05, 0x0f, 0xac, 0x19, 0x99, 0x8f, 0x17, 0x77, 0x42, + 0xe3, 0x09, 0x4d, 0x3f, 0xdc, 0xee, 0x05, 0x67, 0xca, 0x82, 0xcf, 0xc0, 0xcd, 0xab, 0xb2, 0xe4, + 0x79, 0x13, 0x90, 0x99, 0x35, 0x1f, 0x2e, 0xee, 0x1d, 0xdd, 0x91, 0xae, 0x1b, 0x88, 0x75, 0x3e, + 0x5c, 0x02, 0xd4, 0x8d, 0xe4, 0xd9, 0x2e, 0x15, 0xbc, 0x0c, 0x6c, 0x45, 0x3d, 0x38, 0x52, 0x9b, + 0x63, 0xab, 0x03, 0xcf, 0xdc, 0x18, 0xc1, 0x48, 0xbf, 0xd6, 0x59, 0x59, 0x7c, 0xe5, 0x32, 0x70, + 0x14, 0xfe, 0xf0, 0x1f, 0xdc, 0x74, 0xbb, 0x09, 0x7d, 0x86, 0xbe, 0x00, 0xa7, 0xbd, 0x00, 0x87, + 0xe0, 0x46, 0x69, 0x92, 0xc4, 0xd1, 0xd6, 0xb7, 0xf0, 0x16, 0x0c, 0x37, 0x5b, 0x16, 0xaf, 0xde, + 0x7f, 0x4c, 0x3f, 0xc4, 0x89, 0x4f, 0x10, 0x61, 0x6c, 0x0a, 0xeb, 0x55, 0xf2, 0xe6, 0x5d, 0xcc, + 0x7c, 0x9b, 0xfe, 0x80, 0x9b, 0x8c, 0xd7, 0xa2, 0x2a, 0x6b, 0x8e, 0x4f, 0x7a, 0x21, 0xdd, 0x3d, + 0x0b, 0x49, 0x1b, 0xce, 0x53, 0x7a, 0x0a, 0x03, 0x2e, 0x65, 0x25, 0x4d, 0x46, 0x27, 0x73, 0xdc, + 0x56, 0x3b, 0x82, 0x69, 0x13, 0xbd, 0x6f, 0xf6, 0xbb, 0x01, 0x24, 0x7d, 0xeb, 0x5b, 0x78, 0x05, + 0x83, 0x98, 0xb1, 0x94, 0xf9, 0x84, 0x2e, 0x61, 0xdc, 0x8f, 0x15, 0x11, 0x1c, 0xc1, 0xb9, 0x54, + 0x6b, 0x78, 0x4c, 0x69, 0x9c, 0xc0, 0x20, 0x2b, 0x0a, 0x59, 0x07, 0x64, 0x66, 0xcf, 0x3d, 0xa6, + 0x1f, 0xf4, 0x25, 0xdc, 0xfe, 0x2f, 0xdc, 0xeb, 0x70, 0xf5, 0x73, 0x28, 0xfc, 0x8a, 0xe9, 0x07, + 0x7d, 0x05, 0x93, 0x4b, 0xe1, 0x9e, 0xdc, 0xed, 0x88, 0xce, 0xad, 0xe6, 0x66, 0xcd, 0x97, 0x80, + 0xa8, 0xa2, 0xd2, 0xf4, 0x11, 0x8c, 0x7a, 0xf7, 0xa2, 0x0f, 0xf6, 0xae, 0xfe, 0x6c, 0xc0, 0x56, + 0xd2, 0xe7, 0xe0, 0xe9, 0x8f, 0xac, 0xf2, 0x9c, 0x8b, 0xcb, 0xeb, 0x21, 0x38, 0xed, 0x41, 0x6a, + 0xb4, 0xc7, 0x94, 0x7e, 0xed, 0xfd, 0x3a, 0x4c, 0xad, 0xdf, 0x87, 0xa9, 0xf5, 0xe7, 0x30, 0xb5, + 0xfe, 0x06, 0x00, 0x00, 0xff, 0xff, 0x08, 0xac, 0x53, 0x4f, 0xe1, 0x02, 0x00, 0x00, +} diff --git a/pb/p2pd.proto b/pb/p2pd.proto new file mode 100644 index 0000000..5322a3c --- /dev/null +++ b/pb/p2pd.proto @@ -0,0 +1,49 @@ +package p2pd.pb; + +message Request { + enum Type { + CONNECT = 1; + STREAM_OPEN = 2; + STREAM_HANDLER = 3; + } + + required Type type = 1; + + optional ConnectRequest connect = 2; + optional StreamOpenRequest streamOpen = 3; + optional StreamHandlerRequest streamHandler = 4; +} + +message Response { + enum Type { + OK = 1; + ERROR = 2; + } + + required Type type = 1; + optional ErrorResponse error = 2; +} + +message ConnectRequest { + required bytes peer = 1; + repeated bytes addrs = 2; +} + +message StreamOpenRequest { + required bytes peer = 1; + repeated string proto = 2; +} + +message StreamHandlerRequest { + required string proto = 1; + required string path = 2; +} + +message ErrorResponse { + required string msg = 1; +} + +message StreamAccept { + required bytes peer = 1; + required bytes addr = 2; +} \ No newline at end of file diff --git a/stream.go b/stream.go new file mode 100644 index 0000000..ea20f94 --- /dev/null +++ b/stream.go @@ -0,0 +1,66 @@ +package p2pd + +import ( + "io" + "net" + "sync" + + pb "github.com/libp2p/go-libp2p-daemon/pb" + + ggio "github.com/gogo/protobuf/io" + inet "github.com/libp2p/go-libp2p-net" +) + +func (d *Daemon) doStreamPipe(c net.Conn, s inet.Stream) { + var wg sync.WaitGroup + wg.Add(2) + + pipe := func(dst io.Writer, src io.Reader) { + _, err := io.Copy(dst, src) + if err != nil && err != io.EOF { + log.Debugf("stream error: %s", err.Error()) + } + wg.Done() + } + + go pipe(c, s) + go pipe(s, c) + + wg.Wait() + s.Close() +} + +func (d *Daemon) handleStream(s inet.Stream) { + defer s.Close() + p := s.Protocol() + + d.mx.Lock() + path, ok := d.handlers[p] + d.mx.Unlock() + + if !ok { + log.Debugf("unexpected stream: %s", p) + return + } + + c, err := net.Dial("unix", path) + if err != nil { + log.Debugf("error dialing handler at %s: %s", path, err.Error()) + return + } + defer c.Close() + + w := ggio.NewDelimitedWriter(c) + msg := pb.StreamAccept{ + Peer: []byte(s.Conn().RemotePeer()), + Addr: s.Conn().RemoteMultiaddr().Bytes(), + } + err = w.WriteMsg(&msg) + if err != nil { + log.Debugf("error accepting stream: %s", err.Error()) + return + } + + d.doStreamPipe(c, s) + +}