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
// Copyright 2021 Vladimir Melnikov.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! This module describes NLRI data structures
use crate::util::*;
use crate::*;
#[cfg(feature = "serialization")]
use serde::ser::{SerializeSeq, SerializeStruct};
#[cfg(feature = "serialization")]
use serde::de::{self, Visitor};

use std::cmp::Ordering;
pub mod ipv4;
pub use ipv4::*;
pub mod ipv6;
pub use ipv6::*;
pub mod mvpn;
pub use mvpn::*;
pub mod vpls;
pub use vpls::*;
pub mod evpn;
pub use evpn::*;
pub mod flowspec;
pub use flowspec::*;

/// NLRI with bits length
pub trait BgpItem<T: std::marker::Sized> {
    fn extract_bits_from(bits: u8, buf: &[u8]) -> Result<(T, usize), BgpError>;
    fn set_bits_to(&self, buf: &mut [u8]) -> Result<(u8, usize), BgpError>;
    fn prefixlen(&self) -> usize;
}
/// NLRI with 2-byte length on each item
pub trait BgpItemLong<T: std::marker::Sized> {
    fn extract_from(size: usize, buf: &[u8]) -> Result<T, BgpError>;
    fn pack_to(&self, _buf: &mut [u8]) -> Result<usize, BgpError> {
        unimplemented!()
    }
}

#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum BgpAddr {
    None,
    V4(std::net::Ipv4Addr),
    V6(std::net::Ipv6Addr),
    V4RD(BgpIPv4RD),
    V6RD(BgpIPv6RD),
    L2(BgpL2),
    MVPN(BgpMVPN),
}

/// Any kind of prefix - v4 or v6
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum BgpNet {
    /// ipv4 prefix
    V4(BgpAddrV4),
    /// ipv6 prefix
    V6(BgpAddrV6),
}

impl BgpNet {
    /// New net
    pub fn new(addr: std::net::IpAddr, prefixlen: u8) -> BgpNet {
        match addr {
            std::net::IpAddr::V4(ip4) => BgpNet::V4(BgpAddrV4::new(ip4,prefixlen)),
            std::net::IpAddr::V6(ip6) => BgpNet::V6(BgpAddrV6::new(ip6,prefixlen))
        }
    }
    /// Check if given subnet is in this subnet
    /// ```
    /// use std::net::Ipv4Addr;
    /// use zettabgp::prelude::BgpAddrV4;
    ///
    /// assert!(BgpAddrV4::new(Ipv4Addr::new(192,168,0,0),16).contains(&BgpAddrV4::new(Ipv4Addr::new(192,168,0,0),24)));
    /// ```
    pub fn contains(&self, a: &BgpNet) -> bool {
        match self {
            BgpNet::V4(s4) => {
                match a {
                    BgpNet::V4(a4) => s4.contains(a4),
                    _ => false
                }
            },
            BgpNet::V6(s6) => {
                match a {
                    BgpNet::V6(a6) => s6.contains(a6),
                    _ => false
                }
            }
        }
    }
}

impl std::str::FromStr for BgpNet {
    type Err = std::net::AddrParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(ip4) = s.parse::<BgpAddrV4>() {
            return Ok(BgpNet::V4(ip4))   
        };
        Ok(BgpNet::V6(s.parse::<BgpAddrV6>()?))
    }
}
impl std::fmt::Display for BgpNet {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            BgpNet::V4(s4) => {
                s4.fmt(f)
            },
            BgpNet::V6(s6) => {
                s6.fmt(f)
            }
        }
    }
}
#[cfg(feature = "serialization")]
impl serde::Serialize for BgpNet {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.to_string().as_str())
    }
}
#[cfg(feature = "serialization")]
struct BgpNetVisitor;

#[cfg(feature = "serialization")]
impl<'de> Visitor<'de> for BgpNetVisitor {
    type Value = BgpNet;
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("a ipv4/ipv6 prefix")
    }
    fn visit_str<E>(self, value: &str) -> Result<BgpNet, E>
                    where
                        E: serde::de::Error,
                    {
        value.parse::<BgpNet>().map_err(de::Error::custom)
    }
}
#[cfg(feature = "serialization")]
impl<'de> serde::Deserialize<'de> for BgpNet {
    fn deserialize<D>(deserializer: D) -> Result<BgpNet, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_string(BgpNetVisitor)
    }
}

/// Represents variance of NLRI collections
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum BgpAddrs {
    None,
    IPV4U(Vec<BgpAddrV4>),
    IPV4M(Vec<BgpAddrV4>),
    IPV4LU(Vec<Labeled<BgpAddrV4>>),
    VPNV4U(Vec<Labeled<WithRd<BgpAddrV4>>>),
    VPNV4M(Vec<Labeled<WithRd<BgpAddrV4>>>),
    IPV6U(Vec<BgpAddrV6>),
    IPV6M(Vec<BgpAddrV6>),
    IPV6LU(Vec<Labeled<BgpAddrV6>>),
    VPNV6U(Vec<Labeled<WithRd<BgpAddrV6>>>),
    VPNV6M(Vec<Labeled<WithRd<BgpAddrV6>>>),
    L2VPLS(Vec<BgpAddrL2>),
    MVPN(Vec<BgpMVPN>),
    EVPN(Vec<BgpEVPN>),
    FS4U(Vec<BgpFlowSpec<BgpAddrV4>>),
    FS6U(Vec<BgpFlowSpec<FS6>>),
    FSV4U(Vec<BgpFlowSpec<FSV4U>>),
}

pub fn decode_bgpitem_from<T: BgpItem<T>>(buf: &[u8]) -> Result<(T, usize), BgpError> {
    let bits = buf[0];
    let r = T::extract_bits_from(bits, &buf[1..])?;
    Ok((r.0, r.1 + 1))
}
pub fn decode_bgpitems_from<T: BgpItem<T>>(buf: &[u8]) -> Result<(Vec<T>, usize), BgpError> {
    let mut v = Vec::<T>::new();
    let mut curpos = 0;
    while curpos < buf.len() {
        let nlri = decode_bgpitem_from(&buf[curpos..])?;
        v.push(nlri.0);
        curpos += nlri.1;
    }
    return Ok((v, curpos));
}
pub fn encode_bgpitems_to<T: BgpItem<T>>(v: &Vec<T>, buf: &mut [u8]) -> Result<usize, BgpError> {
    let mut curpos = 0;
    for i in v.iter() {
        let r = i.set_bits_to(&mut buf[curpos + 1..])?;
        buf[curpos] = r.0;
        curpos += r.1 + 1;
    }
    return Ok(curpos);
}
pub fn decode_bgpaddritems_from<T: BgpAddrItem<T>>(
    peermode: BgpTransportMode,
    buf: &[u8],
) -> Result<(Vec<T>, usize), BgpError> {
    let mut v = Vec::<T>::new();
    let mut curpos = 0;
    while curpos < buf.len() {
        let nlri = T::decode_from(peermode, &buf[curpos..])?;
        v.push(nlri.0);
        curpos += nlri.1;
    }
    return Ok((v, curpos));
}
pub fn encode_bgpaddritems_to<T: BgpAddrItem<T>>(
    v: &Vec<T>,
    peermode: BgpTransportMode,
    buf: &mut [u8],
) -> Result<usize, BgpError> {
    let mut curpos = 0;
    for i in v {
        curpos += i.encode_to(peermode, &mut buf[curpos..])?;
    }
    Ok(curpos)
}
pub fn decode_long_bgpitems_from<T: BgpItemLong<T>>(
    buf: &[u8],
) -> Result<(Vec<T>, usize), BgpError> {
    let mut v = Vec::<T>::new();
    let mut curpos = 0;
    while curpos < buf.len() {
        let itemlen = getn_u16(&buf[curpos..(curpos + 2)]) as usize;
        v.push(T::extract_from(
            itemlen,
            &buf[curpos + 2..(curpos + itemlen + 2)],
        )?);
        curpos += itemlen + 2;
    }
    return Ok((v, curpos));
}
pub fn encode_long_bgpitems_to<T: BgpItemLong<T>>(
    v: &Vec<T>,
    buf: &mut [u8],
) -> Result<usize, BgpError> {
    let mut curpos = 0;
    for i in v {
        let sz = i.pack_to(&mut buf[curpos + 2..])?;
        setn_u16(sz as u16, &mut buf[curpos..curpos + 2]);
        curpos += sz;
    }
    Ok(curpos)
}
/// BGP VPN route distinguisher
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct BgpRD {
    /// high-order part
    pub rdh: u32,
    /// low-order part
    pub rdl: u32,
}
impl BgpRD {
    /// Creates a new RD from a pair of numbers
    pub fn new(h: u32, l: u32) -> BgpRD {
        BgpRD { rdh: h, rdl: l }
    }
    /// Checks that RD is zero
    pub fn is_zero(&self) -> bool {
        (self.rdh == 0) && (self.rdl == 0)
    }
    /// decodes RD from bytes
    pub fn decode_rd_from(buf: &[u8]) -> Result<(BgpRD, usize), BgpError> {
        if buf.len() >= 8 {
            Ok((
                BgpRD {
                    rdh: getn_u32(&buf[0..4]),
                    rdl: getn_u32(&buf[4..8]),
                },
                8,
            ))
        } else {
            Err(BgpError::static_str("Invalid RD buffer len"))
        }
    }
    /// encodes RD into the buffer
    pub fn encode_rd_to(&self, buf: &mut [u8]) -> Result<usize, BgpError> {
        setn_u32(self.rdh, buf);
        setn_u32(self.rdl, &mut buf[4..8]);
        Ok(8)
    }
}
impl std::str::FromStr for BgpRD {
    type Err = std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() < 2 {
            Ok(BgpRD {
                rdh: parts[0].parse::<u32>()?,
                rdl: 0,
            })
        } else {
            Ok(BgpRD {
                rdh: parts[0].parse::<u32>()?,
                rdl: parts[1].parse::<u32>()?,
            })
        }
    }
}
impl BgpAddrItem<BgpRD> for BgpRD {
    fn decode_from(_mode: BgpTransportMode, buf: &[u8]) -> Result<(BgpRD, usize), BgpError> {
        BgpRD::decode_rd_from(buf)
    }
    fn encode_to(&self, _mode: BgpTransportMode, buf: &mut [u8]) -> Result<usize, BgpError> {
        self.encode_rd_to(buf)
    }
}
impl std::fmt::Display for BgpRD {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if (self.rdh >> 16) == 1 {
            write!(
                f,
                "{}.{}.{}.{}:{}",
                (self.rdh >> 8) & 0xff,
                self.rdh & 0xff,
                (self.rdl >> 24) & 0xff,
                (self.rdl >> 16) & 0xff,
                self.rdl & 0xffff
            )
        } else {
            write!(f, "{}:{}", self.rdh, self.rdl)
        }
    }
}

impl std::fmt::Display for BgpAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            BgpAddr::None => write!(f, "<>"),
            BgpAddr::V4(s) => write!(f, "{}", s),
            BgpAddr::V6(s) => write!(f, "{}", s),
            BgpAddr::V4RD(s) => write!(f, "{}", s),
            BgpAddr::V6RD(s) => write!(f, "{}", s),
            BgpAddr::L2(s) => write!(f, "{}", s),
            BgpAddr::MVPN(s) => write!(f, "{}", s),
        }
    }
}
/// MPLS labels as NLRI component
#[derive(Debug, Clone, Eq, Ord)]
pub struct MplsLabels {
    pub labels: Vec<u32>,
}
impl MplsLabels {
    /// creates a new empty label stack
    pub fn new() -> MplsLabels {
        MplsLabels { labels: Vec::new() }
    }
    /// creates a new label stack from vector
    pub fn fromvec(lbls: Vec<u32>) -> MplsLabels {
        MplsLabels { labels: lbls }
    }
}
/*
 labels does not treated as hash part
*/
impl std::hash::Hash for MplsLabels {
    fn hash<H: std::hash::Hasher>(&self, _state: &mut H) {
        //self.prefix.hash(state)
    }
}
/*
 labels does not produce unique FEC
*/
impl PartialOrd for MplsLabels {
    fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
        None
    }
}
impl PartialEq for MplsLabels {
    fn eq(&self, _other: &Self) -> bool {
        true
    }
}
impl std::fmt::Display for MplsLabels {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:?}", self.labels)
    }
}

impl BgpItem<MplsLabels> for MplsLabels {
    fn extract_bits_from(bits: u8, buf: &[u8]) -> Result<(MplsLabels, usize), BgpError> {
        let mut lbls = Vec::<u32>::new();
        let mut curpos: usize = 0;
        let mut leftbits = bits;
        while leftbits > 0 {
            let labelval = (buf[curpos] as u32) << 12
                | (buf[curpos + 1] as u32) << 4
                | (buf[curpos + 2] as u32) >> 4;
            lbls.push(labelval);
            curpos += 3;
            leftbits -= 24;
            // special values ends stack
            match labelval {
                524288 => break, //withdraw
                0 => break,      //ExplicitNull
                2 => break,      //ExplicitNull6
                3 => break,      //ImplicitNull
                _ => {}
            }
            if (buf[curpos - 1] & 1) != 0 {
                break;
            }
        }
        Ok((MplsLabels { labels: lbls }, curpos))
    }
    fn set_bits_to(&self, buf: &mut [u8]) -> Result<(u8, usize), BgpError> {
        if self.labels.len() == 0 {
            return Ok((0, 0));
        }
        let mut curpos: usize = 0;
        for l in self.labels.iter() {
            buf[curpos] = (l >> 12) as u8;
            buf[curpos + 1] = ((l >> 4) & 0xff) as u8;
            buf[curpos + 2] = ((l << 4) & 0xff) as u8;
            curpos += 3;
        }
        buf[curpos - 1] |= 1;
        Ok(((curpos * 8) as u8, curpos))
    }
    fn prefixlen(&self) -> usize {
        self.labels.len() * 24
    }
}
/// Labeled NLRI
#[derive(Debug, Clone, Hash, Eq)]
pub struct Labeled<T: BgpItem<T>> {
    /// underlying NLRI
    pub prefix: T,
    /// label stack
    pub labels: MplsLabels,
}
impl<T: BgpItem<T>> Labeled<T> {
    /// creates a new labeled NLRI
    pub fn new(lb: MplsLabels, inner: T) -> Labeled<T> {
        Labeled {
            labels: lb,
            prefix: inner,
        }
    }
    /// creates a new labeled NLRI with no labels
    pub fn new_nl(inner: T) -> Labeled<T> {
        Labeled {
            labels: MplsLabels::new(),
            prefix: inner,
        }
    }
}
impl<T: BgpItem<T> + PartialEq> PartialEq for Labeled<T> {
    fn eq(&self, other: &Self) -> bool {
        self.prefix.eq(&other.prefix)
    }
}
impl<T: BgpItem<T> + PartialOrd> PartialOrd for Labeled<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.prefix.partial_cmp(&other.prefix)
    }
}
impl<T: BgpItem<T> + Ord> Ord for Labeled<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.prefix.cmp(&other.prefix)
    }
}
impl<T: BgpItem<T>> BgpItem<Labeled<T>> for Labeled<T> {
    fn extract_bits_from(bits: u8, buf: &[u8]) -> Result<(Labeled<T>, usize), BgpError> {
        let l = MplsLabels::extract_bits_from(bits, &buf)?;
        let p = T::extract_bits_from(bits - ((l.1 * 8) as u8), &buf[l.1..])?;
        Ok((
            Labeled {
                labels: l.0,
                prefix: p.0,
            },
            l.1 + p.1,
        ))
    }
    fn set_bits_to(&self, buf: &mut [u8]) -> Result<(u8, usize), BgpError> {
        let lblp = self.labels.set_bits_to(buf)?;
        let pfxp = self.prefix.set_bits_to(&mut buf[lblp.1..])?;
        Ok((lblp.0 + pfxp.0, lblp.1 + pfxp.1))
    }
    fn prefixlen(&self) -> usize {
        self.labels.prefixlen() + self.prefix.prefixlen()
    }
}
impl<T: BgpItem<T> + std::fmt::Display> std::fmt::Display for Labeled<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.labels.labels.len() < 1 {
            write!(f, "{}", self.prefix)
        } else {
            write!(f, "<l:{}> {}", self.labels, self.prefix)
        }
    }
}
/// NRI with Route distinguisher
#[derive(Clone, Hash, PartialEq, Eq, Ord)]
pub struct WithRd<T: BgpItem<T>> {
    pub prefix: T,
    pub rd: BgpRD,
}
impl<T: BgpItem<T>> WithRd<T> {
    pub fn new(rd: BgpRD, inner: T) -> WithRd<T> {
        WithRd {
            rd: rd,
            prefix: inner,
        }
    }
}
impl<T: BgpItem<T> + PartialOrd> PartialOrd for WithRd<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match self.prefix.partial_cmp(&other.prefix) {
            None => self.rd.partial_cmp(&other.rd),
            Some(pc) => match pc {
                Ordering::Less => Some(Ordering::Less),
                Ordering::Greater => Some(Ordering::Greater),
                Ordering::Equal => self.rd.partial_cmp(&other.rd),
            },
        }
    }
}

impl<T: BgpItem<T>> BgpItem<WithRd<T>> for WithRd<T> {
    fn extract_bits_from(bits: u8, buf: &[u8]) -> Result<(WithRd<T>, usize), BgpError> {
        if buf.len() < 8 {
            return Err(BgpError::static_str("Buffer size too small for RD"));
        }
        let r = BgpRD::decode_from(BgpTransportMode::IPv4, &buf[0..8])?;
        let p = T::extract_bits_from(bits - ((r.1 * 8) as u8), &buf[r.1..])?;
        Ok((
            WithRd {
                rd: r.0,
                prefix: p.0,
            },
            r.1 + p.1,
        ))
    }
    fn set_bits_to(&self, buf: &mut [u8]) -> Result<(u8, usize), BgpError> {
        let rdpos = self.rd.encode_to(BgpTransportMode::IPv4, buf)?;
        let pfxp = self.prefix.set_bits_to(&mut buf[rdpos..])?;
        Ok(((((rdpos * 8) as u8) + pfxp.0), rdpos + pfxp.1))
    }
    fn prefixlen(&self) -> usize {
        64 + self.prefix.prefixlen()
    }
}
impl<T: BgpItem<T> + std::fmt::Debug> std::fmt::Debug for WithRd<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WithRd")
            .field("rd", &self.rd)
            .field("prefix", &self.prefix)
            .finish()
    }
}
impl<T: BgpItem<T> + std::fmt::Display> std::fmt::Display for WithRd<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.rd.is_zero() {
            write!(f, "{}", self.prefix)
        } else {
            write!(f, "<rd:{}> {}", self.rd, self.prefix)
        }
    }
}

impl BgpAddrs {
    /// creates a new empty BgpAddrs
    pub fn new() -> BgpAddrs {
        BgpAddrs::None
    }
    /// returns collection length
    pub fn len(&self) -> usize {
        match self {
            BgpAddrs::None => 0,
            BgpAddrs::IPV4U(v) => v.len(),
            BgpAddrs::IPV4M(v) => v.len(),
            BgpAddrs::IPV4LU(v) => v.len(),
            BgpAddrs::VPNV4U(v) => v.len(),
            BgpAddrs::VPNV4M(v) => v.len(),
            BgpAddrs::IPV6U(v) => v.len(),
            BgpAddrs::IPV6M(v) => v.len(),
            BgpAddrs::IPV6LU(v) => v.len(),
            BgpAddrs::VPNV6U(v) => v.len(),
            BgpAddrs::VPNV6M(v) => v.len(),
            BgpAddrs::L2VPLS(v) => v.len(),
            BgpAddrs::MVPN(v) => v.len(),
            BgpAddrs::EVPN(v) => v.len(),
            BgpAddrs::FS4U(v) => v.len(),
            BgpAddrs::FS6U(v) => v.len(),
            BgpAddrs::FSV4U(v) => v.len(),
        }
    }
    /// returns BGP afi+safi codes
    pub fn get_afi_safi(&self) -> (u16, u8) {
        match &self {
            BgpAddrs::None => (0, 0),
            BgpAddrs::IPV4U(_) => (1, 1),
            BgpAddrs::IPV4M(_) => (1, 2),
            BgpAddrs::IPV4LU(_) => (1, 4),
            BgpAddrs::MVPN(_) => (1, 5),
            BgpAddrs::VPNV4U(_) => (1, 128),
            BgpAddrs::VPNV4M(_) => (1, 129),
            BgpAddrs::FS4U(_) => (1, 133),
            BgpAddrs::FSV4U(_) => (1, 134),
            BgpAddrs::FS6U(_) => (2, 133),
            BgpAddrs::IPV6U(_) => (2, 1),
            BgpAddrs::IPV6M(_) => (2, 2),
            BgpAddrs::IPV6LU(_) => (2, 4),
            BgpAddrs::VPNV6U(_) => (2, 128),
            BgpAddrs::VPNV6M(_) => (2, 129),
            BgpAddrs::L2VPLS(_) => (25, 65),
            BgpAddrs::EVPN(_) => (25, 70),
        }
    }
    pub fn decode_from(
        peer: &BgpSessionParams,
        afi: u16,
        safi: u8,
        buf: &[u8],
    ) -> Result<(BgpAddrs, usize), BgpError> {
        match afi {
            1 => {
                //ipv4
                match safi {
                    1 => {
                        //unicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::IPV4U(r.0), r.1));
                    }
                    2 => {
                        //multicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::IPV4M(r.0), r.1));
                    }
                    4 => {
                        //labeled unicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::IPV4LU(r.0), r.1));
                    }
                    5 => {
                        //mvpn v4
                        let r = match decode_bgpaddritems_from(BgpTransportMode::IPv4, buf) {
                            Ok(q) => q,
                            Err(e) => {
                                eprintln!("MVPN decode error: {:?}\nbuf:{:?}", e, buf);
                                return Err(e);
                            }
                        };
                        return Ok((BgpAddrs::MVPN(r.0), r.1));
                    }
                    128 => {
                        //vpnv4 unicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::VPNV4U(r.0), r.1));
                    }
                    129 => {
                        //vpnv4 multicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::VPNV4M(r.0), r.1));
                    }
                    133 => {
                        //ip4u flowspec
                        let r = decode_bgpaddritems_from(peer.peer_mode, buf)?;
                        return Ok((BgpAddrs::FS4U(r.0), r.1));
                    }
                    134 => {
                        //vpn4u flowspec
                        let r = decode_bgpaddritems_from(peer.peer_mode, buf)?;
                        return Ok((BgpAddrs::FSV4U(r.0), r.1));
                    }
                    n => {
                        return Err(BgpError::from_string(format!(
                            "Unknown safi for ipv4 {:?}",
                            n
                        )))
                    }
                }
            }
            2 => {
                //ipv6
                match safi {
                    1 => {
                        //unicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::IPV6U(r.0), r.1));
                    }
                    2 => {
                        //multicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::IPV6M(r.0), r.1));
                    }
                    4 => {
                        //labeled unicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::IPV6LU(r.0), r.1));
                    }
                    128 => {
                        //vpnv6 unicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::VPNV6U(r.0), r.1));
                    }
                    129 => {
                        //vpnv6 multicast
                        let r = decode_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::VPNV6M(r.0), r.1));
                    }
                    133 => {
                        //ip6u flowspec
                        let r = decode_bgpaddritems_from(peer.peer_mode, buf)?;
                        return Ok((BgpAddrs::FS6U(r.0), r.1));
                    }
                    n => {
                        return Err(BgpError::from_string(format!(
                            "Unknown safi for ipv6 {:?}",
                            n
                        )))
                    }
                }
            }
            25 => {
                //l2
                match safi {
                    65 => {
                        //vpls
                        let r = decode_long_bgpitems_from(buf)?;
                        return Ok((BgpAddrs::L2VPLS(r.0), r.1));
                    }
                    70 => {
                        //evpn
                        let r = decode_bgpaddritems_from(peer.peer_mode, buf)?;
                        return Ok((BgpAddrs::EVPN(r.0), r.1));
                    }
                    n => {
                        return Err(BgpError::from_string(format!(
                            "Unknown safi for l2 {:?}",
                            n
                        )))
                    }
                }
            }
            n => return Err(BgpError::from_string(format!("Unknown afi {:?}", n))),
        }
    }
    pub fn encode_to(&self, peer: &BgpSessionParams, buf: &mut [u8]) -> Result<usize, BgpError> {
        match &self {
            BgpAddrs::None => Ok(0),
            BgpAddrs::IPV4U(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::IPV4M(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::IPV4LU(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::MVPN(v) => encode_bgpaddritems_to(v, peer.peer_mode, buf),
            BgpAddrs::VPNV4U(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::VPNV4M(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::FS4U(v) => encode_bgpaddritems_to(v, peer.peer_mode, buf),
            BgpAddrs::FSV4U(v) => encode_bgpaddritems_to(v, peer.peer_mode, buf),
            BgpAddrs::FS6U(v) => encode_bgpaddritems_to(v, peer.peer_mode, buf),
            BgpAddrs::IPV6U(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::IPV6M(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::IPV6LU(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::VPNV6U(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::VPNV6M(v) => encode_bgpitems_to(v, buf),
            BgpAddrs::L2VPLS(v) => encode_long_bgpitems_to(v, buf),
            BgpAddrs::EVPN(v) => encode_bgpaddritems_to(v, peer.peer_mode, buf),
        }
    }
}
impl std::fmt::Display for BgpAddrs {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "BgpAddrs {:?}", self)
    }
}

#[cfg(feature = "serialization")]
impl serde::Serialize for MplsLabels {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_seq(Some(self.labels.len()))?;
        for l in self.labels.iter() {
            state.serialize_element(&l)?;
        }
        state.end()
    }
}
#[cfg(feature = "serialization")]
impl serde::Serialize for BgpRD {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("RD", 2)?;
        state.serialize_field("rdh", &self.rdh)?;
        state.serialize_field("rdl", &self.rdl)?;
        state.end()
    }
}

#[cfg(feature = "serialization")]
impl<T: BgpItem<T> + std::fmt::Debug + serde::Serialize> serde::Serialize for WithRd<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("WithRd", 2)?;
        state.serialize_field("rd", &self.rd)?;
        state.serialize_field("prefix", &self.prefix)?;
        state.end()
    }
}

#[cfg(feature = "serialization")]
impl<T: BgpItem<T> + std::fmt::Debug + serde::Serialize> serde::Serialize for Labeled<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("Labeled", 2)?;
        state.serialize_field("labels", &self.labels)?;
        state.serialize_field("prefix", &self.prefix)?;
        state.end()
    }
}
#[cfg(feature = "serialization")]
impl serde::Serialize for BgpAddr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(format!("{}", self).as_str())
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cmp_ipv4() {
        assert!(std::net::Ipv4Addr::new(10, 6, 7, 8) == std::net::Ipv4Addr::new(10, 6, 7, 8));
        assert!(std::net::Ipv4Addr::new(10, 0, 0, 1) < std::net::Ipv4Addr::new(11, 0, 0, 1));
    }
    #[test]
    fn test_cmp_rd() {
        assert!(BgpRD::new(1, 1) == BgpRD::new(1, 1));
        assert!(BgpRD::new(1, 1) < BgpRD::new(1, 2));
        assert!(BgpRD::new(2, 1) > BgpRD::new(1, 2));
    }
    #[test]
    fn test_cmp_bgpv4() {
        assert!(
            BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
                == BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
        );
        assert!(
            BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 30)
                < BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
        );
        assert!(
            BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 30)
                > BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 6, 0), 32)
        );
    }
    #[test]
    fn test_cmp_bgpv4rd() {
        assert!(
            WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
            ) == WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
            )
        );
        assert!(
            WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
            ) != WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 2),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
            )
        );
        assert!(
            WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 30)
            ) < WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
            )
        );
        assert!(
            WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 30)
            ) > WithRd::<BgpAddrV4>::new(
                BgpRD::new(1, 1),
                BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 6, 0), 32)
            )
        );
    }
    #[test]
    fn test_cmp_bgpv4lb() {
        assert!(
            Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32))
                == Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(
                    std::net::Ipv4Addr::new(10, 6, 7, 0),
                    32
                ))
        );
        assert!(
            Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 30))
                < Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(
                    std::net::Ipv4Addr::new(10, 6, 7, 0),
                    32
                ))
        );
        assert!(
            Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 30))
                > Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(
                    std::net::Ipv4Addr::new(10, 6, 6, 0),
                    32
                ))
        );
        assert!(
            Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32))
                == Labeled::<BgpAddrV4>::new(
                    MplsLabels::fromvec(vec![1, 2, 3, 4]),
                    BgpAddrV4::new(std::net::Ipv4Addr::new(10, 6, 7, 0), 32)
                )
        );
        assert!(
            Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(std::net::Ipv4Addr::new(10, 0, 0, 1), 32))
                < Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(
                    std::net::Ipv4Addr::new(11, 0, 0, 1),
                    32
                ))
        );
        assert!(
            Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(std::net::Ipv4Addr::new(10, 0, 0, 0), 24))
                < Labeled::<BgpAddrV4>::new_nl(BgpAddrV4::new(
                    std::net::Ipv4Addr::new(11, 0, 0, 1),
                    32
                ))
        );
    }
}