/* * The Unix Channel * * by Michel Beaudouin-Lafon * * Copyright 1990-1997 * Laboratoire de Recherche en Informatique (LRI) * * Addresses * * $Id$ * $CurLog$ */ #include "Address.h" #include "error.h" #include #include #include #include #include #include #ifdef __osf__ extern "C" { #endif #include #ifdef __osf__ } #endif #ifdef __osf__ extern "C" { unsigned short htons (unsigned short); unsigned short ntohs (unsigned short); int gethostname (char*, int); } #endif /*?class UchAddress 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. 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. There are currently two derived classes, for Unix domain addresses and Internet domain addresses. 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. ?*/ /*? Construct an invalid address. ?*/ UchAddress :: UchAddress () : Valid (false) { } /*?nodoc?*/ UchAddress :: ~UchAddress () { } #ifdef DOC // fake entries for inline functions /*? Return true if the address is valid. ?*/ bool UchAddress :: IsValid () { } /*? 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 () { } /*?nextdoc?*/ int UchAddress :: Length () { } /*? Return the address structure and its length. \typ{^{SockAddr}} is a typedef for \typ{^{struct sockaddr}}. ?*/ SockAddr* UchAddress :: GetSockAddr () { } /*?nodoc?*/ char* UchAddress :: 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 (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}): \begin{ccode} typedef union { struct sockaddr sa; // default struct sockaddr_un su; // Unix struct sockaddr_in si; // inet } GEN_ADDR; \end{ccode} ?*/ UchAddress* UchAddress :: 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); #endif case AF_INET : return new UchInetAddress (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; }