forked from openconfig/ondatra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
1199 lines (1007 loc) · 38.3 KB
/
headers.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ondatra
import (
"github.com/openconfig/ondatra/ixnet"
opb "github.com/openconfig/ondatra/proto"
)
// Header is a packet header.
type Header interface {
asPB() *opb.Header
}
// NewEthernetHeader returns a new Ethernet header.
// The header is initialized with none of its properties specified.
// Unless specified, a best effort will be made to infer the VLAN ID and src and dst addresses from the topology.
func NewEthernetHeader() *EthernetHeader {
return &EthernetHeader{&opb.EthernetHeader{}}
}
// EthernetHeader is an Ethernet packet header.
type EthernetHeader struct {
pb *opb.EthernetHeader
}
// WithEtherType sets the EtherType of the Ethernet header to the specified value.
func (h *EthernetHeader) WithEtherType(etherType uint32) *EthernetHeader {
h.pb.EtherType = etherType
return h
}
// WithSrcAddress sets the source MAC address of the Ethernet header to the specified value.
// To generate a range of source addresses, use SrcAddressRange() instead.
func (h *EthernetHeader) WithSrcAddress(addr string) *EthernetHeader {
h.pb.SrcAddr = addrRangeSingle(addr)
return h
}
// SrcAddressRange returns the range of source addresses of the Ethernet header for further configuration.
// By default, the range will be nonrandom values in the interval ["00:00:00:00:00:01", "FF:FF:FF:FF:FF:FF").
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *EthernetHeader) SrcAddressRange() *ixnet.AddressRange {
if h.pb.SrcAddr == nil {
h.pb.SrcAddr = newMACAddrRange()
}
return ixnet.NewAddressRange(h.pb.SrcAddr)
}
// WithDstAddress sets the destination MAC address of the Ethernet header to the specified value.
// To generate a range of destination addresses, use DstAddressRange() instead.
func (h *EthernetHeader) WithDstAddress(addr string) *EthernetHeader {
h.pb.DstAddr = addrRangeSingle(addr)
return h
}
// DstAddressRange returns the range of destination addresses of the Ethernet header for further configuration.
// By default, the range will be nonrandom values in the interval ["00:00:00:00:00:01", "FF:FF:FF:FF:FF:FF").
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *EthernetHeader) DstAddressRange() *ixnet.AddressRange {
if h.pb.DstAddr == nil {
h.pb.DstAddr = newMACAddrRange()
}
return ixnet.NewAddressRange(h.pb.DstAddr)
}
// WithVLANID sets the 12-bit VLAN ID of the Ethernet header to the specified value.
func (h *EthernetHeader) WithVLANID(vid uint16) *EthernetHeader {
h.pb.VlanId = uint32(vid)
return h
}
// WithBadCRC set whether the Ethernet header has an incorrect CRC in the frame
// check sequence.
func (h *EthernetHeader) WithBadCRC(bad bool) *EthernetHeader {
h.pb.BadCrc = true
return h
}
func (h *EthernetHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Eth{h.pb}}
}
// NewGREHeader returns a new GRE header.
// The header is initialized with none of its properties specified.
func NewGREHeader() *GREHeader {
return &GREHeader{&opb.GreHeader{}}
}
// GREHeader is a Generic Route Encapsulation packet header.
type GREHeader struct {
pb *opb.GreHeader
}
// WithKey sets the key of the GRE header.
func (h *GREHeader) WithKey(key uint32) *GREHeader {
h.pb.Key = key
return h
}
// WithSequenceNumber sets sequence number of the GRE header.
func (h *GREHeader) WithSequenceNumber(seq uint32) *GREHeader {
h.pb.Seq = seq
return h
}
func (h *GREHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Gre{h.pb}}
}
// NewIPv4Header returns a new IPv4 header.
// The header is initialized with a hop limit of 64, DSCP of 0 (best effort), and ECN of 0 (not ECN capable).
// Unless specified, a best effort will be made to infer the src and dst addresses from the topology.
func NewIPv4Header() *IPv4Header {
return &IPv4Header{&opb.Ipv4Header{Ttl: 64, Dscp: 0, Ecn: 0}}
}
// IPv4Header is an IPv4 packet header.
type IPv4Header struct {
pb *opb.Ipv4Header
}
// WithDSCP sets the DSCP field of the IPv4 header.
func (h *IPv4Header) WithDSCP(dscp uint8) *IPv4Header {
h.pb.Dscp = uint32(dscp)
return h
}
// WithECN sets the ECN field of the IPv4 header.
func (h *IPv4Header) WithECN(ecn uint8) *IPv4Header {
h.pb.Ecn = uint32(ecn)
return h
}
// WithIdentification set identification field of IPv4 Header
func (h *IPv4Header) WithIdentification(identification int) *IPv4Header {
h.pb.Identification = uint32(identification)
return h
}
// WithDontFragment sets the "don't fragment" bit of the IPv4 header.
func (h *IPv4Header) WithDontFragment(dontFragment bool) *IPv4Header {
h.pb.DontFragment = dontFragment
return h
}
// WithMoreFragments sets the "more fragments" bit of the IPv4 Header
func (h *IPv4Header) WithMoreFragments(moreFragments bool) *IPv4Header {
h.pb.MoreFragments = moreFragments
return h
}
// WithFragmentOffset sets the fragment offset field of the IPv4 header.
func (h *IPv4Header) WithFragmentOffset(fragmentOffset int) *IPv4Header {
h.pb.FragmentOffset = uint32(fragmentOffset)
return h
}
// WithTTL sets the TTL of the IPv4 header.
func (h *IPv4Header) WithTTL(ttl uint8) *IPv4Header {
h.pb.Ttl = uint32(ttl)
return h
}
// WithProtocol sets the protocol field of the IPv4 header.
// If left unspecified, it will be inferred from the next header in the flow.
func (h *IPv4Header) WithProtocol(protocol int) *IPv4Header {
p := uint32(protocol)
h.pb.Protocol = &p
return h
}
// WithHeaderChecksum sets the header checksum field of the IPv4 header.
func (h *IPv4Header) WithHeaderChecksum(checksum uint16) *IPv4Header {
h.pb.Checksum = uint32(checksum)
return h
}
// WithSrcAddress sets the source IP address of the IPv4 header to the specified value.
// To generate a range of source addresses, use SrcAddressRange() instead.
func (h *IPv4Header) WithSrcAddress(addr string) *IPv4Header {
h.pb.SrcAddr = addrRangeSingle(addr)
return h
}
// SrcAddressRange returns the range of source addresses of the IPv4 header for further configuration.
// By default, the range will be nonrandom values in the interval ["0.0.0.1", "255.255.255.255").
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *IPv4Header) SrcAddressRange() *ixnet.AddressRange {
if h.pb.SrcAddr == nil {
h.pb.SrcAddr = newIPv4AddrRange()
}
return ixnet.NewAddressRange(h.pb.SrcAddr)
}
// WithDstAddress sets the destination IP addresses of the IPv4 header to the specified value.
// To generate a range of destination addresses, use DstAddressRange() instead.
func (h *IPv4Header) WithDstAddress(addr string) *IPv4Header {
h.pb.DstAddr = addrRangeSingle(addr)
return h
}
// DstAddressRange returns the range of destination addresses of the IPv4 header for further configuration.
// By default, the range will be nonrandom values in the interval ["0.0.0.1", "255.255.255.255").
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *IPv4Header) DstAddressRange() *ixnet.AddressRange {
if h.pb.DstAddr == nil {
h.pb.DstAddr = newIPv4AddrRange()
}
return ixnet.NewAddressRange(h.pb.DstAddr)
}
func (h *IPv4Header) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Ipv4{h.pb}}
}
// NewIPv6Header returns a new IPv6 header.
// The header is initialized with a hop limit of 64, DSCP of 0 (best effort), and ECN of 0 (not ECN capable).
// Unless specified, a best effort will be made to infer the src and dst addresses from the topology.
func NewIPv6Header() *IPv6Header {
return &IPv6Header{&opb.Ipv6Header{HopLimit: 64, Dscp: 0, Ecn: 0}}
}
// IPv6Header is an IPv6 packet header.
type IPv6Header struct {
pb *opb.Ipv6Header
}
// WithSrcAddress sets the source IP addresses of the IPv6 header to the specified value.
// To generate a range of source addresses, use SrcAddressRange() instead.
func (h *IPv6Header) WithSrcAddress(addr string) *IPv6Header {
h.pb.SrcAddr = addrRangeSingle(addr)
return h
}
// SrcAddressRange returns the range of source addresses of the IPv6 header for further configuration.
// By default, the range will be nonrandom values in the interval ["::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"].
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *IPv6Header) SrcAddressRange() *ixnet.AddressRange {
if h.pb.SrcAddr == nil {
h.pb.SrcAddr = newIPv6AddrRange()
}
return ixnet.NewAddressRange(h.pb.SrcAddr)
}
// WithDstAddress sets the destination IP addresses of the IPv6 header to the specified value.
// To generate a range of destination addresses, use DstAddressRange() instead.
func (h *IPv6Header) WithDstAddress(addr string) *IPv6Header {
h.pb.DstAddr = addrRangeSingle(addr)
return h
}
// DstAddressRange returns the range of destination addresses of the IPv6 header for further configuration.
// By default, the range will be nonrandom values in the interval ["::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"].
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *IPv6Header) DstAddressRange() *ixnet.AddressRange {
if h.pb.DstAddr == nil {
h.pb.DstAddr = newIPv6AddrRange()
}
return ixnet.NewAddressRange(h.pb.DstAddr)
}
// WithFlowLabel sets the flow label of the IPv6 header.
func (h *IPv6Header) WithFlowLabel(flowLabel uint32) *IPv6Header {
h.pb.FlowLabel = intRangeSingle(flowLabel)
return h
}
// FlowLabelRange sets the flow label of the IPv6 header to a range of values and returns the range.
// By default, the range will be nonrandom values in the interval [0, 2^20).
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *IPv6Header) FlowLabelRange() *ixnet.UIntRange {
if h.pb.FlowLabel == nil {
h.pb.FlowLabel = newFlowLabelRange()
}
return ixnet.NewUIntRange(h.pb.FlowLabel)
}
// WithHopLimit sets the hop limit of the IPv6 header.
func (h *IPv6Header) WithHopLimit(hopLimit uint8) *IPv6Header {
h.pb.HopLimit = uint32(hopLimit)
return h
}
// WithDSCP sets the DSCP value of the IPv6 header.
func (h *IPv6Header) WithDSCP(dscp uint8) *IPv6Header {
h.pb.Dscp = uint32(dscp)
return h
}
// WithECN sets the ECN value of the IPv6 header.
func (h *IPv6Header) WithECN(ecn uint8) *IPv6Header {
h.pb.Ecn = uint32(ecn)
return h
}
func (h *IPv6Header) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Ipv6{h.pb}}
}
// NewMPLSHeader returns a new MPLS header.
// The header is initialized with a traffic class of 0 (best effort) and TTL of 255.
func NewMPLSHeader() *MPLSHeader {
return &MPLSHeader{&opb.MplsHeader{Exp: 0, Ttl: 255}}
}
// MPLSHeader is a Multiprotocol Label Switching packet header.
type MPLSHeader struct {
pb *opb.MplsHeader
}
// WithLabel sets the label of the MPLS header to the specified value.
// To generate a range of labels, use LabelRange() instead.
func (h *MPLSHeader) WithLabel(label uint32) *MPLSHeader {
h.pb.Label = intRangeSingle(label)
return h
}
// LabelRange sets the label of the MPLS header to a range of values and returns the range.
// By default, the range will be nonrandom values in the interval [0, 2^20).
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *MPLSHeader) LabelRange() *ixnet.UIntRange {
if h.pb.Label == nil {
h.pb.Label = newFlowLabelRange()
}
return ixnet.NewUIntRange(h.pb.Label)
}
// WithEXP sets the EXP (aka traffic class) of the MPLS header.
func (h *MPLSHeader) WithEXP(exp uint8) *MPLSHeader {
h.pb.Exp = uint32(exp)
return h
}
// WithTTL sets the TTL of the MPLS header.
func (h *MPLSHeader) WithTTL(ttl uint8) *MPLSHeader {
h.pb.Ttl = uint32(ttl)
return h
}
func (h *MPLSHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Mpls{h.pb}}
}
// NewTCPHeader returns a new TCP header.
// The header is initialized with none of its properties specified.
func NewTCPHeader() *TCPHeader {
return &TCPHeader{&opb.TcpHeader{}}
}
// TCPHeader is a Transmission Control Protocol packet header.
type TCPHeader struct {
pb *opb.TcpHeader
}
// WithSrcPort sets the source port of the TCP header to the specified value.
// To generate a range of source ports, use SrcPortRange() instead.
func (h *TCPHeader) WithSrcPort(port uint16) *TCPHeader {
h.pb.SrcPort = intRangeSingle(uint32(port))
return h
}
// SrcPortRange returns the range of source ports of the TCP header for further configuration.
// By default, the range will be nonrandom values in the interval [1, 2^16).
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *TCPHeader) SrcPortRange() *ixnet.UIntRange {
if h.pb.SrcPort == nil {
h.pb.SrcPort = newPortRange()
}
return ixnet.NewUIntRange(h.pb.SrcPort)
}
// WithDstPort sets the destination port of the TCP header to the specified value.
// To generate a range of destination ports, use DstPortRange() instead.
func (h *TCPHeader) WithDstPort(port uint16) *TCPHeader {
h.pb.DstPort = intRangeSingle(uint32(port))
return h
}
// DstPortRange returns the range of destination ports of the TCP header for further configuration.
// By default, the range will be nonrandom values in the interval [1, 2^16).
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *TCPHeader) DstPortRange() *ixnet.UIntRange {
if h.pb.DstPort == nil {
h.pb.DstPort = newPortRange()
}
return ixnet.NewUIntRange(h.pb.DstPort)
}
// WithSequenceNumber sets sequence number of the TCP header.
func (h *TCPHeader) WithSequenceNumber(seq uint32) *TCPHeader {
h.pb.Seq = seq
return h
}
func (h *TCPHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Tcp{h.pb}}
}
// NewUDPHeader returns a new UDP header.
// The header is initialized with none of its properties specified.
func NewUDPHeader() *UDPHeader {
return &UDPHeader{&opb.UdpHeader{}}
}
// UDPHeader is a User Datagram Protocol packet header.
type UDPHeader struct {
pb *opb.UdpHeader
}
// WithSrcPort sets the source port of the TCP header to the specified value.
// To generate a range of source ports, use SrcPortRange() instead.
func (h *UDPHeader) WithSrcPort(port uint16) *UDPHeader {
h.pb.SrcPort = intRangeSingle(uint32(port))
return h
}
// SrcPortRange returns the range of source ports of the TCP header for further configuration.
// By default, the range will be nonrandom values in the interval [1, 2^16).
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *UDPHeader) SrcPortRange() *ixnet.UIntRange {
if h.pb.SrcPort == nil {
h.pb.SrcPort = newPortRange()
}
return ixnet.NewUIntRange(h.pb.SrcPort)
}
// WithDstPort sets the destination port of the TCP header to the specified value.
// To generate a range of destination ports, use DstPortRange() instead.
func (h *UDPHeader) WithDstPort(port uint16) *UDPHeader {
h.pb.DstPort = intRangeSingle(uint32(port))
return h
}
// DstPortRange returns the range of destination ports of the TCP header for further configuration.
// By default, the range will be nonrandom values in the interval [1, 2^16).
// The count of values in the range is not set by default; the user must set it explicitly.
func (h *UDPHeader) DstPortRange() *ixnet.UIntRange {
if h.pb.DstPort == nil {
h.pb.DstPort = newPortRange()
}
return ixnet.NewUIntRange(h.pb.DstPort)
}
func (h *UDPHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Udp{h.pb}}
}
// HTTPHeader is an HTTP packet header.
type HTTPHeader struct {
pb *opb.HttpHeader
}
// NewHTTPHeader returns a new HTTP header.
// The header is initialized with none of its properties specified.
func NewHTTPHeader() *HTTPHeader {
return &HTTPHeader{&opb.HttpHeader{}}
}
func (h *HTTPHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Http{h.pb}}
}
// ICMPHeader is an ICMP packet header.
type ICMPHeader struct {
pb *opb.IcmpHeader
}
// NewICMPHeader returns a new ICMP header.
// The header is initialized as an Echo Reply.
func NewICMPHeader() *ICMPHeader {
return &ICMPHeader{&opb.IcmpHeader{
Type: &opb.IcmpHeader_EchoReply_{&opb.IcmpHeader_EchoReply{}},
}}
}
func (h *ICMPHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Icmp{h.pb}}
}
// ICMPEchoReply is an ICMP echo reply message.
type ICMPEchoReply struct {
pb *opb.IcmpHeader_EchoReply
}
// EchoReply sets the ICMPHeader message type to echo reply.
func (h *ICMPHeader) EchoReply() *ICMPEchoReply {
epb := &opb.IcmpHeader_EchoReply{}
h.pb.Type = &opb.IcmpHeader_EchoReply_{epb}
return &ICMPEchoReply{epb}
}
// ICMPDestinationUnreachable is an ICMP destination unreachable message.
type ICMPDestinationUnreachable struct {
pb *opb.IcmpHeader_DestinationUnreachable
}
// DestinationUnreachable sets the ICMPHeader message type to destination unreachable.
func (h *ICMPHeader) DestinationUnreachable() *ICMPDestinationUnreachable {
dupb := &opb.IcmpHeader_DestinationUnreachable{}
h.pb.Type = &opb.IcmpHeader_DestinationUnreachable_{dupb}
return &ICMPDestinationUnreachable{dupb}
}
// WithNetworkUnreachable sets the destination unreachable code to network unreachable.
func (h *ICMPDestinationUnreachable) WithNetworkUnreachable() *ICMPDestinationUnreachable {
h.pb.Code = opb.IcmpHeader_DestinationUnreachable_NETWORK_UNREACHABLE
return h
}
// WithHostUnreachable sets the destination unreachable code to host unreachable.
func (h *ICMPDestinationUnreachable) WithHostUnreachable() *ICMPDestinationUnreachable {
h.pb.Code = opb.IcmpHeader_DestinationUnreachable_HOST_UNREACHABLE
return h
}
// WithProtocolUnreachable sets the destination unreachable code to protocol unreachable.
func (h *ICMPDestinationUnreachable) WithProtocolUnreachable() *ICMPDestinationUnreachable {
h.pb.Code = opb.IcmpHeader_DestinationUnreachable_PROTOCOL_UNREACHABLE
return h
}
// WithPortUnreachable sets the destination unreachable code to port unreachable.
func (h *ICMPDestinationUnreachable) WithPortUnreachable() *ICMPDestinationUnreachable {
h.pb.Code = opb.IcmpHeader_DestinationUnreachable_PORT_UNREACHABLE
return h
}
// WithFragmentationRequired sets the destination unreachable code to fragmentation required.
func (h *ICMPDestinationUnreachable) WithFragmentationRequired() *ICMPDestinationUnreachable {
h.pb.Code = opb.IcmpHeader_DestinationUnreachable_FRAGMENTATION_REQUIRED
return h
}
// WithSourceRouteFailed sets the destination unreachable code to source route failed.
func (h *ICMPDestinationUnreachable) WithSourceRouteFailed() *ICMPDestinationUnreachable {
h.pb.Code = opb.IcmpHeader_DestinationUnreachable_SOURCE_ROUTE_FAILED
return h
}
// ICMPRedirectMessage is an ICMP redirect message.
type ICMPRedirectMessage struct {
pb *opb.IcmpHeader_RedirectMessage
}
// RedirectMessage sets the ICMPHeader message type to redirect message.
func (h *ICMPHeader) RedirectMessage() *ICMPRedirectMessage {
rmpb := &opb.IcmpHeader_RedirectMessage{}
h.pb.Type = &opb.IcmpHeader_RedirectMessage_{rmpb}
return &ICMPRedirectMessage{rmpb}
}
// WithForNetwork sets the redirect message code to redirect for network.
func (h *ICMPRedirectMessage) WithForNetwork() *ICMPRedirectMessage {
h.pb.Code = opb.IcmpHeader_RedirectMessage_NETWORK
return h
}
// WithForHost sets the redirect message code to redirect for host.
func (h *ICMPRedirectMessage) WithForHost() *ICMPRedirectMessage {
h.pb.Code = opb.IcmpHeader_RedirectMessage_HOST
return h
}
// WithForToSAndNetwork sets the redirect message code to redirect for ToS and network.
func (h *ICMPRedirectMessage) WithForToSAndNetwork() *ICMPRedirectMessage {
h.pb.Code = opb.IcmpHeader_RedirectMessage_TOS_AND_NETWORK
return h
}
// WithForToSAndHost sets the redirect message code to redirect for ToS and host.
func (h *ICMPRedirectMessage) WithForToSAndHost() *ICMPRedirectMessage {
h.pb.Code = opb.IcmpHeader_RedirectMessage_TOS_AND_HOST
return h
}
// WithIP sets the redirect gateway IP address.
func (h *ICMPRedirectMessage) WithIP(ip string) *ICMPRedirectMessage {
h.pb.IpAddr = ip
return h
}
// ICMPEchoRequest is an ICMP echo request message.
type ICMPEchoRequest struct {
pb *opb.IcmpHeader_EchoRequest
}
// EchoRequest sets the ICMPHeader message type to echo request.
func (h *ICMPHeader) EchoRequest() *ICMPEchoRequest {
epb := &opb.IcmpHeader_EchoRequest{}
h.pb.Type = &opb.IcmpHeader_EchoRequest_{epb}
return &ICMPEchoRequest{epb}
}
// ICMPTimeExceeded is an ICMP time exceeded message.
type ICMPTimeExceeded struct {
pb *opb.IcmpHeader_TimeExceeded
}
// TimeExceeded sets the ICMPHeader message type to time exceeded.
func (h *ICMPHeader) TimeExceeded() *ICMPTimeExceeded {
tepb := &opb.IcmpHeader_TimeExceeded{}
h.pb.Type = &opb.IcmpHeader_TimeExceeded_{tepb}
return &ICMPTimeExceeded{tepb}
}
// WithTransit sets the time exceeded message code to time exceeded in transit.
func (h *ICMPTimeExceeded) WithTransit() *ICMPTimeExceeded {
h.pb.Code = opb.IcmpHeader_TimeExceeded_TRANSIT
return h
}
// WithFragmentReassembly sets the time exceeded message code to time exceeded in fragment reassembly.
func (h *ICMPTimeExceeded) WithFragmentReassembly() *ICMPTimeExceeded {
h.pb.Code = opb.IcmpHeader_TimeExceeded_FRAGMENT_REASSEMBLY
return h
}
// ICMPParameterProblem is an ICMP parameter problem message.
type ICMPParameterProblem struct {
pb *opb.IcmpHeader_ParameterProblem
}
// ParameterProblem sets the ICMPHeader message type to parameter problem.
func (h *ICMPHeader) ParameterProblem() *ICMPParameterProblem {
pppb := &opb.IcmpHeader_ParameterProblem{}
h.pb.Type = &opb.IcmpHeader_ParameterProblem_{pppb}
return &ICMPParameterProblem{pppb}
}
// WithPointer sets the pointer for the parameter problem message.
func (h *ICMPParameterProblem) WithPointer(pointer uint32) *ICMPParameterProblem {
h.pb.Pointer = pointer
return h
}
// ICMPTimestamp is an ICMP timestamp message.
type ICMPTimestamp struct {
pb *opb.IcmpHeader_Timestamp
}
// Timestamp sets the ICMPHeader message type to timestamp message.
func (h *ICMPHeader) Timestamp() *ICMPTimestamp {
tpb := &opb.IcmpHeader_Timestamp{}
h.pb.Type = &opb.IcmpHeader_Timestamp_{tpb}
return &ICMPTimestamp{tpb}
}
// WithID sets the ID for the timestamp message.
func (h *ICMPTimestamp) WithID(id uint32) *ICMPTimestamp {
h.pb.Id = id
return h
}
// WithSeq sets the sequence number for the timestamp message.
func (h *ICMPTimestamp) WithSeq(seq uint32) *ICMPTimestamp {
h.pb.Seq = seq
return h
}
// WithOriginateTimestamp sets the originate timestamp for the timestamp message.
func (h *ICMPTimestamp) WithOriginateTimestamp(ts uint32) *ICMPTimestamp {
h.pb.OriginateTs = ts
return h
}
// ICMPTimestampReply is an ICMP timestamp reply message.
type ICMPTimestampReply struct {
pb *opb.IcmpHeader_TimestampReply
}
// TimestampReply sets the ICMPHeader message type to timestamp reply.
func (h *ICMPHeader) TimestampReply() *ICMPTimestampReply {
trpb := &opb.IcmpHeader_TimestampReply{}
h.pb.Type = &opb.IcmpHeader_TimestampReply_{trpb}
return &ICMPTimestampReply{trpb}
}
// WithID sets the ID for the timestamp reply message.
func (h *ICMPTimestampReply) WithID(id uint32) *ICMPTimestampReply {
h.pb.Id = id
return h
}
// WithSeq sets the sequence number for the timestamp reply message.
func (h *ICMPTimestampReply) WithSeq(seq uint32) *ICMPTimestampReply {
h.pb.Seq = seq
return h
}
// WithOriginateTimestamp sets the originate timestamp for the timestamp reply message.
func (h *ICMPTimestampReply) WithOriginateTimestamp(ts uint32) *ICMPTimestampReply {
h.pb.OriginateTs = ts
return h
}
// WithReceiveTimestamp sets the receive timestamp for the timestamp reply message.
func (h *ICMPTimestampReply) WithReceiveTimestamp(ts uint32) *ICMPTimestampReply {
h.pb.ReceiveTs = ts
return h
}
// WithTransmitTimestamp sets the transmit timestamp for the timestamp reply message.
func (h *ICMPTimestampReply) WithTransmitTimestamp(ts uint32) *ICMPTimestampReply {
h.pb.TransmitTs = ts
return h
}
// OSPFHeader is an OSPF packet header.
type OSPFHeader struct {
pb *opb.OspfHeader
}
// NewOSPFHeader returns a new OSPF header.
// The header is initialized as a default Hello packet.
func NewOSPFHeader() *OSPFHeader {
hdr := &OSPFHeader{&opb.OspfHeader{}}
hdr.Hello()
return hdr
}
func (h *OSPFHeader) asPB() *opb.Header {
return &opb.Header{Type: &opb.Header_Ospf{h.pb}}
}
// OSPFHello is an OSPF hello message.
type OSPFHello struct {
pb *opb.OspfHeader_Hello
}
// Hello sets the OSPFHeader message type to a hello message with the following
// default values:
// - Network Mask: 24
// - Hello Interval: 10
// - Router Dead Interval: 40
func (h *OSPFHeader) Hello() *OSPFHello {
hpb := &opb.OspfHeader_Hello{
NetworkMaskLength: 24,
HelloIntervalSec: 10,
RouterDeadIntervalSec: 40,
}
h.pb.Type = &opb.OspfHeader_Hello_{hpb}
return &OSPFHello{hpb}
}
// WithNetworkMaskLength sets the network mask length for the hello message.
func (h *OSPFHello) WithNetworkMaskLength(length uint32) *OSPFHello {
h.pb.NetworkMaskLength = length
return h
}
// WithHelloIntervalSec sets the interval for the hello message.
func (h *OSPFHello) WithHelloIntervalSec(secs uint32) *OSPFHello {
h.pb.HelloIntervalSec = secs
return h
}
// WithRouterPriority sets the router priority for the hello message.
func (h *OSPFHello) WithRouterPriority(prio uint32) *OSPFHello {
h.pb.RouterPriority = prio
return h
}
// WithRouterDeadIntervalSec sets the dead interval for the hello message.
func (h *OSPFHello) WithRouterDeadIntervalSec(secs uint32) *OSPFHello {
h.pb.RouterDeadIntervalSec = secs
return h
}
// WithDesignatedRouter sets the designated router for the hello message.
func (h *OSPFHello) WithDesignatedRouter(ip string) *OSPFHello {
h.pb.DesignatedRouter = ip
return h
}
// WithBackupDesignatedRouter sets the backup designated router for the hello message.
func (h *OSPFHello) WithBackupDesignatedRouter(ip string) *OSPFHello {
h.pb.BackupDesignatedRouter = ip
return h
}
// WithNeighbors sets the neighbor routers for the hello message.
func (h *OSPFHello) WithNeighbors(ips ...string) *OSPFHello {
h.pb.Neighbors = ips
return h
}
// OSPFDatabaseDescription is an OSPF database description message.
type OSPFDatabaseDescription struct {
pb *opb.OspfHeader_DatabaseDescription
}
// DatabaseDescription sets the OSPFHeader message type to a DBD message with
// MTU set to 1500.
func (h *OSPFHeader) DatabaseDescription() *OSPFDatabaseDescription {
dpb := &opb.OspfHeader_DatabaseDescription{Mtu: 1500}
h.pb.Type = &opb.OspfHeader_Dbd{dpb}
return &OSPFDatabaseDescription{dpb}
}
// WithMTU sets the MTU field of the database description message.
func (h *OSPFDatabaseDescription) WithMTU(mtu uint32) *OSPFDatabaseDescription {
h.pb.Mtu = mtu
return h
}
// WithInitial sets the 'initial' bit of the database description message.
func (h *OSPFDatabaseDescription) WithInitial(initial bool) *OSPFDatabaseDescription {
h.pb.Initial = initial
return h
}
// WithMore sets the 'more' bit of the database description message.
func (h *OSPFDatabaseDescription) WithMore(more bool) *OSPFDatabaseDescription {
h.pb.More = more
return h
}
// WithMaster sets the 'master' bit of the database description message.
func (h *OSPFDatabaseDescription) WithMaster(master bool) *OSPFDatabaseDescription {
h.pb.Master = master
return h
}
// WithSeq sets the sequence number of the database description message.
func (h *OSPFDatabaseDescription) WithSeq(seq uint32) *OSPFDatabaseDescription {
h.pb.Seq = seq
return h
}
// OSPFLinkStateRequest is an OSPF link state request message.
type OSPFLinkStateRequest struct {
pb *opb.OspfHeader_LinkStateRequest
}
// LinkStateRequest sets the OSPFHeader message type to a link state request,
// defaulting to asking for a Router link.
func (h *OSPFHeader) LinkStateRequest() *OSPFLinkStateRequest {
lpb := &opb.OspfHeader_LinkStateRequest{Type: opb.OspfHeader_LINK_STATE_TYPE_ROUTER}
h.pb.Type = &opb.OspfHeader_Lsr{lpb}
return &OSPFLinkStateRequest{lpb}
}
// WithLinkStateTypeRouter sets the link state request type to router.
func (h *OSPFLinkStateRequest) WithLinkStateTypeRouter() *OSPFLinkStateRequest {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_ROUTER
return h
}
// WithLinkStateTypeNetwork sets the link state request type to network.
func (h *OSPFLinkStateRequest) WithLinkStateTypeNetwork() *OSPFLinkStateRequest {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_NETWORK
return h
}
// WithLinkStateTypeSummaryNetwork sets the link state request type to a network summary.
func (h *OSPFLinkStateRequest) WithLinkStateTypeSummaryNetwork() *OSPFLinkStateRequest {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_SUMMARY_NETWORK
return h
}
// WithLinkStateTypeSummaryASBR sets the link state request type to an AS boundary router summary.
func (h *OSPFLinkStateRequest) WithLinkStateTypeSummaryASBR() *OSPFLinkStateRequest {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_SUMMARY_ASBR
return h
}
// WithLinkStateTypeASExternal sets the link state request type to AS external.
func (h *OSPFLinkStateRequest) WithLinkStateTypeASExternal() *OSPFLinkStateRequest {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_AS_EXTERNAL
return h
}
// WithLinkStateID sets the link state ID of the link state request.
func (h *OSPFLinkStateRequest) WithLinkStateID(id string) *OSPFLinkStateRequest {
h.pb.LinkStateId = id
return h
}
// WithAdvertisingRouter sets the advertising router ID of the link state request.
func (h *OSPFLinkStateRequest) WithAdvertisingRouter(id string) *OSPFLinkStateRequest {
h.pb.AdvertisingRouter = id
return h
}
// OSPFLinkStateAdvertisementHeader is an OSPF link state advertisement header.
type OSPFLinkStateAdvertisementHeader struct {
pb *opb.OspfHeader_LinkStateAdvertisementHeader
}
// WithAge sets the age in seconds of the link state advertisement.
func (h *OSPFLinkStateAdvertisementHeader) WithAge(sec uint32) *OSPFLinkStateAdvertisementHeader {
h.pb.AgeSeconds = sec
return h
}
// WithLinkStateTypeRouter sets the link state type to router.
func (h *OSPFLinkStateAdvertisementHeader) WithLinkStateTypeRouter() *OSPFLinkStateAdvertisementHeader {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_ROUTER
return h
}
// WithLinkStateTypeNetwork sets the link state type to network.
func (h *OSPFLinkStateAdvertisementHeader) WithLinkStateTypeNetwork() *OSPFLinkStateAdvertisementHeader {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_NETWORK
return h
}
// WithLinkStateTypeSummaryNetwork sets the link state type to a network summary.
func (h *OSPFLinkStateAdvertisementHeader) WithLinkStateTypeSummaryNetwork() *OSPFLinkStateAdvertisementHeader {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_SUMMARY_NETWORK
return h
}
// WithLinkStateTypeSummaryASBR sets the link state type to an AS boundary router summary.
func (h *OSPFLinkStateAdvertisementHeader) WithLinkStateTypeSummaryASBR() *OSPFLinkStateAdvertisementHeader {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_SUMMARY_ASBR
return h
}
// WithLinkStateTypeASExternal sets the link state type to AS external.
func (h *OSPFLinkStateAdvertisementHeader) WithLinkStateTypeASExternal() *OSPFLinkStateAdvertisementHeader {
h.pb.Type = opb.OspfHeader_LINK_STATE_TYPE_AS_EXTERNAL
return h
}
// WithLinkStateID sets the ID of the link state advertisement.
func (h *OSPFLinkStateAdvertisementHeader) WithLinkStateID(id string) *OSPFLinkStateAdvertisementHeader {
h.pb.LinkStateId = id
return h
}
// WithAdvertisingRouter sets the advertising router ID of the link state advertisement.
func (h *OSPFLinkStateAdvertisementHeader) WithAdvertisingRouter(id string) *OSPFLinkStateAdvertisementHeader {
h.pb.AdvertisingRouter = id
return h
}
// WithSeq sets the sequence number of the link state advertisement.
func (h *OSPFLinkStateAdvertisementHeader) WithSeq(seq uint32) *OSPFLinkStateAdvertisementHeader {
h.pb.Seq = seq
return h
}
// OSPFLinkStateUpdate is an OSPF link state update message.
type OSPFLinkStateUpdate struct {
pb *opb.OspfHeader_LinkStateUpdate
}
// LinkStateUpdate sets the OSPFHeader message type to a link state update.
func (h *OSPFHeader) LinkStateUpdate() *OSPFLinkStateUpdate {
lpb := &opb.OspfHeader_LinkStateUpdate{}
h.pb.Type = &opb.OspfHeader_Lsu{lpb}
return &OSPFLinkStateUpdate{lpb}
}
// OSPFAdvertisement respresents an OSPF link state update advertisement.
type OSPFAdvertisement struct {
pb *opb.OspfHeader_LinkStateUpdate_Advertisement
}
// AddAdvertisement adds a link state advertisement to the link state update message.
// Defaults to setting the link state type to 'router'.
func (h *OSPFLinkStateUpdate) AddAdvertisement() *OSPFAdvertisement {
apb := &opb.OspfHeader_LinkStateUpdate_Advertisement{
Header: &opb.OspfHeader_LinkStateAdvertisementHeader{
Type: opb.OspfHeader_LINK_STATE_TYPE_ROUTER,
},
}
h.pb.Advertisements = append(h.pb.Advertisements, apb)
return &OSPFAdvertisement{apb}
}
// AdvertisementHeader returns the advertisement header for the link state advertisement.
func (h *OSPFAdvertisement) AdvertisementHeader() *OSPFLinkStateAdvertisementHeader {
return &OSPFLinkStateAdvertisementHeader{h.pb.Header}
}
// OSPFLinkStateAck is an OSPF link state ack message.
type OSPFLinkStateAck struct {
pb *opb.OspfHeader_LinkStateAck
}
// LinkStateAck sets the OSPFHeader message type to a link state ack.
func (h *OSPFHeader) LinkStateAck() *OSPFLinkStateAck {
lpb := &opb.OspfHeader_LinkStateAck{}
h.pb.Type = &opb.OspfHeader_Lsa{lpb}