Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Aug 5, 2023
0 parents commit 1c36cda
Show file tree
Hide file tree
Showing 20 changed files with 1,177 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: test

on:
- push
- pull_request

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Go Setup
uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Run Tests
run: go test -v ./... -coverprofile cover.out

- name: Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env*
cover.*
addr
32 changes: 32 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
The Clear BSD License

Copyright (c) 2023 Matthew D Love
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<div align="center">
<h3>
<code>go-asn</code>
</h3>
<br/>
Autonomous System Number Utility for Go
<br/>
<br/>
<a href="https://github.com/thatmattlove/go-asn/actions/workflows/test.yml">
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/thatmattlove/go-asn/test.yml?style=for-the-badge">
</a>
<a href="https://app.codecov.io/gh/thatmattlove/go-asn">
<img alt="Codecov" src="https://img.shields.io/codecov/c/github/thatmattlove/go-asn?style=for-the-badge">
</a>
<a href="https://github.com/thatmattlove/go-asn/releases">
<img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/thatmattlove/go-asn?label=version&style=for-the-badge">
</a>

</div>

## Installation

```console
go get github.com/thatmattlove/go-asn
```

## Usage

### Parsing

```go
a, err := asn.Parse("65000")
a, err := asn.FromDecimal("65000")
a, err := asn.FromASDot("64086.59904")
a, err := asn.FromUint64(65001)
a, err := asn.FromBytes(255, 255, 253, 232)
a := asn.From4Bytes(255, 255, 253, 232)
a := asn.From2Bytes(253, 232)
a := asn.FromUint32(65001)
a := asn.MustParse("65000")
a := asn.MustDecimal("65000")
a := asn.MustASDot("0.65000")
```

### Formatting

```go
a := asn.MustParse("65000")
a.Size()
// 2
a.ASPlain()
// 65000
a.ASDot()
// 65000
a.ASDotPlus()
// 0.65000
a.String()
// 65000
a.ByteString()
// {0,0,253,232}

a = asn.MustParse("4200000000")
a.Size()
// 4
a.ASPlain()
// 4200000000
a.ASDot()
// 64086.59904
a.ASDotPlus()
// 64086.59904
a.String()
// 4200000000
a.ByteString()
// {250,86,234,0}
```

### Comparison

```go
a := asn.MustParse("65000")
b := asn.MustParse("65001")
c := asn.MustParse("65002")
d := asn.MustParse("65000")
e := asn.MustParse("64512")
a.Equal(b)
// false
a.Equal(d)
// true
a.LessThan(b)
// true
a.LEqual(c)
// true
a.GreaterThan(e)
// true
a.GEqual(e)
// true
```

### Iteration

```go
start := asn.MustParse("65000")
end := asn.MustParse("65005")

for iter := start.Range(end); iter.Continue(); {
next := iter.Next()
fmt.Println(next.ASPlain())
}
// 65001
// 65002
// 65003
// 65004
// 65505

a := asn.MustParse("65000")
for iter := a.Iter(); iter.Continue(); {
next := iter.Next()
fmt.Println(next.ASPlain())
}
// 65001
// 65002
// ...
// 4294967294
```

![GitHub](https://img.shields.io/github/license/thatmattlove/go-asn?style=for-the-badge&color=black)
60 changes: 60 additions & 0 deletions asn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package asn

// ASN represents a single autonomous system number, a slice of bytes. Both 2-byte (16-bit) and
// 4-byte (32-bit) ASNs are supported.
type ASN []byte

// Size returns either 2 or 4, depending on if the ASN is 2-bytes or 4-bytes.
func (asn ASN) Size() int {
if asn[0] == 0 && asn[1] == 0 {
return 2
}
return 4
}

// Next returns the next ASN after the current ASN. For example, if the current ASN is 65000,
// Next() would return 65001.
func (asn ASN) Next() ASN {
next := make(ASN, BYTE_SIZE)
copy(next, asn)
for i := len(next) - 1; i >= 0; i-- {
if next[i] < 255 {
next[i]++
break
} else {
next[i]--
}
}
return next
}

// Previous returns the previous ASN before the current ASN. For example, if the current ASN is 65001,
// Previous() would return 65000.
func (asn ASN) Previous() ASN {
prev := asn
for i := len(prev) - 1; i >= 0; i-- {
if prev[i] > 0 {
prev[i]--
break
} else {
prev[i] = 255
}
}
return prev
}

// IsGlobal returns true if the ASN is global, i.e. not private.
// See RFC6996.
func (asn ASN) IsGlobal() bool {
return !asn.IsPrivate()
}

// IsGlobal returns true if the ASN is private, i.e. not global.
// See RFC6996.
func (asn ASN) IsPrivate() bool {
n := asn.Uint32()
if asn.Size() == 2 {
return n >= 64512 && n <= 65534
}
return n >= 4200000000 && n <= 4294967294
}
112 changes: 112 additions & 0 deletions asn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package asn_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/thatmattlove/go-asn"
)

func TestASN_Uint32(t *testing.T) {
a := asn.ASN{0, 0, 253, 232}
n := a.Uint32()
assert.Equal(t, uint32(65000), n)
}

func TestASN_Size(t *testing.T) {
type caseT struct {
size int
asn asn.ASN
}
cases := []caseT{
{2, asn.ASN{0, 0, 255, 255}},
{4, asn.ASN{255, 255, 255, 255}},
}
for _, c := range cases {
c := c
t.Run(fmt.Sprint(c.size), func(t *testing.T) {
t.Parallel()
assert.Equal(t, c.size, c.asn.Size())
})
}
}

func TestASN_Next(t *testing.T) {
t.Run("basic", func(t *testing.T) {
t.Parallel()
a := asn.ASN{0, 0, 0x38, 0xbd}
expected := asn.ASN{0, 0, 0x38, 0xbe}
next := a.Next()
assert.True(t, next.Equal(expected), "next=%s, expected=%s", next.ByteString(), expected.ByteString())
})
t.Run("basic2", func(t *testing.T) {
t.Parallel()
a := asn.ASN{255, 255, 255, 254}
e := asn.ASN{255, 255, 255, 255}
next := a.Next()
assert.True(t, next.Equal(e), "next=%s, expected=%s", next.ByteString(), e.ByteString())
})
}

func TestASN_Previous(t *testing.T) {
t.Run("basic", func(t *testing.T) {
t.Parallel()
a := asn.ASN{0, 0, 0x38, 0xbd}
expected := asn.ASN{0, 0, 0x38, 0xbc}
prev := a.Previous()
assert.True(t, prev.Equal(expected), "prev=%s, expected=%s", prev.ByteString(), expected.ByteString())
})
t.Run("basic2", func(t *testing.T) {
t.Parallel()
a := asn.ASN{255, 255, 255, 0}
e := asn.ASN{255, 255, 254, 255}
next := a.Previous()
assert.True(t, next.Equal(e), "next=%s, expected=%s", next.ByteString(), e.ByteString())
})
}

type privateCasesT struct {
asn string
want bool
}

var privateCases = []privateCasesT{
{"65000", true},
{"65534", true},
{"64512", true},
{"64600", true},
{"14525", false},
{"13335", false},
{"4200000000", true},
{"4200000005", true},
{"4200090000", true},
{"4294967294", true},
{"4294967293", true},
{"4204967293", true},
{"4194967294", false},
{"395077", false},
{"4199999999", false},
}

func TestASN_IsPrivate(t *testing.T) {
for _, c := range privateCases {
c := c
t.Run(c.asn, func(t *testing.T) {
t.Parallel()
a := asn.MustParse(c.asn)
assert.Equal(t, c.want, a.IsPrivate())
})
}
}

func TestASN_IsGlobal(t *testing.T) {
for _, c := range privateCases {
c := c
t.Run(c.asn, func(t *testing.T) {
t.Parallel()
a := asn.MustParse(c.asn)
assert.Equal(t, !c.want, a.IsGlobal())
})
}
}
Loading

0 comments on commit 1c36cda

Please sign in to comment.