Skip to content

x/net/ipv4: ipv4.Conn.SetTOS fails on "tcp" net.TCPConn #13612

Closed
@sbowman

Description

@sbowman

If you listen for a general "tcp" connection instead of a "tcp4" connection, ipv4.Conn.SetTOS fails on IPv4 connections (but works with ipv6.Conn.SetTrafficClass on IPv6 connections).

From the https://godoc.org/golang.org/x/net/ipv4 example, if you change net.Listen("tcp4", "0.0.0.0:1024") to net.Listen("tcp", ":1024"):

ln, err := net.Listen("tcp", ":1024")
    if err != nil {
        // error handling
    }
    defer ln.Close()
    for {
        c, err := ln.Accept()
        if err != nil {
            // error handling
        }
        go func(c net.Conn) {
            defer c.Close()

            if err := ipv4.NewConn(c).SetTOS(0x28); err != nil {
                fmt.Println("Error: ", err.Error())
            }
        }(c)
    }

and send an IPv4 request (e.g. curl "127.0.0.1:1024"), the IPv4 connections will not have the TOS set, and SetTOS returns "setsockopt: invalid argument".

IPv6 connections in this same scenario (switch to ipv6.NewConn(c).SetTrafficClass) will work.

In my app, I'm testing for the type of connection by checking TCPConn.RemoteAddr() for IPv6 vs. IPv4 and calling the appropriate NewConn wrapper. IPv6 connections have their TrafficClass set properly. IPv4 connections generate the above error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions