2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 22:51:57 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Bitcoin network addresses
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! This module defines the structures and functions needed to encode
|
|
|
|
//! network addresses in Bitcoin messages.
|
|
|
|
//!
|
|
|
|
|
2018-03-20 16:27:27 +00:00
|
|
|
use std::io;
|
2014-07-18 13:56:17 +00:00
|
|
|
use std::fmt;
|
2018-03-20 16:27:27 +00:00
|
|
|
use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
2014-07-18 13:56:17 +00:00
|
|
|
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
use consensus::encode::{self, Encoder, Decoder};
|
|
|
|
use consensus::encode::{Decodable, Encodable};
|
2014-07-18 13:56:17 +00:00
|
|
|
|
|
|
|
/// A message which can be sent on the Bitcoin network
|
|
|
|
pub struct Address {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Services provided by the peer whose address this is
|
|
|
|
pub services: u64,
|
|
|
|
/// Network byte-order ipv6 address, or ipv4-mapped ipv6 address
|
|
|
|
pub address: [u16; 8],
|
|
|
|
/// Network port
|
|
|
|
pub port: u16
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 16:27:27 +00:00
|
|
|
const ONION : [u16; 3] = [0xFD87, 0xD87E, 0xEB43];
|
|
|
|
|
|
|
|
impl Address {
|
|
|
|
/// Create an address message for a socket
|
|
|
|
pub fn new (socket :&SocketAddr, services: u64) -> Address {
|
|
|
|
let (address, port) = match socket {
|
|
|
|
&SocketAddr::V4(ref addr) => (addr.ip().to_ipv6_mapped().segments(), addr.port()),
|
|
|
|
&SocketAddr::V6(ref addr) => (addr.ip().segments(), addr.port())
|
|
|
|
};
|
|
|
|
Address { address: address, port: port, services: services }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// extract socket address from an address message
|
|
|
|
/// This will return io::Error ErrorKind::AddrNotAvailable if the message contains a Tor address.
|
|
|
|
pub fn socket_addr (&self) -> Result<SocketAddr, io::Error> {
|
|
|
|
let addr = &self.address;
|
|
|
|
if addr[0..3] == ONION {
|
|
|
|
return Err(io::Error::from(io::ErrorKind::AddrNotAvailable));
|
|
|
|
}
|
|
|
|
let ipv6 = Ipv6Addr::new(
|
|
|
|
addr[0],addr[1],addr[2],addr[3],
|
|
|
|
addr[4],addr[5],addr[6],addr[7]
|
|
|
|
);
|
|
|
|
if let Some(ipv4) = ipv6.to_ipv4() {
|
|
|
|
Ok(SocketAddr::V4(SocketAddrV4::new(ipv4, self.port)))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Ok(SocketAddr::V6(SocketAddrV6::new(ipv6, self.port, 0, 0)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 18:08:38 +00:00
|
|
|
fn addr_to_be(addr: [u16; 8]) -> [u16; 8] {
|
|
|
|
[addr[0].to_be(), addr[1].to_be(), addr[2].to_be(), addr[3].to_be(),
|
|
|
|
addr[4].to_be(), addr[5].to_be(), addr[6].to_be(), addr[7].to_be()]
|
|
|
|
}
|
|
|
|
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl<S: Encoder> Encodable<S> for Address {
|
2015-04-07 22:51:57 +00:00
|
|
|
#[inline]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
|
2018-08-12 16:47:31 +00:00
|
|
|
self.services.consensus_encode(s)?;
|
|
|
|
addr_to_be(self.address).consensus_encode(s)?;
|
2015-05-10 18:08:38 +00:00
|
|
|
self.port.to_be().consensus_encode(s)
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2014-08-01 16:01:39 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl<D: Decoder> Decodable<D> for Address {
|
2015-04-07 22:51:57 +00:00
|
|
|
#[inline]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
fn consensus_decode(d: &mut D) -> Result<Address, encode::Error> {
|
2015-04-07 22:51:57 +00:00
|
|
|
Ok(Address {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
services: Decodable::consensus_decode(d)?,
|
|
|
|
address: addr_to_be(Decodable::consensus_decode(d)?),
|
|
|
|
port: u16::from_be(Decodable::consensus_decode(d)?)
|
2015-04-07 22:51:57 +00:00
|
|
|
})
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:35:31 +00:00
|
|
|
impl fmt::Debug for Address {
|
2015-04-07 22:51:57 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO: render services and hex-ize address
|
|
|
|
write!(f, "Address {{services: {:?}, address: {:?}, port: {:?}}}",
|
|
|
|
self.services, &self.address[..], self.port)
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 12:15:48 +00:00
|
|
|
impl Clone for Address {
|
2015-04-07 22:51:57 +00:00
|
|
|
fn clone(&self) -> Address {
|
2018-02-14 16:44:02 +00:00
|
|
|
Address {
|
|
|
|
services: self.services,
|
|
|
|
address: self.address,
|
|
|
|
port: self.port,
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2014-09-10 12:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Address {
|
2015-04-07 22:51:57 +00:00
|
|
|
fn eq(&self, other: &Address) -> bool {
|
|
|
|
self.services == other.services &&
|
|
|
|
&self.address[..] == &other.address[..] &&
|
|
|
|
self.port == other.port
|
|
|
|
}
|
2014-09-10 12:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Address {}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-03-20 16:27:27 +00:00
|
|
|
use std::str::FromStr;
|
2015-04-07 22:51:57 +00:00
|
|
|
use super::Address;
|
2018-03-20 16:27:27 +00:00
|
|
|
use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr};
|
2015-04-07 22:51:57 +00:00
|
|
|
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
use consensus::encode::{deserialize, serialize};
|
2015-04-07 22:51:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_address_test() {
|
|
|
|
assert_eq!(serialize(&Address {
|
|
|
|
services: 1,
|
2015-04-08 22:23:45 +00:00
|
|
|
address: [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001],
|
2015-04-07 22:51:57 +00:00
|
|
|
port: 8333
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
}),
|
|
|
|
vec![1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1, 0x20, 0x8d]);
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_address_test() {
|
2015-04-08 22:23:45 +00:00
|
|
|
let mut addr: Result<Address, _> = deserialize(&[1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0,
|
|
|
|
0, 1, 0x20, 0x8d]);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert!(addr.is_ok());
|
|
|
|
let full = addr.unwrap();
|
2018-03-20 16:27:27 +00:00
|
|
|
assert!(match full.socket_addr().unwrap() {
|
|
|
|
SocketAddr::V4(_) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
);
|
2015-05-10 18:08:38 +00:00
|
|
|
assert_eq!(full.services, 1);
|
|
|
|
assert_eq!(full.address, [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001]);
|
|
|
|
assert_eq!(full.port, 8333);
|
2015-04-07 22:51:57 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
addr = deserialize(&[1u8, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1]);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert!(addr.is_err());
|
|
|
|
}
|
2018-03-20 16:27:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_socket_addr () {
|
|
|
|
let s4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(111,222,123,4)), 5555);
|
|
|
|
let a4 = Address::new(&s4, 9);
|
|
|
|
assert_eq!(a4.socket_addr().unwrap(), s4);
|
|
|
|
let s6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444,
|
|
|
|
0x5555, 0x6666, 0x7777, 0x8888)), 9999);
|
|
|
|
let a6 = Address::new(&s6, 9);
|
|
|
|
assert_eq!(a6.socket_addr().unwrap(), s6);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn onion_test () {
|
|
|
|
let onionaddr = SocketAddr::new(
|
|
|
|
IpAddr::V6(
|
|
|
|
Ipv6Addr::from_str("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").unwrap()), 1111);
|
|
|
|
let addr = Address::new(&onionaddr, 0);
|
|
|
|
assert!(addr.socket_addr().is_err());
|
|
|
|
}
|
2014-08-01 16:01:39 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|