summaryrefslogtreecommitdiff
path: root/comm
diff options
context:
space:
mode:
authorchatty2000-11-28 17:07:43 +0000
committerchatty2000-11-28 17:07:43 +0000
commit682dfb1c696c4ba40bcc269a0e0665388564334f (patch)
tree1767195c3f33cc714810d80cdd2292c96a936db1 /comm
parent1336b976f6ae02de59ae45ec7d2c5eab16ab4ae8 (diff)
downloadivy-league-682dfb1c696c4ba40bcc269a0e0665388564334f.zip
ivy-league-682dfb1c696c4ba40bcc269a0e0665388564334f.tar.gz
ivy-league-682dfb1c696c4ba40bcc269a0e0665388564334f.tar.bz2
ivy-league-682dfb1c696c4ba40bcc269a0e0665388564334f.tar.xz
* Smart(ies) removed by Stephane Sire
* Added buffered hostname to UchInetAddress
Diffstat (limited to 'comm')
-rw-r--r--comm/Address.cc135
1 files changed, 98 insertions, 37 deletions
diff --git a/comm/Address.cc b/comm/Address.cc
index 03391d7..d0853d1 100644
--- a/comm/Address.cc
+++ b/comm/Address.cc
@@ -3,7 +3,7 @@
*
* by Michel Beaudouin-Lafon
*
- * Copyright 1990-1993
+ * Copyright 1990-1997
* Laboratoire de Recherche en Informatique (LRI)
*
* Addresses
@@ -20,9 +20,9 @@
#include <unistd.h>
#include <stdio.h>
#include <memory.h>
-#if 1
#include <sys/utsname.h>
-#endif
+
+
#ifdef __osf__
extern "C" {
#endif
@@ -49,9 +49,9 @@ These functions exist for all the derived classes but are only described here.
An address can be valid or invalid, depending on the constructor being able to create the address or not.
There are currently two derived classes, for Unix domain addresses and Internet domain addresses.
-The class \typ{^{pUchAddress}} implements smart pointers to addresses.
-Smart pointers behave like pointers but they manage a reference count on the
-pointed to objects for an automatic reclaim of dynamically allocated objects.
+Addresses are "ONE WAY" instances, once created they cannot be modified.
+This implies that instead of beeing changed, new ones are created with
+static member function \fun{Decode} for example when a socket is bound.
?*/
/*?
@@ -184,9 +184,14 @@ UchUnixAddress :: StrRepr (char* buf)
// todo:
// hostname / service name ??
//
-/*?nodoc?*/
+/*?
+UchInetAddresses should only be allocated with new because they
+can be deleted and reallocated by \type{UchSocket} class.
+?*/
UchInetAddress :: UchInetAddress ()
-: UchAddress ()
+: UchAddress (),
+ HostName (),
+ HasHostName (false)
{
}
@@ -194,11 +199,13 @@ UchInetAddress :: UchInetAddress ()
Construct an inet address, from a hostname and a port number.
The address is valid only if the host is valid.
If \var{host} is the empty string, the loopback host is used;
-if it is the nil pointer, the wildcard is used.
+if it is the nil pointer, the wildcard (INADDR_ANY) is used.
If the port number is 0, it will be allocated by the system.
?*/
UchInetAddress :: UchInetAddress (const char* name, sword port)
-: UchAddress ()
+: UchAddress (),
+ HostName (),
+ HasHostName (false)
{
if (name && *name) {
struct hostent *host = gethostbyname (name);
@@ -221,7 +228,9 @@ The host ids \var{^{ANYADDR}}, \var{^{LOOPBACK}} and \var{^{BROADCAST}} are pred
\var{BROADCAST} makes it possible to send data to all the hosts of a local area network.
?*/
UchInetAddress :: UchInetAddress (lword host, sword port)
-: UchAddress ()
+: UchAddress (),
+ HostName (),
+ HasHostName (false)
{
Addr.sin_family = AF_INET;
Addr.sin_port = htons (port);
@@ -255,31 +264,6 @@ UchInetAddress :: GetSockAddr ()
return (SockAddr*) &Addr;
}
-/*?nodoc?*/
-char*
-UchInetAddress :: StrRepr (char* buf)
-{
- struct hostent *host;
- char *hname;
- unsigned long saddr;
-
- saddr = Addr.sin_addr.s_addr;
- host = gethostbyaddr ((char*) &saddr, sizeof (long), AF_INET);
- if (host)
- hname = host->h_name;
- else
- if (saddr == INADDR_ANY)
- hname = "ANY";
- else if (saddr == INADDR_LOOPBACK)
- hname = "LOOPBACK";
- else if (saddr == INADDR_BROADCAST)
- hname = "BROADCAST";
- else
- hname = "???";
- sprintf (buf, "%s::%d", hname, ntohs (Addr.sin_port));
- return buf;
-}
-
/*?
This is a global function (static member of class \typ{UchAddress}).
It creates an object of a derived class of \typ{UchAddress} from a generic address
@@ -351,6 +335,8 @@ UchInetAddress :: LoopBack ()
}
#ifdef DOC
+// Ca c'est les super fake declarations pour avoir la DOC
+// merci au LRI...
/*?
Return the host number of the address.
?*/
@@ -364,6 +350,81 @@ Return the port number of the address.
sword
UchInetAddress :: Port ()
{}
-
#endif /* DOC */
+
+/*?
+Return a const string representing the hostname part of the address if any
+or NULL if it does not exist (INADDR_ANY, INADDR_LOOPBACK or INADDR_BROADCAST).
+This is an interface to unix \fun{gethostbyaddr} system call.
+?*/
+const char*
+UchInetAddress :: GetHostName ()
+{
+ struct hostent *host;
+ unsigned long saddr;
+
+ if (HasHostName)
+ return HostName;
+
+ saddr = Addr.sin_addr.s_addr;
+ host = gethostbyaddr ((char*) &saddr, sizeof (long), AF_INET);
+ if (host)
+ HostName = host->h_name;
+#if 0
+ } else
+ HostName = GetNoHostName();
+#endif
+ else {
+ struct utsname un;
+ if (uname (&un) < 0) {
+ Error (ErrFatal, "GetHostName", "cannot get name of local host");
+ HostName = 0;
+ }
+ HostName = un.nodename;
+ }
+ HasHostName = true;
+ return HostName;
+}
+
+/*?nodoc?*/
+const char*
+UchInetAddress :: GetNoHostName ()
+{
+ const char* hname;
+ unsigned long saddr = Addr.sin_addr.s_addr;
+ if (saddr == INADDR_ANY)
+ hname = "ANY";
+ else if (saddr == INADDR_LOOPBACK)
+ hname = "LOOPBACK";
+ else if (saddr == INADDR_BROADCAST)
+ hname = "BROADCAST";
+ else
+ hname = "???";
+ return hname;
+}
+
+/*?
+Return a newly allocated \typ{char*} pointing to a string representing
+the internet address as "hostname:port". If no host name can be found
+then it can be "ANY" (INADDR_ANY), "LOOPBACK" (INADDR_LOOPBACK), "BROADCAST"
+(INADDR_BROADCAST) or "???".
+If argument \var{buf} is not null, return it's value in buf and do not
+allocate any memory.
+?*/
+char*
+UchInetAddress :: StrRepr (char* buf)
+{
+ const char* hname;
+ if (HasHostName) {
+ hname = HostName;
+ if (!hname)
+ hname = GetNoHostName ();
+ } else
+ hname = GetHostName ();
+ if (! buf)
+ buf = new char [ strlen(hname) + 2 + 7 ];
+ sprintf (buf, "%s::%d",hname, ntohs (Addr.sin_port));
+ return buf;
+}
+