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
use core::convert::TryFrom;

use bytes::{Buf, BytesMut};

use super::addr::field_port;
use super::{field, Cmd, Decoder, Encodable, Encoder, Error, HasAddr, Rep, Result, SocksAddr, Ver};

// Requests
//
// Once the method-dependent subnegotiation has completed, the client
// sends the request details.  If the negotiated method includes
// encapsulation for purposes of integrity checking and/or
// confidentiality, these requests MUST be encapsulated in the method-
// dependent encapsulation.
//
// The SOCKS request is formed as follows:
//
// +----+-----+-------+------+----------+----------+
// |VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
// +----+-----+-------+------+----------+----------+
// | 1  |  1  | X'00' |  1   | Variable |    2     |
// +----+-----+-------+------+----------+----------+
//
// Where:
//
// o  VER    protocol version: X'05'
// o  CMD
//    o  CONNECT X'01'
//    o  BIND X'02'
//    o  UDP ASSOCIATE X'03'
// o  RSV    RESERVED
// o  ATYP   address type of following address
//    o  IP V4 address: X'01'
//    o  DOMAINNAME: X'03'
//    o  IP V6 address: X'04'
// o  DST.ADDR       desired destination address
// o  DST.PORT desired destination port in network octet
// order
//
// The SOCKS server will typically evaluate the request based on source
// and destination addresses, and return one or more reply messages, as
// appropriate for the request type.
//
//
// Replies
//
// The SOCKS request information is sent by the client as soon as it has
// established a connection to the SOCKS server, and completed the
// authentication negotiations.  The server evaluates the request, and
// returns a reply formed as follows:
//
// +----+-----+-------+------+----------+----------+
// |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
// +----+-----+-------+------+----------+----------+
// | 1  |  1  | X'00' |  1   | Variable |    2     |
// +----+-----+-------+------+----------+----------+
//
// CONNECT
//
// In the reply to a CONNECT, BND.PORT contains the port number that the
// server assigned to connect to the target host, while BND.ADDR
// contains the associated IP address.  The supplied BND.ADDR is often
// different from the IP address that the client uses to reach the SOCKS
// server, since such servers are often multi-homed.  It is expected
// that the SOCKS server will use DST.ADDR and DST.PORT, and the
// client-side source address and port in evaluating the CONNECT
// request.
//
// BIND
//
// The BIND request is used in protocols which require the client to
// accept connections from the server.  FTP is a well-known example,
// which uses the primary client-to-server connection for commands and
// status reports, but may use a server-to-client connection for
// transferring data on demand (e.g. LS, GET, PUT).
//
// It is expected that the client side of an application protocol will
// use the BIND request only to establish secondary connections after a
// primary connection is established using CONNECT.  In is expected that
// a SOCKS server will use DST.ADDR and DST.PORT in evaluating the BIND
// request.
//
// Two replies are sent from the SOCKS server to the client during a
// BIND operation.  The first is sent after the server creates and binds
// a new socket.  The BND.PORT field contains the port number that the
// SOCKS server assigned to listen for an incoming connection.  The
// BND.ADDR field contains the associated IP address.  The client will
// typically use these pieces of information to notify (via the primary
// or control connection) the application server of the rendezvous
// address.  The second reply occurs only after the anticipated incoming
// connection succeeds or fails.
//
// In the second reply, the BND.PORT and BND.ADDR fields contain the
// address and port number of the connecting host.
//
// UDP ASSOCIATE
//
// The UDP ASSOCIATE request is used to establish an association within
// the UDP relay process to handle UDP datagrams.  The DST.ADDR and
// DST.PORT fields contain the address and port that the client expects
// to use to send UDP datagrams on for the association.  The server MAY
// use this information to limit access to the association.  If the
// client is not in possesion of the information at the time of the UDP
// ASSOCIATE, the client MUST use a port number and address of all
// zeros.
//
// A UDP association terminates when the TCP connection that the UDP
// ASSOCIATE request arrived on terminates.
//
// In the reply to a UDP ASSOCIATE request, the BND.PORT and BND.ADDR
// fields indicate the port number/address where the client MUST send
// UDP request messages to be relayed.
//
// Reply Processing
//
// When a reply (REP value other than X'00') indicates a failure, the
// SOCKS server MUST terminate the TCP connection shortly after sending
// the reply.  This must be no more than 10 seconds after detecting the
// condition that caused a failure.
//
// If the reply code (REP value of X'00') indicates a success, and the
// request was either a BIND or a CONNECT, the client may now start
// passing data.  If the selected authentication method supports
// encapsulation for the purposes of integrity, authentication and/or
// confidentiality, the data are encapsulated using the method-dependent
// encapsulation.  Similarly, when data arrives at the SOCKS server for
// the client, the server MUST encapsulate the data as appropriate for
// the authentication method in use.
//
#[derive(Debug, PartialEq, Clone)]
pub struct Packet<T: AsRef<[u8]>>(HasAddr<T>);

impl<T: AsRef<[u8]>> Packet<T> {
    /// Imbue a raw octet buffer with RFC1928 request packet structure.
    pub fn new_unchecked(buffer: T) -> Packet<T> {
        Packet(HasAddr::new_unchecked(field::ATYP, buffer))
    }

    /// Shorthand for a combination of [new_unchecked] and [check_len].
    ///
    /// [new_unchecked]: #method.new_unchecked
    /// [check_len]: #method.check_len
    pub fn new_checked(buffer: T) -> Result<Packet<T>> {
        let packet = Self::new_unchecked(buffer);
        packet.check_len()?;
        Ok(packet)
    }

    #[inline]
    fn buffer_ref(&self) -> &[u8] {
        self.0.buffer.as_ref()
    }

    /// Ensure that no accessor method will panic if called.
    /// Returns `Err(Error::Truncated)` if the buffer is too short.
    ///
    /// The result of this check is invalidated by calling [set_socks_addr]
    ///
    /// [set_methods]: #method.set_socks_addr
    #[inline]
    pub fn check_len(&self) -> Result<()> {
        self.0.check_addr_len()?;
        if self.buffer_ref().len() > self.total_len() {
            Err(Error::Malformed)
        } else {
            Ok(())
        }
    }

    /// Return the length (unchecked).
    #[inline]
    pub fn total_len(&self) -> usize {
        self.0.len_to_port()
    }

    /// Return the version field.
    #[inline]
    pub fn version(&self) -> u8 {
        let data = self.buffer_ref();
        data[field::VER]
    }

    /// Return the cmd of request or rep of reply.
    #[inline]
    pub fn cmd_or_rep(&self) -> u8 {
        let data = self.buffer_ref();
        data[field::CMD_OR_REP]
    }

    /// Return the atyp.
    #[inline]
    pub fn atyp(&self) -> u8 {
        self.0.atyp()
    }

    /// Return the dst port of request or bnd port of reply (unchecked).
    #[inline]
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    pub fn take_buffer(self) -> T {
        self.0.take_buffer()
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Packet<&'a T> {
    /// Return a pointer to the addr (unchecked).
    #[inline]
    pub fn addr(&self) -> &'a [u8] {
        self.0.addr()
    }

    /// Return a pointer to the socks addr (atyp, addr, and port) (unchecked).
    #[inline]
    pub fn socks_addr(&self) -> &'a [u8] {
        self.0.socks_addr()
    }
}

impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
    #[inline]
    fn buffer_mut(&mut self) -> &mut [u8] {
        self.0.buffer.as_mut()
    }

    /// Set the version field.
    #[inline]
    pub fn set_version(&mut self, value: u8) {
        let data = self.buffer_mut();
        data[field::VER] = value;
    }

    /// Set the cmd or rep.
    #[inline]
    pub fn set_cmd_or_rep(&mut self, value: u8) {
        let data = self.buffer_mut();
        data[field::CMD_OR_REP] = value;
    }

    /// Set the atyp.
    #[inline]
    pub fn set_atyp(&mut self, value: u8) {
        self.0.set_atyp(value)
    }

    /// Set the addr (unchecked).
    #[inline]
    pub fn set_addr(&mut self, value: &[u8]) {
        self.0.set_addr(value)
    }

    /// Set the port (unchecked).
    #[inline]
    pub fn set_port(&mut self, value: u16) {
        self.0.set_port(value)
    }

    /// Set the socks addr (atyp, addr, and port) (unchecked).
    #[inline]
    pub fn set_socks_addr(&mut self, value: &[u8]) {
        self.0.set_socks_addr(value)
    }

    /// Return a mutable pointer to the addr (unchecked).
    #[inline]
    pub fn addr_mut(&mut self) -> &mut [u8] {
        self.0.addr_mut()
    }

    /// Return a mutable pointer to the socks addr (atyp, addr, and port) (unchecked).
    #[inline]
    pub fn socks_addr_mut(&mut self) -> &mut [u8] {
        self.0.socks_addr_mut()
    }
}

impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.buffer_ref()
    }
}

/// A high-level representation of a Cmd packet.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CmdRepr {
    pub ver: Ver,
    pub cmd: Cmd,
    pub addr: SocksAddr,
}

impl CmdRepr {
    /// Parse a packet and return a high-level representation.
    pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&T>) -> Result<CmdRepr> {
        packet.check_len()?;

        // Version 5 is expected.
        if packet.version() != Ver::SOCKS5 as u8 {
            return Err(Error::Malformed);
        }
        if packet.as_ref()[field::RSV] != 0 {
            return Err(Error::Malformed);
        }

        Ok(CmdRepr {
            ver: Ver::SOCKS5,
            cmd: Cmd::try_from(packet.cmd_or_rep())?,
            addr: SocksAddr::try_from(packet.socks_addr())?,
        })
    }

    /// Return the length of that will be emitted from this high-level representation.
    pub fn buffer_len(&self) -> usize {
        let addr_len = self.addr.addr_len();
        field_port(field::ADDR_PORT.start, addr_len).end
    }

    /// Emit a high-level representation into a packet.
    pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, packet: &mut Packet<T>) {
        packet.set_version(Ver::SOCKS5.into());
        packet.set_cmd_or_rep(self.cmd as u8);
        packet.set_socks_addr(&self.addr.to_vec());
    }
}

impl Decoder<CmdRepr> for CmdRepr {
    fn decode(src: &mut BytesMut) -> Result<Option<Self>> {
        let pkt = Packet::new_unchecked(src.as_ref());
        match CmdRepr::parse(&pkt) {
            Ok(repr) => {
                src.advance(repr.buffer_len());
                Ok(Some(repr))
            }
            Err(Error::Truncated) => Ok(None),
            Err(err) => Err(err),
        }
    }
}

impl Encodable for CmdRepr {
    fn encode_into(&self, dst: &mut BytesMut) {
        if dst.len() < self.buffer_len() {
            dst.resize(self.buffer_len(), 0);
        }
        let mut pkt = Packet::new_unchecked(dst);
        self.emit(&mut pkt);
    }
}

impl Encoder<CmdRepr> for CmdRepr {
    fn encode(item: &CmdRepr, dst: &mut BytesMut) {
        item.encode_into(dst);
    }
}

/// A high-level representation of a Rep packet.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RepRepr {
    pub ver: Ver,
    pub rep: Rep,
    pub addr: SocksAddr,
}

impl RepRepr {
    /// Parse a packet and return a high-level representation.
    pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&T>) -> Result<RepRepr> {
        packet.check_len()?;

        // Version 5 is expected.
        if packet.version() != Ver::SOCKS5 as u8 {
            return Err(Error::Malformed);
        }
        if packet.as_ref()[field::RSV] != 0 {
            return Err(Error::Malformed);
        }

        Ok(RepRepr {
            ver: Ver::SOCKS5,
            rep: Rep::try_from(packet.cmd_or_rep())?,
            addr: SocksAddr::try_from(packet.socks_addr())?,
        })
    }

    /// Return the length of that will be emitted from this high-level representation.
    pub fn buffer_len(&self) -> usize {
        let addr_len = self.addr.addr_len();
        field_port(field::ADDR_PORT.start, addr_len).end
    }

    /// Emit a high-level representation into a packet.
    pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, packet: &mut Packet<T>) {
        packet.set_version(Ver::SOCKS5.into());
        packet.set_cmd_or_rep(self.rep as u8);
        packet.set_socks_addr(&self.addr.to_vec());
    }
}

impl Decoder<RepRepr> for RepRepr {
    fn decode(src: &mut BytesMut) -> Result<Option<Self>> {
        let pkt = Packet::new_unchecked(src.as_ref());
        match RepRepr::parse(&pkt) {
            Ok(repr) => {
                src.advance(repr.buffer_len());
                Ok(Some(repr))
            }
            Err(Error::Truncated) => Ok(None),
            Err(err) => Err(err),
        }
    }
}

impl Encodable for RepRepr {
    fn encode_into(&self, dst: &mut BytesMut) {
        if dst.len() < self.buffer_len() {
            dst.resize(self.buffer_len(), 0);
        }
        let mut pkt = Packet::new_unchecked(dst);
        self.emit(&mut pkt);
    }
}

impl Encoder<RepRepr> for RepRepr {
    fn encode(item: &RepRepr, dst: &mut BytesMut) {
        item.encode_into(dst);
    }
}

#[cfg(test)]
mod tests {
    use bytes::BytesMut;
    #[cfg(any(feature = "proto-ipv4", feature = "proto-ipv6"))]
    use smolsocket::SocketAddr;
    #[cfg(feature = "proto-ipv4")]
    use smoltcp::wire::Ipv4Address;
    #[cfg(feature = "proto-ipv6")]
    use smoltcp::wire::Ipv6Address;

    use crate::Atyp;

    use super::*;

    #[cfg(feature = "proto-ipv4")]
    #[test]
    fn test_cmd_invalid_len() {
        let mut truncated_bytes = vec![0x00 as u8; 4];
        let mut truncated = Packet::new_unchecked(&mut truncated_bytes);
        truncated.set_version(Ver::SOCKS5 as u8);
        truncated.set_atyp(Atyp::V4 as u8);
        assert_eq!(truncated.check_len(), Err(Error::Truncated));
        let mut truncated_bytes_mut = BytesMut::new();
        truncated_bytes_mut.extend(truncated_bytes);
        assert_eq!(CmdRepr::decode(&mut truncated_bytes_mut), Ok(None));

        let mut truncated_bytes = vec![0x00 as u8; 5];
        let mut truncated = Packet::new_unchecked(&mut truncated_bytes);
        truncated.set_version(Ver::SOCKS5 as u8);
        truncated.set_atyp(Atyp::V4 as u8);
        assert_eq!(truncated.check_len(), Err(Error::Truncated));

        assert_eq!(truncated.total_len(), 10);
        let mut malformed_bytes = vec![0x00 as u8; truncated.total_len() + 1];
        let mut malformed = Packet::new_unchecked(&mut malformed_bytes);
        malformed.set_version(Ver::SOCKS5 as u8);
        malformed.set_atyp(Atyp::V4 as u8);
        assert_eq!(malformed.check_len(), Err(Error::Malformed));
        let mut malformed_bytes_mut = BytesMut::new();
        malformed_bytes_mut.extend(malformed_bytes);
        assert_eq!(
            CmdRepr::decode(&mut malformed_bytes_mut),
            Err(Error::Malformed)
        );
    }

    #[cfg(feature = "proto-ipv4")]
    #[test]
    fn test_cmd_connect_ip4() {
        let socket_addr = SocketAddr::new_ip4_port(127, 0, 0, 1, 80);
        let socks_addr = SocksAddr::SocketAddr(socket_addr);
        let repr = CmdRepr {
            ver: Ver::SOCKS5,
            cmd: Cmd::Connect,
            addr: socks_addr.clone(),
        };
        assert_eq!(repr.buffer_len(), 10);
        let mut bytes = vec![0x00 as u8; repr.buffer_len()];
        let mut pkt = Packet::new_unchecked(&mut bytes);
        assert_eq!(pkt.atyp(), 0);
        pkt.set_atyp(Atyp::V4 as u8);
        assert_eq!(pkt.atyp(), Atyp::V4 as u8);
        assert_eq!(&pkt.addr_mut(), &Ipv4Address::new(0, 0, 0, 0).as_bytes());
        pkt.set_addr(Ipv4Address::new(192, 168, 0, 1).as_bytes());
        assert_eq!(
            &pkt.addr_mut(),
            &Ipv4Address::new(192, 168, 0, 1).as_bytes()
        );
        assert_eq!(pkt.port(), 0);
        pkt.set_port(8080);
        assert_eq!(pkt.port(), 8080);

        repr.emit(&mut pkt);
        assert_eq!(pkt.socks_addr_mut(), socks_addr.to_vec().as_slice());

        let pkt_to_parse = Packet::new_checked(pkt.as_ref()).expect("should be valid");
        assert_eq!(
            pkt_to_parse.addr(),
            Ipv4Address::new(127, 0, 0, 1).as_bytes()
        );
        let parsed = CmdRepr::parse(&pkt_to_parse).expect("should parse");
        assert_eq!(parsed, repr);
        assert_eq!(parsed.addr.atyp(), Atyp::V4);
        if let SocksAddr::SocketAddr(SocketAddr::V4(socket_addr)) = parsed.addr {
            assert!(socket_addr.addr.is_loopback());
        }

        let mut bytes_mut = BytesMut::new();
        CmdRepr::encode(&repr, &mut bytes_mut);
        let decoded = CmdRepr::decode(&mut bytes_mut);
        assert_eq!(decoded, Ok(Some(repr)));
    }

    #[cfg(feature = "proto-ipv6")]
    #[test]
    fn test_cmd_connect_ip6() {
        let socket_addr = SocketAddr::new_ip6_port(0, 0, 0, 0, 0, 0, 0, 1, 80);
        let socks_addr = SocksAddr::SocketAddr(socket_addr);
        let repr = CmdRepr {
            ver: Ver::SOCKS5,
            cmd: Cmd::Connect,
            addr: socks_addr.clone(),
        };
        assert_eq!(repr.buffer_len(), 22);
        let mut bytes = vec![0x00 as u8; repr.buffer_len()];
        let mut pkt = Packet::new_unchecked(&mut bytes);
        assert_eq!(pkt.atyp(), 0);
        pkt.set_atyp(Atyp::V6 as u8);
        assert_eq!(pkt.atyp(), Atyp::V6 as u8);
        assert_eq!(
            &pkt.addr_mut(),
            &Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 0).as_bytes()
        );
        pkt.set_addr(Ipv6Address::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).as_bytes());
        assert_eq!(
            &pkt.addr_mut(),
            &Ipv6Address::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).as_bytes()
        );
        assert_eq!(pkt.port(), 0);
        pkt.set_port(8080);
        assert_eq!(pkt.port(), 8080);

        repr.emit(&mut pkt);
        assert_eq!(pkt.socks_addr_mut(), socks_addr.to_vec().as_slice());

        let pkt_to_parse = Packet::new_checked(pkt.as_ref()).expect("should be valid");
        assert_eq!(
            pkt_to_parse.addr(),
            Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 1).as_bytes()
        );
        let parsed = CmdRepr::parse(&pkt_to_parse).expect("should parse");
        assert_eq!(parsed, repr);
        assert_eq!(parsed.addr.atyp(), Atyp::V6);
        if let SocksAddr::SocketAddr(SocketAddr::V6(socket_addr)) = parsed.addr {
            assert!(socket_addr.addr.is_loopback());
        }

        let mut bytes_mut = BytesMut::new();
        CmdRepr::encode(&repr, &mut bytes_mut);
        let decoded = CmdRepr::decode(&mut bytes_mut);
        assert_eq!(decoded, Ok(Some(repr)));
    }

    #[test]
    fn test_cmd_connect_domain() {
        let socks_addr = SocksAddr::DomainPort("google.com".to_string(), 443);
        let repr = CmdRepr {
            ver: Ver::SOCKS5,
            cmd: Cmd::Connect,
            addr: socks_addr.clone(),
        };
        assert_eq!(repr.buffer_len(), 17);
        let mut bytes = vec![0x00 as u8; repr.buffer_len()];
        let mut pkt = Packet::new_unchecked(&mut bytes);
        assert_eq!(pkt.atyp(), 0);
        pkt.set_atyp(Atyp::Domain as u8);
        assert_eq!(pkt.atyp(), Atyp::Domain as u8);
        assert_eq!(pkt.addr_mut()[0], 0);
        pkt.addr_mut()[0] = 10;
        assert_eq!(&pkt.addr_mut()[1..], b"\0\0\0\0\0\0\0\0\0\0");
        pkt.set_addr(b"          ");
        assert_eq!(&pkt.addr_mut()[1..], b"          ");
        assert_eq!(pkt.port(), 0);
        pkt.set_port(8080);
        assert_eq!(pkt.port(), 8080);

        repr.emit(&mut pkt);
        assert_eq!(pkt.socks_addr_mut(), socks_addr.to_vec().as_slice());

        let pkt_to_parse = Packet::new_checked(pkt.as_ref()).expect("should be valid");
        assert_eq!(pkt_to_parse.addr()[0], 10);
        assert_eq!(&pkt_to_parse.addr()[1..], b"google.com");
        let parsed = CmdRepr::parse(&pkt_to_parse).expect("should parse");
        assert_eq!(parsed, repr);
        assert_eq!(parsed.addr.atyp(), Atyp::Domain);
        if let SocksAddr::DomainPort(domain, port) = parsed.addr {
            assert_eq!(domain, "google.com".to_string());
            assert_eq!(port, 443);
        }

        let mut bytes_mut = BytesMut::new();
        CmdRepr::encode(&repr, &mut bytes_mut);
        let decoded = CmdRepr::decode(&mut bytes_mut);
        assert_eq!(decoded, Ok(Some(repr)));
    }

    #[cfg(feature = "proto-ipv4")]
    #[test]
    fn test_rep_invalid_len() {
        let mut truncated_bytes = vec![0x00 as u8; 4];
        let mut truncated = Packet::new_unchecked(&mut truncated_bytes);
        truncated.set_version(Ver::SOCKS5 as u8);
        truncated.set_atyp(Atyp::V4 as u8);
        assert_eq!(truncated.check_len(), Err(Error::Truncated));
        let mut truncated_bytes_mut = BytesMut::new();
        truncated_bytes_mut.extend(truncated_bytes);
        assert_eq!(RepRepr::decode(&mut truncated_bytes_mut), Ok(None));

        let mut truncated_bytes = vec![0x00 as u8; 5];
        let mut truncated = Packet::new_unchecked(&mut truncated_bytes);
        truncated.set_version(Ver::SOCKS5 as u8);
        truncated.set_atyp(Atyp::V4 as u8);
        assert_eq!(truncated.check_len(), Err(Error::Truncated));

        assert_eq!(truncated.total_len(), 10);
        let mut malformed_bytes = vec![0x00 as u8; truncated.total_len() + 1];
        let mut malformed = Packet::new_unchecked(&mut malformed_bytes);
        malformed.set_version(Ver::SOCKS5 as u8);
        malformed.set_atyp(Atyp::V4 as u8);
        assert_eq!(malformed.check_len(), Err(Error::Malformed));
        let mut malformed_bytes_mut = BytesMut::new();
        malformed_bytes_mut.extend(malformed_bytes);
        assert_eq!(
            RepRepr::decode(&mut malformed_bytes_mut),
            Err(Error::Malformed)
        );
    }

    #[cfg(feature = "proto-ipv4")]
    #[test]
    fn test_rep_success_ip4() {
        let socket_addr = SocketAddr::new_ip4_port(127, 0, 0, 1, 80);
        let socks_addr = SocksAddr::SocketAddr(socket_addr);
        let repr = RepRepr {
            ver: Ver::SOCKS5,
            rep: Rep::Success,
            addr: socks_addr.clone(),
        };
        assert_eq!(repr.buffer_len(), 10);
        let mut bytes = vec![0x00 as u8; repr.buffer_len()];
        let mut pkt = Packet::new_unchecked(&mut bytes);
        assert_eq!(pkt.atyp(), 0);
        pkt.set_atyp(Atyp::V4 as u8);
        assert_eq!(pkt.atyp(), Atyp::V4 as u8);
        assert_eq!(&pkt.addr_mut(), &Ipv4Address::new(0, 0, 0, 0).as_bytes());
        pkt.set_addr(Ipv4Address::new(192, 168, 0, 1).as_bytes());
        assert_eq!(
            &pkt.addr_mut(),
            &Ipv4Address::new(192, 168, 0, 1).as_bytes()
        );
        assert_eq!(pkt.port(), 0);
        pkt.set_port(8080);
        assert_eq!(pkt.port(), 8080);

        repr.emit(&mut pkt);
        assert_eq!(pkt.socks_addr_mut(), socks_addr.to_vec().as_slice());

        let pkt_to_parse = Packet::new_checked(pkt.as_ref()).expect("should be valid");
        assert_eq!(
            pkt_to_parse.addr(),
            Ipv4Address::new(127, 0, 0, 1).as_bytes()
        );
        let parsed = RepRepr::parse(&pkt_to_parse).expect("should parse");
        assert_eq!(parsed, repr);
        assert_eq!(parsed.addr.atyp(), Atyp::V4);
        if let SocksAddr::SocketAddr(SocketAddr::V4(socket_addr)) = parsed.addr {
            assert!(socket_addr.addr.is_loopback());
        }

        let mut bytes_mut = BytesMut::new();
        RepRepr::encode(&repr, &mut bytes_mut);
        let decoded = RepRepr::decode(&mut bytes_mut);
        assert_eq!(decoded, Ok(Some(repr)));
    }

    #[cfg(feature = "proto-ipv6")]
    #[test]
    fn test_rep_success_ip6() {
        let socket_addr = SocketAddr::new_ip6_port(0, 0, 0, 0, 0, 0, 0, 1, 80);
        let socks_addr = SocksAddr::SocketAddr(socket_addr);
        let repr = RepRepr {
            ver: Ver::SOCKS5,
            rep: Rep::Success,
            addr: socks_addr.clone(),
        };
        assert_eq!(repr.buffer_len(), 22);
        let mut bytes = vec![0x00 as u8; repr.buffer_len()];
        let mut pkt = Packet::new_unchecked(&mut bytes);
        assert_eq!(pkt.atyp(), 0);
        pkt.set_atyp(Atyp::V6 as u8);
        assert_eq!(pkt.atyp(), Atyp::V6 as u8);
        assert_eq!(
            &pkt.addr_mut(),
            &Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 0).as_bytes()
        );
        pkt.set_addr(Ipv6Address::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).as_bytes());
        assert_eq!(
            &pkt.addr_mut(),
            &Ipv6Address::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).as_bytes()
        );
        assert_eq!(pkt.port(), 0);
        pkt.set_port(8080);
        assert_eq!(pkt.port(), 8080);

        repr.emit(&mut pkt);
        assert_eq!(pkt.socks_addr_mut(), socks_addr.to_vec().as_slice());

        let pkt_to_parse = Packet::new_checked(pkt.as_ref()).expect("should be valid");
        assert_eq!(
            pkt_to_parse.addr(),
            Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 1).as_bytes()
        );
        let parsed = RepRepr::parse(&pkt_to_parse).expect("should parse");
        assert_eq!(parsed, repr);
        assert_eq!(parsed.addr.atyp(), Atyp::V6);
        if let SocksAddr::SocketAddr(SocketAddr::V6(socket_addr)) = parsed.addr {
            assert!(socket_addr.addr.is_loopback());
        }

        let mut bytes_mut = BytesMut::new();
        RepRepr::encode(&repr, &mut bytes_mut);
        let decoded = RepRepr::decode(&mut bytes_mut);
        assert_eq!(decoded, Ok(Some(repr)));
    }

    #[test]
    fn test_rep_success_domain() {
        let socks_addr = SocksAddr::DomainPort("google.com".to_string(), 443);
        let repr = RepRepr {
            ver: Ver::SOCKS5,
            rep: Rep::Success,
            addr: socks_addr.clone(),
        };
        assert_eq!(repr.buffer_len(), 17);
        let mut bytes = vec![0x00 as u8; repr.buffer_len()];
        let mut pkt = Packet::new_unchecked(&mut bytes);
        assert_eq!(pkt.atyp(), 0);
        pkt.set_atyp(Atyp::Domain as u8);
        assert_eq!(pkt.atyp(), Atyp::Domain as u8);
        assert_eq!(pkt.addr_mut()[0], 0);
        pkt.addr_mut()[0] = 10;
        assert_eq!(&pkt.addr_mut()[1..], b"\0\0\0\0\0\0\0\0\0\0");
        pkt.set_addr(b"          ");
        assert_eq!(&pkt.addr_mut()[1..], b"          ");
        assert_eq!(pkt.port(), 0);
        pkt.set_port(8080);
        assert_eq!(pkt.port(), 8080);

        repr.emit(&mut pkt);
        assert_eq!(pkt.socks_addr_mut(), socks_addr.to_vec().as_slice());

        let pkt_to_parse = Packet::new_checked(pkt.as_ref()).expect("should be valid");
        assert_eq!(pkt_to_parse.addr()[0], 10);
        assert_eq!(&pkt_to_parse.addr()[1..], b"google.com");
        let parsed = RepRepr::parse(&pkt_to_parse).expect("should parse");
        assert_eq!(parsed, repr);
        assert_eq!(parsed.addr.atyp(), Atyp::Domain);
        if let SocksAddr::DomainPort(domain, port) = parsed.addr {
            assert_eq!(domain, "google.com".to_string());
            assert_eq!(port, 443);
        }

        let mut bytes_mut = BytesMut::new();
        RepRepr::encode(&repr, &mut bytes_mut);
        let decoded = RepRepr::decode(&mut bytes_mut);
        assert_eq!(decoded, Ok(Some(repr)));
    }
}