Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend support for resolving autonomous systems #7

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Make all (go generate, build + test)
run: make all

- name: Update AS Data
run: ./updateASData.sh

- name: Make all (go generate, build + test)
run: make all

- name: Smoke Test
run: sudo timeout --preserve-status --signal=INT 10s ./bin/amd64/socket-connect-bpf -a

Expand All @@ -31,8 +31,8 @@ jobs:
mkdir -p bin/arm64/as
cp ./README.md bin/amd64/
cp ./README.md bin/arm64/
cp as/ip2asn-v4-u32.tsv bin/amd64/as/
cp as/ip2asn-v4-u32.tsv bin/arm64/as/
cp as/*.tsv bin/amd64/as/
cp as/*.tsv bin/arm64/as/
mkdir artifacts
tar czf artifacts/socket-connect-bpf-linux-amd64.tar.gz --directory=bin/amd64/ .
tar czf artifacts/socket-connect-bpf-linux-arm64.tar.gz --directory=bin/arm64/ .
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ bin/**/socket-connect-bpf
*.o
bpf_*_bpfeb.go
bpf_*_bpfel.go

*.tsv
6 changes: 3 additions & 3 deletions as/asnumbers.go → as/asnumbersIPv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

var asMap = make(map[uint8][]ASInfo)

// ParseASNumbers parses the autonomous system (AS) Numbers and IP ranges from a .tsv file
func ParseASNumbers(asTsvFile string) {
// ParseASNumbersIPv4 parses the autonomous system (AS) Numbers and IPv4 ranges from a .tsv file
func ParseASNumbersIPv4(asTsvFile string) {
csvFile, err := os.Open(asTsvFile)

if err != nil {
Expand Down Expand Up @@ -76,7 +76,7 @@ func getNameOnly(desc string) string {
}

// GetASInfo returns information about an autonomous system (AS) of which the given IP is part of.
func GetASInfo(ip net.IP) ASInfo {
func GetASInfoIPv4(ip net.IP) ASInfo {
ipAddr := conv.ToUint(ip)
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, ipAddr)
Expand Down
6 changes: 3 additions & 3 deletions as/asnumbers_test.go → as/asnumbersIPv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
)

func TestIPToAsRange(t *testing.T) {
ParseASNumbers("./ip2asn-v4-u32.tsv")
func TestIP4ToAsRange(t *testing.T) {
ParseASNumbersIPv4("./ip2asn-v4-u32.tsv")
ip := "82.197.176.1"
got := GetASInfo(net.ParseIP(ip))
got := GetASInfoIPv4(net.ParseIP(ip))
wantName := "INIT7"
if got.Name != wantName {
t.Errorf("GetASInfo(%s) = %s; want %s", ip, got.Name, wantName)
Expand Down
74 changes: 74 additions & 0 deletions as/asnumbersIPv6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package as

import (
"bufio"
"bytes"
"fmt"
"net"
"os"
"strconv"
"strings"
)

var asList []ASInfoIPv6

// ParseASNumbersIPv6 parses the autonomous system (AS) Numbers and IPv6 ranges from a .tsv file
func ParseASNumbersIPv6(asTsvFile string) {
csvFile, err := os.Open(asTsvFile)

if err != nil {
fmt.Println("Could not read AS Number file")
fmt.Println(err)
return
}

defer csvFile.Close()

scanner := bufio.NewScanner(csvFile)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) != 5 {
continue // Skip invalid lines
}

start := net.ParseIP(fields[0])
end := net.ParseIP(fields[1])
if start == nil || end == nil {
continue // Skip invalid IP ranges
}

asNumber, _ := strconv.ParseUint(fields[2], 10, 32)

asList = append(asList, ASInfoIPv6{
StartIP: start,
EndIP: end,
AsNumber: uint32(asNumber),
Name: fields[4],
})
}

if err := scanner.Err(); err != nil {
fmt.Println("Could not read AS Number file")
fmt.Println(err)
return
}
}

// GetASInfoIPv6 returns information about an autonomous system (AS) of which the given IP is part of.
func GetASInfoIPv6(ip net.IP) ASInfoIPv6 {
for _, r := range asList {
if bytes.Compare(ip, r.StartIP) >= 0 && bytes.Compare(ip, r.EndIP) <= 0 {
return r
}
}
var empty ASInfoIPv6
return empty
}

// ASInfoIPv6 contains information about an autonomous system (AS)
type ASInfoIPv6 struct {
StartIP net.IP
EndIP net.IP
AsNumber uint32
Name string
}
20 changes: 20 additions & 0 deletions as/asnumbersIPv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package as

import (
"net"
"testing"
)

func TestIP6ToAsRange(t *testing.T) {
ParseASNumbersIPv6("./ip2asn-v6.tsv")
ip := "2620:2d:4000:1::1"
got := GetASInfoIPv6(net.ParseIP(ip))
wantName := "CANONICAL-AS"
if got.Name != wantName {
t.Errorf("GetASInfo(%s) = %s; want %s", ip, got.Name, wantName)
}
wantAsNumber := uint32(41231)
if got.AsNumber != wantAsNumber {
t.Errorf("GetASInfo(%s) = %d; want %d", ip, got.AsNumber, wantAsNumber)
}
}
Loading
Loading