1
- ZSocket
2
- -------
1
+ # ZSocket
2
+
3
3
ZSocket is a library that wraps the linux zero-copy socket syscall to create a ring buffer in a memory mapped file.
4
4
It also contains some utility functions and types to help with a handful of layer 2, 3, and 4 types.
5
5
It is a lot like libcap, except it has easy to understand facilities for writing (injecting packets) to an interface.
6
6
7
7
ZSocket doesn't contain or wrap any C/C++, and it is lock free and thread safe.
8
8
9
9
The following program prints out all know layer types to ZSocket on a given interface:
10
+
10
11
``` go
11
12
package main
12
13
13
14
import (
14
- " fmt"
15
+ " fmt"
15
16
16
- " github.com/newtools/zsocket"
17
- " github.com/newtools/zsocket/nettypes"
17
+ " github.com/newtools/zsocket"
18
+ " github.com/newtools/zsocket/nettypes"
18
19
)
19
20
20
21
func main () {
21
- // args: interfaceIndex, options, maxFrameSize, and maxTotalFrames
22
-
23
- // inerfaceIndex: the index of the net device you want to open a raw socket to
24
- // options: RX and TX, or just one or the other?
25
- // maxFrameSize: must be a power of 2, bigger than zsocket.MINIMUM_FRAME_SIZE,
26
- // and smaller than maximum frame size
27
- // maxTotalFrames: must be at least 16, and be a multiple of 8.
28
- zs , err := zsocket.NewZSocket (14 , zsocket.ENABLE_RX , 2048 , 64 , nettypes.All )
29
- // the above will result in a ring buffer of 64 frames at
30
- // (2048 - zsocket.PacketOffset()) *writeable* bytes each
31
- // for a total of 2048*64 bytes of *unswappable* system memory consumed.
32
- if err != nil {
33
- panic (err)
34
- }
35
- zs.Listen (func (f *nettypes.Frame , frameLen, capturedLen uint16 ) {
36
- fmt.Printf (f.String (capturedLen, 0 ))
37
- })
22
+ // args: interfaceIndex, options, maxFrameSize, and maxTotalFrames
23
+
24
+ // inerfaceIndex: the index of the net device you want to open a raw socket to
25
+ // options: RX and TX, or just one or the other?
26
+ // maxFrameSize: must be a power of 2, bigger than zsocket.MINIMUM_FRAME_SIZE,
27
+ // and smaller than maximum frame size
28
+ // maxTotalFrames: must be at least 16, and be a multiple of 8.
29
+ zs , err := zsocket.NewZSocket (14 , zsocket.ENABLE_RX , 2048 , 64 , nettypes.All )
30
+ // the above will result in a ring buffer of 64 frames at
31
+ // (2048 - zsocket.PacketOffset()) *writeable* bytes each
32
+ // for a total of 2048*64 bytes of *unswappable* system memory consumed.
33
+ if err != nil {
34
+ panic (err)
35
+ }
36
+ zs.Listen (func (f *nettypes.Frame , frameLen, capturedLen uint16 ) {
37
+ fmt.Printf (f.String (capturedLen, 0 ))
38
+ })
38
39
}
39
40
```
40
41
@@ -43,4 +44,4 @@ func main() {
43
44
2 . Learn how to set up a docker container with a custom veth-pair in the utils folder (useful for setting up
44
45
complex virtual networking scenarios)
45
46
46
- 3 . Play around with FakeInterface to (and its examples folder) to play around with networking protocols.
47
+ 3 . Play around with FakeInterface to (and its examples folder) to play around with networking protocols.
0 commit comments