aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.go
blob: cb7e9efe8a09cddd59f5c2a2619802b1536aa041 (plain) (blame)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"log"
	"net"
	"strconv"
	"time"
)

/* todo : use real error code
 * Many of which will be the same
 */
const (
	ipcErrorNoPeer            = 0
	ipcErrorNoKeyValue        = 1
	ipcErrorInvalidKey        = 2
	ipcErrorInvalidValue      = 2
	ipcErrorInvalidPrivateKey = 3
	ipcErrorInvalidPublicKey  = 4
	ipcErrorInvalidPort       = 5
	ipcErrorInvalidIPAddress  = 6
)

type IPCError struct {
	Code int
}

func (s *IPCError) Error() string {
	return fmt.Sprintf("IPC error: %d", s.Code)
}

func (s *IPCError) ErrorCode() int {
	return s.Code
}

func ipcGetOperation(socket *bufio.ReadWriter, dev *Device) {

}

func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {

	scanner := bufio.NewScanner(socket)

	device.mutex.Lock()
	defer device.mutex.Unlock()

	for scanner.Scan() {
		var key string
		var value string
		var peer *Peer

		// Parse line

		line := scanner.Text()
		if line == "\n" {
			break
		}
		fmt.Println(line)
		n, err := fmt.Sscanf(line, "%s=%s\n", &key, &value)
		if n != 2 || err != nil {
			fmt.Println(err, n)
			return &IPCError{Code: ipcErrorNoKeyValue}
		}

		switch key {

		/* Interface configuration */

		case "private_key":
			if value == "" {
				device.privateKey = NoisePrivateKey{}
			} else {
				err := device.privateKey.FromHex(value)
				if err != nil {
					return &IPCError{Code: ipcErrorInvalidPrivateKey}
				}
			}

		case "listen_port":
			_, err := fmt.Sscanf(value, "%ud", &device.address.Port)
			if err != nil {
				return &IPCError{Code: ipcErrorInvalidPort}
			}

		case "fwmark":
			panic(nil) // not handled yet

		case "public_key":
			var pubKey NoisePublicKey
			err := pubKey.FromHex(value)
			if err != nil {
				return &IPCError{Code: ipcErrorInvalidPublicKey}
			}
			found, ok := device.peers[pubKey]
			if ok {
				peer = found
			} else {
				peer = device.NewPeer(pubKey)
			}

		case "replace_peers":
			if key == "true" {
				device.RemoveAllPeers()
			} else if key == "false" {
			} else {
				return &IPCError{Code: ipcErrorInvalidValue}
			}

		default:
			/* Peer configuration */

			if peer == nil {
				return &IPCError{Code: ipcErrorNoPeer}
			}

			switch key {

			case "remove":
				peer.mutex.Lock()
				// device.RemovePeer(peer.publicKey)
				peer = nil

			case "preshared_key":
				err := func() error {
					peer.mutex.Lock()
					defer peer.mutex.Unlock()
					return peer.handshake.presharedKey.FromHex(value)
				}()
				if err != nil {
					return &IPCError{Code: ipcErrorInvalidPublicKey}
				}

			case "endpoint":
				ip := net.ParseIP(value)
				if ip == nil {
					return &IPCError{Code: ipcErrorInvalidIPAddress}
				}
				peer.mutex.Lock()
				// peer.endpoint = ip FIX
				peer.mutex.Unlock()

			case "persistent_keepalive_interval":
				secs, err := strconv.ParseInt(value, 10, 64)
				if secs < 0 || err != nil {
					return &IPCError{Code: ipcErrorInvalidValue}
				}
				peer.mutex.Lock()
				peer.persistentKeepaliveInterval = time.Duration(secs) * time.Second
				peer.mutex.Unlock()

			case "replace_allowed_ips":
				if key == "true" {
					device.routingTable.RemovePeer(peer)
				} else if key == "false" {
				} else {
					return &IPCError{Code: ipcErrorInvalidValue}
				}

			case "allowed_ip":
				_, network, err := net.ParseCIDR(value)
				if err != nil {
					return &IPCError{Code: ipcErrorInvalidValue}
				}
				ones, _ := network.Mask.Size()
				device.routingTable.Insert(network.IP, uint(ones), peer)

			/* Invalid key */

			default:
				return &IPCError{Code: ipcErrorInvalidKey}
			}
		}
	}

	return nil
}

func ipcListen(dev *Device, socket io.ReadWriter) error {

	buffered := func(s io.ReadWriter) *bufio.ReadWriter {
		reader := bufio.NewReader(s)
		writer := bufio.NewWriter(s)
		return bufio.NewReadWriter(reader, writer)
	}(socket)

	for {
		op, err := buffered.ReadString('\n')
		if err != nil {
			return err
		}
		log.Println(op)

		switch op {

		case "set=1\n":
			err := ipcSetOperation(dev, buffered)
			if err != nil {
				fmt.Fprintf(buffered, "errno=%d\n", err.ErrorCode())
				return err
			} else {
				fmt.Fprintf(buffered, "errno=0\n")
			}
			buffered.Flush()

		case "get=1\n":

		default:
			return errors.New("handle this please")
		}
	}

}