File: io.go

package info (click to toggle)
golang-github-farsightsec-go-nmsg 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 500 kB
  • sloc: sh: 21; makefile: 3
file content (97 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2017 by Farsight Security, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package nmsg

/*
#cgo pkg-config: libnmsg
#cgo LDFLAGS: -lnmsg
#include <stdlib.h>
#include <nmsg.h>
*/
import "C"
import "unsafe"

// IO is a handle to a libnmsg io loop connecting one or more Inputs
// with one ore more Outputs.
type IO struct {
	nmsgIO C.nmsg_io_t
}

// NewIO creates and returns a new IO
func NewIO() *IO {
	io := C.nmsg_io_init()
	if io != nil {
		return &IO{io}
	}
	return nil
}

// AddInputChannel opens an NMSG channel and adds it as an Input to the
// IO.
func (io *IO) AddInputChannel(channel string) error {
	cchan := C.CString(channel)
	res := C.nmsg_io_add_input_channel(io.nmsgIO, cchan, nil)
	C.free(unsafe.Pointer(cchan))
	return nmsgError(res)
}

// AddInputSockSpec opens one or more sockets based on the sockspec
// (add/port ,or addr/lowport..highport) and adds it to the IO
// as an input.
func (io *IO) AddInputSockSpec(sockspec string) error {
	css := C.CString(sockspec)
	res := C.nmsg_io_add_input_sockspec(io.nmsgIO, css, nil)
	C.free(unsafe.Pointer(css))
	return nmsgError(res)
}

// AddInput adds a separately created Input to the IO as an input.
func (io *IO) AddInput(i Input) error {
	ni, ok := i.(*nmsgInput)
	if !ok {
		ni = NewCallbackInput(i.Read).(*nmsgInput)
	}
	return nmsgError(C.nmsg_io_add_input(io.nmsgIO, ni.input, nil))
}

// AddOutput adds a separately created Output to the IO as an output.
func (io *IO) AddOutput(o Output) error {
	nout, ok := o.(*nmsgOutput)
	if !ok {
		nout = NewCallbackOutput(o.Write).(*nmsgOutput)
	}
	return nmsgError(C.nmsg_io_add_output(io.nmsgIO, nout.output, nil))
}

// SetMirrored controls whether the IO mirrors output to all outputs
// (mirrored = true) or stripes its output across all outputs.
func (io *IO) SetMirrored(mirrored bool) {
	if mirrored {
		C.nmsg_io_set_output_mode(io.nmsgIO, C.nmsg_io_output_mode_mirror)
		return
	}
	C.nmsg_io_set_output_mode(io.nmsgIO, C.nmsg_io_output_mode_stripe)
}

// SetDebug sets the debug print level of the underlying io.
// Larger numbers are more verbose.
func (io *IO) SetDebug(debug int) {
	C.nmsg_io_set_debug(io.nmsgIO, C.int(debug))
}

// Run starts the IO loop, returning when it is finished or broken
// with Break()
func (io *IO) Run() error {
	return nmsgError(C.nmsg_io_loop(io.nmsgIO))
}

// Break stops the IO main loop.
func (io *IO) Break() {
	C.nmsg_io_breakloop(io.nmsgIO)
}