4.2 KiB
4.2 KiB
Basic Net types for Golang
Table of Contents
This "submodule/subpackage" (such a thing technically doesn’t exist in Golang) is a collection of data taken directly from IANA’s database that contains populated ports [1] and protocols[2].
You probably don’t want to use anything in this parent directory (r00t2.io/sysutils/net
), as it’s only used to generate the data from the IANA’s registries.
1. Ports
You can specify your own port via several methods.
1.1. The long way
package main
import (
`fmt`
`r00t2.io/sysutils/net/ports`
`r00t2.io/sysutils/net/protos`
)
func main() {
var p *ports.IPPort
p = &ports.IPPort{
Number: 22,
Protocol: &protos.IPProto{
Name: "TCP",
Number: 6,
Reference: "[RFC793]",
Reserved: false,
Description: "Transmission Control",
IP6ExtensionHeader: false,
},
ServiceName: "SSH",
Description: "Secure Shell",
Reserved: false,
}
fmt.Printf(
"Port:\n" +
"\tNumber: %v\n" +
"\tProto: %v\n" +
"\tService: %v\n" +
"\tDesc: %v\n" +
"\tReserved?: %v\n",
p.Number, p.Proto.Name, p.ServiceName, p.Description, p.Reserved)
}
1.2. The Shorter Way
package main
import (
`fmt`
`log`
`r00t2.io/sysutils/net/ports`
`r00t2.io/sysutils/net/protos`
)
func main() {
var proto *protos.IPProto
var port *ports.IPPort
var err error
proto = &protos.IPProto{Name: "TCP"}
port = &ports.IPPort{Number:22, Protocol: &proto}
// Populate recurses down into Protocol as well.
if err = port.Populate(); err != nil {
log.Panic(err)
}
fmt.Printf(
"Port:\n" +
"\tNumber: %v\n" +
"\tProto: %v\n" +
"\tService: %v\n" +
"\tDesc: %v\n" +
"\tReserved?: %v\n",
port.Number, port.Proto.Name, port.ServiceName, port.Description, port.Reserved)
}
1.3. The "Gotta Go Fast" Way
package main
import (
`fmt`
`log`
`r00t2.io/sysutils/net/ports`
)
func main() {
var err error
var port *ports.IPPort
// The default protocol is "TCP".
// If a UDP service is found but no TCP for the given port number, UDP is used.
if err = port.FetchNum(22); err != nil {
log.Panic(err)
}
// If you need to specify a protocol (by name) - e.g. it isn't TCP, use FetchNumProto.
port.FetchNumProto(22, "TCP")
fmt.Printf("%#v\n", port)
}
2. Protocols
You can specify your own protocol via several methods.
2.1. The long way
package main
import (
`fmt`
`r00t2.io/sysutils/net/protos`
)
func main() {
var p *protos.IPProto
p = &protos.IPProto{
Name: "TCP",
Number: 6,
Reference: "[RFC793]",
Description: "Transmission Control",
Reserved: false,
IP6ExtensionHeader: false,
}
fmt.Printf(
"Protocol:\n" +
"\tName: %v\n" +
"\tNumber: %v\n" +
"\tReference: %v\n" +
"\tReserved?: %v\n" +
"\tIPv6 Extension Header?: %v\n",
p.Name, p.Number, p.Reference, p.Reserved, p.IP6ExtensionHeader)
}
2.2. The Shorter Way
package main
import (
`fmt`
`log`
`r00t2.io/sysutils/net/protos`
)
func main() {
var proto *protos.IPProto
var err error
proto = &protos.IPProto{Name: "TCP"}
if err = proto.Populate(); err != nil {
log.Panic(err)
}
fmt.Printf(
"Protocol:\n" +
"\tName: %v\n" +
"\tNumber: %v\n" +
"\tReference: %v\n" +
"\tReserved?: %v\n" +
"\tIPv6 Extension Header?: %v\n",
p.Name, p.Number, p.Reference, p.Reserved, p.IP6ExtensionHeader)
}
2.3. The "Gotta Go Fast" Way
package main
import (
`fmt`
`log`
`r00t2.io/sysutils/net/protos`
)
func main() {
var err error
var proto *protos.IPProto
if err = proto.FetchName("TCP"); err != nil {
log.Panic(err)
}
if err = port.FetchNum(6); err != nil {
log.Panic(err)
}
fmt.Printf("%#v\n", proto)
}