From 6993fe84f78071a9dee0309fc61b92332b3bb149 Mon Sep 17 00:00:00 2001 From: richard Date: Sun, 31 Oct 1999 09:48:26 +0000 Subject: [PATCH] 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 --- Source/NSHost.m | 14 ++++++++++---- Source/NSProcessInfo.m | 13 ++----------- Source/TcpPort.m | 41 +++++++---------------------------------- Source/UdpPort.m | 12 +++--------- 4 files changed, 22 insertions(+), 58 deletions(-) diff --git a/Source/NSHost.m b/Source/NSHost.m index 88dea898e..9fb0f09dd 100644 --- a/Source/NSHost.m +++ b/Source/NSHost.m @@ -148,17 +148,23 @@ static NSMutableDictionary*_hostCache = nil; } } +/* + * Max hostname length in line with RFC 1123 + */ +#define GSMAXHOSTNAMELEN 255 + + (NSHost*) currentHost { - char name[MAXHOSTNAMELEN]; - int res; - struct hostent*h; + char name[GSMAXHOSTNAMELEN+1]; + int res; + struct hostent *h; - res = gethostname(name, sizeof(name)); + res = gethostname(name, GSMAXHOSTNAMELEN); if (res < 0) { return nil; } + name[GSMAXHOSTNAMELEN] = '\0'; h = gethostbyname(name); if (h == NULL) diff --git a/Source/NSProcessInfo.m b/Source/NSProcessInfo.m index 66f1647fd..810d4b3a4 100644 --- a/Source/NSProcessInfo.m +++ b/Source/NSProcessInfo.m @@ -54,13 +54,6 @@ #include #include -/* One of these two should have MAXHOSTNAMELEN */ -#if !defined(__WIN32__) || defined(__CYGWIN__) -#include -#include -#include -#endif /* !__WIN32__ */ - #ifdef HAVE_STRERROR #include #endif /* HAVE_STRERROR */ @@ -76,6 +69,7 @@ #include #include #include +#include /* This error message should be called only if the private main function * 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) { - char hn[MAXHOSTNAMELEN]; - - gethostname(hn, MAXHOSTNAMELEN); - _gnu_hostName = [[NSString alloc] initWithCString: hn]; + _gnu_hostName = [[[NSHost currentHost] name] copy]; } return _gnu_hostName; } diff --git a/Source/TcpPort.m b/Source/TcpPort.m index 3603eaf76..a969c8878 100644 --- a/Source/TcpPort.m +++ b/Source/TcpPort.m @@ -49,12 +49,12 @@ #include #include #include +#include #include #include #include #if !defined(__WIN32__) || defined(__CYGWIN__) #include /* for gethostname() */ -#include /* for MAXHOSTNAMELEN */ #include /* for inet_ntoa() */ #include #include @@ -1110,8 +1110,7 @@ static NSMapTable* port_number_2_port; that when we encode the address, another machine can find us. */ { struct hostent *hp; - char hostname[MAXHOSTNAMELEN]; - int len = MAXHOSTNAMELEN; + NSString *hostname; int r; /* 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 Distributed Objects connection to another machine, they get our unique host address that can identify us across the network. */ - if (gethostname (hostname, len) < 0) - { - [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); + hostname = [[NSHost currentHost] name]; + hp = gethostbyname ([hostname cString]); if (!hp) hp = gethostbyname ("localhost"); 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 p->_listening_address.sin_addr.s_addr = inet_addr("127.0.0.1"); #else @@ -1934,27 +1921,13 @@ static NSMapTable *out_port_bag = NULL; struct hostent *hp; const char *host_cstring; struct sockaddr_in addr; - /* Only used if no hostname is passed in. */ - char local_hostname[MAXHOSTNAMELEN]; /* Look up the hostname. */ if (!hostname || ![hostname length]) { - int len = sizeof (local_hostname); - 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'; + hostname = [[NSHost currentHost] name]; } - else - host_cstring = [hostname cString]; + host_cstring = [hostname cString]; hp = gethostbyname ((char*)host_cstring); if (!hp) [self error: "unknown host: \"%s\"", host_cstring]; diff --git a/Source/UdpPort.m b/Source/UdpPort.m index 5fd036e26..50705d9c8 100644 --- a/Source/UdpPort.m +++ b/Source/UdpPort.m @@ -30,9 +30,9 @@ #include #include #include +#include #ifndef __WIN32__ #include -#include /* for MAXHOSTNAMELEN */ #endif /* !__WIN32__ */ #if _AIX #include @@ -126,14 +126,8 @@ static NSMapTable *port_number_2_in_port = NULL; /* Give the socket a name using bind */ { struct hostent *hp; - char hostname[MAXHOSTNAMELEN]; - int len = MAXHOSTNAMELEN; - if (gethostname (hostname, len) < 0) - { - perror ("[UdpInPort +newForReceivingFromPortNumber:] gethostname()"); - abort (); - } - hp = gethostbyname (hostname); + + hp = gethostbyname ([[[NSHost currentHost] name] cString]); if (!hp) /* xxx This won't work with port connections on a network, though. Fix this. Perhaps there is a better way of getting the address