|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "log" |
| 6 | + "net" |
| 7 | + "time" |
| 8 | + "unsafe" |
| 9 | + |
| 10 | + "golang.org/x/sys/unix" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + log.SetFlags(0) |
| 15 | + if err := run(); err != nil { |
| 16 | + log.Fatal(err) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func run() error { |
| 21 | + // |
| 22 | + // TCPリスナーの起動までを Syscall() で実装 |
| 23 | + // なお、利用する関数は SyscallNoError (エラーを返さないバージョン) を意図的に利用 |
| 24 | + // |
| 25 | + const ( |
| 26 | + zero = uintptr(0) // 不要な引数値を表す |
| 27 | + ) |
| 28 | + |
| 29 | + // socket(2) |
| 30 | + var ( |
| 31 | + domain = uintptr(unix.AF_INET) |
| 32 | + sockType = uintptr(unix.SOCK_STREAM) |
| 33 | + protocol = uintptr(0) |
| 34 | + sFd, _ = unix.SyscallNoError(unix.SYS_SOCKET, domain, sockType, protocol) |
| 35 | + ) |
| 36 | + defer func(fd uintptr) { |
| 37 | + _, _ = unix.SyscallNoError(unix.SYS_CLOSE, fd, zero, zero) |
| 38 | + }(sFd) |
| 39 | + |
| 40 | + // setsockopt(2) (SO_REUSEADDR) |
| 41 | + var ( |
| 42 | + level = uintptr(unix.SOL_SOCKET) |
| 43 | + optname = uintptr(unix.SO_REUSEADDR) |
| 44 | + optval = 1 |
| 45 | + optvalPtr = uintptr(unsafe.Pointer(&optval)) |
| 46 | + optlen = uintptr(unsafe.Sizeof(optval)) |
| 47 | + ) |
| 48 | + _, _, _ = unix.Syscall6(unix.SYS_SETSOCKOPT, sFd, level, optname, optvalPtr, optlen, zero) |
| 49 | + |
| 50 | + // ソケットアドレス生成 |
| 51 | + // アドレスを表現する構造体として unix.SockaddrInet4 と unix.RawSockaddrInet4 の2つがあるが |
| 52 | + // unix.Syscall()を利用して直接システムコールを呼び出す場合は、カーネルが期待するメモリレイアウトを |
| 53 | + // そのまま表現する unix.RawSockaddrInet4 を利用する。(要はCの構造体と同じ形の方を使う) |
| 54 | + // unix.RawSockaddrInet4の方は、ネットワークバイトオーダーで値を保持する。 |
| 55 | + var ( |
| 56 | + sAddr unix.RawSockaddrInet4 |
| 57 | + sAddrPtr uintptr |
| 58 | + sAddrLen uintptr |
| 59 | + ) |
| 60 | + sAddr = unix.RawSockaddrInet4{ |
| 61 | + Family: unix.AF_INET, |
| 62 | + Port: htons(8888), |
| 63 | + Addr: [4]byte{127, 0, 0, 1}, |
| 64 | + } |
| 65 | + sAddrPtr = uintptr(unsafe.Pointer(&sAddr)) |
| 66 | + sAddrLen = uintptr(unix.SizeofSockaddrInet4) |
| 67 | + |
| 68 | + // bind(2) |
| 69 | + _, _ = unix.SyscallNoError(unix.SYS_BIND, sFd, sAddrPtr, sAddrLen) |
| 70 | + |
| 71 | + // listen(2) |
| 72 | + _, _ = unix.SyscallNoError(unix.SYS_LISTEN, sFd, uintptr(5), zero) |
| 73 | + |
| 74 | + for { |
| 75 | + // accept(2) |
| 76 | + var ( |
| 77 | + cAddr unix.RawSockaddrInet4 |
| 78 | + cAddrPtr = uintptr(unsafe.Pointer(&cAddr)) |
| 79 | + cAddrLen uint32 = unix.SizeofSockaddrInet4 |
| 80 | + cAddrLenPtr = uintptr(unsafe.Pointer(&cAddrLen)) |
| 81 | + cFd, _ = unix.SyscallNoError(unix.SYS_ACCEPT, sFd, cAddrPtr, cAddrLenPtr) |
| 82 | + ) |
| 83 | + log.Printf("[accept] EP: %v:%d", net.IP(cAddr.Addr[:]), ntohs(cAddr.Port)) |
| 84 | + |
| 85 | + go func(fd uintptr) { |
| 86 | + time.Sleep(1 * time.Second) |
| 87 | + _, _ = unix.SyscallNoError(unix.SYS_CLOSE, fd, zero, zero) |
| 88 | + }(cFd) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// ホストバイトオーダーからネットワークバイトオーダーに変換(Host to Network Short) |
| 93 | +func htons(port uint16) uint16 { |
| 94 | + bytes := make([]byte, 2) |
| 95 | + binary.LittleEndian.PutUint16(bytes, port) |
| 96 | + return binary.BigEndian.Uint16(bytes) |
| 97 | +} |
| 98 | + |
| 99 | +// ネットワークバイトオーダーからホストバイトオーダーに変換(Network to Host Short) |
| 100 | +func ntohs(port uint16) uint16 { |
| 101 | + bytes := make([]byte, 2) |
| 102 | + binary.BigEndian.PutUint16(bytes, port) |
| 103 | + return binary.LittleEndian.Uint16(bytes) |
| 104 | +} |
0 commit comments