summaryrefslogtreecommitdiff
path: root/comm/InetAddress.cc
diff options
context:
space:
mode:
authorsc2000-11-28 17:07:47 +0000
committersc2000-11-28 17:07:47 +0000
commitee937667fd0ecd82faab4c88d756b906fb625f1a (patch)
tree19e679318b5cb87e8be1a05a7bbc9ba5568d0814 /comm/InetAddress.cc
parent1326b11d65f7020f5f6c691305d2c090b064bd04 (diff)
downloadivy-league-ee937667fd0ecd82faab4c88d756b906fb625f1a.zip
ivy-league-ee937667fd0ecd82faab4c88d756b906fb625f1a.tar.gz
ivy-league-ee937667fd0ecd82faab4c88d756b906fb625f1a.tar.bz2
ivy-league-ee937667fd0ecd82faab4c88d756b906fb625f1a.tar.xz
Integration into IvyLeague
Uvh -> Ivl Multiplexer.* is renamed into Scheduler.* A few name conflicts in the merger with ex-DNN have been solved Imakefile is replaced by Makefile Created InetAddress.* and UnixAddress.* from Address.* Created IrdaAddress.* OLD/TextStream has been updated
Diffstat (limited to 'comm/InetAddress.cc')
-rw-r--r--comm/InetAddress.cc251
1 files changed, 251 insertions, 0 deletions
diff --git a/comm/InetAddress.cc b/comm/InetAddress.cc
new file mode 100644
index 0000000..ffb374e
--- /dev/null
+++ b/comm/InetAddress.cc
@@ -0,0 +1,251 @@
+/*
+ * The Unix Channel
+ *
+ * by Michel Beaudouin-Lafon
+ *
+ * Copyright 1990-1997
+ * Laboratoire de Recherche en Informatique (LRI)
+ *
+ * INET Addresses
+ *
+ * $Id$
+ * $CurLog$
+ * Created from Address.cc
+ */
+
+#include "InetAddress.h"
+
+#include <sys/utsname.h>
+#include <stdio.h>
+
+#ifdef __osf__
+extern "C" {
+ unsigned short htons (unsigned short);
+ unsigned short ntohs (unsigned short);
+ int gethostname (char*, int);
+#endif
+#include <netdb.h>
+#ifdef __osf__
+}
+#endif
+
+
+// constructors for inet addresses
+// inet address specified by:
+// hostname / port# (ANY if hostname is NIL, LOOPBACK if hostname is "")
+// hostid / port# (predefined hostids ANYADDR, ...)
+// todo:
+// hostname / service name ??
+//
+/*?
+IvlInetAddresses should only be allocated with new because they
+can be deleted and reallocated by \type{IvlSocket} class.
+?*/
+IvlInetAddress :: IvlInetAddress ()
+: IvlAddress (),
+ HostName (),
+ HasHostName (false)
+{
+}
+
+/*?
+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 (INADDR_ANY) is used.
+If the port number is 0, it will be allocated by the system.
+?*/
+IvlInetAddress :: IvlInetAddress (const char* name, sword port)
+: IvlAddress (),
+ HostName (),
+ HasHostName (false)
+{
+ if (name && *name) {
+ struct hostent *host = gethostbyname (name);
+ if (! host)
+ return;
+ memcpy (&Addr.sin_addr, host->h_addr, host->h_length);
+ } else
+ Addr.sin_addr.s_addr = name ? INADDR_LOOPBACK : INADDR_ANY;
+ Addr.sin_family = AF_INET;
+ Addr.sin_port = htons (port);
+ Valid = true;
+}
+
+/*?
+Construct an inet address, from a host id (internet address) and a port number.
+If the port number is 0, it is assigned by the system.
+The host ids \var{^{ANYADDR}}, \var{^{LOOPBACK}} and \var{^{BROADCAST}} are predefined.
+\var{ANYADDR} is used to receive messages from anywhere.
+\var{LOOPBACK} is the local host address.
+\var{BROADCAST} makes it possible to send data to all the hosts of a local area network.
+?*/
+IvlInetAddress :: IvlInetAddress (lword host, sword port)
+: IvlAddress (),
+ HostName (),
+ HasHostName (false)
+{
+ Addr.sin_family = AF_INET;
+ Addr.sin_port = htons (port);
+ Addr.sin_addr.s_addr = host;
+ Valid = true;
+}
+
+/*?nodoc?*/
+IvlInetAddress :: ~IvlInetAddress ()
+{
+}
+
+/*?nodoc?*/
+int
+IvlInetAddress :: Family ()
+{
+ return AF_INET;
+}
+
+/*?nodoc?*/
+int
+IvlInetAddress :: Length ()
+{
+ return sizeof (Addr);
+}
+
+/*?nodoc?*/
+SockAddr*
+IvlInetAddress :: GetSockAddr ()
+{
+ return (SockAddr*) &Addr;
+}
+
+
+/*?
+This is a global function (static member of class \typ{IvlInetAddress}.
+It returns the internet address of the local host.
+This is different from the predefined address \var{LOOPBACK}:
+\var{LOOPBACK} is the same constant on each machine, so that it cannot
+be communicated across the network.
+The value returned by \fun{LoopBack} is the unique internet address of the local host.
+Thus it can be communicated to another host across the network.
+?*/
+lword
+IvlInetAddress :: LoopBack ()
+{
+ static lword loopback = 0;
+
+ if (loopback)
+ return loopback;
+
+ struct hostent *host;
+
+#if 0
+ char name [128];
+ gethostname (name, sizeof (name));
+ host = gethostbyname (name);
+#else
+ struct utsname un;
+ if (uname (&un) < 0) {
+ Error (ErrFatal, "address", "cannot get name of local host");
+ return 0;
+ }
+ host = gethostbyname (un.nodename);
+#endif
+
+ if (! host) {
+ Error (ErrFatal, "address", "cannot get address of local host");
+ return 0;
+ }
+ return loopback = * ((lword *) host->h_addr);
+}
+
+#ifdef DOC
+// Ca c'est les super fake declarations pour avoir la DOC
+// merci au LRI...
+/*?
+Return the host number of the address.
+?*/
+lword
+IvlInetAddress :: Host ()
+{}
+
+/*?
+Return the port number of the address.
+?*/
+sword
+IvlInetAddress :: 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*
+IvlInetAddress :: 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*
+IvlInetAddress :: 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*
+IvlInetAddress :: 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;
+}