#1 2018-06-07 09:47:09

Eric
Member
Registered: 2012-11-26
Posts: 129
Website

InitSocketInterface bug ?

Hi,

I have encountered an odd issue with InitSocketInterface when used under Win64, it can combine to trigger a Windows bug in ResolveNameToIP, the bug in question is the same as in the stackoverflow post (https://stackoverflow.com/questions/215 … it-windows), you can end up with "DEADF00D" in the result in Win64 because of a Windows bug in deprecated GetHostByName function.

The issue is that when InitSocketInterface is called twice, while the LoadLibrary stuff is protected by SynSockCount, the  SockEnhancedApi variable is not, it is reset to False, so instead of using the GetAddrInfo API, it is the GetHostByName that is being called, which sometimes bugs in Win64.

Offline

#2 2018-06-07 14:02:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,670
Website

Re: InitSocketInterface bug ?

Could you propose a patch to fix it?

Offline

#3 2018-06-07 14:35:37

Eric
Member
Registered: 2012-11-26
Posts: 129
Website

Re: InitSocketInterface bug ?

For a patch, if you want to restrict support to Windows 8.1 and Windows Server 2012 R2, the cleanest option would simply be to remove SockEnhancedApi and all accompanying code (either for all versions, or just for Win64).

If you still want to support the legacy API, I guess the "SockEnhancedApi := False;" could be moved within the "if SynSockCount = 0 then begin" (and same with SockSChannelApi)

function InitSocketInterface(const Stack: TFileName = ''): Boolean;
begin
  result := False;
  EnterCriticalSection(SynSockCS);
  try
    if SynSockCount = 0 then begin
      SockEnhancedApi := False;
      SockSChannelApi := False;
      SockWship6Api := False;
      if Stack = '' then
        LibHandle := LoadLibrary(DLLStackName)
      else 

But since there were explicitly left outside of the "if SynSockCount = 0" I guess there may be side effects ?

Last edited by Eric (2018-06-07 14:36:22)

Offline

#4 2018-06-07 15:41:51

Eric
Member
Registered: 2012-11-26
Posts: 129
Website

Re: InitSocketInterface bug ?

Also about the Windows version dependency, from the doc, it appears the function is available since WinXP, but only got standardized in Windows 8.1 / Windows Server 2012 R2.

Offline

#5 2018-06-08 08:23:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,670
Website

Re: InitSocketInterface bug ?

I don't understand why InitSocketInterface() should be called twice?
It is executed in the initialization section of SynCrtSock, and that's all...

Anyway, I've patched the function as https://synopse.info/fossil/info/5924b5bc17

Offline

#6 2018-06-08 16:19:06

Eric
Member
Registered: 2012-11-26
Posts: 129
Website

Re: InitSocketInterface bug ?

ab wrote:

I don't understand why InitSocketInterface() should be called twice?
It is executed in the initialization section of SynCrtSock, and that's all...

Anyway, I've patched the function as https://synopse.info/fossil/info/5924b5bc17

Thanks!

The reason it's called twice is I have some units and projects using only SynWinSock, so they have their own call InitSocketInterface, and when used in projects that also use SynCrtSock, then InitSocketInterface gets called twice.

If it's not meant to be called more than once, I guess the SynSockCount mechanism could be removed, and the initialization performed directly by SynWinSock ?

Offline

#7 2018-06-08 16:26:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,670
Website

Re: InitSocketInterface bug ?

Yes, it is now better with the patch.

All this was a legacy from the Synapse library socket unit, from which we started SynCrtSock...

Offline

Board footer

Powered by FluxBB