summaryrefslogtreecommitdiff
path: root/comm/Address.cc
diff options
context:
space:
mode:
Diffstat (limited to 'comm/Address.cc')
-rw-r--r--comm/Address.cc343
1 files changed, 27 insertions, 316 deletions
diff --git a/comm/Address.cc b/comm/Address.cc
index d0853d1..01bc037 100644
--- a/comm/Address.cc
+++ b/comm/Address.cc
@@ -10,6 +10,8 @@
*
* $Id$
* $CurLog$
+ * Smart(ies) removed by Stephane Sire
+ * Added buffered hostname to IvlInetAddress
*/
#include "Address.h"
@@ -20,30 +22,20 @@
#include <unistd.h>
#include <stdio.h>
#include <memory.h>
-#include <sys/utsname.h>
+
+/* all this is necessary for function Decode only. This should be investigated */
+#include "UnixAddress.h"
+#include "InetAddress.h"
+#include "IrdaAddress.h"
-#ifdef __osf__
-extern "C" {
-#endif
-#include <netdb.h>
-#ifdef __osf__
-}
-#endif
-#ifdef __osf__
-extern "C" {
- unsigned short htons (unsigned short);
- unsigned short ntohs (unsigned short);
- int gethostname (char*, int);
-}
-#endif
-/*?class UchAddress
+/*?class IvlAddress
This class is a virtual base class: no objects of this class are ever created.
It implements the Unix type \typ{^{struct sockaddr}}, used for creating sockets.
Addresses are used mainly as arguments of constructors for the class
-\typ{UchSocket} and its derived classes.
+\typ{IvlSocket} and its derived classes.
All its member functions are virtual.
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.
@@ -57,13 +49,13 @@ static member function \fun{Decode} for example when a socket is bound.
/*?
Construct an invalid address.
?*/
-UchAddress :: UchAddress ()
+IvlAddress :: IvlAddress ()
: Valid (false)
{
}
/*?nodoc?*/
-UchAddress :: ~UchAddress ()
+IvlAddress :: ~IvlAddress ()
{
}
@@ -74,7 +66,7 @@ UchAddress :: ~UchAddress ()
Return true if the address is valid.
?*/
bool
-UchAddress :: IsValid ()
+IvlAddress :: IsValid ()
{
}
@@ -83,13 +75,13 @@ Return one of \var{AF\_UNSPEC}, \var{AF\_UNIX} or \var{AF\_INET}.
Other values may be defined if supported by the system.
?*/
int
-UchAddress :: Family ()
+IvlAddress :: Family ()
{
}
/*?nextdoc?*/
int
-UchAddress :: Length ()
+IvlAddress :: Length ()
{
}
@@ -98,175 +90,22 @@ Return the address structure and its length.
\typ{^{SockAddr}} is a typedef for \typ{^{struct sockaddr}}.
?*/
SockAddr*
-UchAddress :: GetSockAddr ()
+IvlAddress :: GetSockAddr ()
{
}
/*?nodoc?*/
char*
-UchAddress :: StrRepr (char* buf)
+IvlAddress :: StrRepr (char* buf)
{
}
#endif /* DOC */
-#ifdef UNIX_SOCK
-
-// constructors for Unix addresses
-// a Unix domain address is just a filename
-// -> some problems: check for access, when to unlink it ?
-// to do : generate name from a template (for uniqueness)
-//
-
-/*?nodoc?*/
-UchUnixAddress :: UchUnixAddress ()
-: UchAddress ()
-{
-}
-
-/*?
-Create a Unix domain address associated to file \var{filename}.
-The file is not unlinked when the address is destroyed;
-it should be unlinked by the application whenever the last socket using this address is closed.
-?*/
-UchUnixAddress :: UchUnixAddress (const char* filename)
-: UchAddress ()
-{
- Addr.sun_family = AF_UNIX;
- strcpy (Addr.sun_path, filename);
- Valid = true;
- // should check access to file ???
- // unlink if already there ?
-}
-
-/*?nodoc?*/
-UchUnixAddress :: ~UchUnixAddress ()
-{
- // how to unlink the file ??
-}
-
-/*?nodoc?*/
-int
-UchUnixAddress :: Family ()
-{
- return AF_UNIX;
-}
-
-/*?nodoc?*/
-int
-UchUnixAddress :: Length ()
-{
- if (Valid)
- return (Addr.sun_path + strlen (Addr.sun_path)) - (char*) &Addr;
- return 0;
-}
-
-/*?nodoc?*/
-SockAddr*
-UchUnixAddress :: GetSockAddr ()
-{
- return (SockAddr*) &Addr;
-}
-
-/*?nodoc?*/
-char*
-UchUnixAddress :: StrRepr (char* buf)
-{
- sprintf (buf, "%s", Addr.sun_path);
- return buf;
-}
-
-#endif /* UNIX_SOCK */
-
-// 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 ??
-//
-/*?
-UchInetAddresses should only be allocated with new because they
-can be deleted and reallocated by \type{UchSocket} class.
-?*/
-UchInetAddress :: UchInetAddress ()
-: UchAddress (),
- 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.
-?*/
-UchInetAddress :: UchInetAddress (const char* name, sword port)
-: UchAddress (),
- 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.
-?*/
-UchInetAddress :: UchInetAddress (lword host, sword port)
-: UchAddress (),
- HostName (),
- HasHostName (false)
-{
- Addr.sin_family = AF_INET;
- Addr.sin_port = htons (port);
- Addr.sin_addr.s_addr = host;
- Valid = true;
-}
-
-/*?nodoc?*/
-UchInetAddress :: ~UchInetAddress ()
-{
-}
-
-/*?nodoc?*/
-int
-UchInetAddress :: Family ()
-{
- return AF_INET;
-}
-
-/*?nodoc?*/
-int
-UchInetAddress :: Length ()
-{
- return sizeof (Addr);
-}
-/*?nodoc?*/
-SockAddr*
-UchInetAddress :: GetSockAddr ()
-{
- return (SockAddr*) &Addr;
-}
/*?
-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
+This is a global function (static member of class \typ{IvlAddress}).
+It creates an object of a derived class of \typ{IvlAddress} from a generic address
(thus is cannot be replaced by a constructor).
A generic address is the following union of address structures
(it is typically returned by system calls like \fun{recvfrom}):
@@ -274,157 +113,29 @@ A generic address is the following union of address structures
typedef union {
struct sockaddr sa; // default
struct sockaddr_un su; // Unix
+ struct sockaddr_irda sir; // irda
struct sockaddr_in si; // inet
} GEN_ADDR;
\end{ccode}
?*/
-UchAddress*
-UchAddress :: Decode (GEN_ADDR* addr, int alen)
+IvlAddress*
+IvlAddress :: Decode (GEN_ADDR* addr, int alen)
{
switch (addr->sa.sa_family) {
#ifdef UNIX_SOCK
case AF_UNIX :
addr->su.sun_path [alen] = 0;
- return new UchUnixAddress (addr->su.sun_path);
+ return new IvlUnixAddress (addr->su.sun_path);
+#endif
+#ifdef IRDA_SOCK
+ case AF_IRDA :
+ return new IvlIrdaAddress (addr->sir.sir_name);
#endif
case AF_INET :
- return new UchInetAddress (addr->si.sin_addr.s_addr, addr->si.sin_port);
+ return new IvlInetAddress (addr->si.sin_addr.s_addr, addr->si.sin_port);
default :
return 0;
}
}
-/*?
-This is a global function (static member of class \typ{UchInetAddress}.
-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
-UchInetAddress :: 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
-UchInetAddress :: Host ()
-{}
-
-/*?
-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;
-}
-