ipc_unix.go 2.24 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

Bas van Kervel's avatar
Bas van Kervel committed
17 18 19 20 21 22
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

package comms

import (
	"net"
Bas van Kervel's avatar
Bas van Kervel committed
23
	"os"
24
	"path/filepath"
Bas van Kervel's avatar
Bas van Kervel committed
25 26

	"github.com/ethereum/go-ethereum/rpc/codec"
27
	"github.com/ethereum/go-ethereum/rpc/shared"
28
	"github.com/ethereum/go-ethereum/rpc/useragent"
Bas van Kervel's avatar
Bas van Kervel committed
29 30 31 32 33 34 35 36
)

func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
	c, err := net.DialUnix("unix", nil, &net.UnixAddr{cfg.Endpoint, "unix"})
	if err != nil {
		return nil, err
	}

37 38 39 40 41 42 43 44 45 46 47 48
	coder := codec.New(c)
	msg := shared.Request{
		Id:      0,
		Method:  useragent.EnableUserAgentMethod,
		Jsonrpc: shared.JsonRpcVersion,
		Params:  []byte("[]"),
	}

	coder.WriteResponse(msg)
	coder.Recv()

	return &ipcClient{cfg.Endpoint, c, codec, coder}, nil
49 50 51 52 53 54 55
}

func (self *ipcClient) reconnect() error {
	self.coder.Close()
	c, err := net.DialUnix("unix", nil, &net.UnixAddr{self.endpoint, "unix"})
	if err == nil {
		self.coder = self.codec.New(c)
56 57 58 59 60 61 62 63 64

		msg := shared.Request{
			Id:      0,
			Method:  useragent.EnableUserAgentMethod,
			Jsonrpc: shared.JsonRpcVersion,
			Params:  []byte("[]"),
		}
		self.coder.WriteResponse(msg)
		self.coder.Recv()
65 66 67
	}

	return err
Bas van Kervel's avatar
Bas van Kervel committed
68 69
}

70
func ipcListen(cfg IpcConfig) (net.Listener, error) {
71 72
	// Ensure the IPC path exists and remove any previous leftover
	if err := os.MkdirAll(filepath.Dir(cfg.Endpoint), 0751); err != nil {
73
		return nil, err
74 75
	}
	os.Remove(cfg.Endpoint)
76
	l, err := net.Listen("unix", cfg.Endpoint)
Bas van Kervel's avatar
Bas van Kervel committed
77
	if err != nil {
78
		return nil, err
Bas van Kervel's avatar
Bas van Kervel committed
79 80
	}
	os.Chmod(cfg.Endpoint, 0600)
81
	return l, nil
Bas van Kervel's avatar
Bas van Kervel committed
82
}