@@ -15,18 +15,27 @@ const payloadSize int = 1400
1515
1616//ICMPtunnel simulator
1717type ICMPtunnel struct {
18+ c * icmp.PacketConn
1819}
1920
2021//NewICMPtunnel Creates new IMCP tunnel simulator
2122func NewICMPtunnel () * ICMPtunnel {
2223 return & ICMPtunnel {}
2324}
2425
25- func (ICMPtunnel ) Init () error {
26+ func (s * ICMPtunnel ) Init (bind net.IP ) error {
27+ c , err := icmp .ListenPacket ("ip4:icmp" , bind .String ())
28+ if err != nil {
29+ return err
30+ }
31+ s .c = c
2632 return nil
2733}
2834
29- func (ICMPtunnel ) Cleanup () {
35+ func (s * ICMPtunnel ) Cleanup () {
36+ if s .c != nil {
37+ s .c .Close ()
38+ }
3039}
3140
3241//Hosts returns host used for tunneling
@@ -35,15 +44,9 @@ func (ICMPtunnel) Hosts(scope string, size int) ([]string, error) {
3544}
3645
3746//Simulate IMCP tunneling for given dst
38- func (ICMPtunnel ) Simulate (ctx context.Context , bind net.IP , dst string ) error {
39- c , err := icmp .ListenPacket ("ip4:icmp" , bind .String ())
40- if err != nil {
41- return err
42- }
43- defer c .Close ()
44-
47+ func (s * ICMPtunnel ) Simulate (ctx context.Context , dst string ) error {
4548 deadline , _ := ctx .Deadline ()
46- c .SetDeadline (deadline )
49+ s . c .SetDeadline (deadline )
4750
4851 for i := 0 ; i < pingCount ; i ++ {
4952 r := make ([]byte , payloadSize )
@@ -61,12 +64,12 @@ func (ICMPtunnel) Simulate(ctx context.Context, bind net.IP, dst string) error {
6164 if err != nil {
6265 return err
6366 }
64- if _ , err := c .WriteTo (binmsg , & net.IPAddr {IP : net .ParseIP (dst )}); err != nil {
67+ if _ , err := s . c .WriteTo (binmsg , & net.IPAddr {IP : net .ParseIP (dst )}); err != nil {
6568 return err
6669 }
6770
6871 rb := make ([]byte , 1500 )
69- _ , _ , err = c .ReadFrom (rb )
72+ _ , _ , err = s . c .ReadFrom (rb )
7073 if err != nil {
7174 return err
7275 }
0 commit comments