bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (x)gethostname


From: Simon Josefsson
Subject: Re: (x)gethostname
Date: Fri, 18 Jan 2008 12:49:52 +0100
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/22.1 (gnu/linux)

Simon Josefsson <address@hidden> writes:

>>> For reference, the Windows documentation for gethostname is:
>>> 
>>> http://msdn2.microsoft.com/en-us/library/ms738527.aspx
>>
>> Thanks for the reference. So, to accomodate this function, we need a
>> rpl_gethostname that calls gethostname and accomodates
>>   1. for the need to call WSAStartup() before,
>>      This will require a new module 'sockets' in gnulib, because obviously
>>      we don't want to call WSAStartup() more than once.
>>   2. for the conversion from a WSA* error code to an <errno.h> error code.
>>      This will require, in particular, an 'errno' module and a modification
>>      to the 'strerror' module.
>
> I think you are right.

I started to implement 1).  The documentation for WSAStartup is at:

http://msdn2.microsoft.com/en-us/library/ms742213.aspx

Calling WSAStartup more than once is not a problem if we make sure to
call WSACleanup the same number of times, which I think we have to do.

A sample socket module would look like:

winsocket.h:

#ifndef WINSOCKET_H
# define WINSOCKET_H 1
#define WINSOCK_1_0 0x100
#define WINSOCK_1_1 0x101
#define WINSOCK_2_0 0x200
#define WINSOCK_2_1 0x201
#define WINSOCK_2_2 0x202
int gl_wsastartup (int version);
int gl_wsacleanup (void);
#endif

winsocket.c:

#include <config.h>
/* This includes winsock2.h on MinGW. */
#include <sys/socket.h>
#include "winsocket.h"

int
gl_wsastartup (int version)
{
  WSADATA data;
  int err;

  err = WSAStartup (version, &data);
  if (err != 0)
    return 1;

  if (data.wVersion < version)
    return 2;

  return 0;
}

int
gl_wsacleanup (int version)
{
  int err;

  err = WSACleanup ();
  if (err != 0)
    return 1;

  return 0;
}

Then the mingw rpl_gethostname will look like:

#include <winsocket.h>

int
rpl_gethostname (char *name, size_t len)
{
  int err;

  if (gl_wsastartup (WINSOCK_1_0))
    return ENOTSUP; /* XXX what code to use? */

  err = gethostname (name, len);

  /* WSAGetLastError->errno conversion.. */

  gl_wsacleanup ();

  return err;
}

One alternative is to require that applications must call
WSAStartup/WSACleanup themselves.  This would simplify gnulib code a
lot, and would probably improve efficiency as well (no need to re-load
the winsocket stuff).

/Simon





reply via email to

[Prev in Thread] Current Thread [Next in Thread]