Repository navigation
Intro

During this training we'll build an incrementally sophisticated program to scan for open ports on our local computer. Our port scanner will leverage the standard library's net package's Dial function to attempt a connection over a given port number.
According to DALL-E, the above image is an example of port scanning. 🤷🏾♂️
Port scanning actually looks more like this:

Port scanning is a technique to examine a server or host for open ports. A port scanner sends a series of messages to different ports on a target device and analyzes the responses. Ports are endpoints in an operating system where network connections start and end. There are 65,535 ports in total, and each port can be open (accepting connections), closed (not accepting connections), or filtered (blocked by a firewall).
The series of back and forth between our device and the target (also our own device) is nicely abstracted for us through the standard library's net.Dial function which has to facilitate this process internally. That is the extent to which we will learn to do port scanning, nothing more, nothing less.
We'll use the following program as a starting point for our scanner:
e0-sequential/main.go
package main
import (
"fmt"
"log"
"net"
)
func main() {
for i := 5200; i <= 5500; i++ {
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", i))
if err != nil {
log.Printf("%d CLOSED (%s)\n", i, err)
continue
}
conn.Close()
log.Printf("%d OPEN\n", i)
}
}When we run this program, our output will look similar to the following:
...
2024/05/30 07:31:40 5428 CLOSED (dial tcp [::1]:5428: connect: connection refused)
2024/05/30 07:31:40 5429 CLOSED (dial tcp [::1]:5429: connect: connection refused)
2024/05/30 07:31:40 5430 CLOSED (dial tcp [::1]:5430: connect: connection refused)
2024/05/30 07:31:40 5431 CLOSED (dial tcp [::1]:5431: connect: connection refused)
2024/05/30 07:31:40 5432 OPEN
2024/05/30 07:31:40 5433 CLOSED (dial tcp [::1]:5433: connect: connection refused)
2024/05/30 07:31:40 5434 CLOSED (dial tcp [::1]:5434: connect: connection refused)
2024/05/30 07:31:40 5435 CLOSED (dial tcp [::1]:5435: connect: connection refused)
2024/05/30 07:31:40 5436 CLOSED (dial tcp [::1]:5436: connect: connection refused)
...What we're seeing above is an excerpt of the ports that we're connecting to and treating connection refused errors from net.Dial for those ports as an indication that they're closed. We do have one open port here and that's 5432 which incidentally happens to be the PostgreSQL server the instrutor has running locally.
So what makes this port scanner a good candidate for concurrency? There is no dependency between the checks we're performing. We do not need to wait on the response from one net.Dial call to the next. This classifies our problem as an "embarrasingly parallel" one, meaning we have the opportunity to spin up a goroutine for each one of the calls we're currently making sequentially.
Before we dive into making our port scanner more concurrent, it helps to visualize its execution in its current and sequential form. Luckily, the Go toolchain has us covered.
e0-sequential-pprof/main.go
package main
import (
"fmt"
"log"
"net"
"os"
"runtime/pprof"
)
func main() {
pf, err := os.Create("e0.goroutine.prof")
if err != nil {
log.Fatalln(err)
}
defer pf.Close()
defer pprof.Lookup("goroutine").WriteTo(pf, 0)
for i := 5300; i <= 5500; i++ {
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", i))
if err != nil {
log.Printf("%d CLOSED (%s)\n", i, err)
continue
}
conn.Close()
log.Printf("%d OPEN\n", i)
}
}The above program uses the runtime/pprof package to create a goroutine profile of the program. The file produced can then be fed into the pprof tool from the go toolchain like so:
$ go tool pprof -http=:6060 e0.goroutine.profWhen the browser launches, you should see a graph similar to the following:

- Boxes: Each box represents a function in the call stack. The percentage indicates the proportion of samples that were in that function during the profiling period.
- Arrows: Arrows between boxes indicate a call relationship between functions.
- Numbers on Arrows: These numbers represent the number of goroutine instances that moved from the calling function to the called function during the profiling period.
- At the time the sample is taken, the program has only 1 goroutine running which makes sense given we do not launch any during execution.
- The
net.Dialcalls end up playing such a small role in this sample that they don't appear. - The arrow numbering confirms that only 1 goroutine ever transitions from one function to the next.
Now that we have visualized what a single-goroutine application looks like overall, it's time to talk concurrency.