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: IPConn recvmsg: not implemented on windows/amd64 #44039

Closed
lysShub opened this issue Feb 1, 2021 · 2 comments
Closed

net: IPConn recvmsg: not implemented on windows/amd64 #44039

lysShub opened this issue Feb 1, 2021 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows

Comments

@lysShub
Copy link

lysShub commented Feb 1, 2021

What version of Go are you using (go version)?

$ go version
go version go1.15.3 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

windows 10/amd64

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\LYS\AppData\Local\go-build
set GOENV=C:\Users\LYS\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\OneDrive\code\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\OneDrive\code\go
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\OneDrive\code\go\project\atest\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\LYS\AppData\Local\Temp\go-build157944350=/tmp/go-build -gno-record-gcc-switches

What did you do?

I want read/write raw IP packet,but net/ipv4 not support Windows system. So what can I do this on Windows system.
the code can run on Linux

package main

import (
	"fmt"
	"net"

	"golang.org/x/net/ipv4"
)

func main() {
	c, err := net.ListenPacket("ip4:icmp", "0.0.0.0") 
	if err != nil {
		fmt.Println(1,err)
		return
	}
	defer c.Close()
	ipconn, err := ipv4.NewRawConn(c)
	if err != nil {
		fmt.Println(2,err)
		return
	}
	for {
		buf := make([]byte, 1480)
		hdr, payload, controlMessage, err := ipconn.ReadFrom(buf)
		if err != nil {
			fmt.Println(3, err)
			return
		}
		if payload != nil {
			fmt.Println(hdr, controlMessage)
			fmt.Println(payload)
		}
	}
}

What did you expect to see?

catch some data

What did you see instead?

3 read ip 0.0.0.0: recvmsg: not implemented on windows/amd64

@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows labels Feb 1, 2021
@seankhliao seankhliao changed the title read ip 0.0.0.0: recvmsg: not implemented on windows/amd64 net: IPConn recvmsg: not implemented on windows/amd64 Feb 1, 2021
@seankhliao
Copy link
Member

related #7170

@lysShub
Copy link
Author

lysShub commented Feb 2, 2021

related #7170

thanks, I get it.

package main

import (
	"fmt"
	"unsafe"

	"golang.org/x/sys/windows"
)

func main() {

	var wsaData windows.WSAData
	err := windows.WSAStartup(2<<16+2, &wsaData)
	if err != nil {
		fmt.Println(err)
		return
	}

	sh, err := windows.Socket(windows.AF_INET, windows.SOCK_RAW, windows.IPPROTO_ICMP)
	if err != nil {
		fmt.Println(err)
		return
	}

	var rAddr windows.RawSockaddrInet4
	rAddr.Family = windows.AF_INET
	rAddr.Port = 27015
	rAddr.Addr = [4]byte{192, 168, 1, 1}
	q := (*windows.RawSockaddrAny)(unsafe.Pointer(&rAddr))
	sAddr, err := q.Sockaddr()
	if err != nil {
		fmt.Println(err)
		return
	}

	var aOptVal bool = true
	err = windows.Setsockopt(sh, windows.IPPROTO_IP, 14, (*byte)(unsafe.Pointer(&aOptVal)), int32(unsafe.Sizeof(aOptVal)))
	if err != nil {
		fmt.Println(err)
		return
	}

	// 发送
	var d []byte = make([]byte, 0)
	d = append(d, 8, 0, 247, 95, 0, 1, 0, 159)
	err = windows.Sendto(sh, d, 0, sAddr)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 关闭
	err = windows.Closesocket(sh)
	if err != nil {
		fmt.Println(err)
		return
	}
	err = windows.WSACleanup()
	if err != nil {
		fmt.Println(err)
		return
	}
}

@lysShub lysShub closed this as completed Feb 2, 2021
@golang golang locked and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

3 participants