Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "log" "acln.ro/mem" ) /* IPv4Header is the Internet Protocol version 4 header. RFC 791 illustrates the header as: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ var ipv4Header = mem.Layout{ Fields: []mem.Field{ { Name: "version", Bits: 4, }, { Name: "ihl", Bits: 4, }, // 0 1 2 3 4 5 6 7 // +-----+-----+-----+-----+-----+-----+-----+-----+ // | | | | | | | // | PRECEDENCE | D | T | R | 0 | 0 | // | | | | | | | // +-----+-----+-----+-----+-----+-----+-----+-----+ { Name: "tos", Desc: "type of service", Bits: 8, Layout: mem.Layout{ Fields: []mem.Field{ { Name: "precedence", Bits: 3, }, { Name: "delay", Bits: 1, }, { Name: "throughput", Bits: 1, }, { Name: "reliability", Bits: 1, }, { Name: mem.Reserved, Bits: 2, }, }, }, }, { Name: "tot_len", Desc: "total length of the datagram, in octets", Bits: 16, }, { Name: "id", Bits: 16, }, { Name: "frag_off", Bits: 16, Layout: mem.Layout{ Fields: []mem.Field{ { Name: "flags", Bits: 3, }, { Name: "offset", Bits: 13, }, }, }, }, { Name: "ttl", Desc: "time to live", Bits: 8, }, { Name: "protocol", Desc: "protocol used in data portion", Bits: 8, }, { Name: "check", Desc: "header checksum", Bits: 16, }, { Name: "saddr", Desc: "source address", Bits: 32, }, { Name: "daddr", Desc: "destination address", Bits: 32, }, }, } func main() { if err := ipv4Header.Init(); err != nil { log.Fatal(err) } fmt.Println(ipv4Header.Offsetof["protocol"]) }
Output: 72
Index ¶
Examples ¶
Constants ¶
View Source
const Reserved = "__reserved"
Reserved is a special field name to use for reserved or unused fields.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Field ¶
type Field struct { Name string // name of the field; required Desc string // description; optional Bits int // width of the field, in bits; required Layout Layout // layout of the field itself; optional }
Field is a field in a memory layout.
type Layout ¶
Layout is a memory layout, composed of multiple fields. All field names and names in nested layouts must be unique for a given Layout.
Callers should set Fields to a collection of fields, then call Init, which, if successful, causes the Offsetof map to be populated.
Click to show internal directories.
Click to hide internal directories.