addr

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: Apache-2.0 Imports: 8 Imported by: 7

Documentation

Overview

Package addr contains types for SCION addressing.

A SCION address is composed of the following parts: ISD (ISolation Domain identifier), AS (Autonomous System idenifier), and Host (the host address).

The ISD-AS parts are often considered together. Conventionally, this is abbreviated to "IA".

The allocations and formatting of ISDs and ASes are documented here: https://github.com/scionproto/scion/wiki/ISD-and-AS-numbering. Note that the ':' separator for AS formatting is not used in paths/filenames for compatibility reasons, so '_' is used instead in those contexts.

Index

Examples

Constants

View Source
const (
	IABytes       = 8
	ISDBits       = 16
	ASBits        = 48
	BGPASBits     = 32
	MaxISD    ISD = (1 << ISDBits) - 1
	MaxAS     AS  = (1 << ASBits) - 1
	MaxBGPAS  AS  = (1 << BGPASBits) - 1
)

Variables

View Source
var (
	// ErrUnsupportedSVCAddress indicates an unsupported SVC address.
	ErrUnsupportedSVCAddress = serrors.New("unsupported SVC address")
)

Functions

func FormatAS

func FormatAS(as AS, opts ...FormatOption) string

FormatAS formats the AS number.

func FormatAddrPort added in v0.9.0

func FormatAddrPort(a Addr, port uint16) string

FormatAddrPort formats an Addr with a port to the format

[<ISD>-<AS>,<Host>]:<Port>.

EXPERIMENTAL: This API is experimental. It may be changed to a String() function an a combined AddrPort type instead.

func FormatIA

func FormatIA(ia IA, opts ...FormatOption) string

FormatIA formats the ISD-AS.

func FormatISD

func FormatISD(isd ISD, opts ...FormatOption) string

FormatISD formats the ISD number.

Types

type AS

type AS uint64

AS is the Autonomous System identifier. See formatting and allocations here: https://github.com/scionproto/scion/wiki/ISD-and-AS-numbering#as-numbers

func ParseAS

func ParseAS(as string) (AS, error)

ParseAS parses an AS from a decimal (in the case of the 32bit BGP AS number space) or ipv6-style hex (in the case of SCION-only AS numbers) string.

func ParseFormattedAS

func ParseFormattedAS(as string, opts ...FormatOption) (AS, error)

ParseFormattedAS parses an AS number that was formatted with the FormatAS function. The same options must be provided to successfully parse.

func (AS) MarshalText

func (as AS) MarshalText() ([]byte, error)

func (AS) String

func (as AS) String() string

func (*AS) UnmarshalText

func (as *AS) UnmarshalText(text []byte) error

type Addr added in v0.9.0

type Addr struct {
	IA   IA
	Host Host
}

Addr is a full SCION address, composed of ISD, AS and Host part.

func MustParseAddr added in v0.9.0

func MustParseAddr(s string) Addr

MustParseAddr calls ParseAddr(s) and panics on error. It is intended for use in tests with hard-coded strings.

func ParseAddr added in v0.9.0

func ParseAddr(s string) (Addr, error)

ParseAddr parses s as an address in the format <ISD>-<AS>,<Host>, returning the result as an Addr.

Example
package main

import (
	"fmt"

	"github.com/scionproto/scion/pkg/addr"
)

func main() {
	a, err := addr.ParseAddr("6-ffaa:0:123,198.51.100.1")
	fmt.Printf("ia: %v, host type: %v, host: %v, err: %v\n", a.IA, a.Host.Type(), a.Host, err)
}
Output:

ia: 6-ffaa:0:123, host type: IP, host: 198.51.100.1, err: <nil>
Example (Svc)
package main

import (
	"fmt"

	"github.com/scionproto/scion/pkg/addr"
)

func main() {
	a, err := addr.ParseAddr("6-ffaa:0:123,CS")
	fmt.Printf("ia: %v, host type: %v, host: %v, err: %v\n", a.IA, a.Host.Type(), a.Host, err)
}
Output:

ia: 6-ffaa:0:123, host type: SVC, host: CS, err: <nil>

func ParseAddrPort added in v0.9.0

func ParseAddrPort(s string) (Addr, uint16, error)

ParseAddrPort parses s as a SCION address with a port, in the format

[<ISD>-<AS>,<Host>]:<Port>.

Examples:

  • [isd-as,svc]:port (e.g., [1-ff00:0:110,CS]:80)
  • [isd-as,ipv4]:port (e.g., [1-ff00:0:110,192.0.2.1]:80)
  • [isd-as,ipv6%zone]:port (e.g., [1-ff00:0:110,2001:DB8::1%zone]:80)

EXPERIMENTAL: This API is experimental. It may be changed to return a combined AddrPort type instead.

func (Addr) MarshalText added in v0.9.0

func (a Addr) MarshalText() ([]byte, error)

func (*Addr) Set added in v0.9.0

func (a *Addr) Set(s string) error

Set implements flag.Value interface

func (Addr) String added in v0.9.0

func (a Addr) String() string

func (*Addr) UnmarshalText added in v0.9.0

func (a *Addr) UnmarshalText(b []byte) error

type FormatOption

type FormatOption func(*formatOptions)

func WithDefaultPrefix

func WithDefaultPrefix() FormatOption

WithDefaultPrefix enables the default prefix which depends on the type. For the AS number, the prefix is 'AS'. For the ISD number, the prefix is 'ISD'.

func WithFileSeparator

func WithFileSeparator() FormatOption

WithFileSeparator returns an option that sets the separator to underscore.

func WithSeparator

func WithSeparator(separator string) FormatOption

WithSeparator sets the separator to use for formatting AS numbers. In case of the empty string, the ':' is used.

type Host added in v0.9.0

type Host struct {
	// contains filtered or unexported fields
}

Host represents the AS-local host identifier of a SCION address.

Different address types (IPv4, IPv6, SVC) are all represented with this Host struct, discriminated with a Type() field.

The zero value is a valid object with Host{}.Type() == HostTypeNone.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/scionproto/scion/pkg/addr"
)

func main() {
	hs := []addr.Host{
		{},
		addr.HostIP(netip.MustParseAddr("::1")),
		addr.HostIP(netip.AddrFrom4([4]byte{198, 51, 100, 1})),
		addr.HostSVC(addr.SvcCS),
	}
	for _, h := range hs {
		fmt.Printf("h: %q, h.Type(): %q", h, h.Type())
		switch h.Type() {
		case addr.HostTypeIP:
			fmt.Printf(", h.IP().Is4(): %v", h.IP().Is4())
		case addr.HostTypeSVC:
			fmt.Printf(", h.SVC().IsMulticast(): %v", h.SVC().IsMulticast())
		default:
			fmt.Printf(", h == addr.Host{}: %v", h == addr.Host{})
		}
		fmt.Println()
	}

	// Use Host as map key:
	stuff := make(map[addr.Host]struct{})
	for _, h := range hs {
		stuff[h] = struct{}{}
	}
	_, hasSvcCS := stuff[addr.HostSVC(addr.SvcCS)]
	_, hasSvcDS := stuff[addr.HostSVC(addr.SvcDS)]
	fmt.Printf("has SvcCS: %v, has SvcDS: %v", hasSvcCS, hasSvcDS)

}
Output:

h: "<None>", h.Type(): "None", h == addr.Host{}: true
h: "::1", h.Type(): "IP", h.IP().Is4(): false
h: "198.51.100.1", h.Type(): "IP", h.IP().Is4(): true
h: "CS", h.Type(): "SVC", h.SVC().IsMulticast(): false
has SvcCS: true, has SvcDS: false

func HostIP added in v0.9.0

func HostIP(ip netip.Addr) Host

HostIP returns a Host address representing ip, with type HostTypeIP.

func HostSVC

func HostSVC(svc SVC) Host

HostSvc returns a Host address representing svc, with type HostTypeSVC.

func MustParseHost added in v0.9.0

func MustParseHost(s string) Host

MustParseHost calls ParseHost(s) and panics on error. It is intended for use in tests with hard-coded strings.

func ParseHost added in v0.9.0

func ParseHost(s string) (Host, error)

ParseHost parses s as either a service address or an IP address, returning the result as a Host address. s can either be a SVC address, in the format supported by ParseSVC(s), or an IP address in dotted decimal or IPv6 format.

func (Host) IP added in v0.9.0

func (h Host) IP() netip.Addr

IP returns the IP address represented by h. Panics if h.Type() is not HostTypeIP.

func (Host) SVC added in v0.9.0

func (h Host) SVC() SVC

SVC returns the SVC address represented by h. Panics if h.Type() is not HostTypeSVC.

func (*Host) Set added in v0.9.0

func (h *Host) Set(s string) error

Set implements flag.Value interface

func (Host) String added in v0.9.0

func (h Host) String() string

func (Host) Type added in v0.9.0

func (h Host) Type() HostAddrType

Type returns the type of the address represented by h.

type HostAddrType

type HostAddrType uint8

HostAddrType discriminates between different types of Host addresses.

const (
	HostTypeNone HostAddrType = iota
	HostTypeIP
	HostTypeSVC
)

func (HostAddrType) String

func (t HostAddrType) String() string

type IA

type IA uint64

IA represents the ISD (ISolation Domain) and AS (Autonomous System) Id of a given SCION AS. The highest 16 bit form the ISD number and the lower 48 bits form the AS number.

func IAFrom

func IAFrom(isd ISD, as AS) (IA, error)

IAFrom creates an IA from the ISD and AS number.

func MustIAFrom

func MustIAFrom(isd ISD, as AS) IA

MustIAFrom creates an IA from the ISD and AS number. It panics if any error is encountered. Callers must ensure that the values passed to this function are valid.

func ParseFormattedIA

func ParseFormattedIA(ia string, opts ...FormatOption) (IA, error)

ParseFormattedIA parses an IA that was formatted with the FormatIA function. The same options must be provided to successfully parse.

func ParseIA

func ParseIA(ia string) (IA, error)

ParseIA parses an IA from a string of the format 'isd-as'.

func (IA) AS

func (ia IA) AS() AS

func (IA) Equal

func (ia IA) Equal(other IA) bool

func (IA) ISD

func (ia IA) ISD() ISD

func (IA) IsWildcard

func (ia IA) IsWildcard() bool

IsWildcard returns whether the ia has a wildcard part (isd or as).

func (IA) IsZero

func (ia IA) IsZero() bool

func (IA) MarshalText

func (ia IA) MarshalText() ([]byte, error)

func (*IA) Set

func (ia *IA) Set(s string) error

Set implements flag.Value interface

func (IA) String

func (ia IA) String() string

func (*IA) UnmarshalText

func (ia *IA) UnmarshalText(b []byte) error

type ISD

type ISD uint16

ISD is the ISolation Domain identifier. See formatting and allocations here: https://github.com/scionproto/scion/wiki/ISD-and-AS-numbering#isd-numbers

func ParseFormattedISD

func ParseFormattedISD(isd string, opts ...FormatOption) (ISD, error)

ParseFormattedISD parses an ISD number that was formatted with the FormatISD function. The same options must be provided to successfully parse.

func ParseISD

func ParseISD(s string) (ISD, error)

ParseISD parses an ISD from a decimal string. Note that ISD 0 is parsed without any errors.

func (ISD) String

func (isd ISD) String() string

type SVC added in v0.9.0

type SVC uint16

SVC is a SCION service address. A service address is a short identifier for the service type, and a flag-bit for multicast. The package's SVC constant values are defined without multicast. The Multicast and Base methods set/unset the multicast flag.

const (
	SvcDS       SVC = 0x0001
	SvcCS       SVC = 0x0002
	SvcWildcard SVC = 0x0010
	SvcNone     SVC = 0xffff

	SVCMcast SVC = 0x8000
)

func ParseSVC added in v0.9.0

func ParseSVC(str string) (SVC, error)

ParseSVC returns the SVC address corresponding to str. For anycast SVC addresses, use CS_A and DS_A; shorthand versions without the _A suffix (e.g., CS) also return anycast SVC addresses. For multicast, use CS_M, and DS_M.

func (SVC) Base added in v0.9.0

func (h SVC) Base() SVC

Base returns the SVC identifier with the multicast flag unset.

func (SVC) BaseString added in v0.9.0

func (h SVC) BaseString() string

BaseString returns the upper case name of the service. For unrecognized services, it returns "<SVC:Hex-Value>".

func (SVC) IsMulticast added in v0.9.0

func (h SVC) IsMulticast() bool

IsMulticast returns the value of the multicast flag.

func (SVC) Multicast added in v0.9.0

func (h SVC) Multicast() SVC

Multicast returns the SVC identifier with the multicast flag set.

func (SVC) String added in v0.9.0

func (h SVC) String() string

Jump to

Keyboard shortcuts

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