mem

package module
v0.0.0-...-0e4b2b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2019 License: ISC Imports: 1 Imported by: 0

README

mem

import "acln.ro/mem"

GoDoc

A Go package for describing memory layouts.

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

type Layout struct {
	Fields   []Field
	Offsetof map[string]int
}

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.

func (*Layout) Init

func (l *Layout) Init() error

Init validates the Layout and populates the Offsetof map. Once Init has been called successfully on a Layout, subsequent calls have no effect.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL