Best Syzkaller code snippet using rpctype.Addr
rpc.go
Source:rpc.go
...68 defer conn.Close()69 // RPC connections should execute on the leader. If we are not the leader,70 // proxy the connection to the leader so that clients an connect to any node71 // in the cluster.72 r.traceCluster("rpc connection from: %v", conn.RemoteAddr())73 // Read and execute request.74 typ, buf, err := r.readMessage(conn)75 // Handle unexpected RPC errors76 if err != nil {77 r.sendError(conn, err.Error())78 return79 }80 if !r.store.IsLeader() && typ != internal.RPCType_PromoteRaft {81 r.proxyLeader(conn.(*net.TCPConn), pack(typ, buf))82 return83 }84 typ, resp, err := r.executeMessage(conn, typ, buf)85 // Handle unexpected RPC errors86 if err != nil {87 r.sendError(conn, err.Error())88 return89 }90 // Set the status header and error message91 if reply, ok := resp.(Reply); ok {92 reply.GetHeader().OK = proto.Bool(err == nil)93 if err != nil {94 reply.GetHeader().Error = proto.String(err.Error())95 }96 }97 r.sendResponse(conn, typ, resp)98}99func (r *rpc) readMessage(conn net.Conn) (internal.RPCType, []byte, error) {100 // Read request size.101 var sz uint64102 if err := binary.Read(conn, binary.BigEndian, &sz); err != nil {103 return internal.RPCType_Error, nil, fmt.Errorf("read size: %s", err)104 }105 if sz == 0 {106 return internal.RPCType_Error, nil, fmt.Errorf("invalid message size: %d", sz)107 }108 if sz >= MaxMessageSize {109 return internal.RPCType_Error, nil, fmt.Errorf("max message size of %d exceeded: %d", MaxMessageSize, sz)110 }111 // Read request.112 buf := make([]byte, sz)113 if _, err := io.ReadFull(conn, buf); err != nil {114 return internal.RPCType_Error, nil, fmt.Errorf("read request: %s", err)115 }116 // Determine the RPC type117 rpcType := internal.RPCType(btou64(buf[0:8]))118 buf = buf[8:]119 r.traceCluster("recv %v request on: %v", rpcType, conn.RemoteAddr())120 return rpcType, buf, nil121}122func (r *rpc) executeMessage(conn net.Conn, rpcType internal.RPCType, buf []byte) (internal.RPCType, proto.Message, error) {123 switch rpcType {124 case internal.RPCType_FetchData:125 var req internal.FetchDataRequest126 if err := proto.Unmarshal(buf, &req); err != nil {127 return internal.RPCType_Error, nil, fmt.Errorf("fetch request unmarshal: %v", err)128 }129 resp, err := r.handleFetchData(&req)130 return rpcType, resp, err131 case internal.RPCType_Join:132 var req internal.JoinRequest133 if err := proto.Unmarshal(buf, &req); err != nil {134 return internal.RPCType_Error, nil, fmt.Errorf("join request unmarshal: %v", err)135 }136 resp, err := r.handleJoinRequest(&req)137 return rpcType, resp, err138 case internal.RPCType_PromoteRaft:139 var req internal.PromoteRaftRequest140 if err := proto.Unmarshal(buf, &req); err != nil {141 return internal.RPCType_Error, nil, fmt.Errorf("promote to raft request unmarshal: %v", err)142 }143 resp, err := r.handlePromoteRaftRequest(&req)144 return rpcType, resp, err145 default:146 return internal.RPCType_Error, nil, fmt.Errorf("unknown rpc type:%v", rpcType)147 }148}149func (r *rpc) sendResponse(conn net.Conn, typ internal.RPCType, resp proto.Message) {150 // Marshal the response back to a protobuf151 buf, err := proto.Marshal(resp)152 if err != nil {153 r.logger.Printf("unable to marshal response: %v", err)154 return155 }156 // Encode response back to connection.157 if _, err := conn.Write(pack(typ, buf)); err != nil {158 r.logger.Printf("unable to write rpc response: %s", err)159 }160}161func (r *rpc) sendError(conn net.Conn, msg string) {162 r.traceCluster(msg)163 resp := &internal.ErrorResponse{164 Header: &internal.ResponseHeader{165 OK: proto.Bool(false),166 Error: proto.String(msg),167 },168 }169 r.sendResponse(conn, internal.RPCType_Error, resp)170}171// handleFetchData handles a request for the current nodes meta data172func (r *rpc) handleFetchData(req *internal.FetchDataRequest) (*internal.FetchDataResponse, error) {173 var (174 b []byte175 data *Data176 err error177 )178 for {179 data = r.store.cachedData()180 if data.Index != req.GetIndex() {181 b, err = data.MarshalBinary()182 if err != nil {183 return nil, err184 }185 break186 }187 if !req.GetBlocking() {188 break189 }190 if err := r.store.WaitForDataChanged(); err != nil {191 return nil, err192 }193 }194 return &internal.FetchDataResponse{195 Header: &internal.ResponseHeader{196 OK: proto.Bool(true),197 },198 Index: proto.Uint64(data.Index),199 Term: proto.Uint64(data.Term),200 Data: b}, nil201}202// handleJoinRequest handles a request to join the cluster203func (r *rpc) handleJoinRequest(req *internal.JoinRequest) (*internal.JoinResponse, error) {204 r.traceCluster("join request from: %v", *req.Addr)205 node, err := func() (*NodeInfo, error) {206 // attempt to create the node207 node, err := r.store.CreateNode(*req.Addr)208 // if it exists, return the existing node209 if err == ErrNodeExists {210 node, err = r.store.NodeByHost(*req.Addr)211 if err != nil {212 return node, err213 }214 r.logger.Printf("existing node re-joined: id=%v addr=%v", node.ID, node.Host)215 } else if err != nil {216 return nil, fmt.Errorf("create node: %v", err)217 }218 peers, err := r.store.Peers()219 if err != nil {220 return nil, fmt.Errorf("list peers: %v", err)221 }222 // If we have less than 3 nodes, add them as raft peers if they are not223 // already a peer224 if len(peers) < MaxRaftNodes && !raft.PeerContained(peers, *req.Addr) {225 r.logger.Printf("adding new raft peer: nodeId=%v addr=%v", node.ID, *req.Addr)226 if err = r.store.AddPeer(*req.Addr); err != nil {227 return node, fmt.Errorf("add peer: %v", err)228 }229 }230 return node, err231 }()232 nodeID := uint64(0)233 if node != nil {234 nodeID = node.ID235 }236 if err != nil {237 return nil, err238 }239 // get the current raft peers240 peers, err := r.store.Peers()241 if err != nil {242 return nil, fmt.Errorf("list peers: %v", err)243 }244 return &internal.JoinResponse{245 Header: &internal.ResponseHeader{246 OK: proto.Bool(true),247 },248 EnableRaft: proto.Bool(raft.PeerContained(peers, *req.Addr)),249 RaftNodes: peers,250 NodeID: proto.Uint64(nodeID),251 }, err252}253func (r *rpc) handlePromoteRaftRequest(req *internal.PromoteRaftRequest) (*internal.PromoteRaftResponse, error) {254 r.traceCluster("promote raft request from: %v", *req.Addr)255 // Need to set the local store peers to match what we are about to join256 if err := r.store.SetPeers(req.RaftNodes); err != nil {257 return nil, err258 }259 if err := r.store.enableLocalRaft(); err != nil {260 return nil, err261 }262 if !contains(req.RaftNodes, *req.Addr) {263 req.RaftNodes = append(req.RaftNodes, *req.Addr)264 }265 if err := r.store.SetPeers(req.RaftNodes); err != nil {266 return nil, err267 }268 return &internal.PromoteRaftResponse{269 Header: &internal.ResponseHeader{270 OK: proto.Bool(true),271 },272 Success: proto.Bool(true),273 }, nil274}275// pack returns a TLV style byte slice encoding the size of the payload, the RPC type276// and the RPC data277func pack(typ internal.RPCType, b []byte) []byte {278 buf := u64tob(uint64(len(b)) + 8)279 buf = append(buf, u64tob(uint64(typ))...)280 buf = append(buf, b...)281 return buf282}283// fetchMetaData returns the latest copy of the meta store data from the current284// leader.285func (r *rpc) fetchMetaData(blocking bool) (*Data, error) {286 assert(r.store != nil, "store is nil")287 // Retrieve the current known leader.288 leader := r.store.Leader()289 if leader == "" {290 return nil, errors.New("no leader detected during fetchMetaData")291 }292 var index, term uint64293 data := r.store.cachedData()294 if data != nil {295 index = data.Index296 term = data.Index297 }298 resp, err := r.call(leader, &internal.FetchDataRequest{299 Index: proto.Uint64(index),300 Term: proto.Uint64(term),301 Blocking: proto.Bool(blocking),302 })303 if err != nil {304 return nil, err305 }306 switch t := resp.(type) {307 case *internal.FetchDataResponse:308 // If data is nil, then the term and index we sent matches the leader309 if t.GetData() == nil {310 return nil, nil311 }312 ms := &Data{}313 if err := ms.UnmarshalBinary(t.GetData()); err != nil {314 return nil, fmt.Errorf("rpc unmarshal metadata: %v", err)315 }316 return ms, nil317 case *internal.ErrorResponse:318 return nil, fmt.Errorf("rpc failed: %s", t.GetHeader().GetError())319 default:320 return nil, fmt.Errorf("rpc failed: unknown response type: %v", t.String())321 }322}323// join attempts to join a cluster at remoteAddr using localAddr as the current324// node's cluster address325func (r *rpc) join(localAddr, remoteAddr string) (*JoinResult, error) {326 req := &internal.JoinRequest{327 Addr: proto.String(localAddr),328 }329 resp, err := r.call(remoteAddr, req)330 if err != nil {331 return nil, err332 }333 switch t := resp.(type) {334 case *internal.JoinResponse:335 return &JoinResult{336 RaftEnabled: t.GetEnableRaft(),337 RaftNodes: t.GetRaftNodes(),338 NodeID: t.GetNodeID(),339 }, nil340 case *internal.ErrorResponse:341 return nil, fmt.Errorf("rpc failed: %s", t.GetHeader().GetError())342 default:343 return nil, fmt.Errorf("rpc failed: unknown response type: %v", t.String())344 }345}346// enableRaft attempts to promote a node at remoteAddr using localAddr as the current347// node's cluster address348func (r *rpc) enableRaft(addr string, peers []string) error {349 req := &internal.PromoteRaftRequest{350 Addr: proto.String(addr),351 RaftNodes: peers,352 }353 resp, err := r.call(addr, req)354 if err != nil {355 return err356 }357 switch t := resp.(type) {358 case *internal.PromoteRaftResponse:359 return nil360 case *internal.ErrorResponse:361 return fmt.Errorf("rpc failed: %s", t.GetHeader().GetError())362 default:363 return fmt.Errorf("rpc failed: unknown response type: %v", t.String())364 }...
Addr
Using AI Code Generation
1import (2type Args struct {3}4type Quotient struct {5}6func (t *Arith) Multiply(args *Args, reply *int) error {7}8func (t *Arith) Divide(args *Args, quo *Quotient) error {9 if args.B == 0 {10 return fmt.Errorf("divide by zero")11 }12}13func main() {14 client, err := rpc.Dial("tcp", "
Addr
Using AI Code Generation
1import (2type Args struct {3}4type Quotient struct {5}6func (t *Arith) Multiply(args *Args, reply *int) error {7}8func (t *Arith) Divide(args *Args, quo *Quotient) error {9 if args.B == 0 {10 return fmt.Errorf("divide by zero")11 }12}13func main() {14 arith := new(Arith)15 rpc.Register(arith)16 rpc.HandleHTTP()17 l, e := net.Listen("tcp", ":1234")18 if e != nil {19 log.Fatal("listen error:", e)20 }21 go http.Serve(l, nil)22}23import (24type Args struct {25}26type Quotient struct {27}28func main() {29 client, err := rpc.Dial("tcp", "
Addr
Using AI Code Generation
1import (2type Args struct {3}4func (t *Arith) Multiply(args *Args, reply *int) error {5}6func main() {7 arith := new(Arith)8 rpc.Register(arith)9 rpc.HandleHTTP()10 listener, e := net.Listen("tcp", ":1234")11 if e != nil {12 log.Fatal("listen error:", e)13 }14 go http.Serve(listener, nil)15 client, err := rpc.DialHTTP("tcp", "
Addr
Using AI Code Generation
1import (2func main() {3 client, err := rpc.Dial("tcp", "localhost:1234")4 if err != nil {5 fmt.Println("Error in dialing", err)6 }7 err = client.Call("HelloService.Hello", "hello", &reply)8 if err != nil {9 fmt.Println("Error in calling", err)10 }11 fmt.Println(reply)12 err = client.Call("HelloService.Addr", "addr", &reply2)13 if err != nil {14 fmt.Println("Error in calling", err)15 }16 fmt.Println(reply2)17}18import (19type HelloService struct{}20func (p *HelloService) Hello(request string, reply *string) error {21}22func main() {23 rpc.RegisterName("HelloService", new(HelloService))24 rpc.HandleHTTP()25 l, err := net.Listen("tcp", ":1234")26 if err != nil {27 panic(err)28 }29 http.Serve(l, nil)30}
Addr
Using AI Code Generation
1import (2type Args struct {3}4type Quotient struct {5}6func (t *Arith) Multiply(args *Args, reply *int) error {7}8func (t *Arith) Divide(args *Args, quo *Quotient) error {9 if args.B == 0 {10 return fmt.Errorf("divide by zero")11 }12}13func main() {14 arith := new(Arith)15 rpc.Register(arith)16 rpc.Accept(rpc.NewServer())17}18import (19type Args struct {20}21type Quotient struct {22}23func (t *Arith) Multiply(args *Args, reply *int) error {24}25func (t *Arith) Divide(args *Args, quo *Quotient) error {26 if args.B == 0 {27 return fmt.Errorf("divide by zero")28 }29}30func main() {31 arith := new(Arith)32 rpc.Register(arith)33 l, e := net.Listen("tcp", ":1234")34 if e != nil {35 log.Fatal("listen error:", e)36 }37 for {
Addr
Using AI Code Generation
1import (2type Args struct {3}4type Quotient struct {5}6func (t *Arith) Multiply(args *Args, reply *int) error {7}8func (t *Arith) Divide(args *Args, quo *Quotient) error {9 if args.B == 0 {10 return fmt.Errorf("divide by zero")11 }12}13func main() {14 arith := new(Arith)15 rpc.Register(arith)16 rpc.HandleHTTP()17 l, e := net.Listen("tcp", ":1234")18 if e != nil {19 log.Fatal("listen error:", e)20 }21 go http.Serve(l, nil)22 client, err := rpc.DialHTTP("tcp", "
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!