Implements the XDR standard as specified in RFC 4506 in pure Go (Golang)

Overview

go-xdr

[Build Status] (https://travis-ci.org/davecgh/go-xdr) [![Coverage Status] (https://coveralls.io/repos/davecgh/go-xdr/badge.png?branch=master)] (https://coveralls.io/r/davecgh/go-xdr?branch=master)

Go-xdr implements the data representation portion of the External Data Representation (XDR) standard protocol as specified in RFC 4506 (obsoletes RFC 1832 and RFC 1014) in Pure Go (Golang). A comprehensive suite of tests are provided to ensure proper functionality. It is licensed under the liberal ISC license, so it may be used in open source or commercial projects.

NOTE: Version 1 of this package is still available via the github.com/davecgh/go-xdr/xdr import path to avoid breaking existing clients. However, it is highly recommended that all old clients upgrade to version 2 and all new clients use version 2. In addition to some speed optimizations, version 2 has been been updated to work with standard the io.Reader and io.Writer interfaces instead of raw byte slices. This allows it to be much more flexible and work directly with files, network connections, etc.

Documentation

[GoDoc] (http://godoc.org/github.com/davecgh/go-xdr/xdr2)

Full go doc style documentation for the project can be viewed online without installing this package by using the excellent GoDoc site here: http://godoc.org/github.com/davecgh/go-xdr/xdr2

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/davecgh/go-xdr/xdr2/

Installation

$ go get github.com/davecgh/go-xdr/xdr2

Sample Decode Program

package main

import (
	"bytes"
    "fmt"

    "github.com/davecgh/go-xdr/xdr2"
)

func main() {
	// Hypothetical image header format.
	type ImageHeader struct {
		Signature   [3]byte
		Version     uint32
		IsGrayscale bool
		NumSections uint32
	}

	// XDR encoded data described by the above structure.  Typically this would
	// be read from a file or across the network, but use a manual byte array
	// here as an example.
	encodedData := []byte{
		0xAB, 0xCD, 0xEF, 0x00, // Signature
		0x00, 0x00, 0x00, 0x02, // Version
		0x00, 0x00, 0x00, 0x01, // IsGrayscale
		0x00, 0x00, 0x00, 0x0A, // NumSections
	}

	// Declare a variable to provide Unmarshal with a concrete type and instance
	// to decode into.
	var h ImageHeader
	bytesRead, err := xdr.Unmarshal(bytes.NewReader(encodedData), &h)
	if err != nil {
		fmt.Println(err)
		return
	}
  
	fmt.Println("bytes read:", bytesRead)
	fmt.Printf("h: %+v", h)
}

The struct instance, h, will then contain the following values:

h.Signature = [3]byte{0xAB, 0xCD, 0xEF}
h.Version = 2
h.IsGrayscale = true
h.NumSections = 10

Sample Encode Program

package main

import (
	"bytes"
    "fmt"

    "github.com/davecgh/go-xdr/xdr2"
)

func main() {
	// Hypothetical image header format.
	type ImageHeader struct {
		Signature   [3]byte
		Version     uint32
		IsGrayscale bool
		NumSections uint32
	}

	// Sample image header data.
	h := ImageHeader{[3]byte{0xAB, 0xCD, 0xEF}, 2, true, 10}

	// Use marshal to automatically determine the appropriate underlying XDR
	// types and encode.
	var w bytes.Buffer
	bytesWritten, err := xdr.Marshal(&w, &h)
	if err != nil {
		fmt.Println(err)
		return
	}

	encodedData := w.Bytes()
	fmt.Println("bytes written:", bytesWritten)
	fmt.Println("encoded data:", encodedData)
}

The result, encodedData, will then contain the following XDR encoded byte sequence:

0xAB, 0xCD, 0xEF, 0x00,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x0A,

License

Go-xdr is licensed under the liberal ISC License.

Comments
  • Accepting contributions?

    Accepting contributions?

    Hi @davecgh,

    Are you still maintaining this package and would you be open to accepting a contribution that adds an xdr3 package, that is a copy of xdr2 but adds optionals, unions, and change how pointers work to support using optionals?

    We've been using a modified version of this package here: https://github.com/stellar/go-xdr and if you'd be up for us bringing our changes upstream we'd be up for preparing a PR.

    This is approximately what the diff would look like, but we'd also need to merge downstream some of the changes in your master branch before we opened, so there would be some differences to keep xdr3 as a copy of xdr2 at the point in time the PR is opened: https://github.com/davecgh/go-xdr/compare/master...stellar:master

    No worries if you're no longer maintaining go-xdr, and if that is the case we'll move forward with developing our fork of go-xdr at https://github.com/stellar/go-xdr.

    Cheers, Leigh

    cc @bartekn @ire-and-curses @marcopeereboom @rasky

    opened by leighmcculloch 4
  • Support for time.Time

    Support for time.Time

    I added XDR to my go_serialization_benchmarks and it is currently the fastest.

    Unfortunately, it does not correctly serialize time.Time. This is not surprising as it isn't supported, but it would be great to support it if possible.

    It could perhaps use a scheme similar to JSON, marshaling to RFC3339.

    enhancement 
    opened by alecthomas 4
  • unexpected EOF while decoding 260 bytes

    unexpected EOF while decoding 260 bytes

    Maybe you could help me solving my issue.

    I've posted all necessary information in issue https://github.com/prashanthpai/sunrpc/issues/3 because I thought it could be an issue with his library. Now we figured out that it is more an issue with variable length opaque data on the decoding side ;)

    If you want me to paste all information from the other issue in this issue, I'll do that.

    Thanks :)

    opened by dweidenfeld 2
  • Support for optional data (4.19) of the XDR spec

    Support for optional data (4.19) of the XDR spec

    I tried decoding optional-data 4.19 and came across this comment in the code

    https://github.com/davecgh/go-xdr/blob/master/xdr2/decode.go#L572

    // RFC Sections 4.15 though 4.19 only apply to the data specification language
    // which is not implemented by this package
    

    I don't understand the "only apply to the data specification language" bit. The API I'm using (NFSv3, to be specific) is encoding a linked-list with the above optional-data format and other than decoding each element and then checking for the "follows" 4B for zero before terminating (which is what I'm currently doing here), is there a way to decode this data?

    If the loop-and-check method is the only way, are you averse to me pushing a PR to add support for this? I'm thinking it would require a StructTag to descend down the right path for a slice. Let me know and I'll be happy to get started on that.

    Thanks!

    opened by fdawg4l 1
  • Please add max size to decoder.

    Please add max size to decoder.

    When reading XDR directly of the wire a remote malicious person could craft packets that could use up all of machines memory because there are no limits. It would be helpful if we could set a limit on how large a read can be. For example:

    dec := NewDecoderLimited(myReader, 1024*1024) // limit to 1MB
    _, err := dec.Decode(&myXDR)
    if err == xdr.ErrTooBig {
            myReader.Close()
            return err
    }
    
    enhancement 
    opened by marcopeereboom 1
  • Add a max decoder limit in order to prevent crashes.

    Add a max decoder limit in order to prevent crashes.

    Currently, it is very simple to crash go-xdr by fuzzing a wire protocol. This PR adds a max reader size so that we don't try to allocate giants amount of memory by validating the incoming size first.

    Test coverage is 100%

    opened by marcopeereboom 0
  • Add support for discriminated unions and optional data

    Add support for discriminated unions and optional data

    This PR adds support for:

    • Discriminated unions (RFC 4506, 4.15)
    • Optional data (RFC 4506, 4.19)

    In both cases, the code relies on struct tags to convey the required metadata information. Please see the individual commits for more information.

    opened by rasky 19
Owner
Dave Collins
Dave Collins
Herbert Fischer 197 Jun 10, 2022
A go implementation of the STUN client (RFC 3489 and RFC 5389)

go-stun go-stun is a STUN (RFC 3489, 5389) client implementation in golang (a.k.a. UDP hole punching). RFC 3489: STUN - Simple Traversal of User Datag

Cong Ding 520 Sep 11, 2022
A commandline tool to resolve URI Templates expressions as specified in RFC 6570.

URI Are you tired to build, concat, replace URL(s) (via shell scripts sed/awk/tr) from your awesome commandline pipeline? Well! here is the missing pi

Luca Sepe 17 Jun 9, 2021
CBOR RFC 7049 (Go/Golang) - safe & fast with standard API + toarray & keyasint, CBOR tags, float64/32/16, fuzz tested.

CBOR library in Go fxamacker/cbor is a CBOR encoder & decoder in Go. It has a standard API, CBOR tags, options for duplicate map keys, float64→32→16,

Faye Amacker 489 Sep 20, 2022
Package arp implements the ARP protocol, as described in RFC 826. MIT Licensed.

arp Package arp implements the ARP protocol, as described in RFC 826. MIT Licensed. Portions of this code are taken from the Go standard library. The

Matt Layher 296 Sep 27, 2022
Package dhcp6 implements a DHCPv6 server, as described in RFC 3315. MIT Licensed.

dhcp6 Package dhcp6 implements a DHCPv6 server, as described in IETF RFC 3315. MIT Licensed. At this time, the API is not stable, and may change over

Matt Layher 76 Sep 27, 2022
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

High Performance, Kubernetes Native Object Storage 2 Jun 26, 2022
CoAP Client/Server implementing RFC 7252 for the Go Language

Canopus Canopus is a client/server implementation of the Constrained Application Protocol (CoAP) Updates 25.11.2016 I've added basic dTLS Support base

Zubair Hamed 148 Jan 23, 2022
Fast RFC 5389 STUN implementation in go

STUN Package stun implements Session Traversal Utilities for NAT (STUN) [RFC5389] protocol and client with no external dependencies and zero allocatio

null 486 Sep 17, 2022
Diameter stack and Base Protocol (RFC 6733) for the Go programming language

Diameter Base Protocol Package go-diameter is an implementation of the Diameter Base Protocol RFC 6733 and a stack for the Go programming language. St

Alexandre Fiori 209 Sep 6, 2022
URI Templates (RFC 6570) implemented in Go

uritemplates -- import "github.com/jtacoma/uritemplates" Package uritemplates is a level 4 implementation of RFC 6570 (URI Template, http://tools.ietf

Joshua Tacoma 71 Jan 15, 2022
RFC 4566 SDP implementation in go

SDP Package sdp implements SDP: Session Description Protocol [RFC4566]. Complies to gortc principles as core package. Examples See examples folder. Al

null 114 Sep 27, 2022
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

uuid The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services. This package is based on the g

Google 3.9k Sep 23, 2022
Fast RFC 5389 STUN implementation in go

STUN Package stun implements Session Traversal Utilities for NAT (STUN) [RFC5389] protocol and client with no external dependencies and zero allocatio

null 486 Sep 17, 2022
A simple uuid library based on RFC 4122

UUID generator A simple library that generates uuids. Supported versions: version 1 version 3 version 4 version 5 Supported variants: DCE Microsoft Th

Vitor Oliveira 0 Oct 20, 2021
RFC 1413 compliant fake identd

RFC 1413 compliant fake identd. It is an implementation of the Ident Protocol, but it lies to the clients and always returns fake identities of queried users.

Hyeon Kim (김지현) 2 Jan 30, 2022
Parse a RFC 3339 duration string into time.Duration

duration Parse a RFC3339 duration string into time.Duration There are probably a few unsupported edge cases still to be fixed, please help me find the

Shingirai Chanakira 0 Nov 25, 2021
jsonpointer - an RFC 6901 implementation for Go

jsonpointer - an RFC 6901 implementation for Go Package jsonpointer provides the ability to resolve, assign, and delete values of any type, including

Chance 10 Jun 13, 2022
Implementation of RFC-6238 (Time-Based One-Time Password Algorithm) in Go.

TOTP TOTP (RFC-6238) implementation in Go with no external dependencies. INSTALL You can do little copying the totp.go file or add this package as Go

Sarjono Mukti Aji 1 Jan 18, 2022
Rabbitio - Rabbit stream cipher package RFC 4503 for Go

rabbitio rabbitio is a rabbit stream cipher packge based on RFC 4503 for golang

Sina Ghaderi 7 Feb 8, 2022