github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/dscp/dial.go (about) 1 // Copyright (c) 2017 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 // Package dscp provides helper functions to apply DSCP / ECN / CoS flags to sockets. 6 package dscp 7 8 import ( 9 "net" 10 "syscall" 11 "time" 12 ) 13 14 // DialTCPWithTOS is similar to net.DialTCP but with the socket configured 15 // to the use the given ToS (Type of Service), to specify DSCP / ECN / class 16 // of service flags to use for incoming connections. 17 func DialTCPWithTOS(laddr, raddr *net.TCPAddr, tos byte) (*net.TCPConn, error) { 18 d := net.Dialer{ 19 LocalAddr: laddr, 20 Control: func(network, address string, c syscall.RawConn) error { 21 return SetTOS(network, c, tos) 22 }, 23 } 24 conn, err := d.Dial("tcp", raddr.String()) 25 if err != nil { 26 return nil, err 27 } 28 return conn.(*net.TCPConn), err 29 } 30 31 // DialTimeoutWithTOS is similar to net.DialTimeout but with the socket configured 32 // to the use the given ToS (Type of Service), to specify DSCP / ECN / class 33 // of service flags to use for incoming connections. 34 func DialTimeoutWithTOS(network, address string, timeout time.Duration, tos byte) (net.Conn, 35 error) { 36 d := net.Dialer{ 37 Timeout: timeout, 38 Control: func(network, address string, c syscall.RawConn) error { 39 return SetTOS(network, c, tos) 40 }, 41 } 42 conn, err := d.Dial(network, address) 43 if err != nil { 44 return nil, err 45 } 46 return conn, err 47 } 48 49 // DialTCPTimeoutWithTOS is same as DialTimeoutWithTOS except for enforcing "tcp" and 50 // providing an option to specify local address (source) 51 func DialTCPTimeoutWithTOS(laddr, raddr *net.TCPAddr, tos byte, timeout time.Duration) (net.Conn, 52 error) { 53 d := net.Dialer{ 54 Timeout: timeout, 55 LocalAddr: laddr, 56 Control: func(network, address string, c syscall.RawConn) error { 57 return SetTOS(network, c, tos) 58 }, 59 } 60 conn, err := d.Dial("tcp", raddr.String()) 61 if err != nil { 62 return nil, err 63 } 64 65 return conn, err 66 }