Skip to content

Commit

Permalink
Allow for QOS/DSCP to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
psaab committed Jan 17, 2024
1 parent d89d306 commit 270b7ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
noDownload = kingpin.Flag("no-download", "Disable download test.").Bool()
noUpload = kingpin.Flag("no-upload", "Disable upload test.").Bool()
pingMode = kingpin.Flag("ping-mode", "Select a method for Ping. (support icmp/tcp/http)").Default("http").String()
dscp = kingpin.Flag("dscp", "type of service field in IP header").Short('Q').Int()
debug = kingpin.Flag("debug", "Enable debug mode.").Short('d').Bool()
)

Expand All @@ -52,6 +53,7 @@ func main() {
Keyword: *search,
NoDownload: *noDownload,
NoUpload: *noUpload,
Dscp: *dscp,
}))
speedtestClient.SetNThread(*thread)

Expand Down
20 changes: 20 additions & 0 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"runtime"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -52,6 +53,7 @@ type UserConfig struct {

NoDownload bool
NoUpload bool
Dscp int
}

func parseAddr(addr string) (string, string) {
Expand Down Expand Up @@ -119,13 +121,31 @@ func (s *Speedtest) NewUserConfig(uc *UserConfig) {
}
}

setDscp := func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
level := syscall.IPPROTO_IP
optname := syscall.IP_TOS
if network != "tcp4" {
level = syscall.IPPROTO_IPV6
optname = syscall.IPV6_TCLASS
}
err := syscall.SetsockoptInt(int(fd), level, optname, uc.Dscp)
if err != nil {
dbg.Printf("syscall.SetsockoptInt: %s", err)
return
}
})
}

s.tcpDialer = &net.Dialer{
Control: setDscp,
LocalAddr: tcpSource,
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}

s.ipDialer = &net.Dialer{
Control: setDscp,
LocalAddr: icmpSource,
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
Expand Down

0 comments on commit 270b7ea

Please sign in to comment.