2018-05-03 15:04:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-06-01 21:31:30 +02:00
|
|
|
"net"
|
2017-06-04 21:48:15 +02:00
|
|
|
"strconv"
|
2017-06-29 14:39:21 +02:00
|
|
|
"strings"
|
2017-07-17 16:16:18 +02:00
|
|
|
"sync/atomic"
|
2017-07-18 15:22:56 +02:00
|
|
|
"time"
|
2017-05-30 22:36:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type IPCError struct {
|
2017-07-20 15:06:24 +02:00
|
|
|
Code int64
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IPCError) Error() string {
|
|
|
|
return fmt.Sprintf("IPC error: %d", s.Code)
|
|
|
|
}
|
|
|
|
|
2017-07-20 15:06:24 +02:00
|
|
|
func (s *IPCError) ErrorCode() int64 {
|
|
|
|
return s.Code
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
device.log.Debug.Println("UAPI: Processing get operation")
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
// create lines
|
2017-07-17 16:16:18 +02:00
|
|
|
|
2017-06-28 23:45:45 +02:00
|
|
|
lines := make([]string, 0, 100)
|
|
|
|
send := func(line string) {
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
func() {
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
// lock required resources
|
2017-10-16 21:33:47 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
device.net.mutex.RLock()
|
|
|
|
defer device.net.mutex.RUnlock()
|
|
|
|
|
|
|
|
device.noise.mutex.RLock()
|
|
|
|
defer device.noise.mutex.RUnlock()
|
|
|
|
|
|
|
|
device.routing.mutex.RLock()
|
|
|
|
defer device.routing.mutex.RUnlock()
|
|
|
|
|
|
|
|
device.peers.mutex.Lock()
|
|
|
|
defer device.peers.mutex.Unlock()
|
|
|
|
|
|
|
|
// serialize device related values
|
|
|
|
|
|
|
|
if !device.noise.privateKey.IsZero() {
|
|
|
|
send("private_key=" + device.noise.privateKey.ToHex())
|
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.port != 0 {
|
|
|
|
send(fmt.Sprintf("listen_port=%d", device.net.port))
|
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.fwmark != 0 {
|
|
|
|
send(fmt.Sprintf("fwmark=%d", device.net.fwmark))
|
|
|
|
}
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
// serialize each peer state
|
|
|
|
|
|
|
|
for _, peer := range device.peers.keyMap {
|
2017-06-28 23:45:45 +02:00
|
|
|
peer.mutex.RLock()
|
|
|
|
defer peer.mutex.RUnlock()
|
2018-02-02 16:40:14 +01:00
|
|
|
|
2017-06-28 23:45:45 +02:00
|
|
|
send("public_key=" + peer.handshake.remoteStatic.ToHex())
|
|
|
|
send("preshared_key=" + peer.handshake.presharedKey.ToHex())
|
2017-11-18 23:34:02 +01:00
|
|
|
if peer.endpoint != nil {
|
|
|
|
send("endpoint=" + peer.endpoint.DstToString())
|
2017-06-28 23:45:45 +02:00
|
|
|
}
|
2017-07-18 15:22:56 +02:00
|
|
|
|
|
|
|
nano := atomic.LoadInt64(&peer.stats.lastHandshakeNano)
|
|
|
|
secs := nano / time.Second.Nanoseconds()
|
|
|
|
nano %= time.Second.Nanoseconds()
|
|
|
|
|
|
|
|
send(fmt.Sprintf("last_handshake_time_sec=%d", secs))
|
|
|
|
send(fmt.Sprintf("last_handshake_time_nsec=%d", nano))
|
|
|
|
send(fmt.Sprintf("tx_bytes=%d", peer.stats.txBytes))
|
|
|
|
send(fmt.Sprintf("rx_bytes=%d", peer.stats.rxBytes))
|
2018-04-18 07:24:33 +02:00
|
|
|
send(fmt.Sprintf("persistent_keepalive_interval=%d", peer.persistentKeepaliveInterval))
|
2017-08-04 16:15:53 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
for _, ip := range device.routing.table.AllowedIPs(peer) {
|
2017-06-28 23:45:45 +02:00
|
|
|
send("allowed_ip=" + ip.String())
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
}
|
|
|
|
}()
|
2017-07-17 16:16:18 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
// send lines (does not require resource locks)
|
2017-06-28 23:45:45 +02:00
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
_, err := socket.WriteString(line + "\n")
|
|
|
|
if err != nil {
|
2017-07-17 16:16:18 +02:00
|
|
|
return &IPCError{
|
|
|
|
Code: ipcErrorIO,
|
|
|
|
}
|
2017-06-28 23:45:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:48:15 +02:00
|
|
|
func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
2017-05-30 22:36:49 +02:00
|
|
|
scanner := bufio.NewScanner(socket)
|
2017-07-17 16:16:18 +02:00
|
|
|
logError := device.log.Error
|
|
|
|
logDebug := device.log.Debug
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-06-29 14:39:21 +02:00
|
|
|
var peer *Peer
|
2017-08-04 16:15:53 +02:00
|
|
|
|
2017-08-07 15:25:04 +02:00
|
|
|
dummy := false
|
2017-08-04 16:15:53 +02:00
|
|
|
deviceConfig := true
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
for scanner.Scan() {
|
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
// parse line
|
2017-05-30 22:36:49 +02:00
|
|
|
|
|
|
|
line := scanner.Text()
|
2017-06-29 14:39:21 +02:00
|
|
|
if line == "" {
|
|
|
|
return nil
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
2017-06-29 14:39:21 +02:00
|
|
|
parts := strings.Split(line, "=")
|
|
|
|
if len(parts) != 2 {
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorProtocol}
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
2017-06-29 14:39:21 +02:00
|
|
|
key := parts[0]
|
|
|
|
value := parts[1]
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
/* device configuration */
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
if deviceConfig {
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
switch key {
|
|
|
|
case "private_key":
|
|
|
|
var sk NoisePrivateKey
|
2017-09-26 14:26:12 +02:00
|
|
|
err := sk.FromHex(value)
|
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to set private_key:", err)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Updating device private key")
|
2017-09-26 14:26:12 +02:00
|
|
|
device.SetPrivateKey(sk)
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
case "listen_port":
|
2018-01-13 09:00:37 +01:00
|
|
|
|
|
|
|
// parse port number
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
port, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
2017-10-27 10:43:37 +02:00
|
|
|
logError.Println("Failed to parse listen_port:", err)
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-07-23 16:21:08 +02:00
|
|
|
}
|
2018-01-13 09:00:37 +01:00
|
|
|
|
|
|
|
// update port and rebind
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Updating listen port")
|
|
|
|
|
2018-01-13 09:00:37 +01:00
|
|
|
device.net.mutex.Lock()
|
2017-10-16 21:33:47 +02:00
|
|
|
device.net.port = uint16(port)
|
2018-01-13 09:00:37 +01:00
|
|
|
device.net.mutex.Unlock()
|
|
|
|
|
2018-01-26 22:52:32 +01:00
|
|
|
if err := device.BindUpdate(); err != nil {
|
2017-08-11 16:18:20 +02:00
|
|
|
logError.Println("Failed to set listen_port:", err)
|
2017-09-26 15:15:27 +02:00
|
|
|
return &IPCError{Code: ipcErrorPortInUse}
|
2017-08-11 16:18:20 +02:00
|
|
|
}
|
2017-08-07 15:25:04 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
case "fwmark":
|
2017-11-17 17:25:45 +01:00
|
|
|
|
|
|
|
// parse fwmark field
|
|
|
|
|
|
|
|
fwmark, err := func() (uint32, error) {
|
|
|
|
if value == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
mark, err := strconv.ParseUint(value, 10, 32)
|
|
|
|
return uint32(mark), err
|
|
|
|
}()
|
|
|
|
|
2017-09-26 14:26:12 +02:00
|
|
|
if err != nil {
|
|
|
|
logError.Println("Invalid fwmark", err)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-08-22 17:22:45 +02:00
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Updating fwmark")
|
|
|
|
|
2018-02-18 20:49:03 +01:00
|
|
|
if err := device.BindSetMark(uint32(fwmark)); err != nil {
|
2018-01-26 22:52:32 +01:00
|
|
|
logError.Println("Failed to update fwmark:", err)
|
|
|
|
return &IPCError{Code: ipcErrorPortInUse}
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
case "public_key":
|
|
|
|
// switch to peer configuration
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Transition to peer configuration")
|
2017-08-04 16:15:53 +02:00
|
|
|
deviceConfig = false
|
2017-07-17 16:16:18 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
case "replace_peers":
|
|
|
|
if value != "true" {
|
|
|
|
logError.Println("Failed to set replace_peers, invalid value:", value)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
|
|
|
}
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Removing all peers")
|
2017-08-04 16:15:53 +02:00
|
|
|
device.RemoveAllPeers()
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
default:
|
|
|
|
logError.Println("Invalid UAPI key (device configuration):", key)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
2017-08-04 16:15:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* peer configuration */
|
|
|
|
|
|
|
|
if !deviceConfig {
|
2017-05-30 22:36:49 +02:00
|
|
|
|
|
|
|
switch key {
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
case "public_key":
|
2018-02-02 16:40:14 +01:00
|
|
|
var publicKey NoisePublicKey
|
|
|
|
err := publicKey.FromHex(value)
|
2017-08-04 16:15:53 +02:00
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to get peer by public_key:", err)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
// ignore peer with public key of device
|
2017-08-04 16:15:53 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
device.noise.mutex.RLock()
|
|
|
|
equals := device.noise.publicKey.Equals(publicKey)
|
|
|
|
device.noise.mutex.RUnlock()
|
2017-08-07 15:25:04 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
if equals {
|
2017-08-07 15:25:04 +02:00
|
|
|
peer = &Peer{}
|
|
|
|
dummy = true
|
2018-02-02 16:40:14 +01:00
|
|
|
}
|
2017-08-07 15:25:04 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
// find peer referenced
|
2017-08-07 15:25:04 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
peer = device.LookupPeer(publicKey)
|
2017-08-04 16:15:53 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
if peer == nil {
|
|
|
|
peer, err = device.NewPeer(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to create new peer:", err)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-08-07 15:25:04 +02:00
|
|
|
}
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Created new peer:", peer.String())
|
2017-08-04 16:15:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
peer.mutex.Lock()
|
|
|
|
peer.timer.handshakeDeadline.Reset(RekeyAttemptTime)
|
|
|
|
peer.mutex.Unlock()
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
case "remove":
|
2017-10-16 21:33:47 +02:00
|
|
|
|
|
|
|
// remove currently selected peer from device
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
if value != "true" {
|
|
|
|
logError.Println("Failed to set remove, invalid value:", value)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
|
|
|
}
|
2017-08-07 15:25:04 +02:00
|
|
|
if !dummy {
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Removing peer:", peer.String())
|
2017-08-07 15:25:04 +02:00
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
}
|
|
|
|
peer = &Peer{}
|
|
|
|
dummy = true
|
2017-05-30 22:36:49 +02:00
|
|
|
|
|
|
|
case "preshared_key":
|
2017-10-16 21:33:47 +02:00
|
|
|
|
|
|
|
// update PSK
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Updating pre-shared key for peer:", peer.String())
|
|
|
|
|
|
|
|
peer.handshake.mutex.Lock()
|
2017-08-07 15:25:04 +02:00
|
|
|
err := peer.handshake.presharedKey.FromHex(value)
|
2018-02-02 16:40:14 +01:00
|
|
|
peer.handshake.mutex.Unlock()
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
if err != nil {
|
2017-07-17 16:16:18 +02:00
|
|
|
logError.Println("Failed to set preshared_key:", err)
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-06-01 21:31:30 +02:00
|
|
|
}
|
2017-05-30 22:36:49 +02:00
|
|
|
|
|
|
|
case "endpoint":
|
2017-10-16 21:33:47 +02:00
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
// set endpoint destination
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Updating endpoint for peer:", peer.String())
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
err := func() error {
|
|
|
|
peer.mutex.Lock()
|
|
|
|
defer peer.mutex.Unlock()
|
2017-11-19 00:21:58 +01:00
|
|
|
endpoint, err := CreateEndpoint(value)
|
|
|
|
if err != nil {
|
2017-11-18 23:34:02 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
peer.endpoint = endpoint
|
2017-11-30 23:22:40 +01:00
|
|
|
peer.timer.handshakeDeadline.Reset(RekeyAttemptTime)
|
2017-11-18 23:34:02 +01:00
|
|
|
return nil
|
|
|
|
}()
|
2017-10-16 21:33:47 +02:00
|
|
|
|
2017-06-30 14:41:08 +02:00
|
|
|
if err != nil {
|
2017-07-17 16:16:18 +02:00
|
|
|
logError.Println("Failed to set endpoint:", value)
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-06-01 21:31:30 +02:00
|
|
|
}
|
2017-05-30 22:36:49 +02:00
|
|
|
|
|
|
|
case "persistent_keepalive_interval":
|
2017-08-04 16:15:53 +02:00
|
|
|
|
|
|
|
// update keep-alive interval
|
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
logDebug.Println("UAPI: Updating persistent_keepalive_interval for peer:", peer.String())
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
secs, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
2017-07-17 16:16:18 +02:00
|
|
|
logError.Println("Failed to set persistent_keepalive_interval:", err)
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-06-04 21:48:15 +02:00
|
|
|
}
|
2017-08-04 16:15:53 +02:00
|
|
|
|
2018-04-18 07:24:33 +02:00
|
|
|
old := peer.persistentKeepaliveInterval
|
|
|
|
peer.persistentKeepaliveInterval = uint16(secs)
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
// send immediate keep-alive
|
|
|
|
|
|
|
|
if old == 0 && secs != 0 {
|
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to get tun device status:", err)
|
|
|
|
return &IPCError{Code: ipcErrorIO}
|
|
|
|
}
|
2017-12-29 17:42:09 +01:00
|
|
|
if device.isUp.Get() && !dummy {
|
2017-08-04 16:15:53 +02:00
|
|
|
peer.SendKeepAlive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
case "replace_allowed_ips":
|
2018-02-02 16:40:14 +01:00
|
|
|
|
|
|
|
logDebug.Println("UAPI: Removing all allowed IPs for peer:", peer.String())
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
if value != "true" {
|
2017-07-17 16:16:18 +02:00
|
|
|
logError.Println("Failed to set replace_allowed_ips, invalid value:", value)
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-06-04 21:48:15 +02:00
|
|
|
}
|
2018-02-02 16:40:14 +01:00
|
|
|
|
|
|
|
if dummy {
|
|
|
|
continue
|
2017-08-07 15:25:04 +02:00
|
|
|
}
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
device.routing.mutex.Lock()
|
|
|
|
device.routing.table.RemovePeer(peer)
|
|
|
|
device.routing.mutex.Unlock()
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
case "allowed_ip":
|
2018-02-02 16:40:14 +01:00
|
|
|
|
|
|
|
logDebug.Println("UAPI: Adding allowed_ip to peer:", peer.String())
|
|
|
|
|
2017-06-04 21:48:15 +02:00
|
|
|
_, network, err := net.ParseCIDR(value)
|
|
|
|
if err != nil {
|
2017-07-17 16:16:18 +02:00
|
|
|
logError.Println("Failed to set allowed_ip:", err)
|
2017-08-04 16:15:53 +02:00
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-06-04 21:48:15 +02:00
|
|
|
}
|
2018-02-02 16:40:14 +01:00
|
|
|
|
|
|
|
if dummy {
|
|
|
|
continue
|
2017-08-07 15:25:04 +02:00
|
|
|
}
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2018-02-02 16:40:14 +01:00
|
|
|
ones, _ := network.Mask.Size()
|
|
|
|
device.routing.mutex.Lock()
|
|
|
|
device.routing.table.Insert(network.IP, uint(ones), peer)
|
|
|
|
device.routing.mutex.Unlock()
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
default:
|
2017-08-04 16:15:53 +02:00
|
|
|
logError.Println("Invalid UAPI key (peer configuration):", key)
|
|
|
|
return &IPCError{Code: ipcErrorInvalid}
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 14:39:21 +02:00
|
|
|
func ipcHandle(device *Device, socket net.Conn) {
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
// create buffered read/writer
|
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
defer socket.Close()
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
buffered := func(s io.ReadWriter) *bufio.ReadWriter {
|
|
|
|
reader := bufio.NewReader(s)
|
|
|
|
writer := bufio.NewWriter(s)
|
|
|
|
return bufio.NewReadWriter(reader, writer)
|
|
|
|
}(socket)
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
defer buffered.Flush()
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
op, err := buffered.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
// handle operation
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
var status *IPCError
|
|
|
|
|
|
|
|
switch op {
|
2017-07-17 16:16:18 +02:00
|
|
|
case "set=1\n":
|
|
|
|
device.log.Debug.Println("Config, set operation")
|
2017-08-04 16:15:53 +02:00
|
|
|
status = ipcSetOperation(device, buffered)
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-07-17 16:16:18 +02:00
|
|
|
case "get=1\n":
|
|
|
|
device.log.Debug.Println("Config, get operation")
|
2017-08-04 16:15:53 +02:00
|
|
|
status = ipcGetOperation(device, buffered)
|
2017-07-17 16:16:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
device.log.Error.Println("Invalid UAPI operation:", op)
|
2017-08-04 16:15:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// write status
|
2017-05-30 22:36:49 +02:00
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
if status != nil {
|
|
|
|
device.log.Error.Println(status)
|
|
|
|
fmt.Fprintf(buffered, "errno=%d\n\n", status.ErrorCode())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(buffered, "errno=0\n\n")
|
2017-07-17 16:16:18 +02:00
|
|
|
}
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|