forked from j-keck/lsleases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcscom.go
77 lines (65 loc) · 1.64 KB
/
cscom.go
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
66
67
68
69
70
71
72
73
74
75
76
77
// client server communication
package main
import (
"errors"
"fmt"
"io"
"net"
"strings"
)
var ErrNoServerInstanceRunning = errors.New("no running server instance found - start one with 'lsleases -s'")
func clientListener(clientChan chan []byte) {
ln, err := openListener()
exitOnError(err, "openListener")
defer ln.Close()
for {
con, err := ln.Accept()
exitOnError(err, "uds accept")
buf := make([]byte, 1024)
if n, err := con.Read(buf); err != nil {
fmt.Println("uds read error: ", err)
} else {
clientChan <- buf[:n]
con.Write(<-clientChan)
con.Close()
}
}
}
func tellServer(cmd string) error {
_, err := tellServerAndThen(cmd, func(con net.Conn) ([]byte, error) { return nil, nil })
return err
}
func askServer(cmd string) ([]byte, error) {
return tellServerAndThen(cmd, func(con net.Conn) ([]byte, error) {
var buffer []byte
for {
b := make([]byte, 512)
n, err := con.Read(b)
if err != nil && err != io.EOF {
return nil, err
}
if n == 0 {
break
}
buffer = append(buffer, b[:n]...)
}
return buffer, nil
})
}
func tellServerAndThen(cmd string, andThen func(net.Conn) ([]byte, error)) ([]byte, error) {
con, err := bind()
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") || // no sock file
strings.Contains(err.Error(), "connection refused") || // sock file without running listener
strings.Contains(err.Error(), "Timed out waiting for pipe") { // windows - no running server
return nil, ErrNoServerInstanceRunning
}
return nil, err
}
defer con.Close()
_, err = con.Write([]byte(cmd))
if err != nil {
return nil, err
}
return andThen(con)
}