-
Notifications
You must be signed in to change notification settings - Fork 6
/
ip.go
114 lines (100 loc) · 4.22 KB
/
ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2020 Joakim Kennedy. All rights reserved. Use of
// this source code is governed by the included BSD license.
package stix2
import "fmt"
// IPv4Address represents one or more IPv4 addresses expressed using CIDR
// notation.
type IPv4Address struct {
STIXCyberObservableObject
// Value specifies the values of one or more IPv4 addresses expressed using
// CIDR notation. If a given IPv4Address object represents a single IPv4
// address, the CIDR /32 suffix MAY be omitted. Example: 10.2.4.5/24
Value string `json:"value"`
// ResolvesTo specifies a list of references to one or more Layer 2 Media
// Access Control (MAC) addresses that the IPv4 address resolves to.
ResolvesTo []Identifier `json:"resolves_to_refs,omitempty"`
// BelongsTo specifies a list of references to one or more autonomous
// systems (AS) that the IPv4 address belongs to.
BelongsTo []Identifier `json:"belongs_to_refs,omitempty"`
}
func (o *IPv4Address) MarshalJSON() ([]byte, error) {
return marshalToJSONHelper(o)
}
// AddResolvesTo describes that this IPv4Address resolves to one or more Layer
// 2 Media Access Control (MAC) addresses.
func (c *IPv4Address) AddResolvesTo(id Identifier, opts ...STIXOption) (*Relationship, error) {
if !IsValidIdentifier(id) || !id.ForType(TypeMACAddress) {
return nil, ErrInvalidParameter
}
return NewRelationship(RelationshipTypeResolvesTo, c.ID, id, opts...)
}
// AddBelongsTo describes that this IPv4 Address belongs to one or more
// autonomous systems (AS).
func (c *IPv4Address) AddBelongsTo(id Identifier, opts ...STIXOption) (*Relationship, error) {
if !IsValidIdentifier(id) || !id.ForType(TypeAutonomousSystem) {
return nil, ErrInvalidParameter
}
return NewRelationship(RelationshipTypeBelongsTo, c.ID, id, opts...)
}
// NewIPv4Address creates a new IPv4Address object.
func NewIPv4Address(value string, opts ...STIXOption) (*IPv4Address, error) {
if value == "" {
return nil, ErrInvalidParameter
}
base := newSTIXCyberObservableObject(TypeIPv4Addr)
obj := &IPv4Address{
STIXCyberObservableObject: base,
Value: value,
}
err := applyOptions(obj, opts)
obj.ID = NewObservableIdentifier(fmt.Sprintf("[\"%s\"]", value), TypeIPv4Addr)
return obj, err
}
// IPv6Address represents one or more IPv6 addresses expressed using CIDR
// notation.
type IPv6Address struct {
STIXCyberObservableObject
// Value specifies the values of one or more IPv6 addresses expressed using
// CIDR notation. If a given IPv6Address object represents a single IPv6
// address, the CIDR /128 suffix MAY be omitted.
Value string `json:"value"`
// ResolvesTo specifies a list of references to one or more Layer 2 Media
// Access Control (MAC) addresses that the IPv6 address resolves to.
ResolvesTo []Identifier `json:"resolves_to_refs,omitempty"`
// BelongsTo specifies a list of references to one or more autonomous
// systems (AS) that the IPv6 address belongs to.
BelongsTo []Identifier `json:"belongs_to_refs,omitempty"`
}
func (o *IPv6Address) MarshalJSON() ([]byte, error) {
return marshalToJSONHelper(o)
}
// AddResolvesTo describes that this IPv6Address resolves to one or more Layer
// 2 Media Access Control (MAC) addresses.
func (c *IPv6Address) AddResolvesTo(id Identifier, opts ...STIXOption) (*Relationship, error) {
if !IsValidIdentifier(id) || !id.ForType(TypeMACAddress) {
return nil, ErrInvalidParameter
}
return NewRelationship(RelationshipTypeResolvesTo, c.ID, id, opts...)
}
// AddBelongsTo describes that this IPv6 Address belongs to one or more
// autonomous systems (AS).
func (c *IPv6Address) AddBelongsTo(id Identifier, opts ...STIXOption) (*Relationship, error) {
if !IsValidIdentifier(id) || !id.ForType(TypeAutonomousSystem) {
return nil, ErrInvalidParameter
}
return NewRelationship(RelationshipTypeBelongsTo, c.ID, id, opts...)
}
// NewIPv6Address creates a new IPv6Address object.
func NewIPv6Address(value string, opts ...STIXOption) (*IPv6Address, error) {
if value == "" {
return nil, ErrInvalidParameter
}
base := newSTIXCyberObservableObject(TypeIPv6Addr)
obj := &IPv6Address{
STIXCyberObservableObject: base,
Value: value,
}
err := applyOptions(obj, opts)
obj.ID = NewObservableIdentifier(fmt.Sprintf("[\"%s\"]", value), TypeIPv6Addr)
return obj, err
}