Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 1 | package provider |
| 2 | |
| 3 | // Support for the ARIN IRR. |
| 4 | // ARIN is special. We have to query them via whois. It's also not the same |
| 5 | // whois as you usually see. And also not many autnums (even big players like |
| 6 | // AS15169 and AS112) have IRR entries at all. |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "fmt" |
| 11 | "strings" |
| 12 | |
| 13 | "code.hackerspace.pl/hscloud/bgpwtf/cccampix/irr/whois" |
| 14 | pb "code.hackerspace.pl/hscloud/bgpwtf/cccampix/proto" |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 15 | "google.golang.org/grpc/codes" |
| 16 | "google.golang.org/grpc/status" |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | const ARINWhois = "rr.arin.net:43" |
| 20 | |
| 21 | type arin struct { |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 22 | sem chan struct{} |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 23 | } |
| 24 | |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 25 | func NewARIN(limit int) Provider { |
| 26 | return &arin{ |
| 27 | sem: make(chan struct{}, limit), |
| 28 | } |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 31 | func (a *arin) Query(ctx context.Context, asn uint64) (*pb.IRRQueryResponse, error) { |
| 32 | a.sem <- struct{}{} |
| 33 | defer func() { |
| 34 | <-a.sem |
| 35 | }() |
| 36 | |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 37 | data, err := whois.Query(ctx, ARINWhois, fmt.Sprintf("AS%d", asn)) |
| 38 | if err != nil { |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 39 | return nil, status.Errorf(codes.Unavailable, "could not contact ARIN IRR: %v", err) |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | lines := strings.Split(data, "\n") |
| 43 | |
| 44 | // Convert possibly 'continued' RPSL entries into single-line entries. |
| 45 | // eg. |
| 46 | // import: from AS6083 |
| 47 | // action pref=10; |
| 48 | // accept ANY |
| 49 | // into |
| 50 | // 'import', 'from AS6083, action pref=10; accept ANY' |
| 51 | |
| 52 | attrs := []rpslRawAttribute{} |
| 53 | for _, line := range lines { |
| 54 | if strings.HasPrefix(strings.TrimSpace(line), "%") { |
| 55 | // Comment |
| 56 | continue |
| 57 | } |
| 58 | if strings.TrimSpace(line) == "" { |
| 59 | // Empty line |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | if strings.HasPrefix(line, " ") { |
| 64 | // Continuation |
| 65 | if len(attrs) < 1 { |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 66 | return nil, status.Errorf(codes.Unavailable, "unparseable IRR, continuation with no previous atribute name: %q", line) |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | attrs[len(attrs)-1].value += " " + strings.TrimSpace(line) |
| 70 | } else { |
| 71 | parts := strings.SplitN(line, ":", 2) |
| 72 | if len(parts) != 2 { |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 73 | return nil, status.Errorf(codes.Unavailable, "unparseable IRR, line with no attribute key: %q", line) |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 74 | } |
| 75 | name := strings.TrimSpace(parts[0]) |
| 76 | value := strings.TrimSpace(parts[1]) |
| 77 | attrs = append(attrs, rpslRawAttribute{ |
| 78 | name: name, |
| 79 | value: value, |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 84 | if len(attrs) == 0 { |
| 85 | return nil, status.Errorf(codes.NotFound, "no such ASN") |
| 86 | } |
| 87 | |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 88 | return &pb.IRRQueryResponse{ |
| 89 | Source: pb.IRRQueryResponse_SOURCE_ARIN, |
| 90 | Attributes: parseAttributes(attrs), |
| 91 | }, nil |
| 92 | } |