xdr

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2021 License: Apache-2.0, GPL-3.0-or-later Imports: 7 Imported by: 31

Documentation

Overview

Common runtime (boilerplate support routines) for output of the goxdr XDR compiler.

Index

Constants

View Source
const (
	TRUE  = true
	FALSE = false
)

RFC4506 defines bool as equivalent to an enum with values all-caps TRUE and FALSE. For convenience, we represent an XDR bool as a Go bool instead, and so define these constants for use in union cases. (XDR source files should not use lower-case true and false, as these will not work in other languages or with other XDR compilers.)

Variables

View Source
var XdrCatalog = make(map[uint64]func(uint32) XdrProc)

A catalog of procedures of different RPC programs and versions, mostly useful for debugging (e.g., pretty-printing a log of RPC messages). The key to the map is prog<<32|vers. The function returns a new instance of an appropriate XdrProc.

Functions

func XdrArrayOpaqueScan

func XdrArrayOpaqueScan(dest []byte, ss fmt.ScanState, _ rune) error

Helper function for scanning fix-size opaque arrays.

func XdrPanic

func XdrPanic(s string, args ...interface{})

Throws an XdrError.

func XdrSymChar

func XdrSymChar(r rune) bool

Returns true for A-Z, a-z, 0-9, and _. (Used by the generated code that implements Scanner for enums.)

func XdrToI32

func XdrToI32(i interface{}) int32

Unfortunately, you can't cast a bool to an int. Hence, in situations like a union discriminant where we don't know if a type is equivalent to bool or not, then, short of using reflection, this is how we have to convert to an integer. Note that this expects types int, [u]int32, enum, and bool, rather than pointers to these types.

func XdrToString

func XdrToString(t XdrType) string

Generic function that converts any compiled XDR type to a multi-line string. Useful for debugging.

Types

type XDR

type XDR interface {
	// A function that gets called for each value to be marshalled.
	// The val XdrType argument will be one of the following:
	//
	// * A pointer type implementing XdrNum32 for bool, int, unsigned
	//   int, float, the size of variable-length arrays except string
	//   and opaque, and enums.  In the case of enums, that instance
	//   will just be a pointer to the enum.  In the case of the other
	//   types, it will be a pointer to a defined type that implements
	//   the XdrNum32 interface (since plain go types cannot implement
	//   methods)--e.g., *XdrUint32 for unsigned int.  Variable array
	//   sizes are passed as *XdrSize, which enforces the bound.
	//
	// * A pointer type implementing XdrNum64 for hyper, unsigned
	//   hyper, and double.  These are user-defined versions of int64,
	//   uint64, and float64, called XdrInt64, XdrUint64, and
	//   XdrFloat64, respectively.
	//
	// * An instance of XdrBytes for strings and opaque.
	//   Specifically, strings are passed as XdrString, and
	//   variable-length opaque<> vectors are passed as XdrVecOpaque,
	//   both of which implement XdrVarBytes.
	//
	// * An XdrArrayOpaque containing a slice referencing a byte array
	//   for fixed-length opaque[].  XdrArrayOpaque implements
	//   XdrBytes, but not XdrVarBytes.
	//
	// * An instance of XdrAggregate for structs, unions, and
	//   pointers.  Note that structs and unions are just passed as
	//   pointers to the underlying structures (which all implement
	//   XdrAggregate).  Pointers are passed as an XdrPtr interface
	//   implemented by a defined pointer type (since plain pointers
	//   cannot have methods).
	//
	// Note that the Marshal method is responsible for recursing into
	// XdrAggregate instance by calling the XdrRecurse method.
	// Requiring the Marshal method to recurse manually allows it to
	// refrain from recursing in cases where it needs to special-case
	// the handling of specific types.
	Marshal(name string, val XdrType)

	// This method should just be fmt.Sprintf for XDRs that use name.
	// Those that don't use name can use a trivial method returning ""
	Sprintf(string, ...interface{}) string
}

The interface through which values are serialized, printed, etc.

type XdrAggregate

type XdrAggregate interface {
	XdrType
	XdrRecurse(XDR, string)
}

Any struct, union, pointer, or variable-length array type (except opaque<> and string<>) is passed to XDR.Marshal as a pointer implementing the XdrAggregate interface. It is the responsibility of the XDR.Marshal function to call the XdrRecurse method so as to recurse into the data structure. Placing reponsibility on XDR.Marshal for recursing allows a custom XDR to prune the serialization at particular types (e.g., for pretty-printing a partucular struct in a non-standard way).

type XdrArray

type XdrArray interface {
	XdrAggregate
	XdrArraySize() uint32
}

Any array type is passed as a pointer to a user-defined array supporting the XdrArray interface. This is a subtype of XdrAggregate, and so supports recursing to marshal each individual array member.

type XdrArrayOpaque

type XdrArrayOpaque interface {
	XdrBytes
	XdrArraySize() uint32
}

The interface of XdrTypes of opaque[n] for all n

type XdrBool

type XdrBool bool

The XdrType that bool gets turned into for marshaling.

func XDR_bool

func XDR_bool(v *bool) *XdrBool

func (XdrBool) GetU32

func (v XdrBool) GetU32() uint32

func (*XdrBool) Scan

func (v *XdrBool) Scan(ss fmt.ScanState, r rune) error

func (*XdrBool) SetU32

func (v *XdrBool) SetU32(nv uint32)

func (XdrBool) String

func (v XdrBool) String() string

func (*XdrBool) XdrMarshal

func (v *XdrBool) XdrMarshal(x XDR, name string)

func (*XdrBool) XdrPointer

func (v *XdrBool) XdrPointer() interface{}

func (XdrBool) XdrTypeName

func (XdrBool) XdrTypeName() string

func (XdrBool) XdrValue

func (v XdrBool) XdrValue() interface{}

type XdrBytes

type XdrBytes interface {
	XdrType
	GetByteSlice() []byte
}

A common interface implemented by XdrString, XdrVecOpaque, and XdrArrayOpaque (and hence used to operate on the XdrTypes corresponding to value os ftype string<>, opaque<>, and opaque[]).

type XdrEnum

type XdrEnum interface {
	XdrNum32
	XdrEnumNames() map[int32]string
}

Enums additionally provide access to a map of values to names. You generally don't need to invoke the XdrEnumNames() method (since the String and Scan methods already access the underlying maps), but the presence of this method can be useful to differentiate enums from other XdrNum32 types.

type XdrError

type XdrError string

The error thrown by marshaling functions when data has a bad value.

func (XdrError) Error

func (v XdrError) Error() string

type XdrFloat32

type XdrFloat32 float32

The XdrType that float gets converted to for marshaling.

func XDR_float32

func XDR_float32(v *float32) *XdrFloat32

func (XdrFloat32) GetU32

func (v XdrFloat32) GetU32() uint32

func (*XdrFloat32) Scan

func (v *XdrFloat32) Scan(ss fmt.ScanState, r rune) error

func (*XdrFloat32) SetU32

func (v *XdrFloat32) SetU32(nv uint32)

func (XdrFloat32) String

func (v XdrFloat32) String() string

func (*XdrFloat32) XdrMarshal

func (v *XdrFloat32) XdrMarshal(x XDR, name string)

func (*XdrFloat32) XdrPointer

func (v *XdrFloat32) XdrPointer() interface{}

func (XdrFloat32) XdrTypeName

func (XdrFloat32) XdrTypeName() string

func (XdrFloat32) XdrValue

func (v XdrFloat32) XdrValue() interface{}

type XdrFloat64

type XdrFloat64 float64

The XdrType that double gets converted to for marshaling.

func XDR_float64

func XDR_float64(v *float64) *XdrFloat64

func (XdrFloat64) GetU64

func (v XdrFloat64) GetU64() uint64

func (*XdrFloat64) Scan

func (v *XdrFloat64) Scan(ss fmt.ScanState, r rune) error

func (*XdrFloat64) SetU64

func (v *XdrFloat64) SetU64(nv uint64)

func (XdrFloat64) String

func (v XdrFloat64) String() string

func (*XdrFloat64) XdrMarshal

func (v *XdrFloat64) XdrMarshal(x XDR, name string)

func (*XdrFloat64) XdrPointer

func (v *XdrFloat64) XdrPointer() interface{}

func (XdrFloat64) XdrTypeName

func (XdrFloat64) XdrTypeName() string

func (XdrFloat64) XdrValue

func (v XdrFloat64) XdrValue() interface{}

type XdrIn

type XdrIn struct {
	In io.Reader
}

XDR that unmarshals from canonical binary format

func (XdrIn) Marshal

func (xi XdrIn) Marshal(name string, i XdrType)

func (XdrIn) Sprintf

func (xp XdrIn) Sprintf(f string, args ...interface{}) string

type XdrInt32

type XdrInt32 int32

The XdrType that int gets converted to for marshaling.

func XDR_int32

func XDR_int32(v *int32) *XdrInt32

func (XdrInt32) GetU32

func (v XdrInt32) GetU32() uint32

func (*XdrInt32) Scan

func (v *XdrInt32) Scan(ss fmt.ScanState, r rune) error

func (*XdrInt32) SetU32

func (v *XdrInt32) SetU32(nv uint32)

func (XdrInt32) String

func (v XdrInt32) String() string

func (*XdrInt32) XdrMarshal

func (v *XdrInt32) XdrMarshal(x XDR, name string)

func (*XdrInt32) XdrPointer

func (v *XdrInt32) XdrPointer() interface{}

func (XdrInt32) XdrTypeName

func (XdrInt32) XdrTypeName() string

func (XdrInt32) XdrValue

func (v XdrInt32) XdrValue() interface{}

type XdrInt64

type XdrInt64 int64

The XdrType that hyper gets converted to for marshaling.

func XDR_int64

func XDR_int64(v *int64) *XdrInt64

func (XdrInt64) GetU64

func (v XdrInt64) GetU64() uint64

func (*XdrInt64) Scan

func (v *XdrInt64) Scan(ss fmt.ScanState, r rune) error

func (*XdrInt64) SetU64

func (v *XdrInt64) SetU64(nv uint64)

func (XdrInt64) String

func (v XdrInt64) String() string

func (*XdrInt64) XdrMarshal

func (v *XdrInt64) XdrMarshal(x XDR, name string)

func (*XdrInt64) XdrPointer

func (v *XdrInt64) XdrPointer() interface{}

func (XdrInt64) XdrTypeName

func (XdrInt64) XdrTypeName() string

func (XdrInt64) XdrValue

func (v XdrInt64) XdrValue() interface{}

type XdrNum32

type XdrNum32 interface {
	XdrType
	fmt.Stringer
	fmt.Scanner
	GetU32() uint32
	SetU32(uint32)
}

All quantities that should be serialized as 32-bit numbers (including bools, enums, union discriminants, the bit saying whether or not a pointer is NULL, floats, and vector lenghts) are passed to the XDR.Marshal function as a pointer to a defined type implementing the XdrNum32 interface. The one exception is string<> and opaque<>, for which it is the job of XDR.Marshal to serialize the 32-bit length.

type XdrNum64

type XdrNum64 interface {
	XdrType
	fmt.Stringer
	fmt.Scanner
	GetU64() uint64
	SetU64(uint64)
}

All 64-bit numbers (hyper, unsigned hyper, and double) are passed to XDR.Marshal as a pointer to a defined type implementing XdrNum64.

type XdrOut

type XdrOut struct {
	Out io.Writer
}

XDR that marshals to canonical binary format

func (XdrOut) Marshal

func (xo XdrOut) Marshal(name string, i XdrType)

func (XdrOut) Sprintf

func (xp XdrOut) Sprintf(f string, args ...interface{}) string

type XdrPrint

type XdrPrint struct {
	Out io.Writer
}

Back end that renders an XDR data structure as simple text. Example:

XDR_MyType(&myVal).XdrMarshal(&XdrPrint{os.Stdout}, "")

func (XdrPrint) Marshal

func (xp XdrPrint) Marshal(name string, i XdrType)

func (XdrPrint) Sprintf

func (xp XdrPrint) Sprintf(f string, args ...interface{}) string

type XdrProc

type XdrProc interface {
	Prog() uint32
	Vers() uint32
	Proc() uint32
	ProgName() string
	VersName() string
	ProcName() string
	GetArg() XdrType
	GetRes() XdrType
}

Interface for a data structure containing the argument and result types of a call.

type XdrPtr

type XdrPtr interface {
	// Marshals first the present/not-present bool, then if true, the
	// underlying value.
	XdrAggregate

	GetPresent() bool
	SetPresent(bool)

	// If the present/not-present bool is false, this function does
	// nothing.  Otherwise, it marshals just the value, not the bit.
	XdrMarshalValue(XDR, string)
}

Any pointer type is converted to an XdrType implementing the XdrPtr interface for marshaling. Note XdrPtr is a subtype of XdrAggregate. When a Marshal function does nothing special for XdrPtr and treats the XdrPtr like any other XdrAggregate (calling XdrRecurse), the Marshal function will then get called one or two more times, first with an XdrSize (implementing XdrNum32) to marshal the non-NULL bit, and then again with the underlying value if the pointer is non-NULL. An XDR.Marshal function that wants to special case pointers can access the present bit from the GetPresent and SetPresent methods, then bypass marshaling of the bit by calling XdrMarshalValue instead of XdrRecurse.

type XdrSendCall

type XdrSendCall interface {
	SendCall(context.Context, XdrProc) error
}

An interface for types that can send remote procedure calls and await their reply.

type XdrSize

type XdrSize struct {
	Size  uint32
	Bound uint32
}

This XdrType is used to marshal the length of vectors and the present/not-present value of pointers.

func (XdrSize) GetU32

func (v XdrSize) GetU32() uint32

func (*XdrSize) Scan

func (v *XdrSize) Scan(ss fmt.ScanState, r rune) (err error)

func (*XdrSize) SetU32

func (v *XdrSize) SetU32(nv uint32)

func (XdrSize) String

func (v XdrSize) String() string

func (XdrSize) XdrBound

func (v XdrSize) XdrBound() uint32

func (*XdrSize) XdrMarshal

func (v *XdrSize) XdrMarshal(x XDR, name string)

func (*XdrSize) XdrPointer

func (v *XdrSize) XdrPointer() interface{}

func (XdrSize) XdrTypeName

func (XdrSize) XdrTypeName() string

func (XdrSize) XdrValue

func (v XdrSize) XdrValue() interface{}

type XdrSrv

type XdrSrv interface {
	Prog() uint32
	Vers() uint32
	ProgName() string
	VersName() string
	GetProc(uint32) XdrSrvProc
}

Interface for an RPC version interface bound to a server implementation. The result of GetProc() can be used to marshal the arguments and results as well as actually invoke the function (though Do()).

type XdrSrvProc

type XdrSrvProc interface {
	XdrProc
	Do()
	SetContext(context.Context)
}

Interface for a data structure containing argument and results of a procedure plus a way to invoke the procedure on an instance of the RPC version. In other words, Do() actually performs the work of an RPC on the server side.

type XdrString

type XdrString struct {
	Str   *string
	Bound uint32
}

The XdrType that strings get converted to for marshaling.

func (XdrString) GetByteSlice

func (v XdrString) GetByteSlice() []byte

func (XdrString) GetString

func (v XdrString) GetString() string

func (XdrString) Scan

func (v XdrString) Scan(ss fmt.ScanState, _ rune) error

func (XdrString) SetByteSlice

func (v XdrString) SetByteSlice(bs []byte)

func (XdrString) SetString

func (v XdrString) SetString(s string)

func (XdrString) String

func (v XdrString) String() string

func (XdrString) XdrBound

func (v XdrString) XdrBound() uint32

func (XdrString) XdrMarshal

func (v XdrString) XdrMarshal(x XDR, name string)

func (XdrString) XdrPointer

func (v XdrString) XdrPointer() interface{}

func (XdrString) XdrTypeName

func (XdrString) XdrTypeName() string

func (XdrString) XdrValue

func (v XdrString) XdrValue() interface{}

type XdrType

type XdrType interface {
	// Return a string describing the name of the type (which will
	// reflect the name of a typedef).
	XdrTypeName() string

	// Return the underlying native representation of an XdrType
	// (e.g., int32 for an XdrInt32).
	XdrValue() interface{}

	// Return a pointer to the underlying native representation of an
	// XdrType (often just the same type as the XdrType, but sometimes
	// not, e.g., for primitive types that can't have methods
	// defined).
	XdrPointer() interface{}

	// Calls x.Marshal(name, v).
	XdrMarshal(XDR, string)
}

Types passed to the XDR Marshal method implementing the XdrType interface. Pointers to some generated types T already implement the interface. However, since built-in types such as string and int32 cannot implement interfaces, each type T has a function XDR_T(*T) that returns some type implementing XdrType. These XdrType interfaces also encode limits on string and vector sizes and simplify code by collapsing similar cases (e.g., all 32-bit numbers are cast to a pointer implementing XdrNum32).

For most XdrType instances, XdrPointer() and XdrValue() provide access to the underlying type (which might not implement XdrType), with the following exceptions:

* opaque[] (fixed-length arrays of bytes) are returned as XdrArrayOpaque (by XdrValue()) or nil (by XdrPointer()). This is because XdrArrayOpaque wraps a byte slice rather than an actual array. One generally doesn't want to pass arrays around; moreover, getting a pointer to the actual array provides less information, because one can't test for arrays in a type switch without knowing the exact length of the array, while one can always dynamically test the length of a slice.

* Pointer types, in their XdrRecurse methods, marshal a special XdrNum32 type of 0 or 1 to record whether the pointer is nil or the value is present. Since this bool does not exist, XdrPointer() returns nil (while XdrValue returns a bool).

* For arrays, XdrValue() passes a slice, rather than the full array, so as to avoid copying the array.

func XdrBaseType

func XdrBaseType(v XdrType) XdrType

Unwrap typedefs to obtain the underlying XdrType.

type XdrType_bool

type XdrType_bool = *XdrBool

type XdrType_float32

type XdrType_float32 = *XdrFloat32

type XdrType_float64

type XdrType_float64 = *XdrFloat64

type XdrType_int32

type XdrType_int32 = *XdrInt32

type XdrType_int64

type XdrType_int64 = *XdrInt64

type XdrType_uint32

type XdrType_uint32 = *XdrUint32

type XdrType_uint64

type XdrType_uint64 = *XdrUint64

type XdrType_void

type XdrType_void = *XdrVoid

func XDR_XdrVoid

func XDR_XdrVoid(v *XdrVoid) XdrType_void

type XdrTypedef

type XdrTypedef interface {
	XdrType
	XdrUnwrap() XdrType
}

type XdrUint32

type XdrUint32 uint32

The XdrType that unsigned int gets converted to for marshaling.

func XDR_uint32

func XDR_uint32(v *uint32) *XdrUint32

func (XdrUint32) GetU32

func (v XdrUint32) GetU32() uint32

func (*XdrUint32) Scan

func (v *XdrUint32) Scan(ss fmt.ScanState, r rune) error

func (*XdrUint32) SetU32

func (v *XdrUint32) SetU32(nv uint32)

func (XdrUint32) String

func (v XdrUint32) String() string

func (*XdrUint32) XdrMarshal

func (v *XdrUint32) XdrMarshal(x XDR, name string)

func (*XdrUint32) XdrPointer

func (v *XdrUint32) XdrPointer() interface{}

func (XdrUint32) XdrTypeName

func (XdrUint32) XdrTypeName() string

func (XdrUint32) XdrValue

func (v XdrUint32) XdrValue() interface{}

type XdrUint64

type XdrUint64 uint64

The XdrType that unsigned hyper gets converted to for marshaling.

func XDR_uint64

func XDR_uint64(v *uint64) *XdrUint64

func (XdrUint64) GetU64

func (v XdrUint64) GetU64() uint64

func (*XdrUint64) Scan

func (v *XdrUint64) Scan(ss fmt.ScanState, r rune) error

func (*XdrUint64) SetU64

func (v *XdrUint64) SetU64(nv uint64)

func (XdrUint64) String

func (v XdrUint64) String() string

func (*XdrUint64) XdrMarshal

func (v *XdrUint64) XdrMarshal(x XDR, name string)

func (*XdrUint64) XdrPointer

func (v *XdrUint64) XdrPointer() interface{}

func (XdrUint64) XdrTypeName

func (XdrUint64) XdrTypeName() string

func (XdrUint64) XdrValue

func (v XdrUint64) XdrValue() interface{}

type XdrUnion

type XdrUnion interface {
	XdrAggregate
	XdrValid() bool
	XdrValidTags() map[int32]bool // returns nil if there's a default
	XdrUnionTag() XdrNum32
	XdrUnionTagName() string
	XdrUnionBody() XdrType
	XdrUnionBodyName() string
}

A union type is an XdrAggregate with extra methods for individually accessing the tag and active branch.

type XdrVarBytes

type XdrVarBytes interface {
	XdrBytes
	XdrBound() uint32
	SetByteSlice([]byte)
}

A common interface implemented by XdrString and XdrVecOpaque, the XdrTypes for opaque<> and string<>. Since XdrVarBytes is a subtype of XdrBytes, Marshal functions that want to distinguish between opaque<> and opaque[] should first try a type assertion for XdrVarBytes (to check for opaque<>) and then if that fails try XdrBytes (to check for opaque[]).

type XdrVec

type XdrVec interface {
	XdrAggregate
	XdrBound() uint32
	GetVecLen() uint32
	SetVecLen(uint32)
	XdrMarshalN(XDR, string, uint32)
}

Any vector type is passed as a pointer to a user-defined slice type that implements the XdrVec interface. XdrVec is a superset of XdrAggregate, so calling XdrRecurse will recurse to call XDR.Marshal first on the size (of type XdrSize), then on each element of the slice. An XDR.Marshal function can manually marshal the size and then call XdrMarshalN to marshal n vector elements. (When deserializing, it is advisable *not* to call SetVecLen before calling XdrMarshalN in case the length is huge and would exhaust memory; XdrMarshalN gradually grows the slice size as needed so as to throw a premature EOF error if N is larger than the actual input data.)

type XdrVecOpaque

type XdrVecOpaque struct {
	Bytes *[]byte
	Bound uint32
}

The XdrType that opaque<> gets converted to for marshaling.

func (XdrVecOpaque) GetByteSlice

func (v XdrVecOpaque) GetByteSlice() []byte

func (XdrVecOpaque) Scan

func (v XdrVecOpaque) Scan(ss fmt.ScanState, _ rune) error

func (XdrVecOpaque) SetByteSlice

func (v XdrVecOpaque) SetByteSlice(bs []byte)

func (XdrVecOpaque) String

func (v XdrVecOpaque) String() string

func (XdrVecOpaque) XdrBound

func (v XdrVecOpaque) XdrBound() uint32

func (XdrVecOpaque) XdrMarshal

func (v XdrVecOpaque) XdrMarshal(x XDR, name string)

func (XdrVecOpaque) XdrPointer

func (v XdrVecOpaque) XdrPointer() interface{}

func (XdrVecOpaque) XdrTypeName

func (XdrVecOpaque) XdrTypeName() string

func (XdrVecOpaque) XdrValue

func (v XdrVecOpaque) XdrValue() interface{}

type XdrVoid

type XdrVoid struct{}

An XdrType that marshals as zero bytes, and hence is used to represent void argument and return types of functions.

func (XdrVoid) XdrMarshal

func (XdrVoid) XdrMarshal(XDR, string)

func (XdrVoid) XdrPointer

func (XdrVoid) XdrPointer() interface{}

func (XdrVoid) XdrTypeName

func (XdrVoid) XdrTypeName() string

func (XdrVoid) XdrValue

func (XdrVoid) XdrValue() interface{}

Jump to

Keyboard shortcuts

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