pax_global_header00006660000000000000000000000064140105744330014513gustar00rootroot0000000000000052 comment=47b7bb43ccc744b12e75fa57f77c6303ad2dfd32 go-nmsg-0.2.0/000077500000000000000000000000001401057443300130615ustar00rootroot00000000000000go-nmsg-0.2.0/COPYRIGHT000066400000000000000000000003561401057443300143600ustar00rootroot00000000000000Copyright 2017,2018 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/. go-nmsg-0.2.0/LICENSE000066400000000000000000000405261401057443300140750ustar00rootroot00000000000000Mozilla Public License Version 2.0 ================================== 1. Definitions -------------- 1.1. "Contributor" means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software. 1.2. "Contributor Version" means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor's Contribution. 1.3. "Contribution" means Covered Software of a particular Contributor. 1.4. "Covered Software" means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof. 1.5. "Incompatible With Secondary Licenses" means (a) that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or (b) that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License. 1.6. "Executable Form" means any form of the work other than Source Code Form. 1.7. "Larger Work" means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software. 1.8. "License" means this document. 1.9. "Licensable" means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License. 1.10. "Modifications" means any of the following: (a) any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or (b) any new file in Source Code Form that contains any Covered Software. 1.11. "Patent Claims" of a Contributor means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version. 1.12. "Secondary License" means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses. 1.13. "Source Code Form" means the form of the work preferred for making modifications. 1.14. "You" (or "Your") means an individual or a legal entity exercising rights under this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, "control" means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. 2. License Grants and Conditions -------------------------------- 2.1. Grants Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: (a) under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and (b) under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version. 2.2. Effective Date The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution. 2.3. Limitations on Grant Scope The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor: (a) for any code that a Contributor has removed from Covered Software; or (b) for infringements caused by: (i) Your and any other third party's modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or (c) under Patent Claims infringed by Covered Software in the absence of its Contributions. This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4). 2.4. Subsequent Licenses No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3). 2.5. Representation Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License. 2.6. Fair Use This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents. 2.7. Conditions Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1. 3. Responsibilities ------------------- 3.1. Distribution of Source Form All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients' rights in the Source Code Form. 3.2. Distribution of Executable Form If You distribute Covered Software in Executable Form then: (a) such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and (b) You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients' rights in the Source Code Form under this License. 3.3. Distribution of a Larger Work You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s). 3.4. Notices You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies. 3.5. Application of Additional Terms You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction. 4. Inability to Comply Due to Statute or Regulation --------------------------------------------------- If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it. 5. Termination -------------- 5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice. 5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate. 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination. ************************************************************************ * * * 6. Disclaimer of Warranty * * ------------------------- * * * * Covered Software is provided under this License on an "as is" * * basis, without warranty of any kind, either expressed, implied, or * * statutory, including, without limitation, warranties that the * * Covered Software is free of defects, merchantable, fit for a * * particular purpose or non-infringing. The entire risk as to the * * quality and performance of the Covered Software is with You. * * Should any Covered Software prove defective in any respect, You * * (not any Contributor) assume the cost of any necessary servicing, * * repair, or correction. This disclaimer of warranty constitutes an * * essential part of this License. No use of any Covered Software is * * authorized under this License except under this disclaimer. * * * ************************************************************************ ************************************************************************ * * * 7. Limitation of Liability * * -------------------------- * * * * Under no circumstances and under no legal theory, whether tort * * (including negligence), contract, or otherwise, shall any * * Contributor, or anyone who distributes Covered Software as * * permitted above, be liable to You for any direct, indirect, * * special, incidental, or consequential damages of any character * * including, without limitation, damages for lost profits, loss of * * goodwill, work stoppage, computer failure or malfunction, or any * * and all other commercial damages or losses, even if such party * * shall have been informed of the possibility of such damages. This * * limitation of liability shall not apply to liability for death or * * personal injury resulting from such party's negligence to the * * extent applicable law prohibits such limitation. Some * * jurisdictions do not allow the exclusion or limitation of * * incidental or consequential damages, so this exclusion and * * limitation may not apply to You. * * * ************************************************************************ 8. Litigation ------------- Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party's ability to bring cross-claims or counter-claims. 9. Miscellaneous ---------------- This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor. 10. Versions of the License --------------------------- 10.1. New Versions Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number. 10.2. Effect of New Versions You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward. 10.3. Modified Versions If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License). 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached. Exhibit A - Source Code Form License Notice ------------------------------------------- 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/. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. Exhibit B - "Incompatible With Secondary Licenses" Notice --------------------------------------------------------- This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. go-nmsg-0.2.0/README.md000066400000000000000000000026361401057443300143470ustar00rootroot00000000000000# Pure Golang NMSG Library `go-nmsg` is a pure go implementation of the NMSG container and payload format used by the C [nmsg](https://github.com/farsightsec/nmsg) toolkit and library. ## Synopsis import "github.com/farsightsec/go-nmsg" import "github.com/farsightsec/go-nmsg/nmsg_base" var r io.Reader var w io.Writer ... input := nmsg.NewInput(r, mtu) output := nmsg.BufferedOutput(w) output.SetMaxSize(nmsg.MaxContainerSize, 0) for { payload, err := input.Recv() if err != nil { if nmsg.IsDataError(err) { continue } break } message := payload.Message() switch message.(type) { case *nmsg_base.Dnstap: // process dnstap // write copy to output output.Send(payload) } } output.Close() ## Requirements `go-nmsg` requires the following open source libraries: "google.golang.org/protobuf/proto" "github.com/dnstap/golang-dnstap" ## Limitations `go-nmsg` can pack and unpack the protobuf structure of an NMSG payload, and the protobuf structure of the data contained in the payload. It does not implement the full functionality of the C libnmsg message modules, such as: * Advanced field types (e.g., a protobuf []byte as an IP address) * Generated fields * Formatting of fields for presentation and JSON output Applications needing such functionality in go should use the `cgo-nmsg` package included in this distribution under: "github.com/farsightsec/go-nmsg/cgo-nmsg" go-nmsg-0.2.0/cgo-nmsg/000077500000000000000000000000001401057443300145735ustar00rootroot00000000000000go-nmsg-0.2.0/cgo-nmsg/README.md000066400000000000000000000006651401057443300160610ustar00rootroot00000000000000# Golang bindings for NMSG `cgo-nmsg` provides Golang bindings to the C libnmsg library. The NMSG network message encapsulation library format is an efficient encoding of typed, structured data into payloads which are packed into containers which can be transmitted over the network or stored to disk. For more information, see https://github.com/farsightsec/nmsg/. A pure but limited Golang NMSG library is available with `go-nmsg`. go-nmsg-0.2.0/cgo-nmsg/callbacks.go000066400000000000000000000050571401057443300170500ustar00rootroot00000000000000/* * 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 #include */ import "C" import ( "fmt" "runtime" "sync" "unsafe" ) type outCbEntry struct { index int Output } type inCbEntry struct { index int Input } var cbm sync.Mutex var outCbTable []Output var inCbTable []Input // The C library may not hold a pointer to a Go variable, but we // need to store enough context in the callback user data to find // the go object which registered the callback. We solve this by // allocating memory on the C side (with C.malloc, C.calloc) and // storing a value in that memory which we can use to look up the // Go value on the Go side. // // The approach we take here is to have a package-global list of // Output and Input, and store the index in the list as a C.int // in C-allocated memory. The location of this memory is returned // as an unsafe.Pointer suitable for passing to the (void *user) // argument of libnmsg callback registration functions. func registerOutput(o Output) unsafe.Pointer { cbm.Lock() defer cbm.Unlock() idx := len(outCbTable) outCbTable = append(outCbTable, o) idxptr := C.calloc(C.size_t(1), C.size_t(unsafe.Sizeof(C.int(1)))) *(*C.int)(idxptr) = C.int(idx) return idxptr } func registerInput(i Input) unsafe.Pointer { cbm.Lock() defer cbm.Unlock() idx := len(inCbTable) inCbTable = append(inCbTable, i) idxptr := C.calloc(C.size_t(1), C.size_t(unsafe.Sizeof(C.int(1)))) *(*C.int)(idxptr) = C.int(idx) return idxptr } //export outputCallback func outputCallback(msg C.nmsg_message_t, user unsafe.Pointer) { idx := int(*(*C.int)(user)) if idx < len(outCbTable) { o := outCbTable[idx] o.Write(messageFromC(msg)) return } panic(fmt.Sprintf("outputCallback: invalid index %d", idx)) } //export inputCallback func inputCallback(msg, user unsafe.Pointer) C.nmsg_res { idx := int(*(*C.int)(user)) if idx < len(inCbTable) { i := inCbTable[idx] for { m, err := i.Read() if ErrorRetry(err) { continue } if err != nil { *(*C.nmsg_message_t)(msg) = nil if e, ok := err.(nmsgResError); ok { return C.nmsg_res(e) } return C.nmsg_res_failure } runtime.SetFinalizer(m, nil) *(*C.nmsg_message_t)(msg) = m.message return C.nmsg_res_success } } panic(fmt.Sprintf("inputCallback: invalid index %d", idx)) } go-nmsg-0.2.0/cgo-nmsg/container.go000066400000000000000000000147041401057443300171120ustar00rootroot00000000000000/* * 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 #include */ import "C" import ( "crypto/rand" "encoding/binary" "io" "runtime" "sync" "unsafe" ) // A Container is a collection of NMSG payloads with a target size. type Container struct { config ContainerConfig sequenceID uint64 sequenceNumber uint32 container C.nmsg_container_t } // ContainerConfig contains type ContainerConfig struct { Compress bool Sequence bool Size int } // NewContainer creates a container with the given target size. func NewContainer(conf *ContainerConfig) *Container { c := &Container{config: *conf, container: C.nmsg_container_init(C.size_t(conf.Size))} runtime.SetFinalizer(c, func(c *Container) { C.nmsg_container_destroy(&c.container) }) if conf.Sequence { C.nmsg_container_set_sequence(c.container, C.bool(true)) binary.Read(rand.Reader, binary.BigEndian, &c.sequenceID) } return c } // ErrorFull returns true if the container is full. If the Container Add() // method returns such an error, the message will need to be added to the // next container. func ErrorFull(err error) bool { t, ok := err.(nmsgResError) return ok && t == nmsgResError(C.nmsg_res_container_full) } // ErrorOverfull returns true if the container contains a single payload // and its size is greater than the target size. func ErrorOverfull(err error) bool { t, ok := err.(nmsgResError) return ok && t == nmsgResError(C.nmsg_res_container_overfull) } // Add adds the supplied Message to the Container. func (c *Container) Add(m *Message) error { return nmsgError(C.nmsg_container_add(c.container, m.message)) } // Bytes returns the serialized container and resets the container. func (c *Container) Bytes() []byte { var pbuf *C.uint8_t var pbufLen C.size_t res := C.nmsg_container_serialize(c.container, &pbuf, &pbufLen, C.bool(true), C.bool(c.config.Compress), C.uint32_t(c.sequenceNumber), C.uint64_t(c.sequenceID), ) defer C.free(unsafe.Pointer(pbuf)) if err := nmsgError(res); err != nil { return nil } c.sequenceID++ C.nmsg_container_destroy(&c.container) c.container = C.nmsg_container_init(C.size_t(c.config.Size)) if c.config.Sequence { C.nmsg_container_set_sequence(c.container, C.bool(true)) } return C.GoBytes(unsafe.Pointer(pbuf), C.int(pbufLen)) } // UnpackContainer returns the messages the container contains. func UnpackContainer(b []byte) ([]*Message, error) { var msgarray *C.nmsg_message_t var nmsgs C.size_t res := C.nmsg_container_deserialize( (*C.uint8_t)(unsafe.Pointer(&b[0])), C.size_t(len(b)), &msgarray, &nmsgs) if err := nmsgError(res); err != nil { return nil, err } msgs := make([]*Message, 0, int(nmsgs)) p := unsafe.Pointer(msgarray) for i := 0; i < int(nmsgs); i++ { mp := unsafe.Pointer(uintptr(p) + uintptr(i)*unsafe.Sizeof(*msgarray)) msgs = append(msgs, messageFromC(*(*C.nmsg_message_t)(mp))) } C.free(unsafe.Pointer(msgarray)) return msgs, nil } // A ContainerOutput writes containers to a generic io.Writer. No fragmentation // of oversize containers is performed. type containerOutput struct { mu sync.Mutex w io.Writer c *Container rate *Rate buffered bool empty bool filtervendor uint32 filtermsgtype uint32 source uint32 operator uint32 group uint32 } // NewContainerOutput creates a ContainerOutput writing to the supplied // io.Writer with the given buffer size. func newContainerOutput(w io.Writer, size int) *containerOutput { return &containerOutput{ c: NewContainer(&ContainerConfig{ Size: size, Sequence: true, }), buffered: true, empty: true, w: w, } } func (co *containerOutput) Write(m *Message) error { for { vid, msgtype := m.GetMsgtype() if co.filtervendor > 0 && co.filtervendor != vid { return nil } if co.filtermsgtype > 0 && co.filtermsgtype != msgtype { return nil } if co.source > 0 { m.SetSource(co.source) } if co.operator > 0 { m.SetOperator(co.operator) } if co.group > 0 { m.SetGroup(co.group) } co.mu.Lock() err := co.c.Add(m) if co.buffered && err == nil { co.empty = false co.mu.Unlock() return nil } _, werr := co.w.Write(co.c.Bytes()) co.empty = true r := co.rate co.mu.Unlock() if r != nil { r.Sleep() } if werr == nil && ErrorFull(err) { continue } return werr } } // SetFilterMsgtype instructs the output to only accept Messages // with the given vendor and messagetype, specified by id. func (co *containerOutput) SetFilterMsgtype(vendor, msgtype uint32) { co.filtervendor = vendor co.filtermsgtype = msgtype } // SetFilterMsgtypeByname instructs the output to only accept Messages // with the given vendor and messagetype, specified by name. func (co *containerOutput) SetFilterMsgtypeByname(vendor, msgtype string) { cvendor := C.CString(vendor) cmsgtype := C.CString(msgtype) defer C.free(unsafe.Pointer(cvendor)) defer C.free(unsafe.Pointer(cmsgtype)) cvid := C.nmsg_msgmod_vname_to_vid(cvendor) co.filtervendor = uint32(cvid) co.filtermsgtype = uint32(C.nmsg_msgmod_mname_to_msgtype(cvid, cmsgtype)) } func (co *containerOutput) SetRate(r *Rate) { co.mu.Lock() co.rate = r co.mu.Unlock() } func (co *containerOutput) SetSource(source uint32) { co.source = source } func (co *containerOutput) SetOperator(op uint32) { co.operator = op } func (co *containerOutput) SetGroup(group uint32) { co.group = group } // Flush writes any buffered output to the underlying writer. func (co *containerOutput) Flush() error { co.mu.Lock() written := false defer func() { r := co.rate co.mu.Unlock() if written && r != nil { r.Sleep() } }() if !co.empty { _, werr := co.w.Write(co.c.Bytes()) co.empty = true written = true return werr } return nil } // SetBuffered controls whether the ContainerOutput collects // multiple messages into a container (buffered == true, the // default), or sends a container per message (buffered == false). func (co *containerOutput) SetBuffered(buffered bool) { co.buffered = buffered } // SetCompression controls whether the containers are compressed // before sending. func (co *containerOutput) SetCompression(compress bool) { co.c.config.Compress = compress } go-nmsg-0.2.0/cgo-nmsg/error.go000066400000000000000000000015521401057443300162560ustar00rootroot00000000000000/* * 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 */ import "C" // NmsgError encapsulates an error condition type nmsgResError C.nmsg_res func (n nmsgResError) Error() string { return C.GoString(C.nmsg_res_lookup(uint32(n))) } func nmsgError(res C.nmsg_res) error { if res == C.nmsg_res_success { return nil } return nmsgResError(res) } // ErrorRetry returns true if the error indicates that the nmsg // operation should be retried. func ErrorRetry(err error) bool { if ne, ok := err.(nmsgResError); ok { return ne == nmsgResError(C.nmsg_res_again) } return false } go-nmsg-0.2.0/cgo-nmsg/input.go000066400000000000000000000104671401057443300162710ustar00rootroot00000000000000/* * 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 #include extern nmsg_res inputCallback(nmsg_message_t *msg, void *user); nmsg_res input_callback(nmsg_message_t *msg, void *user) { return inputCallback(msg, user); } */ import "C" import ( "io" "net" "os" "unsafe" ) // An Input is a source of NMSG payloads (Messages). type Input interface { // Read returns a Message or nil, and an error if any. Read() (*Message, error) // SetFilterMsgtype instructs the input to discard all Messages // not of the given vendor id and msgtype, specified by number. SetFilterMsgtype(vendor, msgtype uint32) // SetFilterMsgtypeByname instructs the input to discard all Messages // not of the given vendor id and msgtype, specified by name. SetFilterMsgtypeByname(vendor, msgtype string) // SetFilterSource instructs the input to discard all Messages not // from the supplied source. SetFilterSource(source uint32) // SetFilterOperator instructs the input to discard all Messages not // from the supplied operator. SetFilterOperator(operator uint32) // SetFilterGroup instructs the input to discard all Messages not // in the supplied group. SetFilterGroup(group uint32) } // NmsgInput is an Input managed by libnmsg. It satisfies // the Input interface, and has type nmsgInput struct { file *os.File input C.nmsg_input_t } func (i *nmsgInput) Read() (*Message, error) { var msg C.nmsg_message_t res := C.nmsg_input_read(i.input, &msg) if res == C.nmsg_res_success { return messageFromC(msg), nil } return nil, nmsgError(res) } func (i *nmsgInput) SetFilterMsgtype(vid, msgtype uint32) { C.nmsg_input_set_filter_msgtype(i.input, C.uint(vid), C.uint(msgtype)) } func (i *nmsgInput) SetFilterMsgtypeByname(vendor, msgtype string) { cname := C.CString(vendor) ctype := C.CString(msgtype) C.nmsg_input_set_filter_msgtype_byname(i.input, cname, ctype) C.free(unsafe.Pointer(cname)) C.free(unsafe.Pointer(ctype)) } func (i *nmsgInput) SetFilterSource(source uint32) { C.nmsg_input_set_filter_source(i.input, C.uint(source)) } func (i *nmsgInput) SetFilterOperator(operator uint32) { C.nmsg_input_set_filter_operator(i.input, C.uint(operator)) } func (i *nmsgInput) SetFilterGroup(group uint32) { C.nmsg_input_set_filter_group(i.input, C.uint(group)) } // NewInput creates a new Input from an io.Reader. // Currently, the reader must be a *net.UDPConn or a *os.File func NewInput(r io.Reader) Input { switch r := r.(type) { case *net.UDPConn: f, err := r.File() if err != nil { return nil } return &nmsgInput{f, C.nmsg_input_open_sock(C.int(f.Fd()))} case *os.File: return &nmsgInput{r, C.nmsg_input_open_file(C.int(r.Fd()))} default: return nil // return &containerReader{Reader: r} } } // NewCallbackInput creates an NmsgInput which calls the supplied InputFunc. func NewCallbackInput(i InputFunc) Input { return &nmsgInput{ file: nil, input: C.nmsg_input_open_callback(C.nmsg_cb_message_read(C.input_callback), registerInput(i)), } } // An InputFunc is a function with the same signature as Input.Read(), usable // directly as an Input. // // When used directly as an Input, only the Read() method is implemented. All // others are no-ops. If the functionality of the other methods is desired, // the InputFunc can be passed to NewCallbackInput. type InputFunc func() (*Message, error) // Read calls the underlying function to return the next message. func (i InputFunc) Read() (*Message, error) { return i() } // SetFilterMsgtype satisfies the Input interface with a no-op func (i InputFunc) SetFilterMsgtype(vendor, msgtype uint32) {} // SetFilterMsgtypeByname satisfies the Input interface with a no-op func (i InputFunc) SetFilterMsgtypeByname(vendor, msgtype string) {} // SetFilterSource satisfies the Input interface with a no-op func (i InputFunc) SetFilterSource(source uint32) {} // SetFilterOperator satisfies the Input interface with a no-op func (i InputFunc) SetFilterOperator(operator uint32) {} // SetFilterGroup satisfies the Input interface with a no-op func (i InputFunc) SetFilterGroup(group uint32) {} go-nmsg-0.2.0/cgo-nmsg/io.go000066400000000000000000000050441401057443300155340ustar00rootroot00000000000000/* * 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 #include */ 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) } go-nmsg-0.2.0/cgo-nmsg/message.go000066400000000000000000000256421401057443300165570ustar00rootroot00000000000000/* * 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 #include const char *endline="\n"; unsigned flag_repeated = NMSG_MSGMOD_FIELD_REPEATED; */ import "C" import ( "fmt" "net" "runtime" "unsafe" ) // MessageMod something something type MessageMod struct { nmsgMsgMod C.nmsg_msgmod_t } // MessageModLookupByName something something func MessageModLookupByName(vname, mname string) *MessageMod { vstr := C.CString(vname) mstr := C.CString(mname) defer C.free(unsafe.Pointer(vstr)) defer C.free(unsafe.Pointer(mstr)) return &MessageMod{C.nmsg_msgmod_lookup_byname(vstr, mstr)} } // MessageModLookup something something func MessageModLookup(v, m uint32) *MessageMod { return &MessageMod{C.nmsg_msgmod_lookup(C.uint(v), C.uint(m))} } // A Message is a unit of NMSG data. type Message struct { message C.nmsg_message_t } // NewMessage initializes a message of a type given by // the supplied MessageMod func NewMessage(mod *MessageMod) *Message { return messageFromC(C.nmsg_message_init(mod.nmsgMsgMod)) } // NewMessageFromPayload encapsulates a byte buffer in a payload with // the supplied vendor and message type. func NewMessageFromPayload(payload []byte, vendor uint32, msgtype uint32) *Message { Csiz := C.size_t(len(payload)) // C.CString allocates a buffer to hold the copy of payload // built by string. This buffer is passed to nmsg_message_from_raw_payload, // which takes ownership of the buffer. It will be freed when // nmsg_message_destroy() is called by the Message finalizer. Cbuf := unsafe.Pointer(C.CString(string(payload))) return messageFromC(C.nmsg_message_from_raw_payload( C.unsigned(vendor), C.unsigned(msgtype), (*C.uint8_t)(Cbuf), Csiz, nil)) } func messageDestroy(m *Message) { C.nmsg_message_destroy(&m.message) } // GetMsgtype returns the vendor and payload type of the message. func (msg *Message) GetMsgtype() (vendor, msgtype uint32) { vendor = uint32(C.nmsg_message_get_vid(msg.message)) msgtype = uint32(C.nmsg_message_get_msgtype(msg.message)) return } // Source returns the source id of the message, or zero if the source id // is not set. func (msg *Message) Source() uint32 { return uint32(C.nmsg_message_get_source(msg.message)) } // SetSource sets the source id of the message. func (msg *Message) SetSource(source uint32) { C.nmsg_message_set_source(msg.message, C.uint32_t(source)) } // Operator returns the operator id of the message, or zero if the operator id // is not set. func (msg *Message) Operator() uint32 { return uint32(C.nmsg_message_get_operator(msg.message)) } // SetOperator sets the operator id of the message. func (msg *Message) SetOperator(operator uint32) { C.nmsg_message_set_operator(msg.message, C.uint32_t(operator)) } // Group returns the group id of the message, or zero if the group id // is not set. func (msg *Message) Group() uint32 { return uint32(C.nmsg_message_get_group(msg.message)) } // SetGroup sets the group id of the message. func (msg *Message) SetGroup(group uint32) { C.nmsg_message_set_group(msg.message, C.uint32_t(group)) } func messageFromC(message C.nmsg_message_t) *Message { msg := &Message{message} runtime.SetFinalizer(msg, messageDestroy) return msg } // MarshalJSON formats a JSON representation of the Message func (msg *Message) MarshalJSON() ([]byte, error) { var jsonCstr *C.char err := nmsgError(C.nmsg_message_to_json(msg.message, &jsonCstr)) defer C.free(unsafe.Pointer(jsonCstr)) if err != nil { return nil, err } return []byte(C.GoString(jsonCstr)), nil } // UnmarshalJSON parses a JSON representation of the Message func (msg *Message) UnmarshalJSON(b []byte) error { jsonCstr := C.CString(string(b)) defer C.free(unsafe.Pointer(jsonCstr)) return nmsgError(C.nmsg_message_from_json(jsonCstr, &msg.message)) } // MarshalText converts a Message to presentation format. func (msg *Message) MarshalText() ([]byte, error) { var presCstr *C.char err := nmsgError(C.nmsg_message_to_pres(msg.message, &presCstr, C.endline)) defer C.free(unsafe.Pointer(presCstr)) if err != nil { return nil, err } return []byte(C.GoString(presCstr)), nil } // Enum contains both the numeric Value and the string Description of // an enumerated NMSG field value. type Enum struct { Value uint32 Description string } type fieldValue struct { typ C.nmsg_msgmod_field_type buf unsafe.Pointer size C.int } func (msg *Message) getFieldValue(name string, idx int) (fv fieldValue, err error) { var Csize C.size_t Cname := C.CString(name) defer C.free(unsafe.Pointer(Cname)) Cidx := C.uint(uint(idx)) res := C.nmsg_message_get_field_type(msg.message, Cname, &fv.typ) if err = nmsgError(res); err != nil { return } res = C.nmsg_message_get_field(msg.message, Cname, Cidx, &fv.buf, &Csize) if err = nmsgError(res); err != nil { return } fv.size = C.int(Csize) return } func (msg *Message) setFieldValue(name string, idx int, buf unsafe.Pointer, size int) error { Cname := C.CString(name) defer C.free(unsafe.Pointer(Cname)) Cidx := C.uint(uint(idx)) Csize := C.size_t(size) return nmsgError(C.nmsg_message_set_field(msg.message, Cname, Cidx, (*C.uint8_t)(buf), Csize)) } // GetUintField retrieves the named field of a unsigned int type from a Message. // If the field has an enumerated type, the numeric value is retrieved. func (msg *Message) GetUintField(name string, idx int) (uint64, error) { fv, err := msg.getFieldValue(name, idx) if err != nil { return 0, err } switch fv.typ { case C.nmsg_msgmod_ft_uint16: return uint64(*(*uint16)(fv.buf)), nil case C.nmsg_msgmod_ft_uint32: fallthrough case C.nmsg_msgmod_ft_enum: return uint64(*(*uint32)(fv.buf)), nil case C.nmsg_msgmod_ft_uint64: return *(*uint64)(fv.buf), nil default: return 0, fmt.Errorf("Field %s not of uint type", name) } } // SetUintField sets the value of a field of type uint16, uint32, or uint64. // The bitsize parameter specifies which type, and must be 16, 32, or 64 func (msg *Message) SetUintField(name string, idx, bitsize int, val uint64) error { switch bitsize { case 16: v := uint16(val) return msg.setFieldValue(name, idx, unsafe.Pointer(&v), bitsize) case 32: v := uint32(val) return msg.setFieldValue(name, idx, unsafe.Pointer(&v), bitsize) case 64: v := uint64(val) return msg.setFieldValue(name, idx, unsafe.Pointer(&v), bitsize) default: return fmt.Errorf("Invalid bitsize %d", bitsize) } } // GetIntField retrieves the value of a named field of integer type from // a Message. func (msg *Message) GetIntField(name string, idx int) (int64, error) { fv, err := msg.getFieldValue(name, idx) if err != nil { return 0, err } switch fv.typ { case C.nmsg_msgmod_ft_int16: return int64(*(*int16)(fv.buf)), nil case C.nmsg_msgmod_ft_int32: return int64(*(*int32)(fv.buf)), nil case C.nmsg_msgmod_ft_int64: return *(*int64)(fv.buf), nil default: return 0, fmt.Errorf("Field %s not of int type", name) } } // SetIntField sets the value of an int16, int32, or int64 field in the message. // The bitsize field specifies which size, and must by 16, 32, or 64 func (msg *Message) SetIntField(name string, idx, bitsize int, val int64) error { switch bitsize { case 16: v := int16(val) return msg.setFieldValue(name, idx, unsafe.Pointer(&v), bitsize) case 32: v := int32(val) return msg.setFieldValue(name, idx, unsafe.Pointer(&v), bitsize) case 64: v := int64(val) return msg.setFieldValue(name, idx, unsafe.Pointer(&v), bitsize) default: return fmt.Errorf("Invalid bitsize %d", bitsize) } } // GetBytesField retrieves a field of type bytes from a Message. func (msg *Message) GetBytesField(name string, idx int) ([]byte, error) { fv, err := msg.getFieldValue(name, idx) if err != nil { return nil, err } if fv.typ != C.nmsg_msgmod_ft_bytes { return nil, fmt.Errorf("Field %s not of bytes type", name) } return C.GoBytes(fv.buf, fv.size), nil } // SetBytesField sets the value of a bytes field in a Message func (msg *Message) SetBytesField(name string, idx int, val []byte) error { Cbuf := unsafe.Pointer(&val[0]) return msg.setFieldValue(name, idx, Cbuf, len(val)) } // GetStringField retrieves the value of a string field in a Message func (msg *Message) GetStringField(name string, idx int) (string, error) { fv, err := msg.getFieldValue(name, idx) if err != nil { return "", err } return C.GoStringN((*C.char)(fv.buf), fv.size), nil } // SetStringField sets the value of a string field in a Message func (msg *Message) SetStringField(name string, idx int, val string) error { b := []byte(val) Cbuf := unsafe.Pointer(&b[0]) return msg.setFieldValue(name, idx, Cbuf, len(val)) } // GetIPField retrieves the value of an IP field in a Message func (msg *Message) GetIPField(name string, idx int) (net.IP, error) { fv, err := msg.getFieldValue(name, idx) if err != nil { return nil, err } if fv.typ != C.nmsg_msgmod_ft_ip { return nil, fmt.Errorf("Field %s not of iptype", name) } return net.IP(C.GoBytes(fv.buf, fv.size)), nil } // SetIPField sets the value of an IP field in a Message func (msg *Message) SetIPField(name string, idx int, val net.IP) error { Cbuf := unsafe.Pointer(&val[0]) return msg.setFieldValue(name, idx, Cbuf, len(val)) } // GetDoubleField retrieves the value of a double field in a Message func (msg *Message) GetDoubleField(name string, idx int) (float64, error) { fv, err := msg.getFieldValue(name, idx) if err != nil { return 0, err } if fv.typ != C.nmsg_msgmod_ft_double { return 0, fmt.Errorf("Field %s is not of double type", name) } return *(*float64)(fv.buf), nil } // SetDoubleField sets the value of a double field in a Message func (msg *Message) SetDoubleField(name string, idx int, val float64) error { Cbuf := unsafe.Pointer(&val) return msg.setFieldValue(name, idx, Cbuf, 8) } // GetEnumField returns the string description of a Message field // with an enumerated type. func (msg *Message) GetEnumField(name string, idx int) (string, error) { enumValue, err := msg.GetUintField(name, idx) if err != nil { return "", err } Cname := C.CString(name) defer C.free(unsafe.Pointer(Cname)) var Ename *C.char res := C.nmsg_message_enum_value_to_name( msg.message, Cname, C.unsigned(enumValue), &Ename, ) if err = nmsgError(res); err != nil { return "", err } return C.GoString(Ename), nil } // SetEnumField sets the value of the named Message field to the value // corresponding to the supplied description. func (msg *Message) SetEnumField(name string, idx int, vname string) error { Cname := C.CString(name) defer C.free(unsafe.Pointer(Cname)) Cvname := C.CString(vname) defer C.free(unsafe.Pointer(Cvname)) var v C.uint res := C.nmsg_message_enum_name_to_value(msg.message, Cname, Cvname, &v) if err := nmsgError(res); err != nil { return err } return msg.SetUintField(name, idx, 32, uint64(v)) } go-nmsg-0.2.0/cgo-nmsg/nmsg.go000066400000000000000000000020501401057443300160630ustar00rootroot00000000000000/* * 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 int nmsg_wbufsiz_min = NMSG_WBUFSZ_MIN; int nmsg_wbufsiz_max = NMSG_WBUFSZ_MAX; int nmsg_wbufsiz_ether = NMSG_WBUFSZ_ETHER; int nmsg_wbufsiz_jumbo = NMSG_WBUFSZ_JUMBO; */ import "C" func init() { if C.nmsg_init() != C.nmsg_res_success { panic("failed to initialize nmsg library") } } // Buffer Size constants from libnmsg var ( BufferSizeMax = int(C.nmsg_wbufsiz_max) BufferSizeMin = int(C.nmsg_wbufsiz_min) BufferSizeEther = int(C.nmsg_wbufsiz_ether) BufferSizeJumbo = int(C.nmsg_wbufsiz_jumbo) ) // SetDebug sets the debug print level for the nmsg library. // Debugging messages are sent to stderr. Higher debug values // increase verbosity. func SetDebug(debug int) { C.nmsg_set_debug(C.int(debug)) } go-nmsg-0.2.0/cgo-nmsg/output.go000066400000000000000000000132341401057443300164650ustar00rootroot00000000000000/* * 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 #include extern void outputCallback(nmsg_message_t, void *); void output_callback(nmsg_message_t msg, void *user) { outputCallback(msg, user); } */ import "C" import ( "io" "net" "os" "unsafe" ) // An Output is a destination for NMSG data (Messages) type Output interface { // Write sends the supplied message to the Output. Write(*Message) error // SetBuffered controls whether the output buffers Messages into containers // before sending them. NmsgOutputs are buffered by default, but low volume // sources may choose to turn this off to reduce latency. SetBuffered(bool) // SetCompression controls whether the output compresses // the container data prior to sending. SetCompression(bool) // Flush writes any buffered data to the Output. Flush() error // SetFilterMsgtype instructs the output to discard all Messages // not of the supplied vendor and type, specified by number. SetFilterMsgtype(vendor, msgtype uint32) // SetFilterMsgtypeByname instructs the output to discard all Messages // not of the supplied vendor and type, specified by name. SetFilterMsgtypeByname(vendor, msgtype string) // SetRate sets an output rate limit. The rate is specified // in containers per second, and is checked every freq pauses. // The freq parameter should be about 10-15% of the rate. SetRate(rate *Rate) // SetSource instructs the output to set the source parameter // of all outbound messages to the supplied value. SetSource(source uint32) // SetOperator instructs the output to set the operator parameter // of all outbound messages to the supplied value. SetOperator(group uint32) // SetGroup instructs the output to set the group parameter // of all outbound messages to the supplied value. SetGroup(group uint32) } // An NmsgOutput is an output managed by the nmsg library. type nmsgOutput struct { file *os.File rate *Rate output C.nmsg_output_t } func (o *nmsgOutput) Write(m *Message) error { return nmsgError(C.nmsg_output_write(o.output, m.message)) } func (o *nmsgOutput) SetBuffered(buffered bool) { C.nmsg_output_set_buffered(o.output, C.bool(buffered)) } func (o *nmsgOutput) SetFilterMsgtype(vid, msgtype uint32) { C.nmsg_output_set_filter_msgtype(o.output, C.uint(vid), C.uint(msgtype)) } func (o *nmsgOutput) SetFilterMsgtypeByname(vendor, msgtype string) { cname := C.CString(vendor) ctype := C.CString(msgtype) C.nmsg_output_set_filter_msgtype_byname(o.output, cname, ctype) C.free(unsafe.Pointer(cname)) C.free(unsafe.Pointer(ctype)) } func (o *nmsgOutput) SetRate(r *Rate) { if r == nil { C.nmsg_output_set_rate(o.output, nil) } else { C.nmsg_output_set_rate(o.output, r.rate) } // keep a reference to avoid calling the finalizer o.rate = r } func (o *nmsgOutput) SetSource(source uint32) { C.nmsg_output_set_source(o.output, C.uint(source)) } func (o *nmsgOutput) SetOperator(operator uint32) { C.nmsg_output_set_operator(o.output, C.uint(operator)) } func (o *nmsgOutput) SetGroup(group uint32) { C.nmsg_output_set_group(o.output, C.uint(group)) } func (o *nmsgOutput) SetCompression(compress bool) { C.nmsg_output_set_zlibout(o.output, C.bool(compress)) } func (o *nmsgOutput) Flush() error { return nmsgError(C.nmsg_output_flush(o.output)) } // NewOutput creates an output writing to w, with target // container size of bufsiz. The Writer currently must be a // *os.File or *net.UDPConn. func NewOutput(w io.Writer, bufsiz int) Output { switch w := w.(type) { case *net.UDPConn: f, err := w.File() if err != nil { return nil } return &nmsgOutput{f, nil, C.nmsg_output_open_sock(C.int(f.Fd()), C.size_t(bufsiz))} case *os.File: return &nmsgOutput{w, nil, C.nmsg_output_open_file(C.int(w.Fd()), C.size_t(bufsiz))} default: return newContainerOutput(w, bufsiz) } } // NewCallbackOutput creates an NmsgOutput which calls o.Send() // on every message. func NewCallbackOutput(o OutputFunc) Output { return &nmsgOutput{ file: nil, output: C.nmsg_output_open_callback(C.nmsg_cb_message(C.output_callback), registerOutput(o)), } } // An OutputFunc is a function with the same signature as Output.Write, usable // directly as an Output. // // When used directly as an Output, only the Write() method is defined. All others // are no-ops. type OutputFunc func(*Message) error // Write calls the underlying function with the supplied message func (o OutputFunc) Write(m *Message) error { return o(m) } // Flush satisfies the Output interface with a no-op func (o OutputFunc) Flush() error { return nil } // SetBuffered satisfies the Output interface with a no-op func (o OutputFunc) SetBuffered(bool) {} // SetCompression satisfies the Output interface with a no-op func (o OutputFunc) SetCompression(bool) {} // SetFilterMsgtype satisfies the Output interface with a no-op func (o OutputFunc) SetFilterMsgtype(vendor, msgtype uint32) {} // SetFilterMsgtypeByname satisfies the Output interface with a no-op func (o OutputFunc) SetFilterMsgtypeByname(vendor, msgtype string) {} // SetRate satisfies the Output interface with a no-op func (o OutputFunc) SetRate(r *Rate) {} // SetSource satisfies the Output interface with a no-op func (o OutputFunc) SetSource(source uint32) {} // SetOperator satisfies the Output interface with a no-op func (o OutputFunc) SetOperator(group uint32) {} // SetGroup satisfies the Output interface with a no-op func (o OutputFunc) SetGroup(group uint32) {} go-nmsg-0.2.0/cgo-nmsg/rate.go000066400000000000000000000020671401057443300160620ustar00rootroot00000000000000/* * 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 #include */ import "C" import "runtime" // A Rate provides Rate limiting across one or more outputs. type Rate struct{ rate C.nmsg_rate_t } // NewRate initializes and returns a rate context. The rate parameter // specifies the target rate of packets (containers and fragments) sent // on all outputs using the Rate. The freq parameter specifies how often // (in packets) to check the rate limit. func NewRate(rate, freq uint) *Rate { r := &Rate{C.nmsg_rate_init(C.uint(rate), C.uint(freq))} runtime.SetFinalizer(r, func(r *Rate) { C.nmsg_rate_destroy(&r.rate) }) return r } // Sleep pauses for an appropriate amount of time to maintain the given // output rate. func (r *Rate) Sleep() { C.nmsg_rate_sleep(r.rate) } go-nmsg-0.2.0/cgo-nmsg/xs.go000066400000000000000000000021361401057443300155560ustar00rootroot00000000000000// +build libxs /* * 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 libxs #cgo LDFLAGS: -lnmsg -lxs #include #include #include */ import "C" import "unsafe" var xsContext unsafe.Pointer func init() { xsContext = C.xs_init() } // NewXSInput opens an Input reading from the given XS endpoint. func NewXSInput(xep string) Input { cxep := C.CString(xep) defer C.free(unsafe.Pointer(cxep)) inp := C.nmsg_input_open_xs_endpoint(xsContext, cxep) if inp == nil { return nil } return &nmsgInput{input: inp} } // NewXSOutput creates an output writing to the given XS endpoint. func NewXSOutput(xep string, bufsiz int) Output { cxep := C.CString(xep) defer C.free(unsafe.Pointer(cxep)) outp := C.nmsg_output_open_xs_endpoint(xsContext, cxep, C.size_t(bufsiz)) if outp == nil { return nil } return &nmsgOutput{output: outp} } go-nmsg-0.2.0/container.go000066400000000000000000000212311401057443300153710ustar00rootroot00000000000000/* * Copyright (c) 2017,2018 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 import ( "bytes" "encoding/binary" "errors" "fmt" "io" "math/rand" "google.golang.org/protobuf/proto" ) const ( nmsgVersion = 2 nmsgFlagZlib = 1 nmsgFlagFragment = 2 headerSize = 10 ) var ( nmsgMagic = [4]byte{'N', 'M', 'S', 'G'} errBadMagic = errors.New("Bad NMSG Magic Number") containerOverhead = 10 fragmentOverhead = 10 + 4 + 24 ) type containerHeader struct { Magic [4]byte Flags, Version byte Length uint32 } // isCompressed() and isFragmented() are helper functions for readability. func (h *containerHeader) isCompressed() bool { return h.Flags&nmsgFlagZlib != 0 } func (h *containerHeader) isFragmented() bool { return h.Flags&nmsgFlagFragment != 0 } // A Container encapsulates an Nmsg envelope, and maintains metadata for // sizing containers as payloads are added. type Container struct { // Maximum size of a container. AddPayload attempts to keep the container // under this size. maxSize int // Maximum size of fragment or container. Any containers larger than this // will be fragmented by WriteTo. writeSize int // If true, compress container contents before writing. compress bool // If true, container was populated from compressed data // This is primarily used in fragment reassembly to detect whether the // fragmented data was compressed prior to fragmentation. isCompressed bool // If nonzero, an estimate of the effectiveness of compression, expressed // as compressedSize / uncompressedSize. Default: 0.5 compressionRatio float32 // The current estimated size of the serialized data, before compression size int Nmsg *NmsgFragment } // NewContainer creates a new empty NMSG container. func NewContainer() *Container { c := &Container{size: containerOverhead} c.SetMaxSize(0, 0) return c } // SetMaxSize sets the maximum size (including Marshaling overhead, // container header, and anticipated compression ratio) of a container. // AddPayload attempts to keep the container within this size. // // writeSize specifies the maximum size of containers or fragments. // Containers larger than writeSize will be written as fragments instead // of single containers. // // A writeSize value of 0 is treated as equal to size. func (c *Container) SetMaxSize(size, writeSize int) { if size < MinContainerSize { size = MinContainerSize } if size > MaxContainerSize { size = MaxContainerSize } if writeSize < size { writeSize = size } c.maxSize = size c.writeSize = writeSize } // SetCompression instructs WriteTo to write containers with compressed // (if true) or uncompressed (if false) contents. func (c *Container) SetCompression(compress bool) { c.compress = compress } // SetCompressionRatio sets an estimated compression ratio for the data. // The default value is 2.0 func (c *Container) SetCompressionRatio(ratio float32) { c.compressionRatio = ratio } // SetSequenced sets or unsets sequencing on the container stream. // The sequence number is updated every time WriteTo() is called. func (c *Container) SetSequenced(sequenced bool) { if sequenced { seqid := uint64(rand.Uint32()) << 32 seqid |= uint64(rand.Uint32()) c.Nmsg.SequenceId = proto.Uint64(seqid) c.Nmsg.Sequence = proto.Uint32(0) } else { c.Nmsg.SequenceId = nil c.Nmsg.Sequence = nil } } // AddPayload adds the supplied NmsgPayload to the Container if possible. // // The return value 'full' is true if the container is full and needs to // be emptied with WriteTo(). // // The return value 'ok' is true if the payload was successfully added to // the container, otherwise, AddPayload() must be called again after WriteTo(). // // Both ok and full may be true if the payload is larger than the container's // MaxSize, or if the container is full after adding the payload. func (c *Container) AddPayload(p *NmsgPayload) (ok, full bool) { limit := c.maxSize if c.compress { if c.compressionRatio > 0 { limit = int(float32(limit) * c.compressionRatio) } else { limit *= 2 } } ps := p.payloadSize() if c.size+ps >= limit { full = true } if !full || c.size == containerOverhead || c.size+ps == limit { ok = true c.size += ps c.Nmsg.Payloads = append(c.Nmsg.Payloads, p) c.Nmsg.PayloadCrcs = append(c.Nmsg.PayloadCrcs, nmsgCRC(p.Payload)) } return } // Reset discards payloads and crcs from the Container func (c *Container) Reset() { c.Nmsg.Payloads = c.Nmsg.Payloads[:0] c.Nmsg.PayloadCrcs = c.Nmsg.PayloadCrcs[:0] c.NmsgFragment = nil } // WriteTo writes the Container to Writer w. If the // container requires fragmentation, it will call // w.Write() multiple times. func (c *Container) WriteTo(w io.Writer) (int64, error) { var buf bytes.Buffer header := containerHeader{ Magic: nmsgMagic, Version: nmsgVersion, } defer c.Reset() b, err := proto.Marshal(&c.Nmsg) if err != nil { return 0, err } if c.compress { b, err = zbufDeflate(b) if err != nil { return 0, err } header.Flags |= nmsgFlagZlib } header.Length = uint32(len(b)) if c.Nmsg.Sequence != nil { *c.Nmsg.Sequence++ } c.size = containerOverhead if len(b)+containerOverhead > c.writeSize { return c.writeFragments(w, b) } if err = binary.Write(&buf, binary.BigEndian, &header); err != nil { return 0, err } if _, err = buf.Write(b); err != nil { return 0, err } return buf.WriteTo(w) } func (c *Container) writeFragments(w io.Writer, b []byte) (int64, error) { header := containerHeader{ Magic: nmsgMagic, Version: nmsgVersion, Flags: nmsgFlagFragment, } if c.compress { header.Flags |= nmsgFlagZlib } fragSize := c.writeSize - fragmentOverhead lastFrag := len(b) / fragSize fragID := rand.Uint32() nf := NmsgFragment{ Id: proto.Uint32(fragID), Current: proto.Uint32(uint32(0)), Last: proto.Uint32(uint32(lastFrag)), Crc: proto.Uint32(nmsgCRC(b)), } var written int64 for i := 0; i <= lastFrag; i++ { var buf bytes.Buffer fblen := len(b) if fblen > fragSize { fblen = fragSize } *nf.Current = uint32(i) nf.Fragment = b[:fblen] b = b[fblen:] fbytes, err := proto.Marshal(&nf) if err != nil { return written, err } header.Length = uint32(len(fbytes)) if err = binary.Write(&buf, binary.BigEndian, header); err != nil { return written, err } if _, err = buf.Write(fbytes); err != nil { return written, err } n, err := buf.WriteTo(w) if err != nil { return written, err } written += n } return written, nil } // ReadFrom Reads a Container from the given io.Reader. It returns the // number of container bytes read on success. func (c *Container) ReadFrom(r io.Reader) (n int64, err error) { /* * The bytes.Buffer Grow() method may panic with ErrTooLarge. * We catch this panic (and any other error panic()s and return * an error. */ defer func() { if r := recover(); r != nil { var ok bool if err, ok = r.(error); !ok { err = fmt.Errorf("nmsg.Container ReadFrom: panic %v", r) } } }() var buf bytes.Buffer var h containerHeader if n, err = io.CopyN(&buf, r, headerSize); err != nil { return n, err } err = binary.Read(&buf, binary.BigEndian, &h) if err != nil { return n, &dataError{err} } if h.Magic != nmsgMagic { return 0, &dataError{errBadMagic} } buf.Grow(int(h.Length)) if n, err = io.CopyN(&buf, r, int64(h.Length)); err != nil { return int64(buf.Len()), err } // err = c.fromBytesHeader(buf.Bytes(), &h) err = c.fromNmsgBytes(buf.Bytes(), h.isCompressed(), h.isFragmented()) if err != nil { err = &dataError{err} } return int64(buf.Len()), err } // FromBytes parses the given buffer as an NMSG container and stores // the result in the receiver *Container. func (c *Container) FromBytes(b []byte) error { var h containerHeader buf := bytes.NewBuffer(b) err := binary.Read(buf, binary.BigEndian, &h) if err != nil { return err } if h.Magic != nmsgMagic { return errBadMagic } return c.fromNmsgBytes(buf.Bytes(), h.isCompressed(), h.isFragmented()) } // fromNmsgBytes parses the contents (b) of an NMSG container, according to // whether the container contents are compressed, fragmented, or both. func (c *Container) fromNmsgBytes(b []byte, compressed, fragmented bool) error { var err error cbytes := b c.isCompressed = compressed if compressed { cbytes, err = zbufInflate(b) if err != nil { return err } } if fragmented { c.NmsgFragment = &NmsgFragment{} return proto.Unmarshal(cbytes, c.NmsgFragment) } c.NmsgFragment = nil return proto.Unmarshal(cbytes, &c.Nmsg) } go-nmsg-0.2.0/container_test.go000066400000000000000000000067271401057443300164450ustar00rootroot00000000000000/* * 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_test // These tests verify container compatibility between C libnmsg (wrapped in // cgo-nmsg) and go-nmsg, both with and without compression. import ( "bytes" "log" "testing" cnmsg "github.com/farsightsec/go-nmsg/cgo-nmsg" "github.com/farsightsec/go-nmsg" "github.com/farsightsec/go-nmsg/nmsg_base" ) func compare(a, b []byte) bool { if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true } func TestContainerGoCgoUnpack(t *testing.T) { b := new(bytes.Buffer) c := nmsg.NewContainer() c.SetMaxSize(nmsg.MinContainerSize, nmsg.MinContainerSize) c.AddPayload(testGoMessage(100)) c.WriteTo(b) m, err := cnmsg.UnpackContainer(b.Bytes()) if err != nil { t.Fatal(err) } if len(m) != 1 { t.Fatalf("message count mismatch %d != 1", len(m)) } if checkCgoMessage(m[0], 100) { return } t.Error("payload mismatch") } func TestContainerGoCgoUnpackCompress(t *testing.T) { b := new(bytes.Buffer) c := nmsg.NewContainer() c.SetCompression(true) c.SetMaxSize(nmsg.MinContainerSize, nmsg.MinContainerSize) c.AddPayload(testGoMessage(100)) c.WriteTo(b) byt := b.Bytes() m, err := cnmsg.UnpackContainer(byt) if err != nil { t.Fatal(err) } if len(m) != 1 { t.Fatalf("message count mismatch %d != 1", len(m)) } if checkCgoMessage(m[0], 100) { return } t.Error("payload mismatch") } func testCgoMessage(size int) *cnmsg.Message { mod := cnmsg.MessageModLookupByName("base", "encode") if mod == nil { log.Fatal("module not found") } msg := cnmsg.NewMessage(mod) if err := msg.SetEnumField("type", 0, "TEXT"); err != nil { log.Fatal(err) } if err := msg.SetBytesField("payload", 0, make([]byte, size)); err != nil { log.Fatal(err) } return msg } func checkCgoMessage(m *cnmsg.Message, size int) bool { b, err := m.GetBytesField("payload", 0) if err != nil { return false } return compare(b, make([]byte, size)) } func testGoMessage(size int) *nmsg.NmsgPayload { m := new(nmsg_base.Encode) m.Payload = make([]byte, size) m.Type = nmsg_base.EncodeType_TEXT.Enum() p, err := nmsg.Payload(m) if err != nil { log.Fatal(err) } return p } func checkGoMessage(m nmsg.Message, size int) bool { enc, ok := m.(*nmsg_base.Encode) if !ok { log.Printf("type mismatch: %T != *nmsg_base.Encode", m) return false } return compare(enc.GetPayload(), make([]byte, size)) } func TestContainerCgoGoUnpack(t *testing.T) { c := cnmsg.NewContainer(&cnmsg.ContainerConfig{ Size: cnmsg.BufferSizeMin, }) c.Add(testCgoMessage(100)) i := nmsg.NewInput(bytes.NewReader(c.Bytes()), cnmsg.BufferSizeMin) p, err := i.Recv() if err != nil { t.Fatal(err) } m, err := p.Message() if err != nil { t.Fatal(err) } if checkGoMessage(m, 100) { return } t.Error("payload mismatch") } func TestContainerCgoGoUnpackCompress(t *testing.T) { c := cnmsg.NewContainer(&cnmsg.ContainerConfig{ Size: cnmsg.BufferSizeMin, Compress: true, }) c.Add(testCgoMessage(100)) byt := c.Bytes() i := nmsg.NewInput(bytes.NewReader(byt), cnmsg.BufferSizeMin) p, err := i.Recv() if err != nil { t.Fatal(err) } m, err := p.Message() if err != nil { t.Fatal(err) } if checkGoMessage(m, 100) { return } t.Error("payload mismatch") } go-nmsg-0.2.0/debian/000077500000000000000000000000001401057443300143035ustar00rootroot00000000000000go-nmsg-0.2.0/debian/changelog000066400000000000000000000006131401057443300161550ustar00rootroot00000000000000go-nmsg (0.2.0-1) debian-fsi; urgency=medium * Update to use new protobuf library from google.golang.org/protobuf/proto -- Farsight Security, Inc. Mon, 08 Feb 2021 11:15:57 -0500 go-nmsg (0.1.1-1) debian-farsightsec; urgency=medium * Initial public release. -- Farsight Security, Inc. Fri, 23 Aug 2019 12:00:00 -0500 go-nmsg-0.2.0/debian/compat000066400000000000000000000000021401057443300155010ustar00rootroot000000000000009 go-nmsg-0.2.0/debian/control000066400000000000000000000036351401057443300157150ustar00rootroot00000000000000Source: go-nmsg Section: net Priority: optional Maintainer: Farsight Security, Inc. Build-Depends: debhelper (>= 9), dh-golang, golang-1.11-go, golang-google-protobuf-dev, golang-github-dnstap-go-dnstap-dev, golang-gopkg-yaml.v2-dev, libnmsg-dev, pkg-config Standards-Version: 3.9.8 Vcs-Git: https://github.com/farsightsec/go-nmsg.git Vcs-Browser: https://github.com/farsightsec/go-nmsg XS-Go-Import-Path: github.com/farsightsec/go-nmsg Package: golang-github-farsightsec-go-nmsg-dev Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, golang-google-protobuf-dev Description: Pure Golang NMSG Library go-nmsg is a pure go implementation of the NMSG container and payload format used by the C nmsg toolkit and library. It does not implement the full functionality of the C libnmsg message modules. . The NMSG network message encapsulation library format is an efficient encoding of typed, structured data into payloads which are packed into containers which can be transmitted over the network or stored to disk. Package: golang-github-farsightsec-go-nmsg-base-dev Architecture: any Depends: golang-github-farsightsec-go-nmsg-dev (= ${binary:Version}), ${shlibs:Depends}, ${misc:Depends} Description: NMSG vendor base encoding modules for Golang This package provides generated Go code from Protocol Buffers Version 2 specifications for NMSG vendor base encoding modules: Dns, DnsQR, Email, Encode, Http, IPConn, Linkpair, LogLine, Ncap, Packet, Pkt, and Xml. Package: golang-github-farsightsec-go-nmsg-cgo-nmsg-dev Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, pkg-config, libnmsg-dev, libxs-dev Description: Golang bindings to the C libnmsg library The NMSG network message encapsulation library format is an efficient encoding of typed, structured data into payloads which are packed into containers which can be transmitted over the network or stored to disk. go-nmsg-0.2.0/debian/copyright000066400000000000000000000006401401057443300162360ustar00rootroot00000000000000Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: go-nmsg Files: * Copyright: 2015-2018 Farsight Security, Inc. License: MPL-2.0 License: MPL-2.0 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/. go-nmsg-0.2.0/debian/docs000066400000000000000000000000121401057443300151470ustar00rootroot00000000000000README.md go-nmsg-0.2.0/debian/gbp.conf000066400000000000000000000001311401057443300157150ustar00rootroot00000000000000[DEFAULT] upstream-branch = master upstream-tag = tags/v%(version)s pristine-tar = False go-nmsg-0.2.0/debian/golang-github-farsightsec-go-nmsg-base-dev.install000066400000000000000000000001031401057443300257450ustar00rootroot00000000000000usr/share/gocode/src/github.com/farsightsec/go-nmsg/nmsg_base/*.go go-nmsg-0.2.0/debian/golang-github-farsightsec-go-nmsg-cgo-nmsg-dev.install000066400000000000000000000001021401057443300265440ustar00rootroot00000000000000usr/share/gocode/src/github.com/farsightsec/go-nmsg/cgo-nmsg/*.go go-nmsg-0.2.0/debian/golang-github-farsightsec-go-nmsg-dev.install000066400000000000000000000013631401057443300250460ustar00rootroot00000000000000usr/share/gocode/src/github.com/farsightsec/go-nmsg/container.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/endian.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/input_frag.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/input.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/input_seq.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/nmsg.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/nmsg.pb.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/output.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/payload.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/register.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/sockspec.go usr/share/gocode/src/github.com/farsightsec/go-nmsg/zbuf.go go-nmsg-0.2.0/debian/rules000077500000000000000000000004541401057443300153660ustar00rootroot00000000000000#!/usr/bin/make -f # See debhelper(7) (uncomment to enable) # output every command that modifies files on the build system. #export DH_VERBOSE = 1 # set executable path to see the golang-1.11-go install export PATH := /usr/lib/go-1.11/bin:/usr/bin:/bin %: dh $@ --buildsystem=golang --with=golang go-nmsg-0.2.0/debian/source/000077500000000000000000000000001401057443300156035ustar00rootroot00000000000000go-nmsg-0.2.0/debian/source/format000066400000000000000000000000141401057443300170110ustar00rootroot000000000000003.0 (quilt) go-nmsg-0.2.0/endian.go000066400000000000000000000025111401057443300146450ustar00rootroot00000000000000/* * 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 // The nmsg C library renders checksums in network byte order before presenting // them to the protobuf-c library as uint32 values. While Go's encoding/binary // library can format and parse uint32 values as BigEndian or LittleEndian byte // arrays, this is not sufficient to calculate an integer that will represent // a BigEndian (network) byte array in the host's native byte order. This // requires determining the host's byte order, a task which Go's type system // makes cumbersome. // // This file uses the "unsafe" package to defeat Go's type system for the // purposes of determining whether the package is running on a BigEndian or // LittleEndian machine, and uses this information to implement htonl. import ( "encoding/binary" "unsafe" ) var hostEndian binary.ByteOrder func init() { n := uint32(1) b := *(*[4]byte)(unsafe.Pointer(&n)) if b[0] == 1 { hostEndian = binary.LittleEndian } else { hostEndian = binary.BigEndian } } func htonl(n uint32) uint32 { var buf [4]byte hostEndian.PutUint32(buf[:], n) return binary.BigEndian.Uint32(buf[:]) } go-nmsg-0.2.0/genproto.sh000077500000000000000000000012011401057443300152470ustar00rootroot00000000000000#!/bin/sh go_package() { local file pkg line script file=$1; shift pkg=$1; shift line="option go_package = \"$pkg\";" grep "^$line\$" $file > /dev/null && return script="/^package nmsg/|a|$line|.|w|q|" if grep "^option go_package" $file > /dev/null; then script="/^option go_package/d|1|${script}" fi echo "$script" | tr '|' '\n' | ed $file || exit } dir=$(dirname $0) [ -n "$dir" ] && cd $dir go_package nmsg.proto "github.com/farsightsec/go-nmsg;nmsg" protoc --go_out=../../.. nmsg.proto cd nmsg_base for f in *.proto; do go_package $f "github.com/farsightsec/go-nmsg/nmsg_base" done protoc --go_out=../../../.. *.proto go-nmsg-0.2.0/go.mod000066400000000000000000000002301401057443300141620ustar00rootroot00000000000000module github.com/farsightsec/go-nmsg require ( github.com/dnstap/golang-dnstap v0.4.0 google.golang.org/protobuf v1.25.0 gopkg.in/yaml.v2 v2.4.0 ) go-nmsg-0.2.0/go.sum000066400000000000000000000213161401057443300142170ustar00rootroot00000000000000cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/dnstap/golang-dnstap v0.4.0 h1:KRHBoURygdGtBjDI2w4HifJfMAhhOqDuktAokaSa234= github.com/dnstap/golang-dnstap v0.4.0/go.mod h1:FqsSdH58NAmkAvKcpyxht7i4FoBjKu8E4JUPt8ipSUs= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/farsightsec/golang-framestream v0.3.0 h1:/spFQHucTle/ZIPkYqrfshQqPe2VQEzesH243TjIwqA= github.com/farsightsec/golang-framestream v0.3.0/go.mod h1:eNde4IQyEiA5br02AouhEHCu3p3UzrCdFR4LuQHklMI= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo= github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M= golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= go-nmsg-0.2.0/input.go000066400000000000000000000063051401057443300145530ustar00rootroot00000000000000/* * Copyright (c) 2017,2018 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 import ( "bufio" "fmt" "io" "time" ) // An Input is a source of NMSG Payloads. type Input interface { // Recv() returns the next Nmsg Payload from the input, // blocking if none is available. Recv() (*NmsgPayload, error) // Stats() returns interface statistics Stats() *InputStatistics } // InputStatistics holds useful metrics for input performance. type InputStatistics struct { // Count of total container received, including fragments InputContainers uint64 // Count of total bytes received and processed InputBytes uint64 // Count of containers marked lost by sequence tracking LostContainers uint64 // Count of fragment containers received InputFragments uint64 // Count of fragments expired from cache ExpiredFragments uint64 // Count of containers dropped due to incomplete fragments PartialContainers uint64 } type dataError struct{ error } func (d *dataError) Error() string { return d.error.Error() } // IsDataError returns true of the supplied error is an error unpacking // or decoding the NMSG data rather than an I/O error with the input. func IsDataError(err error) bool { _, ok := err.(*dataError) return ok } type input struct { r io.Reader n Nmsg fcache *fragCache scache *seqCache stats InputStatistics } func (i *input) Stats() *InputStatistics { res := &InputStatistics{} *res = i.stats return res } // NewInput constructs an input from the supplied Reader. // The size parameter sizes the input buffer, and should // be greater than the maximum anticipated container size // for datagram inputs. func NewInput(r io.Reader, size int) Input { return &input{ r: bufio.NewReaderSize(r, size), n: Nmsg{}, fcache: newFragmentCache(2 * time.Minute), scache: newSequenceCache(2 * time.Minute), } } type checksumError struct { calc, wire uint32 } func (c *checksumError) Error() string { return fmt.Sprintf("checksum mismatch: %x != %x", c.calc, c.wire) } func (i *input) Recv() (*NmsgPayload, error) { for len(i.n.Payloads) == 0 { var c Container n, err := c.ReadFrom(i.r) if err != nil { return nil, err } if n == 0 { return nil, io.EOF } i.stats.InputBytes += uint64(n) if c.NmsgFragment != nil { i.stats.InputFragments++ var b []byte if b = i.fcache.Insert(c.NmsgFragment); b == nil { continue } err = c.fromNmsgBytes(b, c.isCompressed, false) if err != nil { return nil, &dataError{err} } } i.stats.InputContainers++ i.stats.LostContainers += uint64(i.scache.Update(&c.Nmsg)) i.scache.Expire() i.n = c.Nmsg } ccount, fcount := i.fcache.Expire() i.stats.PartialContainers += uint64(ccount) i.stats.ExpiredFragments += uint64(fcount) p := i.n.Payloads[0] i.n.Payloads = i.n.Payloads[1:] var err error if len(i.n.PayloadCrcs) > 0 { wire := i.n.PayloadCrcs[0] calc := nmsgCRC(p.Payload) if wire != calc { err = &dataError{&checksumError{calc, wire}} } i.n.PayloadCrcs = i.n.PayloadCrcs[1:] } return p, err } go-nmsg-0.2.0/input_frag.go000066400000000000000000000046361401057443300155570ustar00rootroot00000000000000/* * Copyright (c) 2018 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 // NMSG Fragment Cache. import ( "bytes" "container/list" "sort" "time" ) type fragCacheEntry struct { lastUsed time.Time id uint32 frags fragList } // fragList implements sort.Interface to support sorting fragments on // their "Current" field prior to reassembly. type fragList []*NmsgFragment func (fl fragList) Len() int { return len(fl) } func (fl fragList) Less(i, j int) bool { return fl[i].GetCurrent() < fl[j].GetCurrent() } func (fl fragList) Swap(i, j int) { fl[i], fl[j] = fl[j], fl[i] } type fragCache struct { expiry time.Duration idmap map[uint32]*list.Element lru *list.List } func newFragmentCache(expiry time.Duration) *fragCache { return &fragCache{ expiry: expiry, idmap: make(map[uint32]*list.Element), lru: list.New(), } } // Expire too-old entries from the fragment cache, returning the number // of incomplete containers and fragments dropped. func (fc *fragCache) Expire() (containers, frags int) { for fc.lru.Len() > 0 { lruent := fc.lru.Front() ent := lruent.Value.(*fragCacheEntry) if time.Since(ent.lastUsed) <= fc.expiry { break } containers++ frags += len(ent.frags) fc.lru.Remove(lruent) delete(fc.idmap, ent.id) } return } // Inserts a fragment into the cache. If the fragment completes a fragmented // container, Insert returns the reassembled container body. Otherwise, returns // nil. func (fc *fragCache) Insert(f *NmsgFragment) []byte { id := f.GetId() lruent, ok := fc.idmap[id] if !ok { fc.idmap[id] = fc.lru.PushBack( &fragCacheEntry{ lastUsed: time.Now(), id: id, frags: fragList{f}, }) return nil } ent := lruent.Value.(*fragCacheEntry) for i := range ent.frags { if ent.frags[i].GetCurrent() == f.GetCurrent() { /* duplicate fragment */ return nil } } ent.frags = append(ent.frags, f) if ent.frags.Len() <= int(f.GetLast()) { ent.lastUsed = time.Now() fc.lru.MoveToBack(lruent) return nil } fc.lru.Remove(lruent) delete(fc.idmap, id) /* sort and reassemble fragments */ sort.Sort(ent.frags) var b bytes.Buffer for i := range ent.frags { b.Write(ent.frags[i].GetFragment()) } return b.Bytes() } go-nmsg-0.2.0/input_seq.go000066400000000000000000000032761401057443300154270ustar00rootroot00000000000000/* * Copyright (c) 2018 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 import ( "container/list" "time" ) type seqCacheEntry struct { lastUsed time.Time seqid uint64 nextSeq uint32 } type seqCache struct { expiry time.Duration idmap map[uint64]*list.Element lru *list.List } func newSequenceCache(expiry time.Duration) *seqCache { return &seqCache{ expiry: expiry, idmap: make(map[uint64]*list.Element), lru: list.New(), } } const maxDrop = 1048576 func (sc *seqCache) Update(n *Nmsg) (missed int) { if n.Sequence == nil || n.SequenceId == nil { return } seqid := n.GetSequenceId() lruent, ok := sc.idmap[seqid] if !ok { sc.idmap[seqid] = sc.lru.PushBack( &seqCacheEntry{ lastUsed: time.Now(), seqid: seqid, nextSeq: n.GetSequence() + 1, }) return 0 } seq := n.GetSequence() ent := lruent.Value.(*seqCacheEntry) ent.lastUsed = time.Now() sc.lru.MoveToBack(lruent) if seq == ent.nextSeq { ent.nextSeq++ return 0 } if seq > ent.nextSeq { if seq-ent.nextSeq < maxDrop { missed = int(seq - ent.nextSeq) } ent.nextSeq = seq + 1 return missed } delta := int64(int64(seq) + (1 << 32) - int64(ent.nextSeq)) if delta < maxDrop { missed = int(delta) } ent.nextSeq = seq + 1 return missed } func (sc *seqCache) Expire() { for sc.lru.Len() > 0 { lruent := sc.lru.Front() ent := lruent.Value.(*seqCacheEntry) if time.Since(ent.lastUsed) <= sc.expiry { break } sc.lru.Remove(lruent) delete(sc.idmap, ent.seqid) } } go-nmsg-0.2.0/input_test.go000066400000000000000000000061711401057443300156130ustar00rootroot00000000000000/* * 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_test import ( "bytes" "io" "math" "testing" "github.com/farsightsec/go-nmsg" ) func testReader(t *testing.T, n, size, mtu int) io.Reader { buf := new(bytes.Buffer) // nw := nmsg.NewWriter(w, mtu) o := nmsg.BufferedOutput(buf) o.SetMaxSize(mtu, 0) o.SetSequenced(true) p, err := nmsg.Payload(testMessage(size)) if err != nil { t.Error(err.Error()) return nil } for i := 0; i < n; i++ { o.Send(p) } o.Close() t.Logf("testReader: buf = %d bytes (%d, %d, %d)", buf.Len(), n, size, mtu) return buf } func TestInput(t *testing.T) { for _, mtu := range []int{0, 512, 1500} { for _, n := range []int{1, 10, 100} { for _, size := range []int{64, 256, 4096} { i := nmsg.NewInput(testReader(t, n, size, mtu), mtu) if i != nil { c := 0 for { _, err := i.Recv() if err != nil { if err != io.EOF { t.Error(err) } break } c++ } if c < n { t.Errorf("(%d,%d,%d) expected %d, received %d", n, size, mtu, n, c) } } } } } } func TestInputFragExpire(t *testing.T) { // Fragment expiration is not checked here, only in // coverage. var readers []io.Reader npayloads := 10 payloadSize := 512 mtu := 512 for i := 0; i < 1000; i++ { readers = append(readers, testReader(t, npayloads, payloadSize, mtu)) } inp := nmsg.NewInput(io.MultiReader(readers...), 512) var count int for ; ; count++ { _, err := inp.Recv() if err != nil { break } } if count != npayloads*1000 { t.Errorf("missed input, received %d payloads", count) } } func testLoss(t *testing.T, r io.Reader, loss uint64, title string) { t.Helper() i := nmsg.NewInput(r, nmsg.MaxContainerSize) for { if _, err := i.Recv(); err != nil { break } } stats := i.Stats() if stats.LostContainers != loss { t.Errorf("%s: lost %d (expected %d)", title, stats.LostContainers, loss) } } func TestInputSequenceLoss1(t *testing.T) { var buf bytes.Buffer c := nmsg.NewContainer() c.SetSequenced(true) c.WriteTo(&buf) c.WriteTo(&buf) *c.Nmsg.Sequence++ // skip one c.WriteTo(&buf) testLoss(t, &buf, 1, "drop 1") } func TestInputSequenceInterleaveLoss1(t *testing.T) { var buf bytes.Buffer c1 := nmsg.NewContainer() c2 := nmsg.NewContainer() c1.SetSequenced(true) c2.SetSequenced(true) c1.WriteTo(&buf) c2.WriteTo(&buf) c2.WriteTo(&buf) c1.WriteTo(&buf) c2.WriteTo(&buf) *c1.Nmsg.Sequence++ c1.WriteTo(&buf) c2.WriteTo(&buf) testLoss(t, &buf, 1, "interleaved, drop 1") } func TestInputSequenceWrap(t *testing.T) { var buf bytes.Buffer c := nmsg.NewContainer() c.SetSequenced(true) *c.Nmsg.Sequence = math.MaxUint32 - 1 t.Log("sequence", c.Nmsg.GetSequence()) c.WriteTo(&buf) t.Log("sequence", c.Nmsg.GetSequence()) *c.Nmsg.Sequence++ t.Log("sequence", c.Nmsg.GetSequence()) c.WriteTo(&buf) t.Log("sequence", c.Nmsg.GetSequence()) testLoss(t, &buf, 1, "wrapped, drop 1") } go-nmsg-0.2.0/nmsg.go000066400000000000000000000025041401057443300143550ustar00rootroot00000000000000/* * 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/. */ //go:generate ./genproto.sh package nmsg import ( "hash/crc32" "google.golang.org/protobuf/proto" ) // Container size limits to avoid silly fragmentation and memory // exhaustion. const ( MinContainerSize = 512 MaxContainerSize = 1048576 EtherContainerSize = 1280 invalidContainerSize = MaxContainerSize * 16 ) var crc32c = crc32.MakeTable(crc32.Castagnoli) // nmsgCRC calculates a crc32 checksum compatible with that used by // the nmsg C library. // // As in the C library, the checksum is converted to network byte order // before eventually being encoded as a protocol buffers integer. This // defeats the endian neutrality of protocol buffers, but is necessary // for compatibility with the C library operating on little endian machines. func nmsgCRC(b []byte) uint32 { return htonl(crc32.Checksum(b, crc32c)) } // Message encapsulates a protobuf-encoded payload. // // The values returned by the GetVid() and GetMsgtype() methods return // identify the format of the payload. type Message interface { proto.Message GetVid() uint32 GetMsgtype() uint32 } go-nmsg-0.2.0/nmsg.pb.go000066400000000000000000000302541401057443300147600ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: nmsg.proto package nmsg import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Nmsg struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Payloads []*NmsgPayload `protobuf:"bytes,1,rep,name=payloads" json:"payloads,omitempty"` PayloadCrcs []uint32 `protobuf:"varint,2,rep,name=payload_crcs,json=payloadCrcs" json:"payload_crcs,omitempty"` Sequence *uint32 `protobuf:"varint,3,opt,name=sequence" json:"sequence,omitempty"` SequenceId *uint64 `protobuf:"varint,4,opt,name=sequence_id,json=sequenceId" json:"sequence_id,omitempty"` } func (x *Nmsg) Reset() { *x = Nmsg{} if protoimpl.UnsafeEnabled { mi := &file_nmsg_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Nmsg) String() string { return protoimpl.X.MessageStringOf(x) } func (*Nmsg) ProtoMessage() {} func (x *Nmsg) ProtoReflect() protoreflect.Message { mi := &file_nmsg_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Nmsg.ProtoReflect.Descriptor instead. func (*Nmsg) Descriptor() ([]byte, []int) { return file_nmsg_proto_rawDescGZIP(), []int{0} } func (x *Nmsg) GetPayloads() []*NmsgPayload { if x != nil { return x.Payloads } return nil } func (x *Nmsg) GetPayloadCrcs() []uint32 { if x != nil { return x.PayloadCrcs } return nil } func (x *Nmsg) GetSequence() uint32 { if x != nil && x.Sequence != nil { return *x.Sequence } return 0 } func (x *Nmsg) GetSequenceId() uint64 { if x != nil && x.SequenceId != nil { return *x.SequenceId } return 0 } type NmsgFragment struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id *uint32 `protobuf:"varint,1,req,name=id" json:"id,omitempty"` Current *uint32 `protobuf:"varint,2,req,name=current" json:"current,omitempty"` Last *uint32 `protobuf:"varint,3,req,name=last" json:"last,omitempty"` Fragment []byte `protobuf:"bytes,4,req,name=fragment" json:"fragment,omitempty"` Crc *uint32 `protobuf:"varint,5,opt,name=crc" json:"crc,omitempty"` } func (x *NmsgFragment) Reset() { *x = NmsgFragment{} if protoimpl.UnsafeEnabled { mi := &file_nmsg_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *NmsgFragment) String() string { return protoimpl.X.MessageStringOf(x) } func (*NmsgFragment) ProtoMessage() {} func (x *NmsgFragment) ProtoReflect() protoreflect.Message { mi := &file_nmsg_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use NmsgFragment.ProtoReflect.Descriptor instead. func (*NmsgFragment) Descriptor() ([]byte, []int) { return file_nmsg_proto_rawDescGZIP(), []int{1} } func (x *NmsgFragment) GetId() uint32 { if x != nil && x.Id != nil { return *x.Id } return 0 } func (x *NmsgFragment) GetCurrent() uint32 { if x != nil && x.Current != nil { return *x.Current } return 0 } func (x *NmsgFragment) GetLast() uint32 { if x != nil && x.Last != nil { return *x.Last } return 0 } func (x *NmsgFragment) GetFragment() []byte { if x != nil { return x.Fragment } return nil } func (x *NmsgFragment) GetCrc() uint32 { if x != nil && x.Crc != nil { return *x.Crc } return 0 } type NmsgPayload struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Vid *uint32 `protobuf:"varint,1,req,name=vid" json:"vid,omitempty"` Msgtype *uint32 `protobuf:"varint,2,req,name=msgtype" json:"msgtype,omitempty"` TimeSec *int64 `protobuf:"varint,3,req,name=time_sec,json=timeSec" json:"time_sec,omitempty"` TimeNsec *uint32 `protobuf:"fixed32,4,req,name=time_nsec,json=timeNsec" json:"time_nsec,omitempty"` Payload []byte `protobuf:"bytes,5,opt,name=payload" json:"payload,omitempty"` Source *uint32 `protobuf:"varint,7,opt,name=source" json:"source,omitempty"` Operator *uint32 `protobuf:"varint,8,opt,name=operator" json:"operator,omitempty"` Group *uint32 `protobuf:"varint,9,opt,name=group" json:"group,omitempty"` } func (x *NmsgPayload) Reset() { *x = NmsgPayload{} if protoimpl.UnsafeEnabled { mi := &file_nmsg_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *NmsgPayload) String() string { return protoimpl.X.MessageStringOf(x) } func (*NmsgPayload) ProtoMessage() {} func (x *NmsgPayload) ProtoReflect() protoreflect.Message { mi := &file_nmsg_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use NmsgPayload.ProtoReflect.Descriptor instead. func (*NmsgPayload) Descriptor() ([]byte, []int) { return file_nmsg_proto_rawDescGZIP(), []int{2} } func (x *NmsgPayload) GetVid() uint32 { if x != nil && x.Vid != nil { return *x.Vid } return 0 } func (x *NmsgPayload) GetMsgtype() uint32 { if x != nil && x.Msgtype != nil { return *x.Msgtype } return 0 } func (x *NmsgPayload) GetTimeSec() int64 { if x != nil && x.TimeSec != nil { return *x.TimeSec } return 0 } func (x *NmsgPayload) GetTimeNsec() uint32 { if x != nil && x.TimeNsec != nil { return *x.TimeNsec } return 0 } func (x *NmsgPayload) GetPayload() []byte { if x != nil { return x.Payload } return nil } func (x *NmsgPayload) GetSource() uint32 { if x != nil && x.Source != nil { return *x.Source } return 0 } func (x *NmsgPayload) GetOperator() uint32 { if x != nil && x.Operator != nil { return *x.Operator } return 0 } func (x *NmsgPayload) GetGroup() uint32 { if x != nil && x.Group != nil { return *x.Group } return 0 } var File_nmsg_proto protoreflect.FileDescriptor var file_nmsg_proto_rawDesc = []byte{ 0x0a, 0x0a, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x6e, 0x6d, 0x73, 0x67, 0x22, 0x95, 0x01, 0x0a, 0x04, 0x4e, 0x6d, 0x73, 0x67, 0x12, 0x2d, 0x0a, 0x08, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x4e, 0x6d, 0x73, 0x67, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x52, 0x08, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x63, 0x72, 0x63, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0d, 0x52, 0x0b, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x43, 0x72, 0x63, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0a, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x49, 0x64, 0x22, 0x7a, 0x0a, 0x0c, 0x4e, 0x6d, 0x73, 0x67, 0x46, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x07, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x61, 0x73, 0x74, 0x18, 0x03, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x04, 0x6c, 0x61, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x08, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x63, 0x72, 0x63, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x63, 0x72, 0x63, 0x22, 0xd5, 0x01, 0x0a, 0x0b, 0x4e, 0x6d, 0x73, 0x67, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x69, 0x64, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x03, 0x76, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x73, 0x67, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x07, 0x6d, 0x73, 0x67, 0x74, 0x79, 0x70, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x73, 0x65, 0x63, 0x18, 0x03, 0x20, 0x02, 0x28, 0x03, 0x52, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x53, 0x65, 0x63, 0x12, 0x1b, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x6e, 0x73, 0x65, 0x63, 0x18, 0x04, 0x20, 0x02, 0x28, 0x07, 0x52, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x4e, 0x73, 0x65, 0x63, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x42, 0x25, 0x5a, 0x23, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x3b, 0x6e, 0x6d, 0x73, 0x67, } var ( file_nmsg_proto_rawDescOnce sync.Once file_nmsg_proto_rawDescData = file_nmsg_proto_rawDesc ) func file_nmsg_proto_rawDescGZIP() []byte { file_nmsg_proto_rawDescOnce.Do(func() { file_nmsg_proto_rawDescData = protoimpl.X.CompressGZIP(file_nmsg_proto_rawDescData) }) return file_nmsg_proto_rawDescData } var file_nmsg_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_nmsg_proto_goTypes = []interface{}{ (*Nmsg)(nil), // 0: nmsg.Nmsg (*NmsgFragment)(nil), // 1: nmsg.NmsgFragment (*NmsgPayload)(nil), // 2: nmsg.NmsgPayload } var file_nmsg_proto_depIdxs = []int32{ 2, // 0: nmsg.Nmsg.payloads:type_name -> nmsg.NmsgPayload 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name } func init() { file_nmsg_proto_init() } func file_nmsg_proto_init() { if File_nmsg_proto != nil { return } if !protoimpl.UnsafeEnabled { file_nmsg_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Nmsg); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_nmsg_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*NmsgFragment); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_nmsg_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*NmsgPayload); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_nmsg_proto_rawDesc, NumEnums: 0, NumMessages: 3, NumExtensions: 0, NumServices: 0, }, GoTypes: file_nmsg_proto_goTypes, DependencyIndexes: file_nmsg_proto_depIdxs, MessageInfos: file_nmsg_proto_msgTypes, }.Build() File_nmsg_proto = out.File file_nmsg_proto_rawDesc = nil file_nmsg_proto_goTypes = nil file_nmsg_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg.proto000066400000000000000000000015271401057443300151170ustar00rootroot00000000000000syntax = "proto2"; package nmsg; option go_package = "github.com/farsightsec/go-nmsg;nmsg"; message Nmsg { repeated NmsgPayload payloads = 1; repeated uint32 payload_crcs = 2; optional uint32 sequence = 3; optional uint64 sequence_id = 4; } message NmsgFragment { required uint32 id = 1; required uint32 current = 2; required uint32 last = 3; required bytes fragment = 4; optional uint32 crc = 5; } message NmsgPayload { required uint32 vid = 1; required uint32 msgtype = 2; required int64 time_sec = 3; required fixed32 time_nsec = 4; optional bytes payload = 5; optional uint32 source = 7; optional uint32 operator = 8; optional uint32 group = 9; } go-nmsg-0.2.0/nmsg_base/000077500000000000000000000000001401057443300150175ustar00rootroot00000000000000go-nmsg-0.2.0/nmsg_base/base.go000066400000000000000000000042761401057443300162710ustar00rootroot00000000000000/* * 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_base import ( "github.com/dnstap/golang-dnstap" "github.com/farsightsec/go-nmsg" "google.golang.org/protobuf/proto" ) func (p *Ncap) GetVid() uint32 { return 1 } func (p *Ncap) GetMsgtype() uint32 { return 1 } func (p *Email) GetVid() uint32 { return 1 } func (p *Email) GetMsgtype() uint32 { return 2 } func (p *Linkpair) GetVid() uint32 { return 1 } func (p *Linkpair) GetMsgtype() uint32 { return 3 } func (p *Http) GetVid() uint32 { return 1 } func (p *Http) GetMsgtype() uint32 { return 4 } func (p *IPConn) GetVid() uint32 { return 1 } func (p *IPConn) GetMsgtype() uint32 { return 5 } func (p *LogLine) GetVid() uint32 { return 1 } func (p *LogLine) GetMsgtype() uint32 { return 6 } func (p *Dns) GetVid() uint32 { return 1 } func (p *Dns) GetMsgtype() uint32 { return 7 } func (p *Pkt) GetVid() uint32 { return 1 } func (p *Pkt) GetMsgtype() uint32 { return 8 } func (p *DnsQR) GetVid() uint32 { return 1 } func (p *DnsQR) GetMsgtype() uint32 { return 9 } func (p *Xml) GetVid() uint32 { return 1 } func (p *Xml) GetMsgtype() uint32 { return 10 } func (p *Encode) GetVid() uint32 { return 1 } func (p *Encode) GetMsgtype() uint32 { return 11 } func (p *Packet) GetVid() uint32 { return 1 } func (p *Packet) GetMsgtype() uint32 { return 12 } type Dnstap struct { dnstap.Dnstap } func (d *Dnstap) GetVid() uint32 { return 1 } func (d *Dnstap) GetMsgtype() uint32 { return 13 } func (d *Dnstap) Marshal() ([]byte, error) { return proto.Marshal(&d.Dnstap) } func (d *Dnstap) Unmarshal(b []byte) error { return proto.Unmarshal(b, &d.Dnstap) } func init() { nmsg.Register(&Ncap{}) nmsg.Register(&Email{}) nmsg.Register(&Linkpair{}) nmsg.Register(&Http{}) nmsg.Register(&IPConn{}) nmsg.Register(&LogLine{}) nmsg.Register(&Dns{}) nmsg.Register(&Pkt{}) nmsg.Register(&DnsQR{}) nmsg.Register(&Xml{}) nmsg.Register(&Encode{}) nmsg.Register(&Packet{}) nmsg.Register(&Dnstap{}) } go-nmsg-0.2.0/nmsg_base/compat.go000066400000000000000000000034661401057443300166420ustar00rootroot00000000000000package nmsg_base // // Compatibility definitions for code written against older versions // of the nmsg_base .proto files. // type DnsQRType = DnsQR_DnsQRType const ( DnsQRType_UDP_INVALID = DnsQR_UDP_INVALID DnsQRType_UDP_QUERY_RESPONSE = DnsQR_UDP_QUERY_RESPONSE DnsQRType_UDP_UNANSWERED_QUERY = DnsQR_UDP_UNANSWERED_QUERY DnsQRType_UDP_UNSOLICITED_RESPONSE = DnsQR_UDP_UNSOLICITED_RESPONSE DnsQRType_TCP = DnsQR_TCP DnsQRType_ICMP = DnsQR_ICMP DnsQRType_UDP_QUERY_ONLY = DnsQR_UDP_QUERY_ONLY DnsQRType_UDP_RESPONSE_ONLY = DnsQR_UDP_RESPONSE_ONLY ) type UdpChecksum = DnsQR_UdpChecksum const ( UdpChecksum_ERROR = DnsQR_ERROR UdpChecksum_ABSENT = DnsQR_ABSENT UdpChecksum_INCORRECT = DnsQR_INCORRECT UdpChecksum_CORRECT = DnsQR_CORRECT ) type EmailType = Email_EmailType const ( EmailType_unknown = Email_unknown EmailType_spamtrap = Email_spamtrap EmailType_rej_network = Email_rej_network EmailType_rej_content = Email_rej_content EmailType_rej_user = Email_rej_user ) type EncodeType = Encode_EncodeType const ( EncodeType_TEXT = Encode_TEXT EncodeType_JSON = Encode_JSON EncodeType_YAML = Encode_YAML EncodeType_MSGPACK = Encode_MSGPACK EncodeType_XML = Encode_XML ) type HttpType = Http_HttpType const ( HttpType_unknown = Http_unknown HttpType_sinkhole = Http_sinkhole ) type LinkType = Linkpair_Linktype const ( Linktype_anchor = Linkpair_anchor Linktype_redirect = Linkpair_redirect ) type NcapType Ncap_NcapType const ( NcapType_IPV4 = Ncap_IPV4 NcapType_IPV6 = Ncap_IPV6 NcapType_Legacy = Ncap_Legacy ) type NcapLegacyType = Ncap_NcapLegacyType const ( NcapLegacyType_Ncap_UDP = Ncap_UDP NcapLegacyType_Ncap_TCP = Ncap_TCP NcapLegacyType_Ncap_ICMP = Ncap_ICMP ) go-nmsg-0.2.0/nmsg_base/dns.pb.go000066400000000000000000000146071401057443300165420ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: dns.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Dns struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Section *uint32 `protobuf:"varint,6,opt,name=section" json:"section,omitempty"` Qname []byte `protobuf:"bytes,7,opt,name=qname" json:"qname,omitempty"` Qtype *uint32 `protobuf:"varint,8,opt,name=qtype" json:"qtype,omitempty"` Qclass *uint32 `protobuf:"varint,9,opt,name=qclass" json:"qclass,omitempty"` Rrname []byte `protobuf:"bytes,1,opt,name=rrname" json:"rrname,omitempty"` Rrtype *uint32 `protobuf:"varint,2,opt,name=rrtype" json:"rrtype,omitempty"` Rrclass *uint32 `protobuf:"varint,3,opt,name=rrclass" json:"rrclass,omitempty"` Rrttl *uint32 `protobuf:"varint,4,opt,name=rrttl" json:"rrttl,omitempty"` Rdata [][]byte `protobuf:"bytes,5,rep,name=rdata" json:"rdata,omitempty"` } func (x *Dns) Reset() { *x = Dns{} if protoimpl.UnsafeEnabled { mi := &file_dns_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Dns) String() string { return protoimpl.X.MessageStringOf(x) } func (*Dns) ProtoMessage() {} func (x *Dns) ProtoReflect() protoreflect.Message { mi := &file_dns_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Dns.ProtoReflect.Descriptor instead. func (*Dns) Descriptor() ([]byte, []int) { return file_dns_proto_rawDescGZIP(), []int{0} } func (x *Dns) GetSection() uint32 { if x != nil && x.Section != nil { return *x.Section } return 0 } func (x *Dns) GetQname() []byte { if x != nil { return x.Qname } return nil } func (x *Dns) GetQtype() uint32 { if x != nil && x.Qtype != nil { return *x.Qtype } return 0 } func (x *Dns) GetQclass() uint32 { if x != nil && x.Qclass != nil { return *x.Qclass } return 0 } func (x *Dns) GetRrname() []byte { if x != nil { return x.Rrname } return nil } func (x *Dns) GetRrtype() uint32 { if x != nil && x.Rrtype != nil { return *x.Rrtype } return 0 } func (x *Dns) GetRrclass() uint32 { if x != nil && x.Rrclass != nil { return *x.Rrclass } return 0 } func (x *Dns) GetRrttl() uint32 { if x != nil && x.Rrttl != nil { return *x.Rrttl } return 0 } func (x *Dns) GetRdata() [][]byte { if x != nil { return x.Rdata } return nil } var File_dns_proto protoreflect.FileDescriptor var file_dns_proto_rawDesc = []byte{ 0x0a, 0x09, 0x64, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0xd9, 0x01, 0x0a, 0x03, 0x44, 0x6e, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x71, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x74, 0x79, 0x70, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x71, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x71, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x71, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x72, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x72, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x72, 0x72, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x72, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x72, 0x72, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x72, 0x72, 0x74, 0x74, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x72, 0x72, 0x74, 0x74, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x72, 0x64, 0x61, 0x74, 0x61, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x05, 0x72, 0x64, 0x61, 0x74, 0x61, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_dns_proto_rawDescOnce sync.Once file_dns_proto_rawDescData = file_dns_proto_rawDesc ) func file_dns_proto_rawDescGZIP() []byte { file_dns_proto_rawDescOnce.Do(func() { file_dns_proto_rawDescData = protoimpl.X.CompressGZIP(file_dns_proto_rawDescData) }) return file_dns_proto_rawDescData } var file_dns_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_dns_proto_goTypes = []interface{}{ (*Dns)(nil), // 0: nmsg.base.Dns } var file_dns_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_dns_proto_init() } func file_dns_proto_init() { if File_dns_proto != nil { return } if !protoimpl.UnsafeEnabled { file_dns_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Dns); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_dns_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_dns_proto_goTypes, DependencyIndexes: file_dns_proto_depIdxs, MessageInfos: file_dns_proto_msgTypes, }.Build() File_dns_proto = out.File file_dns_proto_rawDesc = nil file_dns_proto_goTypes = nil file_dns_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/dns.proto000066400000000000000000000006711401057443300166740ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Dns { optional uint32 section = 6; optional bytes qname = 7; optional uint32 qtype = 8; optional uint32 qclass = 9; optional bytes rrname = 1; optional uint32 rrtype = 2; optional uint32 rrclass = 3; optional uint32 rrttl = 4; repeated bytes rdata = 5; } go-nmsg-0.2.0/nmsg_base/dnsqr.pb.go000066400000000000000000000441351401057443300171040ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: dnsqr.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type DnsQR_DnsQRType int32 const ( DnsQR_UDP_INVALID DnsQR_DnsQRType = 0 DnsQR_UDP_QUERY_RESPONSE DnsQR_DnsQRType = 1 DnsQR_UDP_UNANSWERED_QUERY DnsQR_DnsQRType = 2 DnsQR_UDP_UNSOLICITED_RESPONSE DnsQR_DnsQRType = 3 DnsQR_TCP DnsQR_DnsQRType = 4 DnsQR_ICMP DnsQR_DnsQRType = 5 DnsQR_UDP_QUERY_ONLY DnsQR_DnsQRType = 6 DnsQR_UDP_RESPONSE_ONLY DnsQR_DnsQRType = 7 ) // Enum value maps for DnsQR_DnsQRType. var ( DnsQR_DnsQRType_name = map[int32]string{ 0: "UDP_INVALID", 1: "UDP_QUERY_RESPONSE", 2: "UDP_UNANSWERED_QUERY", 3: "UDP_UNSOLICITED_RESPONSE", 4: "TCP", 5: "ICMP", 6: "UDP_QUERY_ONLY", 7: "UDP_RESPONSE_ONLY", } DnsQR_DnsQRType_value = map[string]int32{ "UDP_INVALID": 0, "UDP_QUERY_RESPONSE": 1, "UDP_UNANSWERED_QUERY": 2, "UDP_UNSOLICITED_RESPONSE": 3, "TCP": 4, "ICMP": 5, "UDP_QUERY_ONLY": 6, "UDP_RESPONSE_ONLY": 7, } ) func (x DnsQR_DnsQRType) Enum() *DnsQR_DnsQRType { p := new(DnsQR_DnsQRType) *p = x return p } func (x DnsQR_DnsQRType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (DnsQR_DnsQRType) Descriptor() protoreflect.EnumDescriptor { return file_dnsqr_proto_enumTypes[0].Descriptor() } func (DnsQR_DnsQRType) Type() protoreflect.EnumType { return &file_dnsqr_proto_enumTypes[0] } func (x DnsQR_DnsQRType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *DnsQR_DnsQRType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = DnsQR_DnsQRType(num) return nil } // Deprecated: Use DnsQR_DnsQRType.Descriptor instead. func (DnsQR_DnsQRType) EnumDescriptor() ([]byte, []int) { return file_dnsqr_proto_rawDescGZIP(), []int{0, 0} } type DnsQR_UdpChecksum int32 const ( DnsQR_ERROR DnsQR_UdpChecksum = 0 DnsQR_ABSENT DnsQR_UdpChecksum = 1 DnsQR_INCORRECT DnsQR_UdpChecksum = 2 DnsQR_CORRECT DnsQR_UdpChecksum = 3 ) // Enum value maps for DnsQR_UdpChecksum. var ( DnsQR_UdpChecksum_name = map[int32]string{ 0: "ERROR", 1: "ABSENT", 2: "INCORRECT", 3: "CORRECT", } DnsQR_UdpChecksum_value = map[string]int32{ "ERROR": 0, "ABSENT": 1, "INCORRECT": 2, "CORRECT": 3, } ) func (x DnsQR_UdpChecksum) Enum() *DnsQR_UdpChecksum { p := new(DnsQR_UdpChecksum) *p = x return p } func (x DnsQR_UdpChecksum) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (DnsQR_UdpChecksum) Descriptor() protoreflect.EnumDescriptor { return file_dnsqr_proto_enumTypes[1].Descriptor() } func (DnsQR_UdpChecksum) Type() protoreflect.EnumType { return &file_dnsqr_proto_enumTypes[1] } func (x DnsQR_UdpChecksum) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *DnsQR_UdpChecksum) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = DnsQR_UdpChecksum(num) return nil } // Deprecated: Use DnsQR_UdpChecksum.Descriptor instead. func (DnsQR_UdpChecksum) EnumDescriptor() ([]byte, []int) { return file_dnsqr_proto_rawDescGZIP(), []int{0, 1} } type DnsQR struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Type *DnsQR_DnsQRType `protobuf:"varint,1,req,name=type,enum=nmsg.base.DnsQR_DnsQRType" json:"type,omitempty"` QueryIp []byte `protobuf:"bytes,2,req,name=query_ip,json=queryIp" json:"query_ip,omitempty"` ResponseIp []byte `protobuf:"bytes,3,req,name=response_ip,json=responseIp" json:"response_ip,omitempty"` Proto *uint32 `protobuf:"varint,4,req,name=proto" json:"proto,omitempty"` QueryPort *uint32 `protobuf:"varint,5,req,name=query_port,json=queryPort" json:"query_port,omitempty"` ResponsePort *uint32 `protobuf:"varint,6,req,name=response_port,json=responsePort" json:"response_port,omitempty"` Id *uint32 `protobuf:"varint,7,req,name=id" json:"id,omitempty"` Qname []byte `protobuf:"bytes,8,opt,name=qname" json:"qname,omitempty"` Qtype *uint32 `protobuf:"varint,9,opt,name=qtype" json:"qtype,omitempty"` Qclass *uint32 `protobuf:"varint,10,opt,name=qclass" json:"qclass,omitempty"` Rcode *uint32 `protobuf:"varint,11,opt,name=rcode" json:"rcode,omitempty"` QueryPacket [][]byte `protobuf:"bytes,12,rep,name=query_packet,json=queryPacket" json:"query_packet,omitempty"` QueryTimeSec []int64 `protobuf:"varint,13,rep,name=query_time_sec,json=queryTimeSec" json:"query_time_sec,omitempty"` QueryTimeNsec []int32 `protobuf:"fixed32,14,rep,name=query_time_nsec,json=queryTimeNsec" json:"query_time_nsec,omitempty"` ResponsePacket [][]byte `protobuf:"bytes,15,rep,name=response_packet,json=responsePacket" json:"response_packet,omitempty"` ResponseTimeSec []int64 `protobuf:"varint,16,rep,name=response_time_sec,json=responseTimeSec" json:"response_time_sec,omitempty"` ResponseTimeNsec []int32 `protobuf:"fixed32,17,rep,name=response_time_nsec,json=responseTimeNsec" json:"response_time_nsec,omitempty"` Tcp []byte `protobuf:"bytes,18,opt,name=tcp" json:"tcp,omitempty"` Icmp []byte `protobuf:"bytes,19,opt,name=icmp" json:"icmp,omitempty"` Timeout *float64 `protobuf:"fixed64,20,opt,name=timeout" json:"timeout,omitempty"` UdpChecksum *DnsQR_UdpChecksum `protobuf:"varint,21,opt,name=udp_checksum,json=udpChecksum,enum=nmsg.base.DnsQR_UdpChecksum" json:"udp_checksum,omitempty"` ResolverAddressZeroed *bool `protobuf:"varint,22,opt,name=resolver_address_zeroed,json=resolverAddressZeroed" json:"resolver_address_zeroed,omitempty"` } func (x *DnsQR) Reset() { *x = DnsQR{} if protoimpl.UnsafeEnabled { mi := &file_dnsqr_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *DnsQR) String() string { return protoimpl.X.MessageStringOf(x) } func (*DnsQR) ProtoMessage() {} func (x *DnsQR) ProtoReflect() protoreflect.Message { mi := &file_dnsqr_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use DnsQR.ProtoReflect.Descriptor instead. func (*DnsQR) Descriptor() ([]byte, []int) { return file_dnsqr_proto_rawDescGZIP(), []int{0} } func (x *DnsQR) GetType() DnsQR_DnsQRType { if x != nil && x.Type != nil { return *x.Type } return DnsQR_UDP_INVALID } func (x *DnsQR) GetQueryIp() []byte { if x != nil { return x.QueryIp } return nil } func (x *DnsQR) GetResponseIp() []byte { if x != nil { return x.ResponseIp } return nil } func (x *DnsQR) GetProto() uint32 { if x != nil && x.Proto != nil { return *x.Proto } return 0 } func (x *DnsQR) GetQueryPort() uint32 { if x != nil && x.QueryPort != nil { return *x.QueryPort } return 0 } func (x *DnsQR) GetResponsePort() uint32 { if x != nil && x.ResponsePort != nil { return *x.ResponsePort } return 0 } func (x *DnsQR) GetId() uint32 { if x != nil && x.Id != nil { return *x.Id } return 0 } func (x *DnsQR) GetQname() []byte { if x != nil { return x.Qname } return nil } func (x *DnsQR) GetQtype() uint32 { if x != nil && x.Qtype != nil { return *x.Qtype } return 0 } func (x *DnsQR) GetQclass() uint32 { if x != nil && x.Qclass != nil { return *x.Qclass } return 0 } func (x *DnsQR) GetRcode() uint32 { if x != nil && x.Rcode != nil { return *x.Rcode } return 0 } func (x *DnsQR) GetQueryPacket() [][]byte { if x != nil { return x.QueryPacket } return nil } func (x *DnsQR) GetQueryTimeSec() []int64 { if x != nil { return x.QueryTimeSec } return nil } func (x *DnsQR) GetQueryTimeNsec() []int32 { if x != nil { return x.QueryTimeNsec } return nil } func (x *DnsQR) GetResponsePacket() [][]byte { if x != nil { return x.ResponsePacket } return nil } func (x *DnsQR) GetResponseTimeSec() []int64 { if x != nil { return x.ResponseTimeSec } return nil } func (x *DnsQR) GetResponseTimeNsec() []int32 { if x != nil { return x.ResponseTimeNsec } return nil } func (x *DnsQR) GetTcp() []byte { if x != nil { return x.Tcp } return nil } func (x *DnsQR) GetIcmp() []byte { if x != nil { return x.Icmp } return nil } func (x *DnsQR) GetTimeout() float64 { if x != nil && x.Timeout != nil { return *x.Timeout } return 0 } func (x *DnsQR) GetUdpChecksum() DnsQR_UdpChecksum { if x != nil && x.UdpChecksum != nil { return *x.UdpChecksum } return DnsQR_ERROR } func (x *DnsQR) GetResolverAddressZeroed() bool { if x != nil && x.ResolverAddressZeroed != nil { return *x.ResolverAddressZeroed } return false } var File_dnsqr_proto protoreflect.FileDescriptor var file_dnsqr_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x64, 0x6e, 0x73, 0x71, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0xd3, 0x07, 0x0a, 0x05, 0x44, 0x6e, 0x73, 0x51, 0x52, 0x12, 0x2e, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0e, 0x32, 0x1a, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x44, 0x6e, 0x73, 0x51, 0x52, 0x2e, 0x44, 0x6e, 0x73, 0x51, 0x52, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x69, 0x70, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x07, 0x71, 0x75, 0x65, 0x72, 0x79, 0x49, 0x70, 0x12, 0x1f, 0x0a, 0x0b, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x5f, 0x69, 0x70, 0x18, 0x03, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x0a, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x49, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x18, 0x04, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1d, 0x0a, 0x0a, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x05, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x09, 0x71, 0x75, 0x65, 0x72, 0x79, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x23, 0x0a, 0x0d, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x5f, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x06, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x0c, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x07, 0x20, 0x02, 0x28, 0x0d, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x71, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x74, 0x79, 0x70, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x71, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x71, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x71, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x18, 0x0c, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x0b, 0x71, 0x75, 0x65, 0x72, 0x79, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x12, 0x24, 0x0a, 0x0e, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x73, 0x65, 0x63, 0x18, 0x0d, 0x20, 0x03, 0x28, 0x03, 0x52, 0x0c, 0x71, 0x75, 0x65, 0x72, 0x79, 0x54, 0x69, 0x6d, 0x65, 0x53, 0x65, 0x63, 0x12, 0x26, 0x0a, 0x0f, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x6e, 0x73, 0x65, 0x63, 0x18, 0x0e, 0x20, 0x03, 0x28, 0x0f, 0x52, 0x0d, 0x71, 0x75, 0x65, 0x72, 0x79, 0x54, 0x69, 0x6d, 0x65, 0x4e, 0x73, 0x65, 0x63, 0x12, 0x27, 0x0a, 0x0f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x18, 0x0f, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x0e, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x12, 0x2a, 0x0a, 0x11, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x73, 0x65, 0x63, 0x18, 0x10, 0x20, 0x03, 0x28, 0x03, 0x52, 0x0f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x53, 0x65, 0x63, 0x12, 0x2c, 0x0a, 0x12, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x6e, 0x73, 0x65, 0x63, 0x18, 0x11, 0x20, 0x03, 0x28, 0x0f, 0x52, 0x10, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x4e, 0x73, 0x65, 0x63, 0x12, 0x10, 0x0a, 0x03, 0x74, 0x63, 0x70, 0x18, 0x12, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x74, 0x63, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x63, 0x6d, 0x70, 0x18, 0x13, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x69, 0x63, 0x6d, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x14, 0x20, 0x01, 0x28, 0x01, 0x52, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x3f, 0x0a, 0x0c, 0x75, 0x64, 0x70, 0x5f, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x75, 0x6d, 0x18, 0x15, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1c, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x44, 0x6e, 0x73, 0x51, 0x52, 0x2e, 0x55, 0x64, 0x70, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x75, 0x6d, 0x52, 0x0b, 0x75, 0x64, 0x70, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x75, 0x6d, 0x12, 0x36, 0x0a, 0x17, 0x72, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x72, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x5f, 0x7a, 0x65, 0x72, 0x6f, 0x65, 0x64, 0x18, 0x16, 0x20, 0x01, 0x28, 0x08, 0x52, 0x15, 0x72, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x5a, 0x65, 0x72, 0x6f, 0x65, 0x64, 0x22, 0xaa, 0x01, 0x0a, 0x09, 0x44, 0x6e, 0x73, 0x51, 0x52, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0f, 0x0a, 0x0b, 0x55, 0x44, 0x50, 0x5f, 0x49, 0x4e, 0x56, 0x41, 0x4c, 0x49, 0x44, 0x10, 0x00, 0x12, 0x16, 0x0a, 0x12, 0x55, 0x44, 0x50, 0x5f, 0x51, 0x55, 0x45, 0x52, 0x59, 0x5f, 0x52, 0x45, 0x53, 0x50, 0x4f, 0x4e, 0x53, 0x45, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x55, 0x44, 0x50, 0x5f, 0x55, 0x4e, 0x41, 0x4e, 0x53, 0x57, 0x45, 0x52, 0x45, 0x44, 0x5f, 0x51, 0x55, 0x45, 0x52, 0x59, 0x10, 0x02, 0x12, 0x1c, 0x0a, 0x18, 0x55, 0x44, 0x50, 0x5f, 0x55, 0x4e, 0x53, 0x4f, 0x4c, 0x49, 0x43, 0x49, 0x54, 0x45, 0x44, 0x5f, 0x52, 0x45, 0x53, 0x50, 0x4f, 0x4e, 0x53, 0x45, 0x10, 0x03, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x43, 0x50, 0x10, 0x04, 0x12, 0x08, 0x0a, 0x04, 0x49, 0x43, 0x4d, 0x50, 0x10, 0x05, 0x12, 0x12, 0x0a, 0x0e, 0x55, 0x44, 0x50, 0x5f, 0x51, 0x55, 0x45, 0x52, 0x59, 0x5f, 0x4f, 0x4e, 0x4c, 0x59, 0x10, 0x06, 0x12, 0x15, 0x0a, 0x11, 0x55, 0x44, 0x50, 0x5f, 0x52, 0x45, 0x53, 0x50, 0x4f, 0x4e, 0x53, 0x45, 0x5f, 0x4f, 0x4e, 0x4c, 0x59, 0x10, 0x07, 0x22, 0x40, 0x0a, 0x0b, 0x55, 0x64, 0x70, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x75, 0x6d, 0x12, 0x09, 0x0a, 0x05, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x42, 0x53, 0x45, 0x4e, 0x54, 0x10, 0x01, 0x12, 0x0d, 0x0a, 0x09, 0x49, 0x4e, 0x43, 0x4f, 0x52, 0x52, 0x45, 0x43, 0x54, 0x10, 0x02, 0x12, 0x0b, 0x0a, 0x07, 0x43, 0x4f, 0x52, 0x52, 0x45, 0x43, 0x54, 0x10, 0x03, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_dnsqr_proto_rawDescOnce sync.Once file_dnsqr_proto_rawDescData = file_dnsqr_proto_rawDesc ) func file_dnsqr_proto_rawDescGZIP() []byte { file_dnsqr_proto_rawDescOnce.Do(func() { file_dnsqr_proto_rawDescData = protoimpl.X.CompressGZIP(file_dnsqr_proto_rawDescData) }) return file_dnsqr_proto_rawDescData } var file_dnsqr_proto_enumTypes = make([]protoimpl.EnumInfo, 2) var file_dnsqr_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_dnsqr_proto_goTypes = []interface{}{ (DnsQR_DnsQRType)(0), // 0: nmsg.base.DnsQR.DnsQRType (DnsQR_UdpChecksum)(0), // 1: nmsg.base.DnsQR.UdpChecksum (*DnsQR)(nil), // 2: nmsg.base.DnsQR } var file_dnsqr_proto_depIdxs = []int32{ 0, // 0: nmsg.base.DnsQR.type:type_name -> nmsg.base.DnsQR.DnsQRType 1, // 1: nmsg.base.DnsQR.udp_checksum:type_name -> nmsg.base.DnsQR.UdpChecksum 2, // [2:2] is the sub-list for method output_type 2, // [2:2] is the sub-list for method input_type 2, // [2:2] is the sub-list for extension type_name 2, // [2:2] is the sub-list for extension extendee 0, // [0:2] is the sub-list for field type_name } func init() { file_dnsqr_proto_init() } func file_dnsqr_proto_init() { if File_dnsqr_proto != nil { return } if !protoimpl.UnsafeEnabled { file_dnsqr_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*DnsQR); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_dnsqr_proto_rawDesc, NumEnums: 2, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_dnsqr_proto_goTypes, DependencyIndexes: file_dnsqr_proto_depIdxs, EnumInfos: file_dnsqr_proto_enumTypes, MessageInfos: file_dnsqr_proto_msgTypes, }.Build() File_dnsqr_proto = out.File file_dnsqr_proto_rawDesc = nil file_dnsqr_proto_goTypes = nil file_dnsqr_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/dnsqr.proto000066400000000000000000000042171401057443300172370ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message DnsQR { enum DnsQRType { UDP_INVALID = 0; UDP_QUERY_RESPONSE = 1; UDP_UNANSWERED_QUERY = 2; UDP_UNSOLICITED_RESPONSE = 3; TCP = 4; ICMP = 5; UDP_QUERY_ONLY = 6; UDP_RESPONSE_ONLY = 7; } enum UdpChecksum { ERROR = 0; ABSENT = 1; INCORRECT = 2; CORRECT = 3; } required DnsQRType type = 1; // the 9-tuple required bytes query_ip = 2; required bytes response_ip = 3; required uint32 proto = 4; required uint32 query_port = 5; required uint32 response_port = 6; required uint32 id = 7; optional bytes qname = 8; optional uint32 qtype = 9; optional uint32 qclass = 10; // rcode from the response optional uint32 rcode = 11; // packet data repeated bytes query_packet = 12; repeated int64 query_time_sec = 13; repeated sfixed32 query_time_nsec = 14; repeated bytes response_packet = 15; repeated int64 response_time_sec = 16; repeated sfixed32 response_time_nsec = 17; // only used if type = TCP optional bytes tcp = 18; // only used if type = ICMP optional bytes icmp = 19; // only set for UDP_UNANSWERED_QUERY optional double timeout = 20; // the result of UDP checksum verification of the response datagram. // note that the query datagram isn't checksummed, since a) the relevant // information from the query is almost always included in the response, // b) when capturing from the perspective of an initiator, the outbound // query is commonly subject to UDP checksum offload and will be incorrect // anyway. optional UdpChecksum udp_checksum = 21; // set if the address of the initiator was zeroed. optional bool resolver_address_zeroed = 22; } go-nmsg-0.2.0/nmsg_base/email.pb.go000066400000000000000000000215041401057443300170370ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: email.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Email_EmailType int32 const ( Email_unknown Email_EmailType = 0 Email_spamtrap Email_EmailType = 1 Email_rej_network Email_EmailType = 2 Email_rej_content Email_EmailType = 3 Email_rej_user Email_EmailType = 4 ) // Enum value maps for Email_EmailType. var ( Email_EmailType_name = map[int32]string{ 0: "unknown", 1: "spamtrap", 2: "rej_network", 3: "rej_content", 4: "rej_user", } Email_EmailType_value = map[string]int32{ "unknown": 0, "spamtrap": 1, "rej_network": 2, "rej_content": 3, "rej_user": 4, } ) func (x Email_EmailType) Enum() *Email_EmailType { p := new(Email_EmailType) *p = x return p } func (x Email_EmailType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Email_EmailType) Descriptor() protoreflect.EnumDescriptor { return file_email_proto_enumTypes[0].Descriptor() } func (Email_EmailType) Type() protoreflect.EnumType { return &file_email_proto_enumTypes[0] } func (x Email_EmailType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *Email_EmailType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = Email_EmailType(num) return nil } // Deprecated: Use Email_EmailType.Descriptor instead. func (Email_EmailType) EnumDescriptor() ([]byte, []int) { return file_email_proto_rawDescGZIP(), []int{0, 0} } type Email struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Type *Email_EmailType `protobuf:"varint,8,opt,name=type,enum=nmsg.base.Email_EmailType" json:"type,omitempty"` Headers []byte `protobuf:"bytes,2,opt,name=headers" json:"headers,omitempty"` Srcip []byte `protobuf:"bytes,3,opt,name=srcip" json:"srcip,omitempty"` Srchost []byte `protobuf:"bytes,4,opt,name=srchost" json:"srchost,omitempty"` Helo []byte `protobuf:"bytes,5,opt,name=helo" json:"helo,omitempty"` From []byte `protobuf:"bytes,6,opt,name=from" json:"from,omitempty"` Rcpt [][]byte `protobuf:"bytes,7,rep,name=rcpt" json:"rcpt,omitempty"` Bodyurl [][]byte `protobuf:"bytes,9,rep,name=bodyurl" json:"bodyurl,omitempty"` Body []byte `protobuf:"bytes,10,opt,name=body" json:"body,omitempty"` } func (x *Email) Reset() { *x = Email{} if protoimpl.UnsafeEnabled { mi := &file_email_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Email) String() string { return protoimpl.X.MessageStringOf(x) } func (*Email) ProtoMessage() {} func (x *Email) ProtoReflect() protoreflect.Message { mi := &file_email_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Email.ProtoReflect.Descriptor instead. func (*Email) Descriptor() ([]byte, []int) { return file_email_proto_rawDescGZIP(), []int{0} } func (x *Email) GetType() Email_EmailType { if x != nil && x.Type != nil { return *x.Type } return Email_unknown } func (x *Email) GetHeaders() []byte { if x != nil { return x.Headers } return nil } func (x *Email) GetSrcip() []byte { if x != nil { return x.Srcip } return nil } func (x *Email) GetSrchost() []byte { if x != nil { return x.Srchost } return nil } func (x *Email) GetHelo() []byte { if x != nil { return x.Helo } return nil } func (x *Email) GetFrom() []byte { if x != nil { return x.From } return nil } func (x *Email) GetRcpt() [][]byte { if x != nil { return x.Rcpt } return nil } func (x *Email) GetBodyurl() [][]byte { if x != nil { return x.Bodyurl } return nil } func (x *Email) GetBody() []byte { if x != nil { return x.Body } return nil } var File_email_proto protoreflect.FileDescriptor var file_email_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0xc3, 0x02, 0x0a, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x12, 0x2e, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1a, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x72, 0x63, 0x68, 0x6f, 0x73, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x73, 0x72, 0x63, 0x68, 0x6f, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x65, 0x6c, 0x6f, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x68, 0x65, 0x6c, 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x63, 0x70, 0x74, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x04, 0x72, 0x63, 0x70, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x6f, 0x64, 0x79, 0x75, 0x72, 0x6c, 0x18, 0x09, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x07, 0x62, 0x6f, 0x64, 0x79, 0x75, 0x72, 0x6c, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x56, 0x0a, 0x09, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x75, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x73, 0x70, 0x61, 0x6d, 0x74, 0x72, 0x61, 0x70, 0x10, 0x01, 0x12, 0x0f, 0x0a, 0x0b, 0x72, 0x65, 0x6a, 0x5f, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x10, 0x02, 0x12, 0x0f, 0x0a, 0x0b, 0x72, 0x65, 0x6a, 0x5f, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x10, 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x72, 0x65, 0x6a, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x10, 0x04, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_email_proto_rawDescOnce sync.Once file_email_proto_rawDescData = file_email_proto_rawDesc ) func file_email_proto_rawDescGZIP() []byte { file_email_proto_rawDescOnce.Do(func() { file_email_proto_rawDescData = protoimpl.X.CompressGZIP(file_email_proto_rawDescData) }) return file_email_proto_rawDescData } var file_email_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_email_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_email_proto_goTypes = []interface{}{ (Email_EmailType)(0), // 0: nmsg.base.Email.EmailType (*Email)(nil), // 1: nmsg.base.Email } var file_email_proto_depIdxs = []int32{ 0, // 0: nmsg.base.Email.type:type_name -> nmsg.base.Email.EmailType 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name } func init() { file_email_proto_init() } func file_email_proto_init() { if File_email_proto != nil { return } if !protoimpl.UnsafeEnabled { file_email_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Email); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_email_proto_rawDesc, NumEnums: 1, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_email_proto_goTypes, DependencyIndexes: file_email_proto_depIdxs, EnumInfos: file_email_proto_enumTypes, MessageInfos: file_email_proto_msgTypes, }.Build() File_email_proto = out.File file_email_proto_rawDesc = nil file_email_proto_goTypes = nil file_email_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/email.proto000066400000000000000000000011051401057443300171700ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Email { enum EmailType { unknown = 0; spamtrap = 1; rej_network = 2; rej_content = 3; rej_user = 4; } optional EmailType type = 8; optional bytes headers = 2; optional bytes srcip = 3; optional bytes srchost = 4; optional bytes helo = 5; optional bytes from = 6; repeated bytes rcpt = 7; repeated bytes bodyurl = 9; optional bytes body = 10; } go-nmsg-0.2.0/nmsg_base/encode.pb.go000066400000000000000000000152521401057443300172100ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: encode.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Encode_EncodeType int32 const ( Encode_TEXT Encode_EncodeType = 0 Encode_JSON Encode_EncodeType = 1 Encode_YAML Encode_EncodeType = 2 Encode_MSGPACK Encode_EncodeType = 3 Encode_XML Encode_EncodeType = 4 ) // Enum value maps for Encode_EncodeType. var ( Encode_EncodeType_name = map[int32]string{ 0: "TEXT", 1: "JSON", 2: "YAML", 3: "MSGPACK", 4: "XML", } Encode_EncodeType_value = map[string]int32{ "TEXT": 0, "JSON": 1, "YAML": 2, "MSGPACK": 3, "XML": 4, } ) func (x Encode_EncodeType) Enum() *Encode_EncodeType { p := new(Encode_EncodeType) *p = x return p } func (x Encode_EncodeType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Encode_EncodeType) Descriptor() protoreflect.EnumDescriptor { return file_encode_proto_enumTypes[0].Descriptor() } func (Encode_EncodeType) Type() protoreflect.EnumType { return &file_encode_proto_enumTypes[0] } func (x Encode_EncodeType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *Encode_EncodeType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = Encode_EncodeType(num) return nil } // Deprecated: Use Encode_EncodeType.Descriptor instead. func (Encode_EncodeType) EnumDescriptor() ([]byte, []int) { return file_encode_proto_rawDescGZIP(), []int{0, 0} } type Encode struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Type *Encode_EncodeType `protobuf:"varint,1,req,name=type,enum=nmsg.base.Encode_EncodeType" json:"type,omitempty"` Payload []byte `protobuf:"bytes,2,req,name=payload" json:"payload,omitempty"` } func (x *Encode) Reset() { *x = Encode{} if protoimpl.UnsafeEnabled { mi := &file_encode_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Encode) String() string { return protoimpl.X.MessageStringOf(x) } func (*Encode) ProtoMessage() {} func (x *Encode) ProtoReflect() protoreflect.Message { mi := &file_encode_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Encode.ProtoReflect.Descriptor instead. func (*Encode) Descriptor() ([]byte, []int) { return file_encode_proto_rawDescGZIP(), []int{0} } func (x *Encode) GetType() Encode_EncodeType { if x != nil && x.Type != nil { return *x.Type } return Encode_TEXT } func (x *Encode) GetPayload() []byte { if x != nil { return x.Payload } return nil } var File_encode_proto protoreflect.FileDescriptor var file_encode_proto_rawDesc = []byte{ 0x0a, 0x0c, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0x96, 0x01, 0x0a, 0x06, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x30, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0e, 0x32, 0x1c, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x40, 0x0a, 0x0a, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x54, 0x45, 0x58, 0x54, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x4a, 0x53, 0x4f, 0x4e, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x59, 0x41, 0x4d, 0x4c, 0x10, 0x02, 0x12, 0x0b, 0x0a, 0x07, 0x4d, 0x53, 0x47, 0x50, 0x41, 0x43, 0x4b, 0x10, 0x03, 0x12, 0x07, 0x0a, 0x03, 0x58, 0x4d, 0x4c, 0x10, 0x04, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_encode_proto_rawDescOnce sync.Once file_encode_proto_rawDescData = file_encode_proto_rawDesc ) func file_encode_proto_rawDescGZIP() []byte { file_encode_proto_rawDescOnce.Do(func() { file_encode_proto_rawDescData = protoimpl.X.CompressGZIP(file_encode_proto_rawDescData) }) return file_encode_proto_rawDescData } var file_encode_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_encode_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_encode_proto_goTypes = []interface{}{ (Encode_EncodeType)(0), // 0: nmsg.base.Encode.EncodeType (*Encode)(nil), // 1: nmsg.base.Encode } var file_encode_proto_depIdxs = []int32{ 0, // 0: nmsg.base.Encode.type:type_name -> nmsg.base.Encode.EncodeType 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name } func init() { file_encode_proto_init() } func file_encode_proto_init() { if File_encode_proto != nil { return } if !protoimpl.UnsafeEnabled { file_encode_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Encode); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_encode_proto_rawDesc, NumEnums: 1, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_encode_proto_goTypes, DependencyIndexes: file_encode_proto_depIdxs, EnumInfos: file_encode_proto_enumTypes, MessageInfos: file_encode_proto_msgTypes, }.Build() File_encode_proto = out.File file_encode_proto_rawDesc = nil file_encode_proto_goTypes = nil file_encode_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/encode.proto000066400000000000000000000004721401057443300173440ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Encode { enum EncodeType { TEXT = 0; JSON = 1; YAML = 2; MSGPACK = 3; XML = 4; } required EncodeType type = 1; required bytes payload = 2; } go-nmsg-0.2.0/nmsg_base/http.pb.go000066400000000000000000000273711401057443300167370ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: http.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Http_HttpType int32 const ( Http_unknown Http_HttpType = 0 Http_sinkhole Http_HttpType = 1 ) // Enum value maps for Http_HttpType. var ( Http_HttpType_name = map[int32]string{ 0: "unknown", 1: "sinkhole", } Http_HttpType_value = map[string]int32{ "unknown": 0, "sinkhole": 1, } ) func (x Http_HttpType) Enum() *Http_HttpType { p := new(Http_HttpType) *p = x return p } func (x Http_HttpType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Http_HttpType) Descriptor() protoreflect.EnumDescriptor { return file_http_proto_enumTypes[0].Descriptor() } func (Http_HttpType) Type() protoreflect.EnumType { return &file_http_proto_enumTypes[0] } func (x Http_HttpType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *Http_HttpType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = Http_HttpType(num) return nil } // Deprecated: Use Http_HttpType.Descriptor instead. func (Http_HttpType) EnumDescriptor() ([]byte, []int) { return file_http_proto_rawDescGZIP(), []int{0, 0} } type Http struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Type *Http_HttpType `protobuf:"varint,1,req,name=type,enum=nmsg.base.Http_HttpType" json:"type,omitempty"` Srcip []byte `protobuf:"bytes,2,opt,name=srcip" json:"srcip,omitempty"` Srchost []byte `protobuf:"bytes,3,opt,name=srchost" json:"srchost,omitempty"` Srcport *uint32 `protobuf:"varint,4,opt,name=srcport" json:"srcport,omitempty"` Dstip []byte `protobuf:"bytes,5,opt,name=dstip" json:"dstip,omitempty"` Dstport *uint32 `protobuf:"varint,6,opt,name=dstport" json:"dstport,omitempty"` Request []byte `protobuf:"bytes,7,opt,name=request" json:"request,omitempty"` P0FGenre []byte `protobuf:"bytes,65,opt,name=p0f_genre,json=p0fGenre" json:"p0f_genre,omitempty"` P0FDetail []byte `protobuf:"bytes,66,opt,name=p0f_detail,json=p0fDetail" json:"p0f_detail,omitempty"` P0FDist *int32 `protobuf:"varint,67,opt,name=p0f_dist,json=p0fDist" json:"p0f_dist,omitempty"` P0FLink []byte `protobuf:"bytes,68,opt,name=p0f_link,json=p0fLink" json:"p0f_link,omitempty"` P0FTos []byte `protobuf:"bytes,69,opt,name=p0f_tos,json=p0fTos" json:"p0f_tos,omitempty"` P0FFw *uint32 `protobuf:"varint,70,opt,name=p0f_fw,json=p0fFw" json:"p0f_fw,omitempty"` P0FNat *uint32 `protobuf:"varint,71,opt,name=p0f_nat,json=p0fNat" json:"p0f_nat,omitempty"` P0FReal *uint32 `protobuf:"varint,72,opt,name=p0f_real,json=p0fReal" json:"p0f_real,omitempty"` P0FScore *int32 `protobuf:"varint,73,opt,name=p0f_score,json=p0fScore" json:"p0f_score,omitempty"` P0FMflags *uint32 `protobuf:"varint,74,opt,name=p0f_mflags,json=p0fMflags" json:"p0f_mflags,omitempty"` P0FUptime *int32 `protobuf:"varint,75,opt,name=p0f_uptime,json=p0fUptime" json:"p0f_uptime,omitempty"` } func (x *Http) Reset() { *x = Http{} if protoimpl.UnsafeEnabled { mi := &file_http_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Http) String() string { return protoimpl.X.MessageStringOf(x) } func (*Http) ProtoMessage() {} func (x *Http) ProtoReflect() protoreflect.Message { mi := &file_http_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Http.ProtoReflect.Descriptor instead. func (*Http) Descriptor() ([]byte, []int) { return file_http_proto_rawDescGZIP(), []int{0} } func (x *Http) GetType() Http_HttpType { if x != nil && x.Type != nil { return *x.Type } return Http_unknown } func (x *Http) GetSrcip() []byte { if x != nil { return x.Srcip } return nil } func (x *Http) GetSrchost() []byte { if x != nil { return x.Srchost } return nil } func (x *Http) GetSrcport() uint32 { if x != nil && x.Srcport != nil { return *x.Srcport } return 0 } func (x *Http) GetDstip() []byte { if x != nil { return x.Dstip } return nil } func (x *Http) GetDstport() uint32 { if x != nil && x.Dstport != nil { return *x.Dstport } return 0 } func (x *Http) GetRequest() []byte { if x != nil { return x.Request } return nil } func (x *Http) GetP0FGenre() []byte { if x != nil { return x.P0FGenre } return nil } func (x *Http) GetP0FDetail() []byte { if x != nil { return x.P0FDetail } return nil } func (x *Http) GetP0FDist() int32 { if x != nil && x.P0FDist != nil { return *x.P0FDist } return 0 } func (x *Http) GetP0FLink() []byte { if x != nil { return x.P0FLink } return nil } func (x *Http) GetP0FTos() []byte { if x != nil { return x.P0FTos } return nil } func (x *Http) GetP0FFw() uint32 { if x != nil && x.P0FFw != nil { return *x.P0FFw } return 0 } func (x *Http) GetP0FNat() uint32 { if x != nil && x.P0FNat != nil { return *x.P0FNat } return 0 } func (x *Http) GetP0FReal() uint32 { if x != nil && x.P0FReal != nil { return *x.P0FReal } return 0 } func (x *Http) GetP0FScore() int32 { if x != nil && x.P0FScore != nil { return *x.P0FScore } return 0 } func (x *Http) GetP0FMflags() uint32 { if x != nil && x.P0FMflags != nil { return *x.P0FMflags } return 0 } func (x *Http) GetP0FUptime() int32 { if x != nil && x.P0FUptime != nil { return *x.P0FUptime } return 0 } var File_http_proto protoreflect.FileDescriptor var file_http_proto_rawDesc = []byte{ 0x0a, 0x0a, 0x68, 0x74, 0x74, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0xa0, 0x04, 0x0a, 0x04, 0x48, 0x74, 0x74, 0x70, 0x12, 0x2c, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0e, 0x32, 0x18, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x72, 0x63, 0x68, 0x6f, 0x73, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x73, 0x72, 0x63, 0x68, 0x6f, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x72, 0x63, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x73, 0x72, 0x63, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x64, 0x73, 0x74, 0x69, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x64, 0x73, 0x74, 0x69, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x73, 0x74, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x64, 0x73, 0x74, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x30, 0x66, 0x5f, 0x67, 0x65, 0x6e, 0x72, 0x65, 0x18, 0x41, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x70, 0x30, 0x66, 0x47, 0x65, 0x6e, 0x72, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x70, 0x30, 0x66, 0x5f, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x18, 0x42, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x70, 0x30, 0x66, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12, 0x19, 0x0a, 0x08, 0x70, 0x30, 0x66, 0x5f, 0x64, 0x69, 0x73, 0x74, 0x18, 0x43, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, 0x30, 0x66, 0x44, 0x69, 0x73, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x70, 0x30, 0x66, 0x5f, 0x6c, 0x69, 0x6e, 0x6b, 0x18, 0x44, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x30, 0x66, 0x4c, 0x69, 0x6e, 0x6b, 0x12, 0x17, 0x0a, 0x07, 0x70, 0x30, 0x66, 0x5f, 0x74, 0x6f, 0x73, 0x18, 0x45, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x70, 0x30, 0x66, 0x54, 0x6f, 0x73, 0x12, 0x15, 0x0a, 0x06, 0x70, 0x30, 0x66, 0x5f, 0x66, 0x77, 0x18, 0x46, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x70, 0x30, 0x66, 0x46, 0x77, 0x12, 0x17, 0x0a, 0x07, 0x70, 0x30, 0x66, 0x5f, 0x6e, 0x61, 0x74, 0x18, 0x47, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x70, 0x30, 0x66, 0x4e, 0x61, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x70, 0x30, 0x66, 0x5f, 0x72, 0x65, 0x61, 0x6c, 0x18, 0x48, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x70, 0x30, 0x66, 0x52, 0x65, 0x61, 0x6c, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x30, 0x66, 0x5f, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x49, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x70, 0x30, 0x66, 0x53, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x70, 0x30, 0x66, 0x5f, 0x6d, 0x66, 0x6c, 0x61, 0x67, 0x73, 0x18, 0x4a, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x70, 0x30, 0x66, 0x4d, 0x66, 0x6c, 0x61, 0x67, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x70, 0x30, 0x66, 0x5f, 0x75, 0x70, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x4b, 0x20, 0x01, 0x28, 0x05, 0x52, 0x09, 0x70, 0x30, 0x66, 0x55, 0x70, 0x74, 0x69, 0x6d, 0x65, 0x22, 0x25, 0x0a, 0x08, 0x48, 0x74, 0x74, 0x70, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x75, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x73, 0x69, 0x6e, 0x6b, 0x68, 0x6f, 0x6c, 0x65, 0x10, 0x01, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_http_proto_rawDescOnce sync.Once file_http_proto_rawDescData = file_http_proto_rawDesc ) func file_http_proto_rawDescGZIP() []byte { file_http_proto_rawDescOnce.Do(func() { file_http_proto_rawDescData = protoimpl.X.CompressGZIP(file_http_proto_rawDescData) }) return file_http_proto_rawDescData } var file_http_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_http_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_http_proto_goTypes = []interface{}{ (Http_HttpType)(0), // 0: nmsg.base.Http.HttpType (*Http)(nil), // 1: nmsg.base.Http } var file_http_proto_depIdxs = []int32{ 0, // 0: nmsg.base.Http.type:type_name -> nmsg.base.Http.HttpType 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name } func init() { file_http_proto_init() } func file_http_proto_init() { if File_http_proto != nil { return } if !protoimpl.UnsafeEnabled { file_http_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Http); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_http_proto_rawDesc, NumEnums: 1, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_http_proto_goTypes, DependencyIndexes: file_http_proto_depIdxs, EnumInfos: file_http_proto_enumTypes, MessageInfos: file_http_proto_msgTypes, }.Build() File_http_proto = out.File file_http_proto_rawDesc = nil file_http_proto_goTypes = nil file_http_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/http.proto000066400000000000000000000015541401057443300170700ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Http { enum HttpType { unknown = 0; sinkhole = 1; } required HttpType type = 1; optional bytes srcip = 2; optional bytes srchost = 3; optional uint32 srcport = 4; optional bytes dstip = 5; optional uint32 dstport = 6; optional bytes request = 7; optional bytes p0f_genre = 65; optional bytes p0f_detail = 66; optional int32 p0f_dist = 67; optional bytes p0f_link = 68; optional bytes p0f_tos = 69; optional uint32 p0f_fw = 70; optional uint32 p0f_nat = 71; optional uint32 p0f_real = 72; optional int32 p0f_score = 73; optional uint32 p0f_mflags = 74; optional int32 p0f_uptime = 75; } go-nmsg-0.2.0/nmsg_base/ipconn.pb.go000066400000000000000000000124441401057443300172410ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: ipconn.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type IPConn struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Proto *uint32 `protobuf:"varint,1,opt,name=proto" json:"proto,omitempty"` Srcip []byte `protobuf:"bytes,2,opt,name=srcip" json:"srcip,omitempty"` Srcport *uint32 `protobuf:"varint,3,opt,name=srcport" json:"srcport,omitempty"` Dstip []byte `protobuf:"bytes,4,opt,name=dstip" json:"dstip,omitempty"` Dstport *uint32 `protobuf:"varint,5,opt,name=dstport" json:"dstport,omitempty"` } func (x *IPConn) Reset() { *x = IPConn{} if protoimpl.UnsafeEnabled { mi := &file_ipconn_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *IPConn) String() string { return protoimpl.X.MessageStringOf(x) } func (*IPConn) ProtoMessage() {} func (x *IPConn) ProtoReflect() protoreflect.Message { mi := &file_ipconn_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use IPConn.ProtoReflect.Descriptor instead. func (*IPConn) Descriptor() ([]byte, []int) { return file_ipconn_proto_rawDescGZIP(), []int{0} } func (x *IPConn) GetProto() uint32 { if x != nil && x.Proto != nil { return *x.Proto } return 0 } func (x *IPConn) GetSrcip() []byte { if x != nil { return x.Srcip } return nil } func (x *IPConn) GetSrcport() uint32 { if x != nil && x.Srcport != nil { return *x.Srcport } return 0 } func (x *IPConn) GetDstip() []byte { if x != nil { return x.Dstip } return nil } func (x *IPConn) GetDstport() uint32 { if x != nil && x.Dstport != nil { return *x.Dstport } return 0 } var File_ipconn_proto protoreflect.FileDescriptor var file_ipconn_proto_rawDesc = []byte{ 0x0a, 0x0c, 0x69, 0x70, 0x63, 0x6f, 0x6e, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0x7e, 0x0a, 0x06, 0x49, 0x50, 0x43, 0x6f, 0x6e, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x72, 0x63, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x73, 0x72, 0x63, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x64, 0x73, 0x74, 0x69, 0x70, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x64, 0x73, 0x74, 0x69, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x73, 0x74, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x64, 0x73, 0x74, 0x70, 0x6f, 0x72, 0x74, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_ipconn_proto_rawDescOnce sync.Once file_ipconn_proto_rawDescData = file_ipconn_proto_rawDesc ) func file_ipconn_proto_rawDescGZIP() []byte { file_ipconn_proto_rawDescOnce.Do(func() { file_ipconn_proto_rawDescData = protoimpl.X.CompressGZIP(file_ipconn_proto_rawDescData) }) return file_ipconn_proto_rawDescData } var file_ipconn_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_ipconn_proto_goTypes = []interface{}{ (*IPConn)(nil), // 0: nmsg.base.IPConn } var file_ipconn_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_ipconn_proto_init() } func file_ipconn_proto_init() { if File_ipconn_proto != nil { return } if !protoimpl.UnsafeEnabled { file_ipconn_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*IPConn); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_ipconn_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_ipconn_proto_goTypes, DependencyIndexes: file_ipconn_proto_depIdxs, MessageInfos: file_ipconn_proto_msgTypes, }.Build() File_ipconn_proto = out.File file_ipconn_proto_rawDesc = nil file_ipconn_proto_goTypes = nil file_ipconn_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/ipconn.proto000066400000000000000000000004551401057443300173760ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message IPConn { optional uint32 proto = 1; optional bytes srcip = 2; optional uint32 srcport = 3; optional bytes dstip = 4; optional uint32 dstport = 5; } go-nmsg-0.2.0/nmsg_base/linkpair.pb.go000066400000000000000000000157501401057443300175670ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: linkpair.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Linkpair_Linktype int32 const ( Linkpair_anchor Linkpair_Linktype = 0 Linkpair_redirect Linkpair_Linktype = 1 ) // Enum value maps for Linkpair_Linktype. var ( Linkpair_Linktype_name = map[int32]string{ 0: "anchor", 1: "redirect", } Linkpair_Linktype_value = map[string]int32{ "anchor": 0, "redirect": 1, } ) func (x Linkpair_Linktype) Enum() *Linkpair_Linktype { p := new(Linkpair_Linktype) *p = x return p } func (x Linkpair_Linktype) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Linkpair_Linktype) Descriptor() protoreflect.EnumDescriptor { return file_linkpair_proto_enumTypes[0].Descriptor() } func (Linkpair_Linktype) Type() protoreflect.EnumType { return &file_linkpair_proto_enumTypes[0] } func (x Linkpair_Linktype) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *Linkpair_Linktype) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = Linkpair_Linktype(num) return nil } // Deprecated: Use Linkpair_Linktype.Descriptor instead. func (Linkpair_Linktype) EnumDescriptor() ([]byte, []int) { return file_linkpair_proto_rawDescGZIP(), []int{0, 0} } type Linkpair struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Type *Linkpair_Linktype `protobuf:"varint,1,req,name=type,enum=nmsg.base.Linkpair_Linktype" json:"type,omitempty"` Src []byte `protobuf:"bytes,2,req,name=src" json:"src,omitempty"` Dst []byte `protobuf:"bytes,3,req,name=dst" json:"dst,omitempty"` Headers []byte `protobuf:"bytes,5,opt,name=headers" json:"headers,omitempty"` } func (x *Linkpair) Reset() { *x = Linkpair{} if protoimpl.UnsafeEnabled { mi := &file_linkpair_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Linkpair) String() string { return protoimpl.X.MessageStringOf(x) } func (*Linkpair) ProtoMessage() {} func (x *Linkpair) ProtoReflect() protoreflect.Message { mi := &file_linkpair_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Linkpair.ProtoReflect.Descriptor instead. func (*Linkpair) Descriptor() ([]byte, []int) { return file_linkpair_proto_rawDescGZIP(), []int{0} } func (x *Linkpair) GetType() Linkpair_Linktype { if x != nil && x.Type != nil { return *x.Type } return Linkpair_anchor } func (x *Linkpair) GetSrc() []byte { if x != nil { return x.Src } return nil } func (x *Linkpair) GetDst() []byte { if x != nil { return x.Dst } return nil } func (x *Linkpair) GetHeaders() []byte { if x != nil { return x.Headers } return nil } var File_linkpair_proto protoreflect.FileDescriptor var file_linkpair_proto_rawDesc = []byte{ 0x0a, 0x0e, 0x6c, 0x69, 0x6e, 0x6b, 0x70, 0x61, 0x69, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0xa0, 0x01, 0x0a, 0x08, 0x4c, 0x69, 0x6e, 0x6b, 0x70, 0x61, 0x69, 0x72, 0x12, 0x30, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0e, 0x32, 0x1c, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x4c, 0x69, 0x6e, 0x6b, 0x70, 0x61, 0x69, 0x72, 0x2e, 0x4c, 0x69, 0x6e, 0x6b, 0x74, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x72, 0x63, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x03, 0x73, 0x72, 0x63, 0x12, 0x10, 0x0a, 0x03, 0x64, 0x73, 0x74, 0x18, 0x03, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x03, 0x64, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x22, 0x24, 0x0a, 0x08, 0x4c, 0x69, 0x6e, 0x6b, 0x74, 0x79, 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x61, 0x6e, 0x63, 0x68, 0x6f, 0x72, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x10, 0x01, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_linkpair_proto_rawDescOnce sync.Once file_linkpair_proto_rawDescData = file_linkpair_proto_rawDesc ) func file_linkpair_proto_rawDescGZIP() []byte { file_linkpair_proto_rawDescOnce.Do(func() { file_linkpair_proto_rawDescData = protoimpl.X.CompressGZIP(file_linkpair_proto_rawDescData) }) return file_linkpair_proto_rawDescData } var file_linkpair_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_linkpair_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_linkpair_proto_goTypes = []interface{}{ (Linkpair_Linktype)(0), // 0: nmsg.base.Linkpair.Linktype (*Linkpair)(nil), // 1: nmsg.base.Linkpair } var file_linkpair_proto_depIdxs = []int32{ 0, // 0: nmsg.base.Linkpair.type:type_name -> nmsg.base.Linkpair.Linktype 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name } func init() { file_linkpair_proto_init() } func file_linkpair_proto_init() { if File_linkpair_proto != nil { return } if !protoimpl.UnsafeEnabled { file_linkpair_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Linkpair); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_linkpair_proto_rawDesc, NumEnums: 1, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_linkpair_proto_goTypes, DependencyIndexes: file_linkpair_proto_depIdxs, EnumInfos: file_linkpair_proto_enumTypes, MessageInfos: file_linkpair_proto_msgTypes, }.Build() File_linkpair_proto = out.File file_linkpair_proto_rawDesc = nil file_linkpair_proto_goTypes = nil file_linkpair_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/linkpair.proto000066400000000000000000000005121401057443300177130ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Linkpair { enum Linktype { anchor = 0; redirect = 1; } required Linktype type = 1; required bytes src = 2; required bytes dst = 3; optional bytes headers = 5; } go-nmsg-0.2.0/nmsg_base/logline.pb.go000066400000000000000000000106671401057443300174110ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: logline.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type LogLine struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Category []byte `protobuf:"bytes,1,opt,name=category" json:"category,omitempty"` Message []byte `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"` } func (x *LogLine) Reset() { *x = LogLine{} if protoimpl.UnsafeEnabled { mi := &file_logline_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *LogLine) String() string { return protoimpl.X.MessageStringOf(x) } func (*LogLine) ProtoMessage() {} func (x *LogLine) ProtoReflect() protoreflect.Message { mi := &file_logline_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use LogLine.ProtoReflect.Descriptor instead. func (*LogLine) Descriptor() ([]byte, []int) { return file_logline_proto_rawDescGZIP(), []int{0} } func (x *LogLine) GetCategory() []byte { if x != nil { return x.Category } return nil } func (x *LogLine) GetMessage() []byte { if x != nil { return x.Message } return nil } var File_logline_proto protoreflect.FileDescriptor var file_logline_proto_rawDesc = []byte{ 0x0a, 0x0d, 0x6c, 0x6f, 0x67, 0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0x3f, 0x0a, 0x07, 0x4c, 0x6f, 0x67, 0x4c, 0x69, 0x6e, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_logline_proto_rawDescOnce sync.Once file_logline_proto_rawDescData = file_logline_proto_rawDesc ) func file_logline_proto_rawDescGZIP() []byte { file_logline_proto_rawDescOnce.Do(func() { file_logline_proto_rawDescData = protoimpl.X.CompressGZIP(file_logline_proto_rawDescData) }) return file_logline_proto_rawDescData } var file_logline_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_logline_proto_goTypes = []interface{}{ (*LogLine)(nil), // 0: nmsg.base.LogLine } var file_logline_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_logline_proto_init() } func file_logline_proto_init() { if File_logline_proto != nil { return } if !protoimpl.UnsafeEnabled { file_logline_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*LogLine); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_logline_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_logline_proto_goTypes, DependencyIndexes: file_logline_proto_depIdxs, MessageInfos: file_logline_proto_msgTypes, }.Build() File_logline_proto = out.File file_logline_proto_rawDesc = nil file_logline_proto_goTypes = nil file_logline_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/logline.proto000066400000000000000000000003061401057443300175340ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message LogLine { optional bytes category = 1; optional bytes message = 2; } go-nmsg-0.2.0/nmsg_base/ncap.pb.go000066400000000000000000000233021401057443300166670ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: ncap.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Ncap_NcapType int32 const ( Ncap_IPV4 Ncap_NcapType = 0 Ncap_IPV6 Ncap_NcapType = 1 Ncap_Legacy Ncap_NcapType = 2 ) // Enum value maps for Ncap_NcapType. var ( Ncap_NcapType_name = map[int32]string{ 0: "IPV4", 1: "IPV6", 2: "Legacy", } Ncap_NcapType_value = map[string]int32{ "IPV4": 0, "IPV6": 1, "Legacy": 2, } ) func (x Ncap_NcapType) Enum() *Ncap_NcapType { p := new(Ncap_NcapType) *p = x return p } func (x Ncap_NcapType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Ncap_NcapType) Descriptor() protoreflect.EnumDescriptor { return file_ncap_proto_enumTypes[0].Descriptor() } func (Ncap_NcapType) Type() protoreflect.EnumType { return &file_ncap_proto_enumTypes[0] } func (x Ncap_NcapType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *Ncap_NcapType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = Ncap_NcapType(num) return nil } // Deprecated: Use Ncap_NcapType.Descriptor instead. func (Ncap_NcapType) EnumDescriptor() ([]byte, []int) { return file_ncap_proto_rawDescGZIP(), []int{0, 0} } type Ncap_NcapLegacyType int32 const ( Ncap_UDP Ncap_NcapLegacyType = 0 Ncap_TCP Ncap_NcapLegacyType = 1 Ncap_ICMP Ncap_NcapLegacyType = 2 ) // Enum value maps for Ncap_NcapLegacyType. var ( Ncap_NcapLegacyType_name = map[int32]string{ 0: "UDP", 1: "TCP", 2: "ICMP", } Ncap_NcapLegacyType_value = map[string]int32{ "UDP": 0, "TCP": 1, "ICMP": 2, } ) func (x Ncap_NcapLegacyType) Enum() *Ncap_NcapLegacyType { p := new(Ncap_NcapLegacyType) *p = x return p } func (x Ncap_NcapLegacyType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Ncap_NcapLegacyType) Descriptor() protoreflect.EnumDescriptor { return file_ncap_proto_enumTypes[1].Descriptor() } func (Ncap_NcapLegacyType) Type() protoreflect.EnumType { return &file_ncap_proto_enumTypes[1] } func (x Ncap_NcapLegacyType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *Ncap_NcapLegacyType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = Ncap_NcapLegacyType(num) return nil } // Deprecated: Use Ncap_NcapLegacyType.Descriptor instead. func (Ncap_NcapLegacyType) EnumDescriptor() ([]byte, []int) { return file_ncap_proto_rawDescGZIP(), []int{0, 1} } type Ncap struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Type *Ncap_NcapType `protobuf:"varint,1,req,name=type,enum=nmsg.base.Ncap_NcapType" json:"type,omitempty"` Payload []byte `protobuf:"bytes,2,req,name=payload" json:"payload,omitempty"` // legacy NCAP fields Ltype *Ncap_NcapLegacyType `protobuf:"varint,3,opt,name=ltype,enum=nmsg.base.Ncap_NcapLegacyType" json:"ltype,omitempty"` Srcip []byte `protobuf:"bytes,4,opt,name=srcip" json:"srcip,omitempty"` Dstip []byte `protobuf:"bytes,5,opt,name=dstip" json:"dstip,omitempty"` Lint0 *uint32 `protobuf:"varint,6,opt,name=lint0" json:"lint0,omitempty"` Lint1 *uint32 `protobuf:"varint,7,opt,name=lint1" json:"lint1,omitempty"` } func (x *Ncap) Reset() { *x = Ncap{} if protoimpl.UnsafeEnabled { mi := &file_ncap_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Ncap) String() string { return protoimpl.X.MessageStringOf(x) } func (*Ncap) ProtoMessage() {} func (x *Ncap) ProtoReflect() protoreflect.Message { mi := &file_ncap_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Ncap.ProtoReflect.Descriptor instead. func (*Ncap) Descriptor() ([]byte, []int) { return file_ncap_proto_rawDescGZIP(), []int{0} } func (x *Ncap) GetType() Ncap_NcapType { if x != nil && x.Type != nil { return *x.Type } return Ncap_IPV4 } func (x *Ncap) GetPayload() []byte { if x != nil { return x.Payload } return nil } func (x *Ncap) GetLtype() Ncap_NcapLegacyType { if x != nil && x.Ltype != nil { return *x.Ltype } return Ncap_UDP } func (x *Ncap) GetSrcip() []byte { if x != nil { return x.Srcip } return nil } func (x *Ncap) GetDstip() []byte { if x != nil { return x.Dstip } return nil } func (x *Ncap) GetLint0() uint32 { if x != nil && x.Lint0 != nil { return *x.Lint0 } return 0 } func (x *Ncap) GetLint1() uint32 { if x != nil && x.Lint1 != nil { return *x.Lint1 } return 0 } var File_ncap_proto protoreflect.FileDescriptor var file_ncap_proto_rawDesc = []byte{ 0x0a, 0x0a, 0x6e, 0x63, 0x61, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0xb6, 0x02, 0x0a, 0x04, 0x4e, 0x63, 0x61, 0x70, 0x12, 0x2c, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0e, 0x32, 0x18, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x4e, 0x63, 0x61, 0x70, 0x2e, 0x4e, 0x63, 0x61, 0x70, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x34, 0x0a, 0x05, 0x6c, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1e, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x4e, 0x63, 0x61, 0x70, 0x2e, 0x4e, 0x63, 0x61, 0x70, 0x4c, 0x65, 0x67, 0x61, 0x63, 0x79, 0x54, 0x79, 0x70, 0x65, 0x52, 0x05, 0x6c, 0x74, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x73, 0x72, 0x63, 0x69, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x64, 0x73, 0x74, 0x69, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x64, 0x73, 0x74, 0x69, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6e, 0x74, 0x30, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x6c, 0x69, 0x6e, 0x74, 0x30, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6e, 0x74, 0x31, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x6c, 0x69, 0x6e, 0x74, 0x31, 0x22, 0x2a, 0x0a, 0x08, 0x4e, 0x63, 0x61, 0x70, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x49, 0x50, 0x56, 0x34, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x49, 0x50, 0x56, 0x36, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x4c, 0x65, 0x67, 0x61, 0x63, 0x79, 0x10, 0x02, 0x22, 0x2c, 0x0a, 0x0e, 0x4e, 0x63, 0x61, 0x70, 0x4c, 0x65, 0x67, 0x61, 0x63, 0x79, 0x54, 0x79, 0x70, 0x65, 0x12, 0x07, 0x0a, 0x03, 0x55, 0x44, 0x50, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x43, 0x50, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x49, 0x43, 0x4d, 0x50, 0x10, 0x02, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_ncap_proto_rawDescOnce sync.Once file_ncap_proto_rawDescData = file_ncap_proto_rawDesc ) func file_ncap_proto_rawDescGZIP() []byte { file_ncap_proto_rawDescOnce.Do(func() { file_ncap_proto_rawDescData = protoimpl.X.CompressGZIP(file_ncap_proto_rawDescData) }) return file_ncap_proto_rawDescData } var file_ncap_proto_enumTypes = make([]protoimpl.EnumInfo, 2) var file_ncap_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_ncap_proto_goTypes = []interface{}{ (Ncap_NcapType)(0), // 0: nmsg.base.Ncap.NcapType (Ncap_NcapLegacyType)(0), // 1: nmsg.base.Ncap.NcapLegacyType (*Ncap)(nil), // 2: nmsg.base.Ncap } var file_ncap_proto_depIdxs = []int32{ 0, // 0: nmsg.base.Ncap.type:type_name -> nmsg.base.Ncap.NcapType 1, // 1: nmsg.base.Ncap.ltype:type_name -> nmsg.base.Ncap.NcapLegacyType 2, // [2:2] is the sub-list for method output_type 2, // [2:2] is the sub-list for method input_type 2, // [2:2] is the sub-list for extension type_name 2, // [2:2] is the sub-list for extension extendee 0, // [0:2] is the sub-list for field type_name } func init() { file_ncap_proto_init() } func file_ncap_proto_init() { if File_ncap_proto != nil { return } if !protoimpl.UnsafeEnabled { file_ncap_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Ncap); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_ncap_proto_rawDesc, NumEnums: 2, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_ncap_proto_goTypes, DependencyIndexes: file_ncap_proto_depIdxs, EnumInfos: file_ncap_proto_enumTypes, MessageInfos: file_ncap_proto_msgTypes, }.Build() File_ncap_proto = out.File file_ncap_proto_rawDesc = nil file_ncap_proto_goTypes = nil file_ncap_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/ncap.proto000066400000000000000000000011151401057443300170230ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Ncap { enum NcapType { IPV4 = 0; IPV6 = 1; Legacy = 2; } enum NcapLegacyType { UDP = 0; TCP = 1; ICMP = 2; } required NcapType type = 1; required bytes payload = 2; // legacy NCAP fields optional NcapLegacyType ltype = 3; optional bytes srcip = 4; optional bytes dstip = 5; optional uint32 lint0 = 6; optional uint32 lint1 = 7; } go-nmsg-0.2.0/nmsg_base/packet.pb.go000066400000000000000000000144751401057443300172300ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: packet.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type PacketType int32 const ( // An IPv4 or IPv6 packet. The packet begins immediately with the IP // header and contains the complete packet payload. Distinguishing between // IPv4 and IPv6 packets is done by examining the IP version field in the // IP header. PacketType_IP PacketType = 1 ) // Enum value maps for PacketType. var ( PacketType_name = map[int32]string{ 1: "IP", } PacketType_value = map[string]int32{ "IP": 1, } ) func (x PacketType) Enum() *PacketType { p := new(PacketType) *p = x return p } func (x PacketType) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (PacketType) Descriptor() protoreflect.EnumDescriptor { return file_packet_proto_enumTypes[0].Descriptor() } func (PacketType) Type() protoreflect.EnumType { return &file_packet_proto_enumTypes[0] } func (x PacketType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Do not use. func (x *PacketType) UnmarshalJSON(b []byte) error { num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) if err != nil { return err } *x = PacketType(num) return nil } // Deprecated: Use PacketType.Descriptor instead. func (PacketType) EnumDescriptor() ([]byte, []int) { return file_packet_proto_rawDescGZIP(), []int{0} } type Packet struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields PayloadType *PacketType `protobuf:"varint,1,req,name=payload_type,json=payloadType,enum=nmsg.base.PacketType" json:"payload_type,omitempty"` Payload []byte `protobuf:"bytes,2,req,name=payload" json:"payload,omitempty"` } func (x *Packet) Reset() { *x = Packet{} if protoimpl.UnsafeEnabled { mi := &file_packet_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Packet) String() string { return protoimpl.X.MessageStringOf(x) } func (*Packet) ProtoMessage() {} func (x *Packet) ProtoReflect() protoreflect.Message { mi := &file_packet_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Packet.ProtoReflect.Descriptor instead. func (*Packet) Descriptor() ([]byte, []int) { return file_packet_proto_rawDescGZIP(), []int{0} } func (x *Packet) GetPayloadType() PacketType { if x != nil && x.PayloadType != nil { return *x.PayloadType } return PacketType_IP } func (x *Packet) GetPayload() []byte { if x != nil { return x.Payload } return nil } var File_packet_proto protoreflect.FileDescriptor var file_packet_proto_rawDesc = []byte{ 0x0a, 0x0c, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0x5c, 0x0a, 0x06, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x12, 0x38, 0x0a, 0x0c, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0e, 0x32, 0x15, 0x2e, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x54, 0x79, 0x70, 0x65, 0x52, 0x0b, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a, 0x14, 0x0a, 0x0a, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x06, 0x0a, 0x02, 0x49, 0x50, 0x10, 0x01, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_packet_proto_rawDescOnce sync.Once file_packet_proto_rawDescData = file_packet_proto_rawDesc ) func file_packet_proto_rawDescGZIP() []byte { file_packet_proto_rawDescOnce.Do(func() { file_packet_proto_rawDescData = protoimpl.X.CompressGZIP(file_packet_proto_rawDescData) }) return file_packet_proto_rawDescData } var file_packet_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_packet_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_packet_proto_goTypes = []interface{}{ (PacketType)(0), // 0: nmsg.base.PacketType (*Packet)(nil), // 1: nmsg.base.Packet } var file_packet_proto_depIdxs = []int32{ 0, // 0: nmsg.base.Packet.payload_type:type_name -> nmsg.base.PacketType 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name } func init() { file_packet_proto_init() } func file_packet_proto_init() { if File_packet_proto != nil { return } if !protoimpl.UnsafeEnabled { file_packet_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Packet); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_packet_proto_rawDesc, NumEnums: 1, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_packet_proto_goTypes, DependencyIndexes: file_packet_proto_depIdxs, EnumInfos: file_packet_proto_enumTypes, MessageInfos: file_packet_proto_msgTypes, }.Build() File_packet_proto = out.File file_packet_proto_rawDesc = nil file_packet_proto_goTypes = nil file_packet_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/packet.proto000066400000000000000000000007521401057443300173570ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; enum PacketType { // An IPv4 or IPv6 packet. The packet begins immediately with the IP // header and contains the complete packet payload. Distinguishing between // IPv4 and IPv6 packets is done by examining the IP version field in the // IP header. IP = 1; } message Packet { required PacketType payload_type = 1; required bytes payload = 2; } go-nmsg-0.2.0/nmsg_base/pkt.pb.go000066400000000000000000000104131401057443300165430ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: pkt.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Pkt struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Payload []byte `protobuf:"bytes,1,req,name=payload" json:"payload,omitempty"` LenFrame *uint32 `protobuf:"varint,2,opt,name=len_frame,json=lenFrame" json:"len_frame,omitempty"` } func (x *Pkt) Reset() { *x = Pkt{} if protoimpl.UnsafeEnabled { mi := &file_pkt_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Pkt) String() string { return protoimpl.X.MessageStringOf(x) } func (*Pkt) ProtoMessage() {} func (x *Pkt) ProtoReflect() protoreflect.Message { mi := &file_pkt_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Pkt.ProtoReflect.Descriptor instead. func (*Pkt) Descriptor() ([]byte, []int) { return file_pkt_proto_rawDescGZIP(), []int{0} } func (x *Pkt) GetPayload() []byte { if x != nil { return x.Payload } return nil } func (x *Pkt) GetLenFrame() uint32 { if x != nil && x.LenFrame != nil { return *x.LenFrame } return 0 } var File_pkt_proto protoreflect.FileDescriptor var file_pkt_proto_rawDesc = []byte{ 0x0a, 0x09, 0x70, 0x6b, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0x3c, 0x0a, 0x03, 0x50, 0x6b, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x6c, 0x65, 0x6e, 0x5f, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x6c, 0x65, 0x6e, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_pkt_proto_rawDescOnce sync.Once file_pkt_proto_rawDescData = file_pkt_proto_rawDesc ) func file_pkt_proto_rawDescGZIP() []byte { file_pkt_proto_rawDescOnce.Do(func() { file_pkt_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkt_proto_rawDescData) }) return file_pkt_proto_rawDescData } var file_pkt_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_pkt_proto_goTypes = []interface{}{ (*Pkt)(nil), // 0: nmsg.base.Pkt } var file_pkt_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_pkt_proto_init() } func file_pkt_proto_init() { if File_pkt_proto != nil { return } if !protoimpl.UnsafeEnabled { file_pkt_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Pkt); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_pkt_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_pkt_proto_goTypes, DependencyIndexes: file_pkt_proto_depIdxs, MessageInfos: file_pkt_proto_msgTypes, }.Build() File_pkt_proto = out.File file_pkt_proto_rawDesc = nil file_pkt_proto_goTypes = nil file_pkt_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/pkt.proto000066400000000000000000000003131401057443300166770ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Pkt { required bytes payload = 1; optional uint32 len_frame = 2; } go-nmsg-0.2.0/nmsg_base/xml.pb.go000066400000000000000000000104031401057443300165440ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: xml.proto package nmsg_base import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type Xml struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Xmltype []byte `protobuf:"bytes,1,req,name=xmltype" json:"xmltype,omitempty"` Xmlpayload []byte `protobuf:"bytes,2,req,name=xmlpayload" json:"xmlpayload,omitempty"` } func (x *Xml) Reset() { *x = Xml{} if protoimpl.UnsafeEnabled { mi := &file_xml_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Xml) String() string { return protoimpl.X.MessageStringOf(x) } func (*Xml) ProtoMessage() {} func (x *Xml) ProtoReflect() protoreflect.Message { mi := &file_xml_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Xml.ProtoReflect.Descriptor instead. func (*Xml) Descriptor() ([]byte, []int) { return file_xml_proto_rawDescGZIP(), []int{0} } func (x *Xml) GetXmltype() []byte { if x != nil { return x.Xmltype } return nil } func (x *Xml) GetXmlpayload() []byte { if x != nil { return x.Xmlpayload } return nil } var File_xml_proto protoreflect.FileDescriptor var file_xml_proto_rawDesc = []byte{ 0x0a, 0x09, 0x78, 0x6d, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x2e, 0x62, 0x61, 0x73, 0x65, 0x22, 0x3f, 0x0a, 0x03, 0x58, 0x6d, 0x6c, 0x12, 0x18, 0x0a, 0x07, 0x78, 0x6d, 0x6c, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x07, 0x78, 0x6d, 0x6c, 0x74, 0x79, 0x70, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x78, 0x6d, 0x6c, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x02, 0x20, 0x02, 0x28, 0x0c, 0x52, 0x0a, 0x78, 0x6d, 0x6c, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x2f, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x62, 0x61, 0x73, 0x65, } var ( file_xml_proto_rawDescOnce sync.Once file_xml_proto_rawDescData = file_xml_proto_rawDesc ) func file_xml_proto_rawDescGZIP() []byte { file_xml_proto_rawDescOnce.Do(func() { file_xml_proto_rawDescData = protoimpl.X.CompressGZIP(file_xml_proto_rawDescData) }) return file_xml_proto_rawDescData } var file_xml_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_xml_proto_goTypes = []interface{}{ (*Xml)(nil), // 0: nmsg.base.Xml } var file_xml_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_xml_proto_init() } func file_xml_proto_init() { if File_xml_proto != nil { return } if !protoimpl.UnsafeEnabled { file_xml_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Xml); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_xml_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_xml_proto_goTypes, DependencyIndexes: file_xml_proto_depIdxs, MessageInfos: file_xml_proto_msgTypes, }.Build() File_xml_proto = out.File file_xml_proto_rawDesc = nil file_xml_proto_goTypes = nil file_xml_proto_depIdxs = nil } go-nmsg-0.2.0/nmsg_base/xml.proto000066400000000000000000000003041401057443300167010ustar00rootroot00000000000000syntax = "proto2"; package nmsg.base; option go_package = "github.com/farsightsec/go-nmsg/nmsg_base"; message Xml { required bytes xmltype = 1; required bytes xmlpayload = 2; } go-nmsg-0.2.0/output.go000066400000000000000000000075161401057443300147610ustar00rootroot00000000000000/* * 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 import ( "io" "sync" "time" ) // An Output encapsulates NmsgPayloads in Nmsg containers and writes them to // an io.Writer. type Output interface { // Send sends an Nmsg Payload along the output. Implementations // may queue the payload for later sending, so the caller should // not modify the payload after calling Send. // // Send may be safely called from multiple goroutines. Send(*NmsgPayload) error // SetSequenced controls whether the Nmsg containers generated by // the Output have sequence numbers. SetSequenced(bool) // SetCompression controls whether the Output generates compressed // containers or uncompressed. SetCompression(bool) // SetCompressionRatio sets the anticipated compression ratio for // compressed containers. SetCompressionRatio(float32) // SetMaxSize sets the maximum size of a container the Output will // buffer, and the maximum size of a container or fragment the Output // will write. For Ethernet, consider using nmsg.EtherContainerSize. SetMaxSize(size int, writeSize int) // Close shuts down the output, flushing any queued payloads. // It will not close the underlying io.Writer. Close() error } type output struct { w io.Writer *Container } func (o *output) Send(p *NmsgPayload) error { c := o.Container c.AddPayload(p) _, err := c.WriteTo(o.w) return err } func (o *output) Close() error { return nil } // UnbufferedOutput returns an Output which writes an Nmsg container for // each payload. func UnbufferedOutput(w io.Writer) Output { return &output{w: w, Container: NewContainer()} } type bufferedOutput struct { output mu sync.Mutex } func (o *bufferedOutput) Send(p *NmsgPayload) error { o.mu.Lock() defer o.mu.Unlock() var ok, full bool for !ok { ok, full = o.AddPayload(p) if !full { return nil } _, err := o.WriteTo(o.w) if err != nil { return err } } return nil } func (o *bufferedOutput) Close() error { o.mu.Lock() defer o.mu.Unlock() if len(o.Nmsg.Payloads) > 0 { _, err := o.WriteTo(o.w) return err } return nil } // BufferedOutput creates an Output which collects NmsgPayloads and sends // them in containers as close as possible to the size set by SetMaxSize() func BufferedOutput(w io.Writer) Output { o := new(bufferedOutput) o.output = output{w: w, Container: NewContainer()} return o } type timedBufferedOutput struct { bufferedOutput timer *time.Timer d time.Duration err error } func (t *timedBufferedOutput) Send(p *NmsgPayload) error { t.mu.Lock() defer t.mu.Unlock() if t.err != nil { return t.err } // We are sending the first payload on a new or recently-flushed // output. Reset or restart flush timer. if len(t.Nmsg.Payloads) == 0 && !t.timer.Reset(t.d) { t.timer = time.AfterFunc(t.d, t.flush) } var ok, full bool for !ok { ok, full = t.AddPayload(p) if !full { break } t.timer.Reset(t.d) _, err := t.WriteTo(t.w) if err != nil { t.err = err return err } } return nil } func (t *timedBufferedOutput) Close() error { t.timer.Stop() return t.bufferedOutput.Close() } func (t *timedBufferedOutput) flush() { t.mu.Lock() defer t.mu.Unlock() if len(t.Nmsg.Payloads) > 0 { _, t.err = t.WriteTo(t.w) } } // TimedBufferedOutput creates an Output which collects NmsgPayloads // and sends them in containers as close as possible to the size provided to // SetMaxSize or after the given Duration, whichever comes first. func TimedBufferedOutput(w io.Writer, d time.Duration) Output { t := &timedBufferedOutput{d: d} t.bufferedOutput.output = output{w: w, Container: NewContainer()} t.timer = time.AfterFunc(d, t.flush) return t } go-nmsg-0.2.0/output_test.go000066400000000000000000000207121401057443300160110ustar00rootroot00000000000000/* * 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_test import ( "bytes" "errors" "testing" "time" "github.com/farsightsec/go-nmsg" ) type countWriter struct { count, total int closed bool t *testing.T } // Testing output. // countWriter Implements io.WriteCloser, plus a Count() method returning // how many times it has been called and a Total() method returning the // number of bytes written. func (w *countWriter) Count() int { return w.count } func (w *countWriter) Total() int { return w.total } func (w *countWriter) Write(b []byte) (int, error) { w.t.Logf("Writing %d bytes", len(b)) w.count++ w.total += len(b) return len(b), nil } func newCountWriter(t *testing.T) *countWriter { return &countWriter{t: t} } // bufWriter augments bytes.Buffer with a Clos() method to // satisfy io.WriteCloser type bufWriter struct { *bytes.Buffer } func newBufWriter() *bufWriter { return &bufWriter{new(bytes.Buffer)} } func TestUnBufferedOutput(t *testing.T) { c := newCountWriter(t) p, err := nmsg.Payload(testMessage(1000)) if err != nil { t.Errorf(err.Error()) } o := nmsg.UnbufferedOutput(c) o.SetMaxSize(1500, 0) if err := o.Send(p); err != nil { t.Errorf(err.Error()) } if c.Count() < 1 { t.Errorf("No write occurred") } if c.Total() < 1000 { t.Errorf("Write was too short") } if err := o.Close(); err != nil { t.Errorf("Close failed") } } func TestBufferedOutput(t *testing.T) { c := newCountWriter(t) o := nmsg.BufferedOutput(c) o.SetMaxSize(1500, 0) o.SetSequenced(true) // this should go in the buffer, and not be written if err := o.Send(testPayload(800)); err != nil { t.Errorf(err.Error()) } if c.Count() > 0 { t.Error("Buffer did not suppress write") } // this should flush the buffer, causing one write, // then go into the buffer, not causing a second write. if err := o.Send(testPayload(800)); err != nil { t.Errorf(err.Error()) } if c.Count() < 1 { t.Error("Buffer did not write") } if c.Count() > 1 { t.Error("Buffer did not suppress write") } // this should flush the buffer, causing one write, // then bypass the buffer and be written in two fragments if err := o.Send(testPayload(1700)); err != nil { t.Errorf(err.Error()) } if err := o.Close(); err != nil { t.Errorf(err.Error()) } if c.Count() < 4 { t.Errorf("Missing writes: %d should be 4", c.Count()) } if c.Count() > 4 { t.Error("Extra writes") } } func TestBufferedOutputNoConfig(t *testing.T) { c := newCountWriter(t) o := nmsg.BufferedOutput(c) // this should go in the buffer with the default // MinContainerSize maximum, and not be written if err := o.Send(testPayload(300)); err != nil { t.Errorf(err.Error()) } if c.Count() > 0 { t.Error("Buffer did not suppress write") } // this should flush the buffer, causing one write, // then go into the buffer, not causing a second write. if err := o.Send(testPayload(300)); err != nil { t.Errorf(err.Error()) } if c.Count() < 1 { t.Error("Buffer did not write") } if c.Count() > 1 { t.Error("Buffer did not suppress write") } // this should flush the buffer, causing one write, // then bypass the buffer and be written in two fragments if err := o.Send(testPayload(600)); err != nil { t.Errorf(err.Error()) } if err := o.Close(); err != nil { t.Errorf(err.Error()) } if c.Count() < 4 { t.Errorf("Missing writes: %d should be 4", c.Count()) } if c.Count() > 4 { t.Error("Extra writes") } } func TestTimedBufferedOutput(t *testing.T) { c := newCountWriter(t) o := nmsg.TimedBufferedOutput(c, 100*time.Millisecond) o.SetMaxSize(1500, 0) o.SetSequenced(true) // This should wait about 100ms to send if err := o.Send(testPayload(100)); err != nil { t.Error(err.Error()) } if c.Count() > 0 { t.Error("Write not delayed") } time.Sleep(110 * time.Millisecond) if c.Count() < 1 { t.Error("Write timed out.") } if err := o.Close(); err != nil { t.Error(err.Error()) } } func TestTimedBufferedOutputNoConfig(t *testing.T) { c := newCountWriter(t) o := nmsg.TimedBufferedOutput(c, 100*time.Millisecond) o.SetSequenced(true) // This should wait about 100ms to send if err := o.Send(testPayload(100)); err != nil { t.Error(err.Error()) } if c.Count() > 0 { t.Error("Write not delayed") } time.Sleep(110 * time.Millisecond) if c.Count() < 1 { t.Error("Write timed out.") } if err := o.Close(); err != nil { t.Error(err.Error()) } for i := 0; i < 10; i++ { o.Send(testPayload(100)) } time.Sleep(110 * time.Millisecond) if c.Count() < 2 { t.Error("Writes timed out") } } func TestTimedBufferReset(t *testing.T) { c := newCountWriter(t) o := nmsg.TimedBufferedOutput(c, 100*time.Millisecond) o.SetMaxSize(1500, 0) o.SetSequenced(true) if err := o.Send(testPayload(750)); err != nil { t.Error(err.Error()) } time.Sleep(50 * time.Millisecond) // This should trigger a write, leave this payload in // the buffer, and reset the timer for another 100ms. if err := o.Send(testPayload(750)); err != nil { t.Error(err.Error()) } time.Sleep(25 * time.Millisecond) if c.Count() < 1 { t.Error("Write failed to happen") } if c.Count() > 1 { t.Error("Spurious write happened") } // Check at start + 100ms, to make sure the buffer didn't fire twice time.Sleep(25 * time.Millisecond) if c.Count() > 1 { t.Error("premature second write") } // Check in after start + 150ms, second write should have happened. time.Sleep(55 * time.Millisecond) if c.Count() < 2 { t.Error("second write late") } time.Sleep(55 * time.Millisecond) // The previous write caused the timer to expire, and it will need to // be restarted. Test that code path with one more sequence of Sends // which will force a flush. for i := 0; i < 3; i++ { if err := o.Send(testPayload(750)); err != nil { t.Error(err.Error()) } } time.Sleep(25 * time.Millisecond) if c.Count() < 3 { t.Error("third write late") } time.Sleep(80 * time.Millisecond) if c.Count() < 4 { t.Error("Final write late") } o.Close() } type countdownWriter int func (c *countdownWriter) Write(b []byte) (int, error) { if *c > 0 { (*c)-- return len(b), nil } return 0, errors.New("writer finished") } func newCountdownWriter(n int) *countdownWriter { c := countdownWriter(n) return &c } func TestTimedBufferedOutputError(t *testing.T) { cw := newCountdownWriter(1) o := nmsg.TimedBufferedOutput(cw, 100*time.Millisecond) o.SetMaxSize(1500, 0) if err := o.Send(testPayload(750)); err != nil { t.Error(err.Error()) } if err := o.Send(testPayload(750)); err != nil { t.Error(err.Error()) } // write should occur above, and leave one payload in buffer, // to be flushed by the next, which should return an error if err := o.Send(testPayload(750)); err == nil { t.Error("no error") } } func TestTimedBufferedOutputTimedError(t *testing.T) { cw := newCountdownWriter(0) o := nmsg.TimedBufferedOutput(cw, 100*time.Millisecond) if err := o.Send(testPayload(100)); err != nil { t.Error(err) } <-time.After(110 * time.Millisecond) // At this point, a timer-driven flush should have triggered // a writer error, which should be returned on the next Send. if err := o.Send(testPayload(100)); err == nil { t.Error("no error") } } type nullwriter struct{} func (n nullwriter) Write(b []byte) (int, error) { return len(b), nil } func BenchmarkUnbufferedOutput(b *testing.B) { var w nullwriter p, err := nmsg.Payload(testMessage(1000)) if err != nil { b.Error(err.Error()) } o := nmsg.UnbufferedOutput(w) o.SetMaxSize(1500, 0) for i := 0; i < b.N; i++ { if err := o.Send(p); err != nil { b.Error(err.Error()) return } } o.Close() } func BenchmarkBufferedOutput(b *testing.B) { var w nullwriter p, err := nmsg.Payload(testMessage(1000)) if err != nil { b.Error(err.Error()) } o := nmsg.BufferedOutput(w) o.SetMaxSize(1500, 0) o.SetSequenced(true) for i := 0; i < b.N; i++ { if err := o.Send(p); err != nil { b.Error(err.Error()) return } } o.Close() } func BenchmarkTimedBufferedOutput(b *testing.B) { var w nullwriter p, err := nmsg.Payload(testMessage(1000)) if err != nil { b.Error(err.Error()) } o := nmsg.TimedBufferedOutput(w, 100*time.Millisecond) o.SetMaxSize(1500, 0) o.SetSequenced(true) for i := 0; i < b.N; i++ { if err := o.Send(p); err != nil { b.Error(err.Error()) return } } o.Close() } go-nmsg-0.2.0/payload.go000066400000000000000000000033671401057443300150520ustar00rootroot00000000000000/* * 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 import ( "encoding/binary" "time" "google.golang.org/protobuf/proto" ) // Payload encapsulates an nmsg message in a NmsgPayload, suitable for sending to // an Output func Payload(m Message) (*NmsgPayload, error) { mbytes, err := proto.Marshal(m) if err != nil { return nil, err } now := time.Now().UnixNano() return &NmsgPayload{ Vid: proto.Uint32(m.GetVid()), Msgtype: proto.Uint32(m.GetMsgtype()), TimeSec: proto.Int64(now / 1000000000), TimeNsec: proto.Uint32(uint32(now % 1000000000)), Payload: mbytes, }, nil } // SetSource sets the NmsgPayload source identifier. func (p *NmsgPayload) SetSource(s uint32) { p.Source = proto.Uint32(s) } // SetOperator sets the NmsgPayload operator identifier. func (p *NmsgPayload) SetOperator(o uint32) { p.Operator = proto.Uint32(o) } // SetGroup sets the NmsgPayload group identifier. func (p *NmsgPayload) SetGroup(g uint32) { p.Group = proto.Uint32(g) } // Message returns the message encapsulated in the NmsgPayload, // Unmarshaled func (p *NmsgPayload) Message() (Message, error) { m, err := NewMessage(*p.Vid, *p.Msgtype) if err != nil { return nil, err } err = proto.Unmarshal(p.Payload, m) if err != nil { return nil, err } return m, nil } func (p *NmsgPayload) payloadSize() int { var ibuf [binary.MaxVarintLen64]byte psiz := proto.Size(p) // tag + varint length of encoded p psiz += 1 + binary.PutUvarint(ibuf[:], uint64(psiz)) // tag + varint CRC32 psiz += 1 + binary.MaxVarintLen32 return psiz } go-nmsg-0.2.0/payload_test.go000066400000000000000000000023661401057443300161070ustar00rootroot00000000000000/* * 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_test import ( "testing" "github.com/farsightsec/go-nmsg" ) func testMessage(length int) nmsg.Message { return &TestMessage{Bytes: make([]byte, length)} } func testPayload(length int) *nmsg.NmsgPayload { p, err := nmsg.Payload(testMessage(length)) if err != nil { return nil } return p } func (t *TestMessage) GetVid() uint32 { return 10 } func (t *TestMessage) GetMsgtype() uint32 { return 20 } func init() { nmsg.Register(&TestMessage{}) } func TestRegister(t *testing.T) { msg, err := nmsg.NewMessage(10, 20) if err != nil { t.Error(err) } if _, ok := msg.(*TestMessage); !ok { t.Errorf("NewMessage returned wrong type") } } func TestPayload(t *testing.T) { p, err := nmsg.Payload(testMessage(1000)) if err != nil { t.Errorf("nmsg.Payload(): %s", err) } m, err := p.Message() if err != nil { t.Error(err) } if tp, ok := m.(*TestMessage); !ok { t.Errorf("Wrong type from payload") } else if len(tp.Bytes) != 1000 { t.Error("decode failed") } } go-nmsg-0.2.0/payload_test.pb_test.go000066400000000000000000000104611401057443300175410ustar00rootroot00000000000000// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0-devel // protoc (unknown) // source: payload_test.proto package nmsg_test import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) type TestMessage struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Bytes []byte `protobuf:"bytes,2,opt,name=bytes" json:"bytes,omitempty"` } func (x *TestMessage) Reset() { *x = TestMessage{} if protoimpl.UnsafeEnabled { mi := &file_payload_test_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *TestMessage) String() string { return protoimpl.X.MessageStringOf(x) } func (*TestMessage) ProtoMessage() {} func (x *TestMessage) ProtoReflect() protoreflect.Message { mi := &file_payload_test_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use TestMessage.ProtoReflect.Descriptor instead. func (*TestMessage) Descriptor() ([]byte, []int) { return file_payload_test_proto_rawDescGZIP(), []int{0} } func (x *TestMessage) GetBytes() []byte { if x != nil { return x.Bytes } return nil } var File_payload_test_proto protoreflect.FileDescriptor var file_payload_test_proto_rawDesc = []byte{ 0x0a, 0x12, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x22, 0x23, 0x0a, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x62, 0x79, 0x74, 0x65, 0x73, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x66, 0x61, 0x72, 0x73, 0x69, 0x67, 0x68, 0x74, 0x73, 0x65, 0x63, 0x2f, 0x67, 0x6f, 0x2d, 0x6e, 0x6d, 0x73, 0x67, 0x3b, 0x6e, 0x6d, 0x73, 0x67, 0x5f, 0x74, 0x65, 0x73, 0x74, } var ( file_payload_test_proto_rawDescOnce sync.Once file_payload_test_proto_rawDescData = file_payload_test_proto_rawDesc ) func file_payload_test_proto_rawDescGZIP() []byte { file_payload_test_proto_rawDescOnce.Do(func() { file_payload_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_payload_test_proto_rawDescData) }) return file_payload_test_proto_rawDescData } var file_payload_test_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_payload_test_proto_goTypes = []interface{}{ (*TestMessage)(nil), // 0: nmsg_test.TestMessage } var file_payload_test_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_payload_test_proto_init() } func file_payload_test_proto_init() { if File_payload_test_proto != nil { return } if !protoimpl.UnsafeEnabled { file_payload_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TestMessage); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_payload_test_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_payload_test_proto_goTypes, DependencyIndexes: file_payload_test_proto_depIdxs, MessageInfos: file_payload_test_proto_msgTypes, }.Build() File_payload_test_proto = out.File file_payload_test_proto_rawDesc = nil file_payload_test_proto_goTypes = nil file_payload_test_proto_depIdxs = nil } go-nmsg-0.2.0/payload_test.proto000066400000000000000000000002321401057443300166330ustar00rootroot00000000000000syntax = "proto2"; package nmsg_test; option go_package = "github.com/farsightsec/go-nmsg;nmsg_test"; message TestMessage { optional bytes bytes = 2; } go-nmsg-0.2.0/register.go000066400000000000000000000032041401057443300152330ustar00rootroot00000000000000/* * 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 import ( "fmt" "reflect" ) var types map[uint32]map[uint32]reflect.Type // Register records the supplied message's type, indexed by its MessageType // and VendorID, for the purposes of decoding protobuf-encoded payloads. // // Register should be called from the init() function of the module defining // the payload type. It is not safe to call from multiple goroutines, and // may not be called if any goroutine is concurrently decoding NMSG payloads. func Register(m Message) { if types == nil { types = make(map[uint32]map[uint32]reflect.Type) } vid := m.GetVid() v, ok := types[vid] if !ok { v = make(map[uint32]reflect.Type) types[vid] = v } msgtype := m.GetMsgtype() v[msgtype] = reflect.TypeOf(m) } type unknownVendor uint32 func (v unknownVendor) Error() string { return fmt.Sprintf("Vendor %d has no registered Msgtypes.", v) } type unknownMsgtype struct{ vid, msgtype uint32 } func (t unknownMsgtype) Error() string { return fmt.Sprintf("Msgtype %d is not registered for vendor %d.", t.msgtype, t.vid) } // NewMessage creates a new Message with an underlying type identified // by vid, msgtype. func NewMessage(vid, msgtype uint32) (Message, error) { v, ok := types[vid] if !ok { return nil, unknownVendor(vid) } t, ok := v[msgtype] if !ok { return nil, unknownMsgtype{vid, msgtype} } return reflect.New(t.Elem()).Interface().(Message), nil } go-nmsg-0.2.0/sockspec.go000066400000000000000000000053331401057443300152260ustar00rootroot00000000000000/* * Copyright (c) 2018 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 import ( "encoding/json" "fmt" "net" "strconv" "strings" ) // A Sockspec is an address of a single socket (addr/port) or a series of // sockets with contiguous port numbers (addr/loport..hiport) type Sockspec struct { Addr *net.UDPAddr Hiport int } // ParseSockspec creates a Sockspec from its text representaion v. func ParseSockspec(v string) (*Sockspec, error) { s := &Sockspec{} return s, s.Set(v) } // Set initializes a Sockspec from its text representation v. Set satisfies // flag.Value allowing a sockspec to be conveniently specified as a command // line parameter. func (s *Sockspec) Set(v string) error { l := strings.SplitN(v, "/", 2) if len(l) != 2 { return fmt.Errorf("Invalid sockspec: %s", v) } p := strings.SplitN(l[1], "..", 2) addr := fmt.Sprintf("%s:%s", l[0], p[0]) uaddr, err := net.ResolveUDPAddr("udp", addr) if err != nil { return fmt.Errorf("Invalid addr %s: %v", addr, err) } s.Addr = uaddr if len(p) == 1 { s.Hiport = uaddr.Port return nil } hiport, err := strconv.ParseUint(p[1], 10, 16) if err != nil { return fmt.Errorf("Invalid high port %s: %v", p[1], err) } if int(hiport) <= uaddr.Port { return fmt.Errorf("Invalid port range %s", l[1]) } s.Hiport = int(hiport) return nil } // UnmarshalJSON satisifies json.Unmarshaler allowing Sockspecs to be parsed // from JSON configurations. func (s *Sockspec) UnmarshalJSON(b []byte) error { var v string if err := json.Unmarshal(b, &v); err != nil { return err } return s.Set(v) } // UnmarshalYAML satisifies yaml.Unmarshaler allowing Sockspecs to be parsed // from YAML configurations. func (s *Sockspec) UnmarshalYAML(u func(interface{}) error) error { var v string if err := u(&v); err != nil { return err } return s.Set(v) } // Addrs returns the list of UDP socket addresses of the Sockspec, or nil // if the Sockspec is uninitialized. func (s *Sockspec) Addrs() []*net.UDPAddr { var addrs []*net.UDPAddr if s.Addr == nil { return nil } for i := s.Addr.Port; i <= s.Hiport; i++ { a := &net.UDPAddr{} *a = *s.Addr a.Port = i addrs = append(addrs, a) } return addrs } // String returns the string representation of the Sockspec. If the Sockspec // is uninitialized, String returns the empty string. func (s *Sockspec) String() string { if s.Addr == nil { return "" } if s.Hiport > s.Addr.Port { return fmt.Sprintf("%s/%d..%d", s.Addr.IP.String(), s.Addr.Port, s.Hiport) } return fmt.Sprintf("%s/%d", s.Addr.IP.String(), s.Addr.Port) } go-nmsg-0.2.0/sockspec_test.go000066400000000000000000000036321401057443300162650ustar00rootroot00000000000000/* * Copyright (c) 2018 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 import ( "encoding/json" "testing" yaml "gopkg.in/yaml.v2" ) var sockspecTestCases = []struct { sockspec string valid bool naddrs int }{ {"127.0.0.1/382", true, 1}, {"127.0.0.1/382..390", true, 9}, {"foobar", false, 0}, {"127.0.0.1/foobar", false, 0}, {"127.0.0.1/390..382", false, 0}, {"127.0.0.1/390..foobar", false, 0}, {"invalid_hostname/381", false, 0}, } func testSockSpecCommon(t *testing.T, parse func(string, *Sockspec) error) { t.Helper() for _, tc := range sockspecTestCases { var ss Sockspec if err := parse(tc.sockspec, &ss); err != nil { if tc.valid { t.Errorf("%s: %v", tc.sockspec, err) } continue } if !tc.valid { t.Errorf("parsed invalid sockspec %s", tc.sockspec) continue } if len(ss.Addrs()) != tc.naddrs { t.Errorf("%s: expected %d addrs, got %d", tc.sockspec, tc.naddrs, len(ss.Addrs())) } if ss.String() != tc.sockspec { t.Errorf("%s parsed to %s (%#v)", tc.sockspec, &ss, ss) } } } func TestSockSpecSet(t *testing.T) { testSockSpecCommon(t, func(s string, ss *Sockspec) error { return ss.Set(s) }) } func TestSockSpecParse(t *testing.T) { testSockSpecCommon(t, func(s string, ss *Sockspec) error { parsed, err := ParseSockspec(s) *ss = *parsed return err }) } func TestSockSpecJSON(t *testing.T) { testSockSpecCommon(t, func(s string, ss *Sockspec) error { b, err := json.Marshal(s) if err != nil { return err } return json.Unmarshal(b, ss) }) } func TestSockSpecYAML(t *testing.T) { testSockSpecCommon(t, func(s string, ss *Sockspec) error { b, err := yaml.Marshal(s) if err != nil { return err } return yaml.Unmarshal(b, ss) }) } go-nmsg-0.2.0/zbuf.go000066400000000000000000000017311401057443300143600ustar00rootroot00000000000000/* * 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 import ( "bytes" "compress/zlib" "encoding/binary" "io" ) func zbufDeflate(b []byte) ([]byte, error) { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, uint32(len(b))) w := zlib.NewWriter(buf) if _, err := w.Write(b); err != nil { return nil, err } if err := w.Close(); err != nil { return nil, err } return buf.Bytes(), nil } func zbufInflate(b []byte) ([]byte, error) { br := bytes.NewReader(b) var ilen uint32 binary.Read(br, binary.BigEndian, &ilen) buf := bytes.NewBuffer(make([]byte, 0, int(ilen))) r, err := zlib.NewReader(br) if err != nil { return nil, err } if _, err = io.Copy(buf, r); err != nil { return nil, err } r.Close() return buf.Bytes(), nil } go-nmsg-0.2.0/zlib_test.go000066400000000000000000000020521401057443300154060ustar00rootroot00000000000000/* * 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_test import ( "bytes" "testing" "github.com/farsightsec/go-nmsg" ) func TestZlib(t *testing.T) { b := new(bytes.Buffer) m := testMessage(20) p, err := nmsg.Payload(m) if err != nil { t.Fatal(err) } out := nmsg.UnbufferedOutput(b) out.SetCompression(true) out.SetMaxSize(1500, 0) if err := out.Send(p); err != nil { t.Fatal(err) } inp := nmsg.NewInput(b, 1500) p, err = inp.Recv() if err != nil { t.Fatal(err) } mm, err := p.Message() if err != nil { t.Fatal(err) } mi, ok := mm.(*TestMessage) if !ok { t.Error("received message of wrong type") } if len(mi.Bytes) != len(m.(*TestMessage).Bytes) { t.Error("received message of wrong length") } for i := range mi.Bytes { if mi.Bytes[i] != 0 { t.Fatal("received message with wrong data") } } }