Skip to content

uv: implement a way to get client's ip/port. #3815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

luqmana
Copy link
Member

@luqmana luqmana commented Oct 19, 2012

Can now get the remote ip address and port of a client upon accepting.

Quick example:

extern mod std;

use core::comm;
use std::uv;
use std::net::{ip, tcp};
use std::uv::*;

fn main() {

    let iotask = uv::global_loop::get();
    let addr = ip::v4::parse_addr("0.0.0.0");
    let port = 4000;

    tcp::listen(addr, port, 128u, iotask,
        |kill_ch| {
            info!("Listening on %s:%u.", ip::format_addr(&addr), port);
        },
        |conn, kill_ch| {
            do task::spawn_sched(task::ThreadPerCore) {
                match tcp::accept(conn) {
                    result::Ok(move sock) => {

                        let addr = sock.getpeername();
                        info!("accept %s:%u", ip::format_addr(&addr),
                                              ip::get_port(&addr));

                    }
                    result::Err(move e) => {
                        comm::send(kill_ch, Some(e));
                    }
                }
            }
        }
    );

}

@luqmana
Copy link
Member Author

luqmana commented Oct 19, 2012

I couldn't figure out a way to determine if ipv4 or ipv6 was in use hence the separate getpeername and getpeername6.
Update: see subsequent commit.

@brson
Copy link
Contributor

brson commented Oct 20, 2012

get_peer_name (or get_peer_ip) might be more consistent with rust naming conventions for the function in net::tcp

@brson
Copy link
Contributor

brson commented Oct 20, 2012

A regression test would be nice, though I realize that the existing tests are very dense and setting up a simple test to exercise getpeername does not appear simple.

@brson
Copy link
Contributor

brson commented Oct 20, 2012

Here's a test: brson@eb154f3

@luqmana are you ok with changing the name to get_peer_ip?

@luqmana
Copy link
Member Author

luqmana commented Oct 20, 2012

Yep, i'm ok with it. I'll update it in just a bit with the rename + the test.

@luqmana
Copy link
Member Author

luqmana commented Oct 20, 2012

Actually, I think get_peer_addr would make more sense since getting the port from the "ip" seems awkward, i.e:

let ip = sock.get_peer_ip();
let port = ip::get_port(&ip);

vs

let addr = sock.get_peer_addr();
let port = ip::get_port(&addr);

@brson
Copy link
Contributor

brson commented Oct 21, 2012

I rebased this onto incoming. Thanks!

@brson brson closed this Oct 21, 2012
RalfJung pushed a commit to RalfJung/rust that referenced this pull request Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants