Thread: RE: [HACKERS] LIBPQ for WIN32

RE: [HACKERS] LIBPQ for WIN32

From
Magnus Hagander
Date:
> > With the current implementation, I don't beleive it will make a
> difference -
> > I don't think that any part of the Winsock system is
> actually hidden if
> you
> > ask for a lower version. But it is _permitted_ by the
> specification that
> the
> > DLL can hide parts that belong to a higher version than the
> one requested.
> >
> > So I'm not 100% sure... Does anybody have access to a
> Winsock?@that
> actually
> > hides some details when you ask for the wrong version?
> >
>
> By the specs there may be the DLL that doesn't support lower versions.
> In that case my code doesn't work well.
> But we can delay to call WSAStartup() after the first socket
> call in LIBPQ.
> My example code in fe-connect.c is such as follows.
This looks like a great way to do it :-)


>     hp = gethostbyname(conn->pghost);
> #ifdef    WIN32
>     if ((hp == NULL) && (GetLastError() == WSANOTINITIALISED))
>     {
>         WSADATA    wsaData;
>         if (WSAStartup(MAKEWORD(1,1),&wsaData))
>         {
>             fprintf(stderr, "Failed to start
> winsock: %i\n", WSAGetLastError());
>             exit(1);

This is not the way to do it, though - what if it's a gui program.
Should instead be, like the other error handling:

            sprintf(conn->errorMessage,
                    "connectDB() -- Failed to start
winsock: %i\n",
                    WSAGetLstError());
            goto connect_errReturn;

>         }
>         ....
>         ???? for WSACleanup() ????
>         ....
Yes, this is the problem. Perhaps set a global flag in the DLL that is set
if we have ever called WSAStartup() from the DLL. Then we can check in
DllMain() at process detachment if we have to close the winsock?

Will you write up a patch or should I?

//Magnus

RE: [HACKERS] LIBPQ for WIN32

From
"Taral"
Date:
OK... Whether or not Winsock enables a 2.0 or 1.1 interface is pretty
irrelevant, unless you're using new features like
setsockopt(somethingorother) [I can't remember]. Otherwise, the standard BSD
socket functions (socket, bind, connect, listen, etc.) are unchanged.

A much better way to do this is to do a WSAStartup() in DllMain on
PROCESS_ATTACH requesting any interface from 1.1 to 2.2, and FAIL TO LOAD if
the WSAStartup() fails to provide a valid interface. That way, we just
always call WSACleanup() in DllMain on PROCESS_DETACH.

(Note that delaying WSAStartup() until the first socket call makes your code
ugly, and can delay the first socket call unnecessarily. Since we're almost
guaranteed to be calling sockets, we should initialize winsock when we
initialize.)

As for any applications that might not use libpq... They should delay-load
libpq.dll. I saw a reference to it in the MSDN library... dunno if win32
supports it.

Taral

> -----Original Message-----
> From: owner-pgsql-hackers@postgreSQL.org
> [mailto:owner-pgsql-hackers@postgreSQL.org]On Behalf Of Magnus Hagander
> Sent: Tuesday, September 29, 1998 7:06 AM
> To: 'Hiroshi Inoue'; pgsql-hackers
> Subject: RE: [HACKERS] LIBPQ for WIN32
>
>
> > > With the current implementation, I don't beleive it will make a
> > difference -
> > > I don't think that any part of the Winsock system is
> > actually hidden if
> > you
> > > ask for a lower version. But it is _permitted_ by the
> > specification that
> > the
> > > DLL can hide parts that belong to a higher version than the
> > one requested.
> > >
> > > So I'm not 100% sure... Does anybody have access to a
> > Winsock?@that
> > actually
> > > hides some details when you ask for the wrong version?
> > >
> >
> > By the specs there may be the DLL that doesn't support lower versions.
> > In that case my code doesn't work well.
> > But we can delay to call WSAStartup() after the first socket
> > call in LIBPQ.
> > My example code in fe-connect.c is such as follows.
> This looks like a great way to do it :-)
>
>
> >     hp = gethostbyname(conn->pghost);
> > #ifdef    WIN32
> >     if ((hp == NULL) && (GetLastError() == WSANOTINITIALISED))
> >     {
> >         WSADATA    wsaData;
> >         if (WSAStartup(MAKEWORD(1,1),&wsaData))
> >         {
> >             fprintf(stderr, "Failed to start
> > winsock: %i\n", WSAGetLastError());
> >             exit(1);
>
> This is not the way to do it, though - what if it's a gui program.
> Should instead be, like the other error handling:
>
>             sprintf(conn->errorMessage,
>                     "connectDB() -- Failed to start
> winsock: %i\n",
>                     WSAGetLstError());
>             goto connect_errReturn;
>
> >         }
> >         ....
> >         ???? for WSACleanup() ????
> >         ....
> Yes, this is the problem. Perhaps set a global flag in the DLL that is set
> if we have ever called WSAStartup() from the DLL. Then we can check in
> DllMain() at process detachment if we have to close the winsock?
>
> Will you write up a patch or should I?
>
> //Magnus
>


RE: [HACKERS] LIBPQ for WIN32

From
"Hiroshi Inoue"
Date:
>
> > > With the current implementation, I don't beleive it will make a
> > difference -
> > > I don't think that any part of the Winsock system is
> > actually hidden if
> > you
> > > ask for a lower version. But it is _permitted_ by the
> > specification that
> > the
> > > DLL can hide parts that belong to a higher version than the
> > one requested.
> > >
> > > So I'm not 100% sure... Does anybody have access to a
> > Winsock?@that
> > actually
> > > hides some details when you ask for the wrong version?
> > >
> >
> > By the specs there may be the DLL that doesn't support lower versions.
> > In that case my code doesn't work well.
> > But we can delay to call WSAStartup() after the first socket
> > call in LIBPQ.
> > My example code in fe-connect.c is such as follows.
> This looks like a great way to do it :-)
>
>
> >     hp = gethostbyname(conn->pghost);
> > #ifdef    WIN32
> >     if ((hp == NULL) && (GetLastError() == WSANOTINITIALISED))
> >     {
> >         WSADATA    wsaData;
> >         if (WSAStartup(MAKEWORD(1,1),&wsaData))
> >         {
> >             fprintf(stderr, "Failed to start
> > winsock: %i\n", WSAGetLastError());
> >             exit(1);
>
> This is not the way to do it, though - what if it's a gui program.
> Should instead be, like the other error handling:
>
>             sprintf(conn->errorMessage,
>                     "connectDB() -- Failed to start
> winsock: %i\n",
>                     WSAGetLstError());
>             goto connect_errReturn;
>
> >         }
> >         ....
> >         ???? for WSACleanup() ????
> >         ....
> Yes, this is the problem. Perhaps set a global flag in the DLL that is set
> if we have ever called WSAStartup() from the DLL. Then we can check in
> DllMain() at process detachment if we have to close the winsock?
>
> Will you write up a patch or should I?
>

Judging from your reply to Vince,it is preferable to call WSAStartup() and
corrspoiding WSACleanup() in DllMain.
If so,my original code is OK except error handling and the value of version
number ?

Hiroshi Inoue
Inoue@tpf.co.jp