home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / MsgOps / etherCode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  6.8 KB  |  220 lines

  1.  
  2. /*
  3.  * @(#)etherCode.c    1.3  2/23/90
  4.  */
  5.  
  6. /* File: /u1/Eden/Kernel/MsgOps/EtherCode.c */
  7.  
  8. /*
  9.  * $Header:   /u1/Eden/Kernel/MsgOps/RCS/EtherCode.v  Revision 3.4  85/03/14 21:03:23  eric Exp$
  10.  * INTERFACE:   None.
  11.  *
  12.  * FUNCTION:    Provides the Ethernet address primitives.
  13.  *
  14.  * IMPORTS:     /u1/Eden/Source/MsgOps/EtherTypes.h.
  15.  *
  16.  * EXPORTS:     mEtherNull, mEtherCopy, mEtherIsEqual, mEqualNetwork, 
  17.  *              mMakeEtherNetAddr, mGetHostName.
  18.  *
  19.  * DESIGN:      Ethernet addresses are Unix 4.2 sockaddr_in structures
  20.  *              (see socket(2) ).
  21.  *
  22.  * $Log:        /u1/Eden/Source/MsgOps/RCS/EtherCode.v $
  23.  * Revision 3.6  86/12/05  22:45:17  eric
  24.  * Put default port number into mMakeEtherNetAddress.
  25.  * Revision 3.5  86/04/02  15:19:17  oystr
  26.  * Got rid of mMakeNextEtherNetAddress which is not used
  27.  * by any body.  God only knows how much more of the stuff we
  28.  * are hauling is obsolete.
  29.  *
  30.  * Revision 3.4  85/03/14  21:03:23  eric
  31.  * Fixed MXTrace levels.
  32.  *
  33.  * Revision 3.3  84/11/29  21:03:23  schwartz
  34.  * Changed routine mGetHostName to use table of Eden machine string names /
  35.  * ethernet addresses instead of Unix system call gethostbyaddr (which does
  36.  * file I/O)
  37.  *
  38.  * Revision 3.2  84/11/15  03:00:04  schwartz
  39.  * Added the routine mGetHostName.
  40.  *
  41.  * Revision 3.1  84/08/19  19:40:02  schwartz
  42.  * Added the routines mEqualNetwork, mMakeEtherNetAddr and 
  43.  * mMakeNextEtherNetAddr.
  44.  *
  45.  * Revision 3.0  84/07/07  23:21:31  schwartz
  46.  * Changed code to accomodate usage of Unix 4.2 sockets for network
  47.  * interprocess communication
  48.  *
  49.  * Revision 1.7  83/10/14  16:37:12  mager
  50.  * Made mEtherNull and mEtherCopy block moves.
  51.  * 
  52.  * Revision 1.5  83/02/25  12:15:26  cady
  53.  * Added new trace levels.
  54.  * 
  55.  * Revision 1.4  83/02/24  16:39:32  cady
  56.  * Replaced conditional debug trace with dynamic trace.
  57.  * Added conditional Kernel compilation.
  58.  * 
  59.  * Revision 1.3  83/02/22  12:05:38  cady
  60.  * Replaced #if Debug with #ifdef Debug.
  61.  * 
  62.  * Revision 1.2  83/01/06  17:36:08  cady
  63.  * Fixed mEtherPrint fFile type definition.
  64.  * 
  65.  * Revision 1.1  83/01/06  14:10:22  cady
  66.  * Initial revision
  67.  * 
  68.  * 5-Jan-1983   Initial implementation.  S. Cady.
  69.  */
  70.  
  71. #include <stdio.h>
  72. #include <netdb.h>
  73.  
  74. #include "Kernel/h/mmTypes.h"
  75. #include "Kernel/h/mmEthrTypes.h"
  76. #include "Kernel/h/mmCodes.h"
  77. extern char *inet_ntoa();
  78.  
  79.  
  80. /****************************************************************/
  81. /*                                                              */
  82. /*                      mEtherNull                              */
  83. /*                                                              */
  84. /*  mEtherNull sets the specified variable to the null Ethernet */
  85. /*  address.                                                    */
  86. /*                                                              */
  87. /****************************************************************/
  88.  
  89. void mEtherNull( fEther
  90.                )
  91.   EtherNetAddress *fEther;
  92. {
  93.   static EtherNetAddress NULLETHER = {0};  /* All zeros, believe it or not */
  94.  
  95.     MXTraceMsg(6, "mEtherNull( %d )\n", fEther);
  96.  
  97.   *fEther = NULLETHER;    
  98. }
  99.  
  100. /****************************************************************/
  101. /*                                                              */
  102. /*                      mEtherCopy                              */
  103. /*                                                              */
  104. /*  mEtherCopy copies the Ethernet address fSource into fDest.  */
  105. /*                                                              */
  106. /****************************************************************/
  107.  
  108. void mEtherCopy( fSource,
  109.                  fDest
  110.                )
  111.   EtherNetAddress *fSource;
  112.   EtherNetAddress *fDest;
  113. {
  114.   MXTraceMsg(5, "mEtherCopy( %d, %d )\n", fSource, fDest);
  115.  
  116.   *fDest = *fSource;
  117.  
  118. }
  119.                
  120. /****************************************************************/
  121. /*                                                              */
  122. /*                      mEtherIsEqual                           */
  123. /*                                                              */
  124. /*  mEtherIsEqual compares two Ethernet addresses and returns   */
  125. /*  True if they are equal and False if they are not equal.     */
  126. /*                                                              */
  127. /****************************************************************/
  128.  
  129. Boolean mEtherIsEqual( fEther1,
  130.                        fEther2
  131.                      )
  132.   EtherNetAddress *fEther1;
  133.   EtherNetAddress *fEther2;
  134. {
  135.   MXTraceMsg(6, "mEtherIsEqual( %d, %d )\n", fEther1, fEther2);
  136.  
  137.   return ( (Boolean) 
  138.        (fEther1->sin_addr.s_addr == fEther2->sin_addr.s_addr)
  139.      );
  140. }
  141.  
  142. /* mEqualNetwork Returns true is fAddr1 and fAddr2 are in the same
  143.    local network */
  144. Boolean mEqualNetwork(fAddr1, fAddr2)
  145. EtherNetAddress *fAddr1, *fAddr2;
  146. {
  147.     return( inet_netof(fAddr1->sin_addr) ==
  148.         inet_netof(fAddr2->sin_addr) );
  149. }
  150.  
  151. /* Initialize fields of EtherNetAddress given name of host and service name.
  152.    A null service name means take any port. */
  153.  
  154. KKStatus mMakeEtherNetAddr(fHostName,    /* "in" parameter */
  155.                            fServiceName, /* "in" parameter */
  156.                            fDefaultPort, /* "in" parameter */
  157.                            fAddr)        /* "out" parameter */
  158. char *fHostName;
  159. char *fServiceName;
  160. int     fDefaultPort;
  161. EtherNetAddress *fAddr;
  162. {
  163.     struct hostent *hp;
  164.     struct servent *sp;
  165.     unsigned short portNumber;
  166.  
  167.     hp = gethostbyname (fHostName);
  168.     if (hp == NULL)
  169.         return (MMSF_NoPhysHost);
  170.  
  171.     if (fServiceName == NULL) 
  172.         portNumber = 0;  /* This requests Unix to take any available port */
  173.     else {
  174.         sp = NULL; /* getservbyname(fServiceName, "udp"); */
  175.         if (sp == NULL)
  176.             portNumber = htons((u_short) fDefaultPort);
  177.         else
  178.             portNumber = sp->s_port;
  179.     }
  180.  
  181.     COPYADDR(hp->h_addr,&(fAddr->sin_addr.s_addr), hp->h_length);
  182.     fAddr->sin_port = portNumber;
  183.     fAddr->sin_family = AF_INET;
  184.  
  185.     return(MMSS_Success);
  186. }
  187.  
  188. /* mGetHostName returns a pointer to a character string containing the
  189.    name of the physical host at Ethernet address fEtherAddr */
  190. char *mGetHostName(fEtherAddr)
  191. EtherNetAddress *fEtherAddr;
  192. {
  193.     extern int NumEmMachines;
  194.     extern char *EmMachineNames[];
  195.     extern EtherNetAddress EmMachineAddrs[];
  196.     char buf[200];
  197.     int i;
  198.     struct hostent *host;
  199.     struct in_addr theEtherAddr;
  200.  
  201.     for (i = 0; i < NumEmMachines; i++)
  202.         if (mEtherIsEqual(&(EmMachineAddrs[i]), fEtherAddr)) 
  203.             return(EmMachineNames[i]);
  204.  
  205.     /* Hmm, no local name */
  206.  
  207.     theEtherAddr = fEtherAddr->sin_addr;
  208.     host = gethostbyaddr((char *)&theEtherAddr, sizeof(theEtherAddr), AF_INET);
  209.     if (host != NULL) {
  210.     sprintf(buf, "%s", host->h_name);
  211.     } else {
  212.     sprintf(buf, "%s", inet_ntoa(theEtherAddr));
  213.     };
  214.     return(buf);
  215. }
  216.  
  217. /****************************************************************/
  218. /*                      End of Ethernet Library                 */
  219. /****************************************************************/
  220.