github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/comms/ipc_unix.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
    18  
    19  package comms
    20  
    21  import (
    22  	"net"
    23  	"os"
    24  
    25  	"github.com/ethereum/go-ethereum/logger"
    26  	"github.com/ethereum/go-ethereum/logger/glog"
    27  	"github.com/ethereum/go-ethereum/rpc/codec"
    28  	"github.com/ethereum/go-ethereum/rpc/shared"
    29  	"github.com/ethereum/go-ethereum/rpc/useragent"
    30  )
    31  
    32  func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
    33  	c, err := net.DialUnix("unix", nil, &net.UnixAddr{cfg.Endpoint, "unix"})
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	coder := codec.New(c)
    39  	msg := shared.Request{
    40  		Id:      0,
    41  		Method:  useragent.EnableUserAgentMethod,
    42  		Jsonrpc: shared.JsonRpcVersion,
    43  		Params:  []byte("[]"),
    44  	}
    45  
    46  	coder.WriteResponse(msg)
    47  	coder.Recv()
    48  
    49  	return &ipcClient{cfg.Endpoint, c, codec, coder}, nil
    50  }
    51  
    52  func (self *ipcClient) reconnect() error {
    53  	self.coder.Close()
    54  	c, err := net.DialUnix("unix", nil, &net.UnixAddr{self.endpoint, "unix"})
    55  	if err == nil {
    56  		self.coder = self.codec.New(c)
    57  
    58  		msg := shared.Request{
    59  			Id:      0,
    60  			Method:  useragent.EnableUserAgentMethod,
    61  			Jsonrpc: shared.JsonRpcVersion,
    62  			Params:  []byte("[]"),
    63  		}
    64  		self.coder.WriteResponse(msg)
    65  		self.coder.Recv()
    66  	}
    67  
    68  	return err
    69  }
    70  
    71  func startIpc(cfg IpcConfig, codec codec.Codec, initializer func(conn net.Conn) (shared.EthereumApi, error)) error {
    72  	os.Remove(cfg.Endpoint) // in case it still exists from a previous run
    73  
    74  	l, err := net.ListenUnix("unix", &net.UnixAddr{Name: cfg.Endpoint, Net: "unix"})
    75  	if err != nil {
    76  		return err
    77  	}
    78  	os.Chmod(cfg.Endpoint, 0600)
    79  
    80  	go func() {
    81  		for {
    82  			conn, err := l.AcceptUnix()
    83  			if err != nil {
    84  				glog.V(logger.Error).Infof("Error accepting ipc connection - %v\n", err)
    85  				continue
    86  			}
    87  
    88  			id := newIpcConnId()
    89  			glog.V(logger.Debug).Infof("New IPC connection with id %06d started\n", id)
    90  
    91  			api, err := initializer(conn)
    92  			if err != nil {
    93  				glog.V(logger.Error).Infof("Unable to initialize IPC connection - %v\n", err)
    94  				conn.Close()
    95  				continue
    96  			}
    97  
    98  			go handle(id, conn, api, codec)
    99  		}
   100  
   101  		os.Remove(cfg.Endpoint)
   102  	}()
   103  
   104  	glog.V(logger.Info).Infof("IPC service started (%s)\n", cfg.Endpoint)
   105  
   106  	return nil
   107  }