-
Notifications
You must be signed in to change notification settings - Fork 471
Add getsockname shim for connecting and connected sockets
#4985
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
base: master
Are you sure you want to change the base?
Changes from all commits
c3a2c3e
62681d2
33dd2e4
39d12d8
c4f8fe3
c748fb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| warning: connecting sockets return unspecified socket addresses on Windows hosts | ||
| --> tests/pass-dep/libc/libc-socket-no-blocking.rs:LL:CC | ||
| | | ||
| LL | libc::getsockname(client_sockfd, storage, len) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Called `getsockname` on connecting socket | ||
| | | ||
| = note: Windows hosts do not provide `local_addr` information while the socket is still connecting, which might break the assumptions of code compiled for Unix targets | ||
| = note: an unspecified socket address (e.g. `0.0.0.0:0`) will be returned instead | ||
| = note: this is on thread `main` | ||
| = note: stack backtrace: | ||
| 0: test_getsockname_ipv4_connect_nonblock::{closure#0} | ||
| at tests/pass-dep/libc/libc-socket-no-blocking.rs:LL:CC | ||
| 1: libc_utils::net::sockname | ||
| at tests/pass-dep/libc/../../utils/libc.rs:LL:CC | ||
| 2: libc_utils::net::sockname_ipv4 | ||
| at tests/pass-dep/libc/../../utils/libc.rs:LL:CC | ||
| 3: test_getsockname_ipv4_connect_nonblock | ||
| at tests/pass-dep/libc/libc-socket-no-blocking.rs:LL:CC | ||
| 4: main | ||
| at tests/pass-dep/libc/libc-socket-no-blocking.rs:LL:CC | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ fn main() { | |
| test_getsockname_ipv4(); | ||
| test_getsockname_ipv4_random_port(); | ||
| test_getsockname_ipv4_unbound(); | ||
| test_getsockname_ipv4_connect(); | ||
| test_getsockname_ipv6(); | ||
|
|
||
| test_getpeername_ipv4(); | ||
|
|
@@ -427,6 +428,34 @@ fn test_getsockname_ipv4_unbound() { | |
| assert_eq!(addr.sin_addr.s_addr, sock_addr.sin_addr.s_addr); | ||
| } | ||
|
|
||
| /// Test the `getsockname` syscall on a connected IPv4 socket. | ||
| fn test_getsockname_ipv4_connect() { | ||
| let (server_sockfd, addr) = net::make_listener_ipv4().unwrap(); | ||
| let client_sockfd = | ||
| unsafe { errno_result(libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0)).unwrap() }; | ||
|
|
||
| // Spawn the server thread. | ||
| let server_thread = thread::spawn(move || net::accept_ipv4(server_sockfd).unwrap()); | ||
|
|
||
| net::connect_ipv4(client_sockfd, addr).unwrap(); | ||
|
|
||
| let (_, sock_addr) = net::sockname_ipv4(|storage, len| unsafe { | ||
| libc::getsockname(client_sockfd, storage, len) | ||
| }) | ||
| .unwrap(); | ||
|
|
||
| // We want to ensure that the local address is not the unspecified address. | ||
| // Because the bound address could be of any local interface, we cannot | ||
| // test for localhost. | ||
|
Comment on lines
+448
to
+449
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The socket API would let us
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the socket API allows to do this but we cannot represent this with mio because we only have |
||
| let addr = net::sock_addr_ipv4([0, 0, 0, 0], 0); | ||
|
|
||
| assert_eq!(addr.sin_family, sock_addr.sin_family); | ||
| assert_ne!(addr.sin_addr.s_addr, sock_addr.sin_addr.s_addr); | ||
| assert!(sock_addr.sin_port > 0); | ||
|
|
||
| server_thread.join().unwrap(); | ||
| } | ||
|
|
||
| /// Test the `getsockname` syscall on an IPv6 socket which is bound. | ||
| /// The `getsockname` syscall should return the same address as to | ||
| /// which the socket was bound to. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.