locka99 / opcua

A client and server implementation of the OPC UA specification written in Rust
Mozilla Public License 2.0
480 stars 129 forks source link

simple-client can not connect to simple-server #164

Open EmmanuelP opened 2 years ago

EmmanuelP commented 2 years ago

Hi,

Trying to follow the example given in README, I have launched simple-server and simple client with:

cd opcua/samples/simple-server
cargo run

and:

cd opcua/samples/simple-client
cargo run

But the client fails to connect to the server:

opcua/samples/simple-client on ξ‚  master [?] is πŸ“¦ v0.9.0 via πŸ¦€ v1.58.1 ❯ cargo run
warning: field is never read: `max_chunk_count`
  --> core/src/comms/message_writer.rs:29:5
   |
29 |     max_chunk_count: usize,
   |     ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `opcua-core` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `/home/pacaud/Sources/opcua/target/debug/opcua-simple-client`
2022-02-18 15:44:06.015 - ERROR - opcua_client::comms::tcp_transport - Could not connect to host [::1]:4855, Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }
2022-02-18 15:44:06.115 - ERROR - opcua_client::comms::tcp_transport - Connected failed with status BadCommunicationError
2022-02-18 15:44:06.117 - ERROR - opcua_client::comms::tcp_transport - Connected failed with status BadCommunicationError
2022-02-18 15:44:06.117 - ERROR - opcua_client::comms::tcp_transport - Could not connect to host [::1]:4855, Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }
2022-02-18 15:44:16.116 - ERROR - opcua_client::comms::tcp_transport - Connected failed with status BadCommunicationError
2022-02-18 15:44:16.117 - ERROR - opcua_client::session::session - session:1 Session has given up trying to connect to the server after 3 retries
2022-02-18 15:44:16.117 - ERROR - opcua_client::comms::tcp_transport - Could not connect to host [::1]:4855, Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }
2022-02-18 15:44:16.117 - ERROR - opcua_client::client - Cannot get endpoints for server, error - BadNotConnected
opcua/samples/simple-client on ξ‚  master [?] is πŸ“¦ v0.9.0 via πŸ¦€ v1.58.1 took 10s ❯ 

This is the server console output:

opcua/samples/simple-server on ξ‚  master [?] is πŸ“¦ v0.9.0 via πŸ¦€ v1.58.1 ❯ cargo run
warning: field is never read: `max_chunk_count`
  --> core/src/comms/message_writer.rs:29:5
   |
29 |     max_chunk_count: usize,
   |     ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `opcua-core` (lib) generated 1 warning
warning: unused variable: `receive_buffer_size`
   --> server/src/comms/tcp_transport.rs:533:9
    |
533 |         receive_buffer_size: usize,
    |         ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_receive_buffer_size`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: associated function is never used: `deregister_session`
   --> server/src/session_diagnostics.rs:123:19
    |
123 |     pub(crate) fn deregister_session(&self, session: &Session, address_space: &mut AddressSpace) {
    |                   ^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: associated function is never used: `service_counter`
   --> server/src/session_diagnostics.rs:139:19
    |
139 |     pub(crate) fn service_counter(
    |                   ^^^^^^^^^^^^^^^

warning: field is never read: `bytes_read`
  --> server/src/comms/tcp_transport.rs:98:5
   |
98 |     pub bytes_read: usize,
   |     ^^^^^^^^^^^^^^^^^^^^^

warning: associated function is never used: `deregister_session`
   --> server/src/session.rs:539:19
    |
539 |     pub(crate) fn deregister_session(&self, address_space: Arc<RwLock<AddressSpace>>) {
    |                   ^^^^^^^^^^^^^^^^^^

warning: field is never read: `handled_state`
  --> server/src/subscriptions/subscription.rs:86:5
   |
86 |     pub handled_state: HandledState,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `opcua-server` (lib) generated 6 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.12s
     Running `/home/pacaud/Sources/opcua/target/debug/opcua-simple-server`

I understand the client configuration is in ../client.conf, but failed to find what to change that will let the client connect correctly to simple-server.

I was able to connect to the server using UaExpert.

This is on fedora 35, using master opcua-rs (8701d1ff637acf4a6b0cee8094d882961ff3d821), whith untouched server.conf and client.conf.

EmmanuelP commented 2 years ago

I understand the client configuration is in ../client.conf

My bad, simple-client is built using ClientBuilder.

The problem is on fedora, locahost resolves to ::1 and not 127.0.0.1.

Changing server.conf to consistantly use localhost instead of 127.0.0.1 solves this issue.

diff --git a/samples/server.conf b/samples/server.conf
index 28174648..06a5df96 100644
--- a/samples/server.conf
+++ b/samples/server.conf
@@ -12,7 +12,7 @@ pki_dir: "./pki"
 discovery_server_url: "opc.tcp://localhost:4840/UADiscovery"
 tcp_config:
   hello_timeout: 5
-  host: 127.0.0.1
+  host: localhost
   port: 4855
 limits:
   clients_can_modify_address_space: false
@@ -39,7 +39,7 @@ user_tokens:
     user: unused
     pass: unused1
 discovery_urls:
-  - "opc.tcp://127.0.0.1:4855/"
+  - "opc.tcp://localhost:4855/"
 default_endpoint: ~
 endpoints:
   aes128-sha256-rsaoaep_sign:
locka99 commented 2 years ago

Thanks I think this should be easy enough to fix although I haven't tested with IPv6 so I don't know if there are issues to be found with that form addressing, e.g. in cert info