2019-11-09 15:41:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
2019-11-09 17:10:53 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2019-11-09 15:41:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var addresses = []string{
|
|
|
|
"8.8.8.8:53", // will succeed
|
|
|
|
//"8.8.8.8:88", // will fail
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeout time.Duration = 2000 * time.Millisecond
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
func parseSockAddr(addr string) (unix.Sockaddr, error) {
|
2019-11-09 15:41:15 +00:00
|
|
|
tAddr, err := net.ResolveTCPAddr("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var addr4 [4]byte
|
|
|
|
if tAddr.IP != nil {
|
|
|
|
copy(addr4[:], tAddr.IP.To4()) // copy last 4 bytes of slice to array
|
|
|
|
}
|
2019-11-09 17:10:53 +00:00
|
|
|
return &unix.SockaddrInet4{Port: tAddr.Port, Addr: addr4}, nil
|
2019-11-09 15:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Ping(address string, timeout time.Duration) error {
|
2019-11-09 17:10:53 +00:00
|
|
|
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("socket", err)
|
|
|
|
}
|
2019-11-09 17:10:53 +00:00
|
|
|
unix.CloseOnExec(fd)
|
2019-11-09 15:41:15 +00:00
|
|
|
log.Println("Socket FD:", fd)
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
err = unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_QUICKACK, 0)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("setsockopt", err)
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
var zeroLinger = unix.Linger{Onoff: 1, Linger: 0}
|
|
|
|
err = unix.SetsockoptLinger(fd, unix.SOL_SOCKET, unix.SO_LINGER, &zeroLinger)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("setsockoptlinger", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rAddr, err := parseSockAddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to parse address")
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
err = unix.Connect(fd, rAddr)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("connect", err)
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
epfd, err := unix.EpollCreate1(unix.EPOLL_CLOEXEC)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("epoll_create1", err)
|
|
|
|
}
|
|
|
|
log.Println("Epoll FD:", epfd)
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
var event unix.EpollEvent
|
2019-11-10 10:12:34 +00:00
|
|
|
event.Events = (unix.EPOLLOUT | unix.EPOLLIN | unix.EPOLLET)
|
2019-11-09 15:41:15 +00:00
|
|
|
event.Fd = int32(fd)
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
err = unix.EpollCtl(epfd, unix.EPOLL_CTL_ADD, fd, &event)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("epoll_ctl", err)
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
var epollEvents [1]unix.EpollEvent
|
2019-11-09 15:41:15 +00:00
|
|
|
var nEvents int
|
2019-11-09 17:10:53 +00:00
|
|
|
nEvents, err = unix.EpollWait(epfd, epollEvents[:], int(timeout.Milliseconds()))
|
2019-11-09 15:41:15 +00:00
|
|
|
|
2019-11-11 14:28:29 +00:00
|
|
|
log.Println("EpollWait err:", err)
|
2019-11-10 17:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("epoll_wait", err)
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:41:15 +00:00
|
|
|
log.Println("EpollWait #:", nEvents)
|
|
|
|
if nEvents == 0 {
|
|
|
|
return errors.New("no events found")
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:10:53 +00:00
|
|
|
errCode, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_ERROR)
|
2019-11-09 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return os.NewSyscallError("getsockoptint", err)
|
|
|
|
}
|
|
|
|
log.Println("SO_ERROR:", errCode)
|
|
|
|
|
|
|
|
log.Println("EpollEvent:", epollEvents[0])
|
2019-11-09 17:10:53 +00:00
|
|
|
if epollEvents[0].Events&unix.EPOLLOUT != 0 {
|
2019-11-09 15:41:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("UNKNOWN ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetPrefix("TCP_PING:")
|
|
|
|
|
|
|
|
for i := 0; i < len(addresses); i++ {
|
|
|
|
log.Println("PINGING:", addresses[i])
|
|
|
|
err := Ping(addresses[i], timeout)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("FAILURE:", err)
|
|
|
|
} else {
|
|
|
|
log.Println("SUCCESS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|