2012-07-04 16:53:12 -05:00
|
|
|
//! Types/fns concerning Internet Protocol (IP), versions 4 & 6
|
2012-04-29 23:53:17 -05:00
|
|
|
|
|
|
|
import vec;
|
|
|
|
import uint;
|
2012-06-22 17:31:56 -05:00
|
|
|
import iotask = uv::iotask::iotask;
|
|
|
|
import interact = uv::iotask::interact;
|
|
|
|
import comm::methods;
|
2012-04-29 23:53:17 -05:00
|
|
|
|
2012-06-17 22:36:36 -05:00
|
|
|
import sockaddr_in = uv::ll::sockaddr_in;
|
|
|
|
import sockaddr_in6 = uv::ll::sockaddr_in6;
|
2012-06-22 17:31:56 -05:00
|
|
|
import addrinfo = uv::ll::addrinfo;
|
|
|
|
import uv_getaddrinfo_t = uv::ll::uv_getaddrinfo_t;
|
2012-06-17 22:36:36 -05:00
|
|
|
import uv_ip4_addr = uv::ll::ip4_addr;
|
|
|
|
import uv_ip4_name = uv::ll::ip4_name;
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
import uv_ip6_addr = uv::ll::ip6_addr;
|
|
|
|
import uv_ip6_name = uv::ll::ip6_name;
|
2012-06-22 17:31:56 -05:00
|
|
|
import uv_getaddrinfo = uv::ll::getaddrinfo;
|
2012-06-25 10:02:34 -05:00
|
|
|
import uv_freeaddrinfo = uv::ll::freeaddrinfo;
|
2012-06-22 17:31:56 -05:00
|
|
|
import create_uv_getaddrinfo_t = uv::ll::getaddrinfo_t;
|
2012-06-25 10:02:34 -05:00
|
|
|
import set_data_for_req = uv::ll::set_data_for_req;
|
|
|
|
import get_data_for_req = uv::ll::get_data_for_req;
|
2012-06-22 17:31:56 -05:00
|
|
|
import ll = uv::ll;
|
2012-04-29 23:53:17 -05:00
|
|
|
|
2012-05-19 16:06:48 -05:00
|
|
|
export ip_addr, parse_addr_err;
|
2012-04-30 17:10:24 -05:00
|
|
|
export format_addr;
|
2012-06-22 17:31:56 -05:00
|
|
|
export v4, v6;
|
|
|
|
export get_addr;
|
2012-07-06 21:06:58 -05:00
|
|
|
export ipv4, ipv6;
|
2012-04-29 23:53:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// An IP address
|
2012-04-29 23:53:17 -05:00
|
|
|
enum ip_addr {
|
2012-07-04 16:53:12 -05:00
|
|
|
/// An IPv4 address
|
2012-06-17 22:36:36 -05:00
|
|
|
ipv4(sockaddr_in),
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
ipv6(sockaddr_in6)
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Human-friendly feedback on why a parse_addr attempt failed
|
2012-05-19 16:06:48 -05:00
|
|
|
type parse_addr_err = {
|
2012-07-14 00:57:48 -05:00
|
|
|
err_msg: ~str
|
2012-05-19 16:06:48 -05:00
|
|
|
};
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert a `ip_addr` to a str
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * ip - a `std::net::ip::ip_addr`
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
fn format_addr(ip: ip_addr) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ip {
|
2012-08-03 21:59:04 -05:00
|
|
|
ipv4(addr) => unsafe {
|
|
|
|
let result = uv_ip4_name(&addr);
|
|
|
|
if result == ~"" {
|
|
|
|
fail ~"failed to convert inner sockaddr_in address to str"
|
2012-06-17 22:36:36 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
result
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ipv6(addr) => unsafe {
|
|
|
|
let result = uv_ip6_name(&addr);
|
|
|
|
if result == ~"" {
|
|
|
|
fail ~"failed to convert inner sockaddr_in address to str"
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
result
|
2012-05-05 11:21:19 -05:00
|
|
|
}
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Represents errors returned from `net::ip::get_addr()`
|
2012-06-22 17:31:56 -05:00
|
|
|
enum ip_get_addr_err {
|
|
|
|
get_addr_unknown_error
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Attempts name resolution on the provided `node` string
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `node` - a string representing some host address
|
|
|
|
* * `iotask` - a `uv::iotask` used to interact with the underlying event loop
|
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
2012-07-11 18:49:02 -05:00
|
|
|
* A `result<~[ip_addr], ip_get_addr_err>` instance that will contain
|
2012-07-04 16:53:12 -05:00
|
|
|
* a vector of `ip_addr` results, in the case of success, or an error
|
|
|
|
* object in the case of failure
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_addr(++node: ~str, iotask: iotask)
|
2012-07-11 18:49:02 -05:00
|
|
|
-> result::result<~[ip_addr], ip_get_addr_err> unsafe {
|
2012-07-02 16:03:38 -05:00
|
|
|
do comm::listen |output_ch| {
|
2012-07-24 14:35:34 -05:00
|
|
|
do str::as_buf(node) |node_ptr, len| {
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"slice len %?", len});
|
2012-06-22 17:31:56 -05:00
|
|
|
let handle = create_uv_getaddrinfo_t();
|
|
|
|
let handle_ptr = ptr::addr_of(handle);
|
|
|
|
let handle_data: get_addr_data = {
|
|
|
|
output_ch: output_ch
|
|
|
|
};
|
|
|
|
let handle_data_ptr = ptr::addr_of(handle_data);
|
2012-07-02 16:03:38 -05:00
|
|
|
do interact(iotask) |loop_ptr| {
|
2012-06-22 17:31:56 -05:00
|
|
|
let result = uv_getaddrinfo(
|
|
|
|
loop_ptr,
|
|
|
|
handle_ptr,
|
|
|
|
get_addr_cb,
|
|
|
|
node_ptr,
|
|
|
|
ptr::null(),
|
|
|
|
ptr::null());
|
2012-08-06 14:34:08 -05:00
|
|
|
match result {
|
2012-08-03 21:59:04 -05:00
|
|
|
0i32 => {
|
2012-06-25 10:02:34 -05:00
|
|
|
set_data_for_req(handle_ptr, handle_data_ptr);
|
2012-06-22 17:31:56 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-06-22 17:31:56 -05:00
|
|
|
output_ch.send(result::err(get_addr_unknown_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output_ch.recv()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 23:53:17 -05:00
|
|
|
mod v4 {
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert a str to `ip_addr`
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* Fails if the string is not a valid IPv4 address
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * ip - a string of the format `x.x.x.x`
|
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
|
|
|
* * an `ip_addr` of the `ipv4` variant
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_addr(ip: ~str) -> ip_addr {
|
2012-08-06 14:34:08 -05:00
|
|
|
match try_parse_addr(ip) {
|
2012-08-03 21:59:04 -05:00
|
|
|
result::ok(addr) => copy(addr),
|
|
|
|
result::err(err_data) => fail err_data.err_msg
|
2012-05-19 16:06:48 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-26 11:07:24 -05:00
|
|
|
// the simple, old style numberic representation of
|
|
|
|
// ipv4
|
|
|
|
type ipv4_rep = { a: u8, b: u8, c: u8, d:u8 };
|
2012-07-11 17:00:40 -05:00
|
|
|
|
|
|
|
trait as_unsafe_u32 {
|
|
|
|
unsafe fn as_u32() -> u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl x of as_unsafe_u32 for ipv4_rep {
|
2012-06-26 11:07:24 -05:00
|
|
|
// this is pretty dastardly, i know
|
|
|
|
unsafe fn as_u32() -> u32 {
|
|
|
|
*((ptr::addr_of(self)) as *u32)
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_to_ipv4_rep(ip: ~str) -> result::result<ipv4_rep, ~str> {
|
2012-06-30 18:19:07 -05:00
|
|
|
let parts = vec::map(str::split_char(ip, '.'), |s| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match uint::from_str(s) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(n) if n <= 255u => n,
|
|
|
|
_ => 256u
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
|
|
|
});
|
2012-05-19 16:06:48 -05:00
|
|
|
if vec::len(parts) != 4u {
|
2012-07-30 18:01:07 -05:00
|
|
|
result::err(fmt!{"'%s' doesn't have 4 parts", ip})
|
2012-06-26 11:07:24 -05:00
|
|
|
}
|
2012-05-19 16:06:48 -05:00
|
|
|
else if vec::contains(parts, 256u) {
|
2012-07-30 18:01:07 -05:00
|
|
|
result::err(fmt!{"invalid octal in addr '%s'", ip})
|
2012-06-26 11:07:24 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result::ok({a: parts[0] as u8, b: parts[1] as u8,
|
|
|
|
c: parts[2] as u8, d: parts[3] as u8})
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn try_parse_addr(ip: ~str) -> result::result<ip_addr,parse_addr_err> {
|
2012-06-17 22:36:36 -05:00
|
|
|
unsafe {
|
2012-06-26 11:07:24 -05:00
|
|
|
let INADDR_NONE = ll::get_INADDR_NONE();
|
2012-06-26 14:47:44 -05:00
|
|
|
let ip_rep_result = parse_to_ipv4_rep(ip);
|
2012-06-26 11:07:24 -05:00
|
|
|
if result::is_err(ip_rep_result) {
|
|
|
|
let err_str = result::get_err(ip_rep_result);
|
2012-08-01 19:30:05 -05:00
|
|
|
return result::err({err_msg: err_str})
|
2012-06-26 11:07:24 -05:00
|
|
|
}
|
|
|
|
// ipv4_rep.as_u32 is unsafe :/
|
|
|
|
let input_is_inaddr_none =
|
|
|
|
result::get(ip_rep_result).as_u32() == INADDR_NONE;
|
|
|
|
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
let new_addr = uv_ip4_addr(ip, 22);
|
|
|
|
let reformatted_name = uv_ip4_name(&new_addr);
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"try_parse_addr: input ip: %s reparsed ip: %s",
|
|
|
|
ip, reformatted_name});
|
2012-06-26 14:47:44 -05:00
|
|
|
let ref_ip_rep_result = parse_to_ipv4_rep(reformatted_name);
|
2012-06-26 11:07:24 -05:00
|
|
|
if result::is_err(ref_ip_rep_result) {
|
|
|
|
let err_str = result::get_err(ref_ip_rep_result);
|
2012-08-01 19:30:05 -05:00
|
|
|
return result::err({err_msg: err_str})
|
2012-06-26 11:07:24 -05:00
|
|
|
}
|
|
|
|
if result::get(ref_ip_rep_result).as_u32() == INADDR_NONE &&
|
|
|
|
!input_is_inaddr_none {
|
2012-08-01 19:30:05 -05:00
|
|
|
return result::err(
|
2012-07-14 00:57:48 -05:00
|
|
|
{err_msg: ~"uv_ip4_name produced invalid result."})
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-06-25 10:02:34 -05:00
|
|
|
result::ok(ipv4(copy(new_addr)))
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
2012-05-19 16:06:48 -05:00
|
|
|
}
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
|
|
|
}
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
mod v6 {
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert a str to `ip_addr`
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* Fails if the string is not a valid IPv6 address
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * ip - an ipv6 string. See RFC2460 for spec.
|
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
|
|
|
* * an `ip_addr` of the `ipv6` variant
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_addr(ip: ~str) -> ip_addr {
|
2012-08-06 14:34:08 -05:00
|
|
|
match try_parse_addr(ip) {
|
2012-08-03 21:59:04 -05:00
|
|
|
result::ok(addr) => copy(addr),
|
|
|
|
result::err(err_data) => fail err_data.err_msg
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn try_parse_addr(ip: ~str) -> result::result<ip_addr,parse_addr_err> {
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
unsafe {
|
|
|
|
// need to figure out how to establish a parse failure..
|
|
|
|
let new_addr = uv_ip6_addr(ip, 22);
|
|
|
|
let reparsed_name = uv_ip6_name(&new_addr);
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"v6::try_parse_addr ip: '%s' reparsed '%s'",
|
|
|
|
ip, reparsed_name});
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
// '::' appears to be uv_ip6_name() returns for bogus
|
|
|
|
// parses..
|
2012-07-14 00:57:48 -05:00
|
|
|
if ip != ~"::" && reparsed_name == ~"::" {
|
2012-07-30 18:01:07 -05:00
|
|
|
result::err({err_msg:fmt!{"failed to parse '%s'",
|
|
|
|
ip}})
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result::ok(ipv6(new_addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 17:59:04 -05:00
|
|
|
type get_addr_data = {
|
2012-07-11 18:49:02 -05:00
|
|
|
output_ch: comm::chan<result::result<~[ip_addr],ip_get_addr_err>>
|
2012-06-27 17:59:04 -05:00
|
|
|
};
|
|
|
|
|
2012-07-03 18:32:02 -05:00
|
|
|
extern fn get_addr_cb(handle: *uv_getaddrinfo_t, status: libc::c_int,
|
2012-06-27 17:59:04 -05:00
|
|
|
res: *addrinfo) unsafe {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"in get_addr_cb");
|
2012-06-27 17:59:04 -05:00
|
|
|
let handle_data = get_data_for_req(handle) as
|
|
|
|
*get_addr_data;
|
|
|
|
if status == 0i32 {
|
|
|
|
if res != (ptr::null::<addrinfo>()) {
|
2012-07-11 18:49:02 -05:00
|
|
|
let mut out_vec = ~[];
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"initial addrinfo: %?", res});
|
2012-06-27 17:59:04 -05:00
|
|
|
let mut curr_addr = res;
|
|
|
|
loop {
|
|
|
|
let new_ip_addr = if ll::is_ipv4_addrinfo(curr_addr) {
|
|
|
|
ipv4(copy((
|
|
|
|
*ll::addrinfo_as_sockaddr_in(curr_addr))))
|
|
|
|
}
|
|
|
|
else if ll::is_ipv6_addrinfo(curr_addr) {
|
|
|
|
ipv6(copy((
|
|
|
|
*ll::addrinfo_as_sockaddr_in6(curr_addr))))
|
|
|
|
}
|
|
|
|
else {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"curr_addr is not of family AF_INET or "+
|
|
|
|
~"AF_INET6. Error.");
|
2012-06-27 17:59:04 -05:00
|
|
|
(*handle_data).output_ch.send(
|
|
|
|
result::err(get_addr_unknown_error));
|
|
|
|
break;
|
|
|
|
};
|
2012-07-11 18:49:02 -05:00
|
|
|
out_vec += ~[new_ip_addr];
|
2012-06-27 17:59:04 -05:00
|
|
|
|
|
|
|
let next_addr = ll::get_next_addrinfo(curr_addr);
|
|
|
|
if next_addr == ptr::null::<addrinfo>() as *addrinfo {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"null next_addr encountered. no mas");
|
2012-06-27 17:59:04 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curr_addr = next_addr;
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"next_addr addrinfo: %?", curr_addr});
|
2012-06-27 17:59:04 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"successful process addrinfo result, len: %?",
|
|
|
|
vec::len(out_vec)});
|
2012-06-27 17:59:04 -05:00
|
|
|
(*handle_data).output_ch.send(result::ok(out_vec));
|
2012-05-19 16:06:48 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"addrinfo pointer is NULL");
|
2012-06-27 17:59:04 -05:00
|
|
|
(*handle_data).output_ch.send(
|
|
|
|
result::err(get_addr_unknown_error));
|
2012-05-19 16:06:48 -05:00
|
|
|
}
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
2012-06-27 17:59:04 -05:00
|
|
|
else {
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"status != 0 error in get_addr_cb");
|
2012-06-27 17:59:04 -05:00
|
|
|
(*handle_data).output_ch.send(
|
|
|
|
result::err(get_addr_unknown_error));
|
|
|
|
}
|
|
|
|
if res != (ptr::null::<addrinfo>()) {
|
|
|
|
uv_freeaddrinfo(res);
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
log(debug, ~"leaving get_addr_cb");
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
#[test]
|
2012-06-26 18:13:50 -05:00
|
|
|
fn test_ip_ipv4_parse_and_format_ip() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let localhost_str = ~"127.0.0.1";
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
assert (format_addr(v4::parse_addr(localhost_str))
|
|
|
|
== localhost_str)
|
|
|
|
}
|
|
|
|
#[test]
|
2012-06-26 18:13:50 -05:00
|
|
|
fn test_ip_ipv6_parse_and_format_ip() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let localhost_str = ~"::1";
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
let format_result = format_addr(v6::parse_addr(localhost_str));
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"results: expected: '%s' actual: '%s'",
|
|
|
|
localhost_str, format_result});
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
assert format_result == localhost_str;
|
|
|
|
}
|
|
|
|
#[test]
|
2012-06-26 18:13:50 -05:00
|
|
|
fn test_ip_ipv4_bad_parse() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match v4::try_parse_addr(~"b4df00d") {
|
2012-08-03 21:59:04 -05:00
|
|
|
result::err(err_info) => {
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"got error as expected %?", err_info});
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
assert true;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
result::ok(addr) => {
|
2012-07-30 18:01:07 -05:00
|
|
|
fail fmt!{"Expected failure, but got addr %?", addr};
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
2012-06-27 17:28:03 -05:00
|
|
|
#[ignore(target_os="win32")]
|
2012-06-26 18:13:50 -05:00
|
|
|
fn test_ip_ipv6_bad_parse() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match v6::try_parse_addr(~"::,~2234k;") {
|
2012-08-03 21:59:04 -05:00
|
|
|
result::err(err_info) => {
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"got error as expected %?", err_info});
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
assert true;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
result::ok(addr) => {
|
2012-07-30 18:01:07 -05:00
|
|
|
fail fmt!{"Expected failure, but got addr %?", addr};
|
std: adding net::ip::v6 utils and rudimentary tests, huzzah! (see caveats)
libuv's own ip vetting code appears to in a somewhat woeful state,
for both ipv4 and ipv6 (there are some notes in the tests for net_ip, as
well as stuff added in uv_ll). They are aware of this and welcome patches.
I have rudimentary code in place that can verify whether the provided str
ip was, in fact, validly parsed by libuv, making a few assumptions:
* for ipv4, we assume that the platform's INADDR_NONE val is 0xffffffff ,
I should write a helper to return this value from the platform's libc
headers instead of hard-coding it in rust.
* for ipv6, we assume that the library will always return '::' for
malformed inputs.. as is the case in 64bit ubuntu. I need to verify this
on other platforms.. but at least the debugging output is in place, so
if expectations don't line up, it'll be straightforward to address
2012-06-17 22:42:48 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
2012-06-25 10:02:34 -05:00
|
|
|
#[test]
|
2012-06-29 17:35:47 -05:00
|
|
|
#[ignore(reason = "valgrind says it's leaky")]
|
2012-06-26 18:13:50 -05:00
|
|
|
fn test_ip_get_addr() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let localhost_name = ~"localhost";
|
2012-06-25 10:02:34 -05:00
|
|
|
let iotask = uv::global_loop::get();
|
|
|
|
let ga_result = get_addr(localhost_name, iotask);
|
|
|
|
if result::is_err(ga_result) {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"got err result from net::ip::get_addr();"
|
2012-06-25 10:02:34 -05:00
|
|
|
}
|
|
|
|
// note really sure how to realiably test/assert
|
|
|
|
// this.. mostly just wanting to see it work, atm.
|
|
|
|
let results = result::unwrap(ga_result);
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"test_get_addr: Number of results for %s: %?",
|
|
|
|
localhost_name, vec::len(results)});
|
2012-07-02 16:03:38 -05:00
|
|
|
for vec::each(results) |r| {
|
2012-08-06 14:34:08 -05:00
|
|
|
let ipv_prefix = match r {
|
2012-08-03 21:59:04 -05:00
|
|
|
ipv4(_) => ~"IPv4",
|
|
|
|
ipv6(_) => ~"IPv6"
|
2012-06-25 10:02:34 -05:00
|
|
|
};
|
2012-07-30 18:01:07 -05:00
|
|
|
log(debug, fmt!{"test_get_addr: result %s: '%s'",
|
|
|
|
ipv_prefix, format_addr(r)});
|
2012-06-25 10:02:34 -05:00
|
|
|
}
|
2012-06-26 13:29:57 -05:00
|
|
|
// at least one result.. this is going to vary from system
|
|
|
|
// to system, based on stuff like the contents of /etc/hosts
|
|
|
|
assert vec::len(results) > 0;
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
|
|
|
#[test]
|
2012-06-29 17:35:47 -05:00
|
|
|
#[ignore(reason = "valgrind says it's leaky")]
|
2012-06-26 18:13:50 -05:00
|
|
|
fn test_ip_get_addr_bad_input() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let localhost_name = ~"sjkl234m,./sdf";
|
2012-06-26 13:29:57 -05:00
|
|
|
let iotask = uv::global_loop::get();
|
|
|
|
let ga_result = get_addr(localhost_name, iotask);
|
|
|
|
assert result::is_err(ga_result);
|
2012-04-29 23:53:17 -05:00
|
|
|
}
|
2012-07-06 21:06:58 -05:00
|
|
|
}
|