Skip to content

Commit e9cdc4d

Browse files
committed
🐛 use standard icmp package
1 parent b1d29fb commit e9cdc4d

7 files changed

Lines changed: 92 additions & 44 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Binaries for programs and plugins
2-
lmap
2+
/lmap
33
*.exe
44
*.exe~
55
*.dll

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module github.com/LinuxHub-Group/lmap
22

33
go 1.16
4+
5+
require (
6+
github.com/ysmood/got v0.30.0
7+
golang.org/x/net v0.0.0-20220607020251-c690dde0001d
8+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/ysmood/got v0.30.0 h1:n6KVknQ2gjU2FsEVvMrpEw20uoqkhRDvAoNK69bOV/U=
2+
github.com/ysmood/got v0.30.0/go.mod h1:pE1l4LOwOBhQg6A/8IAatkGp7uZjnalzrZolnlhhMgY=
3+
golang.org/x/net v0.0.0-20220607020251-c690dde0001d h1:4SFsTMi4UahlKoloni7L4eYzhFRifURQLw+yv0QDCx8=
4+
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
5+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
7+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
9+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
10+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

pkg/lmap/check_ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func CheckIP(subnet string, isVerbose bool) {
3232
t := time.Now()
3333
hosts, _ := GetAllIPsFromCIDR(subnet)
3434
for index := range hosts {
35-
time.Sleep(500)
35+
//time.Sleep(500)
3636
checkerGroup.Add(1)
3737

3838
go func(index int) {

pkg/lmap/common.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ package lmap
2020

2121
import "net"
2222

23-
type ICMP struct {
24-
Type uint8
25-
Code uint8
26-
Checksum uint16
27-
Identifier uint16
28-
SequenceNum uint16
29-
}
30-
3123
type HostInfo struct {
3224
host net.IP
3325
isUsed bool

pkg/lmap/ping.go

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,73 @@
1919
package lmap
2020

2121
import (
22-
"bytes"
2322
"encoding/binary"
23+
"golang.org/x/net/icmp"
2424
"log"
2525
"net"
2626
"time"
2727
)
2828

2929
func Ping(ip net.IP) bool {
30-
var icmp ICMP
31-
//开始填充数据包
32-
icmp.Type = 8 //8->echo message 0->reply message
30+
recvBuf := make([]byte, 8)
3331

34-
recvBuf := make([]byte, 32)
35-
var buffer bytes.Buffer
32+
// Start listening for icmp replies
33+
conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
3634

37-
//先在buffer中写入icmp数据报求去校验和
38-
_ = binary.Write(&buffer, binary.BigEndian, icmp)
39-
icmp.Checksum = checkSum(buffer.Bytes())
40-
//然后清空buffer并把求完校验和的icmp数据报写入其中准备发送
41-
buffer.Reset()
42-
_ = binary.Write(&buffer, binary.BigEndian, icmp)
43-
44-
conn, err := net.DialTimeout("ip4:icmp", ip.String(), 1*time.Second)
4535
if err != nil {
36+
log.Println("dial error:", err)
4637
return false
4738
}
48-
_, err = conn.Write(buffer.Bytes())
39+
defer conn.Close()
40+
41+
sendBytes := []byte{8, 0, 247, 255, 0, 0, 0, 0}
42+
//expectCheckSum := int(checkSum([]byte{0, 0, 0, 0, 0, 0, 0, 0}))
43+
expectCheckSum := 65535
44+
45+
_, err = conn.WriteTo(sendBytes, &net.IPAddr{
46+
IP: ip,
47+
})
4948
if err != nil {
50-
log.Println("conn.Write error:", err)
5149
return false
5250
}
5351
_ = conn.SetReadDeadline(time.Now().Add(time.Second * 2))
54-
num, err := conn.Read(recvBuf)
52+
53+
_, _, err = conn.ReadFrom(recvBuf)
5554
if err != nil {
5655
return false
5756
}
5857

59-
_ = conn.SetReadDeadline(time.Time{})
58+
recvType := recvBuf[0]
59+
recvCheckSum := int(binary.BigEndian.Uint16(recvBuf[2:4]))
6060

61-
return string(recvBuf[0:num]) != ""
62-
}
61+
_ = conn.SetReadDeadline(time.Time{})
6362

64-
func checkSum(data []byte) uint16 {
65-
var (
66-
sum uint32
67-
length = len(data)
68-
index int
69-
)
70-
for length > 1 {
71-
sum += uint32(data[index])<<8 + uint32(data[index+1])
72-
index += 2
73-
length -= 2
63+
if recvType != 0 {
64+
return false
7465
}
75-
if length > 0 {
76-
sum += uint32(data[index])
66+
67+
if recvCheckSum != expectCheckSum {
68+
return false
7769
}
78-
sum += sum >> 16
7970

80-
return uint16(^sum)
71+
return true
8172
}
73+
74+
//func checkSum(data []byte) uint16 {
75+
// var (
76+
// sum uint32
77+
// length = len(data)
78+
// index int
79+
// )
80+
// for length > 1 {
81+
// sum += uint32(data[index])<<8 + uint32(data[index+1])
82+
// index += 2
83+
// length -= 2
84+
// }
85+
// if length > 0 {
86+
// sum += uint32(data[index])
87+
// }
88+
// sum += sum >> 16
89+
//
90+
// return uint16(^sum)
91+
//}

pkg/lmap/ping_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* lmap (LinuxHub's Nmap) is the nmap next generation pro plus max.
3+
* Copyright (C) <2022> <LinuxHub-Group>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package lmap
20+
21+
import (
22+
"github.com/ysmood/got"
23+
"net"
24+
"testing"
25+
)
26+
27+
func TestPing(t *testing.T) {
28+
ip := net.ParseIP("192.168.0.106")
29+
res := Ping(ip)
30+
got.T(t).True(res)
31+
}

0 commit comments

Comments
 (0)