-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnmsg.go
49 lines (41 loc) · 1.32 KB
/
nmsg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* 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
}