aboutsummaryrefslogtreecommitdiff
path: root/ctlsock/ctlsock.go
blob: 47df9d05cf34c65f3303511b2a27cdfa7c618d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package ctlsock is a Go library that can be used to query the
// gocryptfs control socket interface. This interface can be
// activated by passing `-ctlsock /tmp/my.sock` to gocryptfs on the
// command line.
// See gocryptfs-xray for a usage example.
package ctlsock

import (
	"encoding/json"
	"fmt"
	"net"
	"time"
)

func (r *ResponseStruct) Error() string {
	return fmt.Sprintf("errno %d: %s", r.ErrNo, r.ErrText)
}

// CtlSock encapsulates a control socket
type CtlSock struct {
	Conn net.Conn
}

// There was at least one user who hit the earlier 1 second timeout. Raise to 10
// seconds which ought to be enough for anyone.
const ctlsockTimeout = 10 * time.Second

// New opens the socket at `socketPath` and stores it in a `CtlSock` object.
func New(socketPath string) (*CtlSock, error) {
	conn, err := net.DialTimeout("unix", socketPath, ctlsockTimeout)
	if err != nil {
		return nil, err
	}
	return &CtlSock{Conn: conn}, nil
}

// Query sends a request to the control socket returns the response.
func (c *CtlSock) Query(req *RequestStruct) (*ResponseStruct, error) {
	c.Conn.SetDeadline(time.Now().Add(ctlsockTimeout))
	msg, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	_, err = c.Conn.Write(msg)
	if err != nil {
		return nil, err
	}
	buf := make([]byte, 5000)
	n, err := c.Conn.Read(buf)
	if err != nil {
		return nil, err
	}
	buf = buf[:n]
	var resp ResponseStruct
	json.Unmarshal(buf, &resp)
	if resp.ErrNo != 0 {
		return nil, &resp
	}
	return &resp, nil
}

// Close closes the socket
func (c *CtlSock) Close() {
	c.Conn.Close()
}