Recent

Author Topic: [SOLVED] Synapse: How to communicate with *.local machine on *nix.  (Read 1489 times)

tk

  • Sr. Member
  • ****
  • Posts: 368
I have a problem using the Synapse library on *nix systems (Linux, MAC).
The following code works on Windows but does not on *nix:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   sock: TUDPBlockSocket;
  4. begin
  5.   sock := TUDPBlockSocket.Create;
  6.   try
  7.     sock.Connect('mypc.local', '1000');
  8.     sock.SendByte(0);
  9.     sock.CloseSocket;
  10.   finally
  11.     sock.Free;
  12.   end;
  13. end;

Synapse calls Winsock.GetAddrInfo on Windows which works and returns correct IP.
But it calls netdb's ResolveName on *nix, which directly implements DNS protocol and does not work (cannot work by design).
« Last Edit: November 04, 2024, 07:53:28 pm by tk »

rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #1 on: November 04, 2024, 09:19:57 am »
But it calls netdb's ResolveName on *nix, which directly implements DNS protocol and does not work (cannot work by design).

Quote
and netdb resolvename only checks the DNS servers listed in /etc/resolv.conf.
https://wiki.freepascal.org/Netdb

So you have a DNS server in resolv.conf which doesn't resolve mypc.local ???
Why does that server not resolve your mypc hostname?

TRon

  • Hero Member
  • *****
  • Posts: 3658
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #2 on: November 04, 2024, 09:21:12 am »
Note that ports < 1024 are 'protected' so unless you are running as root (or have another provision in place that allows you).
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #3 on: November 04, 2024, 09:22:33 am »
Note that ports < 1024 are 'protected' so unless you are running as root (or have another provision in place that allows you).
+1 Haha. Good one for spotting that  :D

tk

  • Sr. Member
  • ****
  • Posts: 368
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #4 on: November 04, 2024, 11:34:37 am »
So you have a DNS server in resolv.conf which doesn't resolve mypc.local ???
Why does that server not resolve your mypc hostname?

$ nslookup mypc.local reports error on all systems.
$ ping mypc.local works on all systems.

https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo
The getaddrinfo function provides protocol-independent translation from an ANSI host name to an address.
The problem on *nix is that Synapse uses DNS protocol by default which is incorrect.

Note that ports < 1024 are 'protected' so unless you are running as root (or have another provision in place that allows you).

Sorry just mistyped in this sample. Not the cause of this problem (in my program I use dynamic ports in the range 49152–65535, eg. 50000).

cdbc

  • Hero Member
  • *****
  • Posts: 1678
    • http://www.cdbc.dk
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #5 on: November 04, 2024, 11:40:47 am »
Hi
Sorry, but what's wrong with 'localhost'?!?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

tk

  • Sr. Member
  • ****
  • Posts: 368
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #6 on: November 04, 2024, 11:55:47 am »
Sorry, but what's wrong with 'localhost'?!?

Does not solve this problem and also in my case 'mypc.local' <> 'localhost', is another device on LAN.


rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #7 on: November 04, 2024, 12:01:04 pm »
Synapse calls Winsock.GetAddrInfo on Windows which works and returns correct IP.
But it calls netdb's ResolveName on *nix, which directly implements DNS protocol and does not work (cannot work by design).
What version of Synapse are you using?
I think the *nix version in Synapse 40.1 also uses getaddrinfo (at least... if you haven't defined FORCEOLDAPI).

BTW. for me nslookup hostname_local_machine works fine on Linux for my internal systems.
So you are using a DNS which doesn't resolve internal systems correctly.
That does work most of the time but isn't ideal.

Edit: Using sock.Connect('local_machine_hostname', 'open_port'); also works on a Linux testmachine here.
Edit #2: Only works for clients which correctly registers itself to the local DNS server (and only if you use that local DNS server in resolv.conf).

BTW. Did you try without the .local after the name. Sometimes the resolver doesn't like the suffix in the query.
« Last Edit: November 04, 2024, 12:49:13 pm by rvk »

tk

  • Sr. Member
  • ****
  • Posts: 368
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #8 on: November 04, 2024, 01:22:41 pm »
What version of Synapse are you using?
I think the *nix version in Synapse 40.1 also uses getaddrinfo (at least... if you haven't defined FORCEOLDAPI).

I use Synapse 40 but there is no difference from Synapse 40.1 in this regard.
*nix version does not call getaddrinfo.
No such code can be found in ssfpc.inc which is used when compiling with FPC.
I can see it in sslinux.inc but this is not compiled with FPC (I assume this was originally a Kylix code?).

BTW. for me nslookup hostname_local_machine works fine on Linux for my internal systems.
So you are using a DNS which doesn't resolve internal systems correctly.
That does work most of the time but isn't ideal.

Edit: Using sock.Connect('local_machine_hostname', 'open_port'); also works on a Linux testmachine here.
Edit #2: Only works for clients which correctly registers itself to the local DNS server (and only if you use that local DNS server in resolv.conf).

BTW. Did you try without the .local after the name. Sometimes the resolver doesn't like the suffix in the query.

I must use 'devicename.local'. It is a recommended way to address the device I need to communicate with.
As said, ping works on all systems, web access as well (http://devicename.local put into browser works fine).
I'm not familiar with what protocols are (or can be) used to determine this IP but it is definitely not the DNS protocol.
In Wireshark on Windows I can see a Netbios Name Query and also a MDNS call.

rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #9 on: November 04, 2024, 01:23:14 pm »
I see in synsock.pas that Synpase uses a separate ssfpc.inc instead of the sslinux.inc it uses for Delphi.

In that ssfpc.inc there is no implementation for the newer resolve methods (as used in the Delphi version).

But this might be a matter for Synapse unless you use the build in FPC socket procedures.

Ha, yes. You already found the problem...

The ssfpc.inc doesn't implement this new method so you could ask for a feature request in the Synapse github for this.
Or resolve the address yourself before connecting.
« Last Edit: November 04, 2024, 01:25:14 pm by rvk »

tk

  • Sr. Member
  • ****
  • Posts: 368
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #10 on: November 04, 2024, 02:16:56 pm »

rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #11 on: November 04, 2024, 02:53:46 pm »
$ nslookup mypc.local reports error on all systems.
$ ping mypc.local works on all systems.
BTW. I'm still puzzled by this.

If your mypc.local isn't in the (local) DNS server, I'm not sure how Linux gets its hostname.

I can understand if Windows does it differently but for me pinging an unknown hostname for my local DNS server doesn't work in Linux.

(It does on Windows. But not on Linux.)

tk

  • Sr. Member
  • ****
  • Posts: 368
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #12 on: November 04, 2024, 03:04:37 pm »
If your mypc.local isn't in the (local) DNS server, I'm not sure how Linux gets its hostname.
I can understand if Windows does it differently but for me pinging an unknown hostname for my local DNS server doesn't work in Linux.
(It does on Windows. But not on Linux.)

There is no local DNS server at all.
When it comes to the ping command, then it runs via those other naming protocols on Linux and MAC as well.
I'll check the behavior with Wireshark later when I'm back at those machines.

Alexx2000

  • Newbie
  • Posts: 6
Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #13 on: November 04, 2024, 03:05:45 pm »
I faced the same problem, my solution.

tk

  • Sr. Member
  • ****
  • Posts: 368
[SOLVED] Re: Synapse: How to communicate with *.local machine on *nix.
« Reply #14 on: November 04, 2024, 07:52:43 pm »
I faced the same problem, my solution.

Yep, this your fix works like a charm! Thank you!

 

TinyPortal © 2005-2018