On Windows, when will a 'socket()' call return WSAEPROVIDERFAILEDINIT?

Viewed 36

Suppose our client application does as below:

WSADATA wsa_data;
int     ret;
SOCKET  sock;
ret = WSAStartup (MAKEWORD (2, 2), &wsa_data);
if (ret ==0) {
  sock = socket (AF_INET6, SOCK_DGRAM, IPROTO_UDP);
}

When will it happen that sock == INVALID_SOCKET and WSAGetLastError() would return WSAEPROVIDERFAILEDINIT?

Microsoft's documentation for the socket() function says it can return WSAEPROVIDERFAILEDINIT when:

The service provider failed to initialize. This error is returned if a layered service provider (LSP) or namespace provider was improperly installed or the provider fails to operate correctly.

Can a TCP/IP transport service provider, that is not a custom service provider, fail to initialize during a socket() call? If yes, why? Under what circumstances? And how can it be remedied?

1 Answers

It will probably never happen with the default provider and unlikely with a 3rd-party provider.

WSAEPROVIDERFAILEDINIT can be returned by Windows itself if it cannot load the provider or if it can't find the WSPStartup or more likely NSPStartup exported function. I'm not sure if this can only happen during WSAStartup or if Windows is allowed to delay this call until the provider is needed.

A provider itself can presumably fail with WSAEPROVIDERFAILEDINIT at any time. Imagine a LSP that logs every connection but for whatever reason there is a problem with the database it is using and it might choose to fail the socket with this error.

(All of this is speculation and you should not handle each possible error in a specific way unless you have a reason to.)

Related