Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: Dial("ip4:icmp", ...) returns operation not permitted error because it uses a raw socket #19815

Closed
AlpherJang opened this issue Apr 1, 2017 · 3 comments

Comments

Projects
None yet
5 participants
@AlpherJang
Copy link

commented Apr 1, 2017

Hey, I almost started learning Golang, and then follow the book to try using socket programming. But when I use it
Ip4: icmp request remote host, the system returns to me such an error:
Fatal error: dial ip4: icmp 78.216.200.70: socket: operation not permitted
My system is os sierra

package main

import (
	"os"
	"fmt"
	"net"
	"bytes"
	"io"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Usage: ", os.Args[0], "host")
		os.Exit(1)
	}
	service := os.Args[1]

	conn, err := net.Dial("ip4:icmp", service)
	checkError(err)

	var msg [512]byte
	msg[0] = 8  // echo
	msg[1] = 0  // code 0
	msg[2] = 0  // checksum
	msg[3] = 0  // checksum
	msg[4] = 0  // identifier[0]
	msg[5] = 13 //identifier[1]
	msg[6] = 0  // sequence[0]
	msg[7] = 37 // sequence[1]

	len := 8

	check := checkSum(msg[0:len])

	msg[2] = byte(check >> 8)
	msg[3] = byte(check & 255)

	_, err = conn.Write(msg[0:len])
	checkError(err)

	_, err = conn.Read(msg[0:])

	fmt.Println("Got response")

	if msg[5] == 13 {
		fmt.Println("Identifier matches")
	}
	if msg[7] == 37 {
		fmt.Println("Sequence matches")
	}
	os.Exit(0)
}

func checkSum(msg []byte) uint16 {
	sum := 0

	for n := 1; n < len(msg)-1; n += 2 {
		sum += int(msg[n])*256 + int(msg[n+1])
	}
	sum = (sum >> 16) + (sum & 0xffff)
	sum += (sum >> 16)
	var answer uint16 = uint16(^sum)
	return answer
}

func readFully(conn net.Conn) ([]byte, error) {
	defer conn.Close()
	result := bytes.NewBuffer(nil)

	var buf [512]byte

	for {
		n, err := conn.Read(buf[0:])
		result.Write(buf[0:n])
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, err
		}
	}
	return result.Bytes(), nil
}

func checkError(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
	}
	os.Exit(1)
}

I hope that someone can why this situation is how to solve

@choleraehyq

This comment has been minimized.

Copy link
Contributor

commented Apr 1, 2017

Please use English bro.

@dongweigogo

This comment has been minimized.

Copy link

commented Apr 1, 2017

试试google翻译
try google translate

@AlpherJang AlpherJang closed this Apr 1, 2017

@llitfkitfk

This comment has been minimized.

Copy link

commented Jun 13, 2017

@mikioh mikioh changed the title golang报socket:operation not permitted net: Dial("ip4:icmp", ...) returns operation not permitted error because it uses a raw socket Jun 13, 2017

@golang golang locked and limited conversation to collaborators Jun 13, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.