Skip to content
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

WIP: Add ClientInfo type to ListeningSocket #61

Conversation

jcspencer
Copy link
Collaborator

@jcspencer jcspencer commented Apr 27, 2024

This PR is very much a work-in-progress, and I'm still unsure on a few things - but here goes nothing!

This adds:

  • A type ClientInfo type parameter to ListeningSocket
  • A fn client_info(stream: &Self::Stream) -> io::Result<Self::ClientInfo> method to ListeningSocket

The idea here being that a new Session is initialized with information about the connecting client, such as:

New connection from client: UnixClientInfo {
   socket_addr: (unnamed),
   credentials: UCred { pid: Some(73721), uid: 501, gid: 20 }
}

or on Windows:

New connection from client: NamedPipeClientInfo { pid: 2684, session_id: 1 }

Now, for the issues:

  • Type information: I can't figure out the best way to pass type information to the Agent implementation so that it can actually extract fields from the ClientInfo (specific to the Stream type)
  • Windows: Oh man, don't even get me started on Windows!
    • To get anything usable from the name pipe, we need to make two unsafe calls to get the PID and Session ID for the client...

Alternatively, we can let Agent implementations extract this information from sockets themselves, but this would likely require an enum specifying the socket type, so you can consume socket-specific types. Not quite sure what the best option is - would love your thoughts!

@jcspencer jcspencer force-pushed the jcspencer/add-client-info-to-listener branch from 70b8d60 to 9b5703b Compare April 27, 2024 04:38
@jcspencer
Copy link
Collaborator Author

For what it's worth, you can do something like this in nightly, but it isn't exactly pleasant (though it does work!):

impl Agent for KeyStorageAgent {
    fn new_session<S>(&mut self, socket: &S::Stream) -> impl Session
    where
        S: ListeningSocket + std::fmt::Debug + Send,
    {
        if let Ok(client_info) = S::client_info(socket) {
            match &client_info as &dyn Any {
                _ if let Some(tcp) = s.downcast_ref::<TcpClientInfo>() => {
                    println!("New connection from TCP client: {:?}", tcp);
                },
                #[cfg(unix)]
                _ if let Some(unix) = s.downcast_ref::<UnixClientInfo>() => {
                    println!("New connection from Unix client: {:?}", unix);
                },
                #[cfg(windows)]
                _ if let Some(named_pipe) = s.downcast_ref::<NamedPipeClientInfo>() => {
                    println!("New connection from Named Pipe client: {:?}", named_pipe);
                },
                _ => {}
            };
            
        }

        KeyStorage {
            identities: Arc::clone(&self.identities),
        }
    }
}

@wiktor-k wiktor-k force-pushed the wiktor/add-socket-to-new-session branch 7 times, most recently from b495ec6 to 1998f6d Compare May 6, 2024 09:24
@wiktor-k wiktor-k force-pushed the wiktor/add-socket-to-new-session branch 2 times, most recently from 8c4425d to 99f1f8b Compare May 6, 2024 10:08
@jcspencer
Copy link
Collaborator Author

Closing in favor of #60

@jcspencer jcspencer closed this May 11, 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