NetStrikeForceOP•

I'm building small network tools as an exercise to learn Golang.

I'm not even a developer, so I'll be more than happy to hear all criticism, suggestions and feedback in general. In this case I took an existing tool, forked the repo and expanded its functionality.

5
xiegeo•

Sure you are not? How long have you been learning?

time.Sleep(1e9) use time.Second here, you can also do time.Second - responseTime as that is closer to what most ping tools does.

if err != nil {
    fmt.Println(fmt.Sprintf("%s port %d closed.", host, port))
}

You can use fmt.Printf("... \n",...) to add a new line to Printf. Also print the error too, as it is not always port closed.

2
NetStrikeForceOP•

I learned some coding when I was a child: Qbasic for a year, then COBOL, then Clipper with Dbase... it was the 90s and I was like 12. Outside that I've done the odd script in Bash, Python and PowerShell. I only held once a programming job for a summer. I was 18 and the job consisted on migrating an existing tool from something called MUMPS to Visual Basic. I just did a tiny part as we were a team and decided I didn't want to do that as a career.

So yeah, I know some outdated concepts and I think I can read code and learn from others, but I'm definitely not a Dev hehe.

I'm taking your suggestions on board and will work on them later today. A thousand hanks for taking your time to have a look!

1
xiegeo•

That sucks having your first view into it as a career is a migration project. I can see how that would just kill the joy in it.

3
NetStrikeForceOP•

time.Sleep(1e9) use time.Second here, you can also do time.Second - responseTime as that is closer to what most ping tools does.

Do you mean this?

time.Sleep(time.Second - responseTime)

I understand that would keep the overall running time more predictable (100 repetitions = 100 seconds running time). I think it's a good idea (thanks!), however it poses a new question for me:

By default the timeout is 5 seconds, so I should definitely change this to 1 second; however I wonder if I should keep the option to change it or not - does it really make sense to go higher than 1s these days? For now I'll leave the option there.

Also print the error too, as it is not always port closed.

Yep, good point. I think I'll test with different errors so I can react different to them. E.g. I don't think it makes sense to keep sending requests if DNS resolution fails.

Thanks again!

1
xiegeo•

The default timeout have something to-do with "tcp retransmission", basically it resents the syn packet at increasing intervals until act packet is received or times out. TCP tries hard at hiding packet lose.

Ignoring retransmission, pings in general should not take more than a second, unless it is a satellite connection. However there could be networks with big buffers and low bandwidth, I have seen ping as high as 30 seconds on a throttled cellular data plan.

As for DNS resolution, you can do that separately using net.ResolveIPAddr so that you can keep both the DNS error handling logic and the time it takes separate from your timing loop.

2
NetStrikeForceOP•

Awesome advice, thanks! I'm very familiar with TCP retransmission, but for some reason I wasn't even thinking about it. If TCP retransmits after 200ms this might hide a packet loss issue and show it as latency. I don't know either how it made any sense to have a 5 second timeout for a TCP 3 way handshake... It could make sense for ICMP as there are no retransmissions nor sessions.

I'll look further into the DNS part as I'd like to play with it too.

Thank you once more!

1