-
Notifications
You must be signed in to change notification settings - Fork 32
/
conn.go
199 lines (167 loc) · 4.25 KB
/
conn.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package wireless
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
const CONN_MAX_LISTEN_BUFF = 3 * 1024 // Allow 3kB of buffer for listening to events
func init() {
rand.Seed(time.Now().Unix())
}
// Conn represents a connection to a WPA supplicant control interface
type Conn struct {
Interface string
lsockname string
conn *net.UnixConn
currentCommandResponse chan string
subs []*Subscription
log *log.Logger
quit chan bool
}
// Dial will dial the WPA control interface with the given
// interface name
func Dial(iface string) (*Conn, error) {
c := &Conn{Interface: iface, log: log.New(ioutil.Discard, "", log.LstdFlags)}
err := c.init()
if err != nil {
return nil, err
}
return c, nil
}
// WithLogOutput will set the log output of the connection. By default it is
// set to ioutil.Discard
func (c *Conn) WithLogOutput(w io.Writer) {
c.log.SetOutput(w)
}
// Close will close the connection to the WPA control interface
func (c *Conn) Close() error {
close(c.quit)
c.conn.Close()
return os.Remove(c.lsockname)
}
func (c *Conn) listen() {
buf := make([]byte, CONN_MAX_LISTEN_BUFF)
for {
select {
case <-c.quit:
return
default:
bytesRead, err := c.conn.Read(buf)
c.processMessage(bytesRead, buf, err)
}
}
}
func (c *Conn) processMessage(bytesRead int, data []byte, err error) {
if err != nil {
if !IsUseOfClosedNetworkConnectionError(err) {
c.log.Println("Error:", err)
}
return
}
msg := string(data[:bytesRead])
if msg[0] != '<' {
c.currentCommandResponse <- msg
return
}
var ev Event
if strings.Index(msg, "<3>CTRL-") == 0 {
ev, err = NewEventFromMsg(msg)
if err != nil {
c.log.Println("Error:", err)
return
}
} else {
ev.Name = "logs"
ev.Arguments = map[string]string{"msg": msg[3:]}
}
c.publishEvent(ev)
}
func (c *Conn) init() error {
addr, err := net.ResolveUnixAddr("unixgram", "/var/run/wpa_supplicant/"+c.Interface)
if err != nil {
return err
}
if v := os.Getenv("LOG"); v != "" {
c.WithLogOutput(os.Stderr)
}
c.lsockname = fmt.Sprintf("/tmp/wpa_ctrl_%d_%d", os.Getpid(), rand.Intn(10000))
laddr, err := net.ResolveUnixAddr("unixgram", c.lsockname)
if err != nil {
return err
}
c.conn, err = net.DialUnix("unixgram", laddr, addr)
if err != nil {
return err
}
c.log.Println("Local addr: ", c.conn.LocalAddr())
c.currentCommandResponse = make(chan string, 1)
c.quit = make(chan bool)
go c.listen()
err = c.SendCommandBool("ATTACH")
if err != nil {
return err
}
return nil // ok
}
// SendCommand will call SendCommandWithContext with a 2 second timeout
func (c *Conn) SendCommand(command ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
return c.SendCommandWithContext(ctx, command...)
}
// SendCommandWithContext will send the command with a context
func (c *Conn) SendCommandWithContext(ctx context.Context, command ...string) (string, error) {
c.log.Println("<<<", command)
_, err := c.conn.Write([]byte(strings.Join(command, " ")))
if err != nil {
return "", err
}
for {
select {
case resp := <-c.currentCommandResponse:
c.log.Println(">>>", resp)
return resp, nil
case <-ctx.Done():
return "", ErrCmdTimeout
}
}
}
// SendCommandBool will send a command and return an error
// if the response was not OK
func (c *Conn) SendCommandBool(command ...string) error {
return c.SendCommandBoolWithContext(context.Background(), command...)
}
func (c *Conn) SendCommandBoolWithContext(ctx context.Context, command ...string) error {
resp, err := c.SendCommandWithContext(ctx, command...)
if err != nil {
return err
}
if resp != "OK\n" {
return errors.New(strings.TrimSpace(resp))
}
return nil
}
// SendCommandInt will send a command where the response is expected to be an integer
func (c *Conn) SendCommandInt(command ...string) (int, error) {
return c.SendCommandIntWithContext(context.Background(), command...)
}
func (c *Conn) SendCommandIntWithContext(ctx context.Context, command ...string) (int, error) {
resp, err := c.SendCommandWithContext(ctx, command...)
if err != nil {
return 0, err
}
i, err := strconv.Atoi(strings.TrimSpace(resp))
if err != nil {
return 0, err
}
return i, nil
}