2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2020-05-02 08:08:26 +00:00
|
|
|
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-01-26 19:11:58 +00:00
|
|
|
"bytes"
|
2020-04-01 16:27:02 +00:00
|
|
|
"errors"
|
2017-05-30 20:36:49 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-06-01 19:31:30 +00:00
|
|
|
"net"
|
2017-06-04 19:48:15 +00:00
|
|
|
"strconv"
|
2017-06-29 12:39:21 +00:00
|
|
|
"strings"
|
2021-01-26 19:11:58 +00:00
|
|
|
"sync"
|
2017-07-17 14:16:18 +00:00
|
|
|
"sync/atomic"
|
2017-07-18 13:22:56 +00:00
|
|
|
"time"
|
2019-05-14 07:09:52 +00:00
|
|
|
|
2019-11-07 16:13:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
2019-05-14 07:09:52 +00:00
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
2017-05-30 20:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IPCError struct {
|
2021-01-15 21:24:38 +00:00
|
|
|
code int64 // error code
|
|
|
|
err error // underlying/wrapped error
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 01:49:27 +00:00
|
|
|
func (s IPCError) Error() string {
|
2021-01-15 21:24:38 +00:00
|
|
|
return fmt.Sprintf("IPC error %d: %v", s.code, s.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s IPCError) Unwrap() error {
|
|
|
|
return s.err
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 01:49:27 +00:00
|
|
|
func (s IPCError) ErrorCode() int64 {
|
2021-01-15 21:24:38 +00:00
|
|
|
return s.code
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipcErrorf(code int64, msg string, args ...interface{}) *IPCError {
|
|
|
|
return &IPCError{code: code, err: fmt.Errorf(msg, args...)}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:11:58 +00:00
|
|
|
var byteBufferPool = &sync.Pool{
|
|
|
|
New: func() interface{} { return new(bytes.Buffer) },
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// IpcGetOperation implements the WireGuard configuration protocol "get" operation.
|
|
|
|
// See https://www.wireguard.com/xplatform/#configuration-protocol for details.
|
2020-12-22 18:08:25 +00:00
|
|
|
func (device *Device) IpcGetOperation(w io.Writer) error {
|
2021-01-26 19:11:58 +00:00
|
|
|
buf := byteBufferPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer byteBufferPool.Put(buf)
|
|
|
|
sendf := func(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(buf, format, args...)
|
|
|
|
buf.WriteByte('\n')
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
func() {
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// lock required resources
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.net.RLock()
|
|
|
|
defer device.net.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.staticIdentity.RLock()
|
|
|
|
defer device.staticIdentity.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.peers.RLock()
|
|
|
|
defer device.peers.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
|
|
|
// serialize device related values
|
|
|
|
|
2018-05-13 21:14:43 +00:00
|
|
|
if !device.staticIdentity.privateKey.IsZero() {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("private_key=%s", device.staticIdentity.privateKey.ToHex())
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.port != 0 {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("listen_port=%d", device.net.port)
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.fwmark != 0 {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("fwmark=%d", device.net.fwmark)
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// serialize each peer state
|
|
|
|
|
|
|
|
for _, peer := range device.peers.keyMap {
|
2019-01-03 18:04:00 +00:00
|
|
|
peer.RLock()
|
|
|
|
defer peer.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("public_key=%s", peer.handshake.remoteStatic.ToHex())
|
|
|
|
sendf("preshared_key=%s", peer.handshake.presharedKey.ToHex())
|
|
|
|
sendf("protocol_version=1")
|
2017-11-18 22:34:02 +00:00
|
|
|
if peer.endpoint != nil {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("endpoint=%s", peer.endpoint.DstToString())
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
2017-07-18 13:22:56 +00:00
|
|
|
|
|
|
|
nano := atomic.LoadInt64(&peer.stats.lastHandshakeNano)
|
|
|
|
secs := nano / time.Second.Nanoseconds()
|
|
|
|
nano %= time.Second.Nanoseconds()
|
|
|
|
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("last_handshake_time_sec=%d", secs)
|
|
|
|
sendf("last_handshake_time_nsec=%d", nano)
|
|
|
|
sendf("tx_bytes=%d", atomic.LoadUint64(&peer.stats.txBytes))
|
|
|
|
sendf("rx_bytes=%d", atomic.LoadUint64(&peer.stats.rxBytes))
|
|
|
|
sendf("persistent_keepalive_interval=%d", atomic.LoadUint32(&peer.persistentKeepaliveInterval))
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2018-05-13 21:14:43 +00:00
|
|
|
for _, ip := range device.allowedips.EntriesForPeer(peer) {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("allowed_ip=%s", ip.String())
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-07-17 14:16:18 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// send lines (does not require resource locks)
|
2021-01-26 19:11:58 +00:00
|
|
|
if _, err := w.Write(buf.Bytes()); err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorIO, "failed to write output: %w", err)
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// IpcSetOperation implements the WireGuard configuration protocol "set" operation.
|
|
|
|
// See https://www.wireguard.com/xplatform/#configuration-protocol for details.
|
2021-01-15 21:24:38 +00:00
|
|
|
func (device *Device) IpcSetOperation(r io.Reader) (err error) {
|
2021-01-25 17:35:35 +00:00
|
|
|
device.ipcSetMu.Lock()
|
|
|
|
defer device.ipcSetMu.Unlock()
|
|
|
|
|
2021-01-15 21:24:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
device.log.Error.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
peer := new(ipcSetPeer)
|
2017-08-04 14:15:53 +00:00
|
|
|
deviceConfig := true
|
|
|
|
|
2021-01-15 21:24:38 +00:00
|
|
|
scanner := bufio.NewScanner(r)
|
2017-05-30 20:36:49 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2017-06-29 12:39:21 +00:00
|
|
|
if line == "" {
|
2021-01-15 22:32:34 +00:00
|
|
|
// Blank line means terminate operation.
|
2017-06-29 12:39:21 +00:00
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-06-29 12:39:21 +00:00
|
|
|
parts := strings.Split(line, "=")
|
|
|
|
if len(parts) != 2 {
|
2021-01-15 21:24:38 +00:00
|
|
|
return ipcErrorf(ipc.IpcErrorProtocol, "failed to parse line %q, found %d =-separated parts, want 2", line, len(parts))
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-06-29 12:39:21 +00:00
|
|
|
key := parts[0]
|
|
|
|
value := parts[1]
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if key == "public_key" {
|
|
|
|
if deviceConfig {
|
2017-08-04 14:15:53 +00:00
|
|
|
deviceConfig = false
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2021-01-15 22:32:34 +00:00
|
|
|
// Load/create the peer we are now configuring.
|
|
|
|
err := device.handlePublicKeyLine(peer, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
var err error
|
|
|
|
if deviceConfig {
|
|
|
|
err = device.handleDeviceLine(key, value)
|
|
|
|
} else {
|
|
|
|
err = device.handlePeerLine(peer, key, value)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 18:12:46 +00:00
|
|
|
|
2021-01-25 17:21:43 +00:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorIO, "failed to read input: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-15 22:32:34 +00:00
|
|
|
}
|
2019-09-28 18:12:46 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
func (device *Device) handleDeviceLine(key, value string) error {
|
|
|
|
switch key {
|
|
|
|
case "private_key":
|
|
|
|
var sk NoisePrivateKey
|
|
|
|
err := sk.FromMaybeZeroHex(value)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set private_key: %w", err)
|
|
|
|
}
|
|
|
|
device.log.Debug.Println("UAPI: Updating private key")
|
|
|
|
device.SetPrivateKey(sk)
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "listen_port":
|
|
|
|
port, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to parse listen_port: %w", err)
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// update port and rebind
|
|
|
|
device.log.Debug.Println("UAPI: Updating listen port")
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
device.net.Lock()
|
|
|
|
device.net.port = uint16(port)
|
|
|
|
device.net.Unlock()
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if err := device.BindUpdate(); err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorPortInUse, "failed to set listen_port: %w", err)
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "fwmark":
|
2021-01-25 17:27:06 +00:00
|
|
|
mark, err := strconv.ParseUint(value, 10, 32)
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid fwmark: %w", err)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
device.log.Debug.Println("UAPI: Updating fwmark")
|
2021-01-25 17:27:06 +00:00
|
|
|
if err := device.BindSetMark(uint32(mark)); err != nil {
|
2021-01-15 22:32:34 +00:00
|
|
|
return ipcErrorf(ipc.IpcErrorPortInUse, "failed to update fwmark: %w", err)
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "replace_peers":
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set replace_peers, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
device.log.Debug.Println("UAPI: Removing all peers")
|
|
|
|
device.RemoveAllPeers()
|
2017-11-18 22:34:02 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
default:
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid UAPI device key: %v", key)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// An ipcSetPeer is the current state of an IPC set operation on a peer.
|
|
|
|
type ipcSetPeer struct {
|
|
|
|
*Peer // Peer is the current peer being operated on
|
|
|
|
dummy bool // dummy reports whether this peer is a temporary, placeholder peer
|
|
|
|
created bool // new reports whether this is a newly created peer
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
func (device *Device) handlePublicKeyLine(peer *ipcSetPeer, value string) error {
|
|
|
|
// Load/create the peer we are configuring.
|
|
|
|
var publicKey NoisePublicKey
|
|
|
|
err := publicKey.FromHex(value)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to get peer by public key: %w", err)
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// Ignore peer with the same public key as this device.
|
|
|
|
device.staticIdentity.RLock()
|
|
|
|
peer.dummy = device.staticIdentity.publicKey.Equals(publicKey)
|
|
|
|
device.staticIdentity.RUnlock()
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if peer.dummy {
|
|
|
|
peer.Peer = &Peer{}
|
|
|
|
} else {
|
|
|
|
peer.Peer = device.LookupPeer(publicKey)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
peer.created = peer.Peer == nil
|
|
|
|
if peer.created {
|
|
|
|
peer.Peer, err = device.NewPeer(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to create new peer: %w", err)
|
|
|
|
}
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Created")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error {
|
|
|
|
switch key {
|
|
|
|
case "update_only":
|
|
|
|
// allow disabling of creation
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set update only, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
if peer.created && !peer.dummy {
|
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
peer.Peer = &Peer{}
|
|
|
|
peer.dummy = true
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "remove":
|
|
|
|
// remove currently selected peer from device
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set remove, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
if !peer.dummy {
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Removing")
|
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
}
|
|
|
|
peer.Peer = &Peer{}
|
|
|
|
peer.dummy = true
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "preshared_key":
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Updating preshared key")
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
peer.handshake.mutex.Lock()
|
|
|
|
err := peer.handshake.presharedKey.FromHex(value)
|
|
|
|
peer.handshake.mutex.Unlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set preshared key: %w", err)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "endpoint":
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Updating endpoint")
|
2021-01-25 17:32:09 +00:00
|
|
|
endpoint, err := conn.CreateEndpoint(value)
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set endpoint %v: %w", value, err)
|
|
|
|
}
|
2021-01-25 17:32:09 +00:00
|
|
|
peer.Lock()
|
|
|
|
defer peer.Unlock()
|
|
|
|
peer.endpoint = endpoint
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "persistent_keepalive_interval":
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Updating persistent keepalive interval")
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
secs, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set persistent keepalive interval: %w", err)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
old := atomic.SwapUint32(&peer.persistentKeepaliveInterval, uint32(secs))
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// Send immediate keepalive if we're turning it on and before it wasn't on.
|
|
|
|
if old == 0 && secs != 0 {
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorIO, "failed to get tun device status: %w", err)
|
|
|
|
}
|
|
|
|
if device.isUp.Get() && !peer.dummy {
|
|
|
|
peer.SendKeepalive()
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "replace_allowed_ips":
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Removing all allowedips")
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to replace allowedips, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
if peer.dummy {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
device.allowedips.RemoveByPeer(peer.Peer)
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "allowed_ip":
|
|
|
|
device.log.Debug.Println(peer, "- UAPI: Adding allowedip")
|
2018-09-03 05:04:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
_, network, err := net.ParseCIDR(value)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set allowed ip: %w", err)
|
|
|
|
}
|
|
|
|
if peer.dummy {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ones, _ := network.Mask.Size()
|
|
|
|
device.allowedips.Insert(network.IP, uint(ones), peer.Peer)
|
2018-09-03 05:04:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "protocol_version":
|
|
|
|
if value != "1" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid protocol version: %v", value)
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2021-01-15 22:32:34 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid UAPI peer key: %v", key)
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 13:30:57 +00:00
|
|
|
func (device *Device) IpcGet() (string, error) {
|
2020-12-22 18:08:25 +00:00
|
|
|
buf := new(strings.Builder)
|
|
|
|
if err := device.IpcGetOperation(buf); err != nil {
|
2020-12-22 13:30:57 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) IpcSet(uapiConf string) error {
|
2020-12-22 18:08:25 +00:00
|
|
|
return device.IpcSetOperation(strings.NewReader(uapiConf))
|
2020-12-22 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
func (device *Device) IpcHandle(socket net.Conn) {
|
2017-07-17 14:16:18 +00:00
|
|
|
defer socket.Close()
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-07-17 14:16:18 +00: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 21:45:45 +00:00
|
|
|
|
2021-01-25 18:00:43 +00:00
|
|
|
for {
|
|
|
|
op, err := buffered.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-25 18:00:43 +00:00
|
|
|
// handle operation
|
|
|
|
switch op {
|
|
|
|
case "set=1\n":
|
|
|
|
err = device.IpcSetOperation(buffered.Reader)
|
|
|
|
case "get=1\n":
|
|
|
|
nextByte, err := buffered.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if nextByte != '\n' {
|
|
|
|
err = ipcErrorf(ipc.IpcErrorInvalid, "trailing character in UAPI get: %c", nextByte, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = device.IpcGetOperation(buffered.Writer)
|
|
|
|
default:
|
|
|
|
device.log.Error.Println("invalid UAPI operation:", op)
|
|
|
|
return
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-25 18:00:43 +00:00
|
|
|
// write status
|
|
|
|
var status *IPCError
|
|
|
|
if err != nil && !errors.As(err, &status) {
|
|
|
|
// shouldn't happen
|
|
|
|
status = ipcErrorf(ipc.IpcErrorUnknown, "other UAPI error: %w", err)
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
buffered.Flush()
|
2017-07-17 14:16:18 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|