Skip to content

Instantly share code, notes, and snippets.

@martinomburajr
Last active May 20, 2019 11:06
Show Gist options
  • Select an option

  • Save martinomburajr/716e07a567d7dc1c7ce3c30ae7acc92a to your computer and use it in GitHub Desktop.

Select an option

Save martinomburajr/716e07a567d7dc1c7ce3c30ae7acc92a to your computer and use it in GitHub Desktop.
Creating a socket in Golang
// This unimplemnted function is found in the syscall/syscall_linx_386.go file and is referenced by the socket function below.
// It is implemented in the syscall/asm_linux_s390x.s file
func rawsocketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err Errno)
// The bind method mirrors the Unix API for bind calls. A call to rawsocketcall is made (see methods above)
func bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
_, e := socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
if e != 0 {
err = e
}
return
}
// The socket method mirrors the Unix API for Socket calls. A call to rawsocketcall is made (see method above and below)
func socket(domain int, typ int, proto int) (fd int, err error) {
fd, e := rawsocketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0)
if e != 0 {
err = e
}
return
}
// func rawsocketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err int)
// Kernel interface gets call sub-number and pointer to a0.
// This method is found in the syscall/asm_linux_s390x.s file
TEXT ·rawsocketcall(SB),NOSPLIT,$0-72
MOVD $SYS_SOCKETCALL, R1 // syscall entry
MOVD call+0(FP), R2 // socket call number
MOVD $a0+8(FP), R3 // pointer to call arguments
MOVD $0, R4
MOVD $0, R5
MOVD $0, R6
MOVD $0, R7
SYSCALL
MOVD $0xfffffffffffff001, R8
CMPUBLT R2, R8, oksock1
MOVD $-1, n+56(FP)
NEG R2, R2
MOVD R2, err+64(FP)
RET
oksock1:
MOVD R2, n+56(FP)
MOVD $0, err+64(FP)
RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment