From 949ccff832a08acc9bb3a0eadb0a60536b4adb47 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sat, 17 Jun 2017 12:51:25 +0200 Subject: pkg/rpctype: from from rpctype --- pkg/rpctype/rpc.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/rpctype/rpctype.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 pkg/rpctype/rpc.go create mode 100644 pkg/rpctype/rpctype.go (limited to 'pkg') diff --git a/pkg/rpctype/rpc.go b/pkg/rpctype/rpc.go new file mode 100644 index 000000000..8400b0e95 --- /dev/null +++ b/pkg/rpctype/rpc.go @@ -0,0 +1,88 @@ +// Copyright 2017 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package rpctype + +import ( + "fmt" + "net" + "net/rpc" + "time" + + . "github.com/google/syzkaller/pkg/log" +) + +type RpcServer struct { + ln net.Listener + s *rpc.Server +} + +func NewRpcServer(addr string, receiver interface{}) (*RpcServer, error) { + ln, err := net.Listen("tcp", addr) + if err != nil { + return nil, fmt.Errorf("failed to listen on %v: %v", addr, err) + } + s := rpc.NewServer() + s.Register(receiver) + serv := &RpcServer{ + ln: ln, + s: s, + } + return serv, nil +} + +func (serv *RpcServer) Serve() { + for { + conn, err := serv.ln.Accept() + if err != nil { + Logf(0, "failed to accept an rpc connection: %v", err) + continue + } + conn.(*net.TCPConn).SetKeepAlive(true) + conn.(*net.TCPConn).SetKeepAlivePeriod(time.Minute) + go serv.s.ServeConn(conn) + } +} + +func (serv *RpcServer) Addr() net.Addr { + return serv.ln.Addr() +} + +type RpcClient struct { + conn net.Conn + c *rpc.Client +} + +func NewRpcClient(addr string) (*RpcClient, error) { + conn, err := net.DialTimeout("tcp", addr, 60*time.Second) + if err != nil { + return nil, err + } + conn.(*net.TCPConn).SetKeepAlive(true) + conn.(*net.TCPConn).SetKeepAlivePeriod(time.Minute) + cli := &RpcClient{ + conn: conn, + c: rpc.NewClient(conn), + } + return cli, nil +} + +func (cli *RpcClient) Call(method string, args, reply interface{}) error { + cli.conn.SetDeadline(time.Now().Add(5 * 60 * time.Second)) + err := cli.c.Call(method, args, reply) + cli.conn.SetDeadline(time.Time{}) + return err +} + +func (cli *RpcClient) Close() { + cli.c.Close() +} + +func RpcCall(addr, method string, args, reply interface{}) error { + c, err := NewRpcClient(addr) + if err != nil { + return err + } + defer c.Close() + return c.Call(method, args, reply) +} diff --git a/pkg/rpctype/rpctype.go b/pkg/rpctype/rpctype.go new file mode 100644 index 000000000..8b251b717 --- /dev/null +++ b/pkg/rpctype/rpctype.go @@ -0,0 +1,78 @@ +// Copyright 2015 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +// Package rpctype contains types of message passed via net/rpc connections +// between various parts of the system. +package rpctype + +type RpcInput struct { + Call string + Prog []byte + CallIndex int + Signal []uint32 + Cover []uint32 +} + +type RpcCandidate struct { + Prog []byte + Minimized bool +} + +type ConnectArgs struct { + Name string +} + +type ConnectRes struct { + Prios [][]float32 + Inputs []RpcInput + MaxSignal []uint32 + Candidates []RpcCandidate + EnabledCalls string + NeedCheck bool +} + +type CheckArgs struct { + Name string + Kcov bool + Leak bool + Fault bool + UserNamespaces bool + Calls []string +} + +type NewInputArgs struct { + Name string + RpcInput +} + +type PollArgs struct { + Name string + MaxSignal []uint32 + Stats map[string]uint64 +} + +type PollRes struct { + Candidates []RpcCandidate + NewInputs []RpcInput + MaxSignal []uint32 +} + +type HubConnectArgs struct { + Name string + Key string + Fresh bool + Calls []string + Corpus [][]byte +} + +type HubSyncArgs struct { + Name string + Key string + Add [][]byte + Del []string +} + +type HubSyncRes struct { + Inputs [][]byte + More int +} -- cgit mrf-deployment