home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / comm / amitcp-3.0ß2.lha / AmiTCP / src / amitcp / api / getxbyy.c < prev    next >
C/C++ Source or Header  |  1994-03-26  |  12KB  |  484 lines

  1. RCS_ID_C="$Id: getxbyy.c,v 1.11 1994/03/26 09:48:48 too Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  * 
  7.  * Created: Mon Apr 19 12:01:27 1993 too
  8.  * Last modified: Thu Mar 24 21:02:19 1994 too
  9.  *
  10.  * HISTORY
  11.  * $Log: getxbyy.c,v $
  12.  * Revision 1.11  1994/03/26  09:48:48  too
  13.  * Fixed bug in getservbyname() to check given protocol string.
  14.  * Cleaned getXbyname() calls to use matchAlias() function instead
  15.  * of separate `for' loops, removing the need of `gotos'.
  16.  *
  17.  * Revision 1.10  1993/06/12  22:58:21  too
  18.  * Moved allocDataBuffer into allocdatabuffer.[hc]
  19.  *
  20.  * Revision 1.9  1993/06/07  12:37:20  too
  21.  * Changed inet_ntoa, netdatabase functions and WaitSelect() use
  22.  * separate buffers for their dynamic buffers
  23.  *
  24.  * Revision 1.8  1993/06/01  16:37:20  too
  25.  * removed reduntant hostbuf.h include. includes api/gethtbynamadr.h for
  26.  * some prototypes
  27.  *
  28.  * Revision 1.7  1993/05/14  11:34:59  ppessi
  29.  * Fixed NetDataBase locking.
  30.  *
  31.  * Revision 1.7  1993/05/14  11:34:59  ppessi
  32.  * Fixed NetDataBase locking.
  33.  *
  34.  * Revision 1.6  93/05/05  16:09:40  16:09:40  puhuri (Markus Peuhkuri)
  35.  * Fixes for final demo.
  36.  * 
  37.  * Revision 1.5  93/05/04  14:08:54  14:08:54  too (Tomi Ollila)
  38.  * Changed local api function names to the same as the names given
  39.  * to API programmer, but w/ starting _
  40.  * 
  41.  * Revision 1.4  93/05/04  12:12:04  12:12:04  jraja (Jarno Tapio Rajahalme)
  42.  * fix.
  43.  * 
  44.  * Revision 1.3  93/05/02  17:28:52  17:28:52  jraja (Jarno Tapio Rajahalme)
  45.  * Added Obtain & ReleaseSemaphores around NDB usage,
  46.  * moved Forbid()/Permit() inner.
  47.  * 
  48.  * Revision 1.2  93/04/28  13:49:06  13:49:06  too (Tomi Ollila)
  49.  * Moved macro HOSTBUF and inline function stpcpy() to hostbuf.h
  50.  * includes kern/amiga_netdb.h instead of kern/amiga_config.h
  51.  * 
  52.  * Revision 1.1  93/04/27  10:23:20  10:23:20  too (Tomi Ollila)
  53.  * Initial revision
  54.  * 
  55.  *
  56.  */
  57.  
  58. #include <conf.h>
  59.  
  60. #include <sys/param.h>
  61. #include <sys/systm.h>
  62.  
  63. #include <sys/types.h>
  64. #include <sys/socket.h>
  65. #include <netinet/in.h>
  66. #include <netdb.h>
  67.  
  68. #include <kern/amiga_includes.h>
  69.  
  70. #include <api/amiga_api.h>
  71. #include <api/amiga_libcallentry.h>
  72. #include <kern/amiga_netdb.h>
  73. #include <api/allocdatabuffer.h>
  74.  
  75. #include <api/gethtbynamadr.h> /* prototypes (NO MORE BUGS HERE) */
  76.  
  77. int strcasecmp(const char *, const char *);
  78.  
  79. #if __SASC
  80. #define strcasecmp stricmp
  81. #endif
  82.  
  83.  
  84. static long copyGenent(struct SocketBase * libPtr, 
  85.                struct DataBuffer * DB,
  86.                struct GenentNode *ent)
  87. {
  88.   long diff;
  89.  
  90.   if (allocDataBuffer(DB, ent->gn_EntSize) == FALSE) {
  91.     writeErrnoValue(libPtr, ENOMEM);
  92.     return 0;
  93.   }
  94.   
  95.   /*
  96.    * how much to add to old pointers
  97.    */
  98.   diff = (caddr_t)DB->db_Addr - (caddr_t)&ent->gn_Ent;
  99.  
  100.   /*
  101.    * copy given ent verbatim
  102.    */
  103.   bcopy((caddr_t)&ent->gn_Ent, DB->db_Addr, ent->gn_EntSize);
  104.  
  105.   return diff;
  106. }
  107.  
  108.  
  109. /*
  110.  * Host queries if Nameserver is not in use *****************************
  111.  */
  112.  
  113. /*
  114.  * Match name in aliaslist, used by `...byname()' -queries
  115.  */
  116.  
  117. static BOOL matchAlias(char ** aliases, const char * name)
  118. {
  119.   for ( ; *aliases != 0; aliases++)
  120.     if (strcmp(*aliases, name) == 0)
  121.       return TRUE;
  122.  
  123.   return FALSE;
  124. }
  125.   
  126.  
  127. /*
  128.  * Makehostent() must be called when NDB_Semaphore is obtained.
  129.  */
  130. static struct hostent * makehostent(struct SocketBase * libPtr,
  131.                     struct HostentNode * ent)
  132. {
  133.   long diff;
  134.   short i;
  135.  
  136.   if ((diff = copyGenent(libPtr, &libPtr->hostents,
  137.              (struct GenentNode *)ent)) == 0)
  138.     return NULL; /* failed to allocate memory */
  139.  
  140.   /*
  141.    * patch pointers
  142.    */
  143. #define HOSTENT ((struct hostent *)libPtr->hostents.db_Addr)
  144.   HOSTENT->h_name += diff;
  145.  
  146.   HOSTENT->h_aliases = (char **)((caddr_t)HOSTENT->h_aliases + diff);
  147.   for (i = 0; HOSTENT->h_aliases[i]; i++)
  148.     HOSTENT->h_aliases[i] += diff;
  149.   /* NULL remains null */
  150.  
  151.   HOSTENT->h_addr_list = (char **)((caddr_t)HOSTENT->h_addr_list + diff);
  152.   for (i = 0; HOSTENT->h_addr_list[i]; i++)
  153.     HOSTENT->h_addr_list[i] += diff;
  154.   /* NULL remains null */
  155.  
  156.   return HOSTENT;
  157. #undef HOSTENT
  158. }
  159.  
  160.  
  161. struct hostent * _gethtbyname(struct SocketBase * libPtr,
  162.                   const char * name)
  163. {
  164.   struct HostentNode * entNode;
  165.   struct hostent * host;
  166.  
  167.   LOCK_R_NDB(NDB);
  168.  
  169.   for (entNode = (struct HostentNode *)NDB->ndb_Hosts.mlh_Head;
  170.        entNode->hn_Node.mln_Succ;
  171.        entNode = (struct HostentNode *)entNode->hn_Node.mln_Succ)
  172.     if (strcasecmp(entNode->hn_Ent.h_name, (char *)name) == 0 ||
  173.     matchAlias(entNode->hn_Ent.h_aliases, name)) {
  174.       host = makehostent(libPtr, entNode);
  175.       UNLOCK_NDB(NDB);
  176.       return host;
  177.     }
  178.   UNLOCK_NDB(NDB);
  179.   writeErrnoValue(libPtr, 0);
  180.   return NULL;
  181. }
  182.  
  183.  
  184. struct hostent * _gethtbyaddr(struct SocketBase * libPtr,
  185.                   const char * addr, int len, int type)
  186. {
  187.   struct HostentNode * entNode;
  188.   struct hostent * host;
  189.  
  190.   LOCK_R_NDB(NDB);
  191.   for (entNode = (struct HostentNode *)NDB->ndb_Hosts.mlh_Head;
  192.        entNode->hn_Node.mln_Succ;
  193.        entNode = (struct HostentNode *)entNode->hn_Node.mln_Succ)
  194.     if (entNode->hn_Ent.h_addrtype == type &&
  195.     ! bcmp(entNode->hn_Ent.h_addr, addr, len)) {
  196.       host = makehostent(libPtr, entNode);
  197.       UNLOCK_NDB(NDB);
  198.       return host;
  199.     }
  200.   UNLOCK_NDB(NDB);
  201.   writeErrnoValue(libPtr, 0);
  202.   return NULL;
  203. }
  204.  
  205.  
  206. /*
  207.  * Network queries ****************************************************
  208.  */
  209.  
  210.  
  211. /*
  212.  * Makenetent() must be called when NDB_Semaphore is obtained.
  213.  */
  214. static struct netent * makenetent(struct SocketBase * libPtr,
  215.                   struct NetentNode * ent)
  216. {
  217.   long diff;
  218.   short i;
  219.  
  220.   if ((diff = copyGenent(libPtr, &libPtr->netents,
  221.              (struct GenentNode *)ent)) == 0)
  222.     return NULL;
  223.  
  224.   /*
  225.    * patch pointers
  226.    */
  227. #define NETENT ((struct netent *)libPtr->netents.db_Addr)
  228.   NETENT->n_name += diff;
  229.  
  230.   NETENT->n_aliases = (char **)((caddr_t)NETENT->n_aliases + diff);
  231.   for (i = 0; NETENT->n_aliases[i]; i++)
  232.     NETENT->n_aliases[i] += diff;
  233.   /* NULL remains null */
  234.  
  235.   return NETENT;
  236. #undef NETENT
  237. }  
  238.  
  239.  
  240. struct netent * SAVEDS RAF2(_getnetbyname,
  241.              struct SocketBase *,    libPtr,    a6,
  242.              const char *,        name,    a0)
  243. #if 0
  244. {
  245. #endif
  246.   struct NetentNode * entNode;
  247.   struct netent * net;
  248.   
  249.   CHECK_TASK2();
  250.  
  251.   LOCK_R_NDB(NDB);
  252.   for (entNode = (struct NetentNode *)NDB->ndb_Networks.mlh_Head;
  253.        entNode->nn_Node.mln_Succ;
  254.        entNode = (struct NetentNode *)entNode->nn_Node.mln_Succ)
  255.     if (strcmp(entNode->nn_Ent.n_name, name) == 0 ||
  256.     matchAlias(entNode->nn_Ent.n_aliases, name)) {
  257.       net = makenetent(libPtr, entNode);
  258.       UNLOCK_NDB(NDB);
  259.       return net;
  260.     }
  261.   UNLOCK_NDB(NDB);
  262.   writeErrnoValue(libPtr, 0);
  263.   return NULL;
  264. }
  265.  
  266. struct netent * SAVEDS RAF3(_getnetbyaddr,
  267.              struct SocketBase *,    libPtr,    a6,
  268.              long,            netw,    d0,
  269.              long,            type,    d1)
  270. #if 0
  271. {
  272. #endif
  273.   struct NetentNode * entNode;
  274.   struct netent * net;
  275.  
  276.   CHECK_TASK2();
  277.  
  278.   LOCK_R_NDB(NDB);
  279.   for (entNode = (struct NetentNode *)NDB->ndb_Networks.mlh_Head;
  280.        entNode->nn_Node.mln_Succ;
  281.        entNode = (struct NetentNode *)entNode->nn_Node.mln_Succ)
  282.     if (entNode->nn_Ent.n_addrtype == type && entNode->nn_Ent.n_net == netw) {
  283.       net = makenetent(libPtr, entNode);
  284.       UNLOCK_NDB(NDB);
  285.       return net;
  286.     }
  287.   UNLOCK_NDB(NDB);
  288.   writeErrnoValue(libPtr, 0);
  289.   return NULL;
  290. }
  291.  
  292.  
  293. /*
  294.  * Service queries ****************************************************
  295.  */
  296.  
  297. /*
  298.  * Makeservent() must be called when NDB_Semaphore is obtained.
  299.  */
  300. static struct servent * makeservent(struct SocketBase * libPtr,
  301.                     struct ServentNode * ent)
  302. {
  303.   long diff;
  304.   short i;
  305.  
  306.   if ((diff = copyGenent(libPtr, &libPtr->servents,
  307.              (struct GenentNode *)ent)) == 0)
  308.     return NULL;
  309.  
  310.   /*
  311.    * patch pointers
  312.    */
  313. #define SERVENT ((struct servent *)libPtr->servents.db_Addr)
  314.   SERVENT->s_name += diff;
  315.  
  316.   SERVENT->s_aliases = (char **)((caddr_t)SERVENT->s_aliases + diff);
  317.   for (i = 0; SERVENT->s_aliases[i]; i++)
  318.     SERVENT->s_aliases[i] += diff;
  319.   /* NULL remains null */
  320.  
  321.   SERVENT->s_proto += diff;
  322.  
  323.   return SERVENT;
  324. #undef SERVENT
  325. }  
  326.  
  327.  
  328. /*
  329.  * findservent is needed for external call.
  330.  */
  331. struct ServentNode * findServentNode(struct NetDataBase * ndb,
  332.                      const char * name, const char *