环境是在windows7下的LiteIDE集成环境,代码如下:
```go
package main
import (
"bytes"
"encoding/binary"
"fmt"
"net"
)
type ICMP struct {
Type uint8
Code uint8
Checksum uint16
Identifier uint16
SequenceNum uint16
}
func CheckSum(data []byte) uint16 {
var (
sum uint32
length int = len(data)
index int
)
for length > 1 {
sum += uint32(data[index])<<8 + uint32(data[index+1])
index += 2
length -= 2
}
if length > 0 {
sum += uint32(data[index])
}
sum += (sum >> 16)
return uint16(^sum)
}
func main() {
var (
icmp ICMP
laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.11")}
raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.1")}
)
//如果你要使用网络层的其他协议还可以设置成 ip:ospf、ip:arp 等
conn, err := net.DialIP("ip4:icmp", &laddr, &raddr)
if err != nil {
fmt.Println(err.Error())
return
}
defer conn.Close()
//开始填充数据包
icmp.Type = 8 //8->echo message 0->reply message
icmp.Code = 0
icmp.Checksum = 0
icmp.Identifier = 0
icmp.SequenceNum = 0
var (
buffer bytes.Buffer
)
//先在buffer中写入icmp数据报求去校验和
binary.Write(&buffer, binary.BigEndian, icmp)
icmp.Checksum = CheckSum(buffer.Bytes())
//然后清空buffer并把求完校验和的icmp数据报写入其中准备发送
buffer.Reset()
binary.Write(&buffer, binary.BigEndian, icmp)
if _, err := conn.Write(buffer.Bytes()); err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("send icmp packet success!")
}
```
运行提示:
socket: An attempt was made to access a socket in a way forbidden by its access permissions.
求指点调通~
因为明显英文的提示就是说:禁止创建一个socket,我知道在linux下加sudo权限就好了,至于windows,我不用,所以也不懂,帮不到你,不好意思
#7
更多评论
把conn, err := net.DialIP("ip4:icmp", &laddr, &raddr) 换成
conn, err := net.Dial("ip4:icmp", address) 试试
#1
换掉以后,并注解掉//laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.11")}
// raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.1")}两行,仍显示:
socket: An attempt was made to access a socket in a way forbidden by its access permissions.
#2