go_sysutils/net/README.adoc

239 lines
4.3 KiB
Plaintext
Raw Normal View History

= Basic Net types for Golang
brent saner <bts@r00t2.io>
Last updated {localdatetime}
:doctype: book
:data-uri:
:imagesdir: images
:sectlinks:
:sectnums:
:sectnumlevels: 7
:toc: preamble
:toc2: left
:idprefix:
:toclevels: 7
//:source-highlighter: rouge
2021-02-24 00:49:50 -05:00
WARNING: This part of `sysutils` is not finished. Work will resume later.
This "submodule/subpackage" (such a thing technically doesn't exist in Golang) is a collection of data taken directly from IANA's https://www.iana.org/protocols[database^] that contains populated ports
footnote:[https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml[Service Name and Transport Protocol Port Number Registry^]] and
protocolsfootnote:[https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml[Protocol Numbers^]].
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.
== Ports
You can specify your own port via several methods.
=== The long way
[source,go]
----
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)
}
----
=== The Shorter Way
[source,go]
----
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)
}
----
=== The "Gotta Go Fast" Way
[source,go]
----
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)
}
----
== Protocols
You can specify your own protocol via several methods.
=== The long way
[source,go]
----
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)
}
----
=== The Shorter Way
[source,go]
----
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)
}
----
=== The "Gotta Go Fast" Way
[source,go]
----
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)
}
----