Fixes for maxhostnamelen - all use NSHost.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@5113 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-10-31 09:48:26 +00:00
parent 6b14ba31f0
commit 6993fe84f7
4 changed files with 22 additions and 58 deletions

View file

@ -148,17 +148,23 @@ static NSMutableDictionary*_hostCache = nil;
} }
} }
/*
* Max hostname length in line with RFC 1123
*/
#define GSMAXHOSTNAMELEN 255
+ (NSHost*) currentHost + (NSHost*) currentHost
{ {
char name[MAXHOSTNAMELEN]; char name[GSMAXHOSTNAMELEN+1];
int res; int res;
struct hostent*h; struct hostent *h;
res = gethostname(name, sizeof(name)); res = gethostname(name, GSMAXHOSTNAMELEN);
if (res < 0) if (res < 0)
{ {
return nil; return nil;
} }
name[GSMAXHOSTNAMELEN] = '\0';
h = gethostbyname(name); h = gethostbyname(name);
if (h == NULL) if (h == NULL)

View file

@ -54,13 +54,6 @@
#include <config.h> #include <config.h>
#include <base/preface.h> #include <base/preface.h>
/* One of these two should have MAXHOSTNAMELEN */
#if !defined(__WIN32__) || defined(__CYGWIN__)
#include <unistd.h>
#include <sys/param.h>
#include <netdb.h>
#endif /* !__WIN32__ */
#ifdef HAVE_STRERROR #ifdef HAVE_STRERROR
#include <errno.h> #include <errno.h>
#endif /* HAVE_STRERROR */ #endif /* HAVE_STRERROR */
@ -76,6 +69,7 @@
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSProcessInfo.h> #include <Foundation/NSProcessInfo.h>
#include <Foundation/NSAutoreleasePool.h> #include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSHost.h>
/* This error message should be called only if the private main function /* This error message should be called only if the private main function
* was not executed successfully. This may happen ONLY if another library * was not executed successfully. This may happen ONLY if another library
@ -540,10 +534,7 @@ int main(int argc, char *argv[], char *env[])
{ {
if (!_gnu_hostName) if (!_gnu_hostName)
{ {
char hn[MAXHOSTNAMELEN]; _gnu_hostName = [[[NSHost currentHost] name] copy];
gethostname(hn, MAXHOSTNAMELEN);
_gnu_hostName = [[NSString alloc] initWithCString: hn];
} }
return _gnu_hostName; return _gnu_hostName;
} }

View file

@ -49,12 +49,12 @@
#include <Foundation/NSPortMessage.h> #include <Foundation/NSPortMessage.h>
#include <Foundation/NSPortNameServer.h> #include <Foundation/NSPortNameServer.h>
#include <Foundation/NSLock.h> #include <Foundation/NSLock.h>
#include <Foundation/NSHost.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#if !defined(__WIN32__) || defined(__CYGWIN__) #if !defined(__WIN32__) || defined(__CYGWIN__)
#include <unistd.h> /* for gethostname() */ #include <unistd.h> /* for gethostname() */
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#include <netinet/in.h> /* for inet_ntoa() */ #include <netinet/in.h> /* for inet_ntoa() */
#include <fcntl.h> #include <fcntl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -1110,8 +1110,7 @@ static NSMapTable* port_number_2_port;
that when we encode the address, another machine can find us. */ that when we encode the address, another machine can find us. */
{ {
struct hostent *hp; struct hostent *hp;
char hostname[MAXHOSTNAMELEN]; NSString *hostname;
int len = MAXHOSTNAMELEN;
int r; int r;
/* Set the re-use socket option so that we don't get this socket /* Set the re-use socket option so that we don't get this socket
@ -1186,25 +1185,13 @@ static NSMapTable* port_number_2_port;
machine so that, when we encoded our _LISTENING_ADDRESS for a machine so that, when we encoded our _LISTENING_ADDRESS for a
Distributed Objects connection to another machine, they get our Distributed Objects connection to another machine, they get our
unique host address that can identify us across the network. */ unique host address that can identify us across the network. */
if (gethostname (hostname, len) < 0) hostname = [[NSHost currentHost] name];
{ hp = gethostbyname ([hostname cString]);
[p release];
[NSException raise: NSInternalInconsistencyException
format: @"[TcpInPort +newForReceivingFromPortNumber:] gethostname(): %s",
strerror(errno)];
}
/* Terminate the name at the first dot. */
{
char *first_dot = strchr (hostname, '.');
if (first_dot)
*first_dot = '\0';
}
hp = gethostbyname (hostname);
if (!hp) if (!hp)
hp = gethostbyname ("localhost"); hp = gethostbyname ("localhost");
if (hp == 0) if (hp == 0)
{ {
NSLog(@"Unable to get IP address of '%s' or of 'localhost'", hostname); NSLog(@"Unable to get IP address of '%@' or of 'localhost'", hostname);
#ifndef HAVE_INET_ATON #ifndef HAVE_INET_ATON
p->_listening_address.sin_addr.s_addr = inet_addr("127.0.0.1"); p->_listening_address.sin_addr.s_addr = inet_addr("127.0.0.1");
#else #else
@ -1934,27 +1921,13 @@ static NSMapTable *out_port_bag = NULL;
struct hostent *hp; struct hostent *hp;
const char *host_cstring; const char *host_cstring;
struct sockaddr_in addr; struct sockaddr_in addr;
/* Only used if no hostname is passed in. */
char local_hostname[MAXHOSTNAMELEN];
/* Look up the hostname. */ /* Look up the hostname. */
if (!hostname || ![hostname length]) if (!hostname || ![hostname length])
{ {
int len = sizeof (local_hostname); hostname = [[NSHost currentHost] name];
char *first_dot;
if (gethostname (local_hostname, len) < 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"[TcpInPort newForSendingToPortNumber:onHost:] gethostname(): %s",
strerror(errno)];
}
host_cstring = local_hostname;
first_dot = strchr (host_cstring, '.');
if (first_dot)
*first_dot = '\0';
} }
else host_cstring = [hostname cString];
host_cstring = [hostname cString];
hp = gethostbyname ((char*)host_cstring); hp = gethostbyname ((char*)host_cstring);
if (!hp) if (!hp)
[self error: "unknown host: \"%s\"", host_cstring]; [self error: "unknown host: \"%s\"", host_cstring];

View file

@ -30,9 +30,9 @@
#include <base/Array.h> #include <base/Array.h>
#include <Foundation/NSLock.h> #include <Foundation/NSLock.h>
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSHost.h>
#ifndef __WIN32__ #ifndef __WIN32__
#include <unistd.h> #include <unistd.h>
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#endif /* !__WIN32__ */ #endif /* !__WIN32__ */
#if _AIX #if _AIX
#include <sys/select.h> #include <sys/select.h>
@ -126,14 +126,8 @@ static NSMapTable *port_number_2_in_port = NULL;
/* Give the socket a name using bind */ /* Give the socket a name using bind */
{ {
struct hostent *hp; struct hostent *hp;
char hostname[MAXHOSTNAMELEN];
int len = MAXHOSTNAMELEN; hp = gethostbyname ([[[NSHost currentHost] name] cString]);
if (gethostname (hostname, len) < 0)
{
perror ("[UdpInPort +newForReceivingFromPortNumber:] gethostname()");
abort ();
}
hp = gethostbyname (hostname);
if (!hp) if (!hp)
/* xxx This won't work with port connections on a network, though. /* xxx This won't work with port connections on a network, though.
Fix this. Perhaps there is a better way of getting the address Fix this. Perhaps there is a better way of getting the address