Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

This struct represents a high-level SMB client, and it is highly encouraged to use it for interacting with SMB servers, instead of manually creating connections.

§General usage

When connecting to a new share, even if it’s on the same server, you must always connect to the share using Client::share_connect.

§Drop behavior

When the client drops, the held connections are not forcibly closed, but rather kept alive until all their references are dropped. For example, if a file is opened from the client, and the client is dropped, the connection, session and tree of the opened file, will still be alive, but you will not be able to use the client to interact with them.

To force a closure of all connections and their managed resources, use the Client::close method.

§Example

use smb::{Client, ClientConfig, UncPath, FileCreateArgs, FileAccessMask};
use std::str::FromStr;
#[cfg(feature = "async")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // instantiate the client
    let client = Client::new(ClientConfig::default());

    // Connect to a share
    let target_path = UncPath::from_str(r"\\server\share").unwrap();
    client.share_connect(&target_path, "username", "password".to_string()).await?;

    // And open a file on the server
    let file_to_open = target_path.with_path("file.txt");
    let file_open_args = FileCreateArgs::make_open_existing(FileAccessMask::new().with_generic_read(true));
    let file = client.create_file(&file_to_open, &file_open_args).await?;
    // now, you can do a bunch of operations against `file`, and close it at the end.
    Ok(())
}

Implementations§

Source§

impl Client

Source

pub fn new(config: ClientConfig) -> Self

Creates a new Client instance with the given configuration.

Source

pub fn config(&self) -> &ClientConfig

Source

pub async fn close(&self) -> Result<()>

Shuts down the client, and all its managed connections.

Any resource held by the client will not be accessible after calling this method, directly or indirectly.

See Drop behavior for more information.

Source

pub async fn list_shares(&self, server: &str) -> Result<Vec<ShareInfo1>>

Lists all shares on the specified server.

Source

pub async fn share_connect( &self, target: &UncPath, user_name: &str, password: String, ) -> Result<()>

Connects to a share on the specified server.

This method is the equivalent for executing a net use command on a local windows machine.

Once the connection completes, the client will be able to access resource under the specified share, without needing to re-authenticate.

If the share is already connected, this method will do nothing, and will log a warning indicating the double-connection attempt.

§Arguments
  • target - The UNC path of the share to connect to. The method refers to the server and share components in this path.
  • user_name - The username to use for authentication.
  • password - The password to use for authentication.
§Returns

The connected share - a Tree instance.

§Notes

This is the best high-level method that performs share connection, but it might not suit advanced use cases.

You can replace calls to this method by performing the connection, session and share setup manually, just like it does, using the Client::connect method:

// instantiate the client
// Connect to a share
let target_path = UncPath::from_str(r"\\server\share").unwrap();
let connection = client.share_connect(&target_path, "username", "password".to_string()).await?;
Source

pub async fn connect(&self, server: &str) -> Result<Arc<Connection>>

Makes a connection to the specified server. If a matching connection already exists, returns it.

Note: You should usually connect the client through the Client::share_connect method. Using this method, for example, will require you to hold a reference to trees, or otherwise they will disconnect (as opposed to the share_connect method, which assures keeping the tree alive!)

See Client::connect_to_address to connect to a server using a specific socket address.

§Arguments
  • server - The target server to make the connection for.
§Returns

The connected connection, if succeeded. Error if failed to make the connection,

Source

pub async fn connect_to_address( &self, server: &str, server_address: SocketAddr, ) -> Result<Arc<Connection>>

Makes a connection to the specified server and address. If a matching connection already exists, returns it.

Note: You should usually connect the client through the Client::share_connect method. Using this method, for example, will require you to hold a reference to trees, or otherwise they will disconnect (as opposed to the share_connect method, which assures keeping the tree alive!)

See Client::connect to connect to a server using DNS resolution.

§Arguments
  • server - The target server to make the connection for.
  • server_address - An optional socket address to connect to. If the port is set to 0, the default port will be used according to the transport that is used.
§Returns

The connected connection, if succeeded. Error if failed to make the connection, or failed to connect the remote.

Source

pub async fn connect_transport_to_address( &self, server: &str, server_address: SocketAddr, transport: TransportConfig, ) -> Result<Arc<Connection>>

Just like Client::connect_to_address, but allows specifying a custom transport configuration.

Source

pub async fn get_connection(&self, server: &str) -> Result<Arc<Connection>>

Returns the underlying Connection for the specified server, after a successful call to Client::connect or Client::share_connect.

Source

pub async fn get_connection_ip(&self, ip: IpAddr) -> Result<Arc<Connection>>

Source

pub async fn get_session(&self, path: &UncPath) -> Result<Arc<Session>>

Source

pub async fn get_channels( &self, path: &UncPath, ) -> Result<HashMap<u32, AltChannelInfo>>

Returns a map of channel IDs to their corresponding connections for the specified session,

Source

pub async fn get_tree(&self, path: &UncPath) -> Result<Arc<Tree>>

Returns the underlying Tree for the specified UNC path, after a successful call to Client::share_connect.

Source

pub async fn create_file( &self, path: &UncPath, args: &FileCreateArgs, ) -> Result<Resource>

Creates (or opens) a file on the specified path, using the specified args.

See FileCreateArgs for detailed information regarding the file open options.

The function also handles DFS resolution if it is enabled in the client configuration.

§Arguments
  • path - The UNC path of the file to create or open.
  • args - The arguments to use when creating or opening the file.
§Returns

A result containing the created or opened file resource, or an error.

Source

pub async fn ipc_connect( &self, server: &str, username: &str, password: String, ) -> Result<()>

Similar Client::share_connect, but connects to the SMB pipes share (IPC$).

After calling this method, the Client::open_pipe method can be used to open named pipes.

Source

pub async fn _ipc_connect( &self, server: &str, identity: &AuthIdentity, ) -> Result<()>

Source

pub async fn open_pipe(&self, server: &str, pipe_name: &str) -> Result<Pipe>

Opens a named pipe on the specified server. Use this when intending to communicate with a service using a named pipe, for convenience.

§Arguments
  • server - The name of the server hosting the pipe.
  • pipe_name - The name of the pipe to open.
§Returns

A result containing the opened Pipe resource, or an error.

§Notes

before calling this method, you MUST call the Client::ipc_connect method, that connects to the IPC$ share on the server, which then allows for communication with the named pipe.

Trait Implementations§

Source§

impl Default for Client

Source§

fn default() -> Self

Starts the client with default configuration.

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnsafeUnpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more