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

  1. RCS_ID_C="$Id: amiga_api.c,v 3.7 1994/04/02 11:12:59 jraja 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: Tue Jan 26 20:44:28 1993 too
  8.  * Last modified: Mon Feb 14 16:48:27 1994 jraja
  9.  *
  10.  * HISTORY
  11.  * $Log: amiga_api.c,v $
  12.  * Revision 3.7  1994/04/02  11:12:59  jraja
  13.  * Added initialization of the resolver variables.
  14.  *
  15.  * Revision 3.6  1994/03/22  07:36:28  jraja
  16.  * Added initialization of the fdCallback to NULL. Also clearing the
  17.  * fdCallback pointer before the final CloseSocket sweep on the library
  18.  * close.
  19.  * Removed f_void definition, already defined in apicalls.h.
  20.  *
  21.  * Revision 3.5  1994/02/14  14:49:20  jraja
  22.  * Changed the default log tag to be NULL (no tag).
  23.  *
  24.  * Revision 3.4  1994/01/20  02:23:44  jraja
  25.  * Changed writeErrnoValue() to use only errno sizes of 1, 2 or 4 bytes.
  26.  *
  27.  * Revision 3.3  1994/01/12  07:17:58  jraja
  28.  * Added initialization of the Syslog() related variables of the library base.
  29.  *
  30.  * Revision 3.2  1994/01/07  15:40:29  too
  31.  * Bug fixes after revision 3.1. Now tested.
  32.  *
  33.  * Revision 3.1  1994/01/04  14:24:05  too
  34.  * Addeed compile time fd_mask and long sizeof checks.
  35.  * Added allocation of socket usage bitmask (at then end of dTable)
  36.  *
  37.  * Revision 1.50  1993/11/26  16:23:42  too
  38.  * Added sendbreaktotasks() function
  39.  *
  40.  * Revision 1.49  1993/08/09  21:28:22  ppessi
  41.  * Added revision headers and release string.
  42.  *
  43.  * Revision 1.48  1993/08/06  08:42:44  jraja
  44.  * Removed the version check, since it does not work.
  45.  *
  46.  * Revision 1.47  1993/08/05  10:35:52  jraja
  47.  * Added version check to the ELL_Open(). Now all Open requests with
  48.  * version less than 2 will be rejected.
  49.  * No requester or alert is set up yet (Add later).
  50.  *
  51.  * Revision 1.46  1993/07/15  20:17:25  too
  52.  * Changed library revision to VERSION 2 REVISION 1
  53.  *
  54.  * Revision 1.45  1993/06/12  22:56:08  too
  55.  * Added freeDataBuffer() calls to UL_Close so now selitem and netdb
  56.  * buffers are freed at CloseLIbrary()
  57.  *
  58.  * Revision 1.44  1993/06/07  12:37:20  too
  59.  * Changed inet_ntoa, netdatabase functions and WaitSelect() use
  60.  * separate buffers for their dynamic buffers
  61.  *
  62.  */
  63.  
  64. #include <bsdsocket.library_rev.h>
  65. #define RELEASESTRING "AmiTCP/IP release 3 "
  66.  
  67. /*
  68.  * NOTE: Exec has turned off task switching while in Open, Close, Expunge and
  69.  *     Reserved functions (via Forbid()/Permit()) so we should not take
  70.  *     too long in them.
  71.  */
  72.  
  73. #include <conf.h>
  74.  
  75. #include <sys/param.h>
  76. #include <sys/systm.h>
  77. #include <sys/syslog.h>
  78.  
  79. #include <kern/amiga_includes.h>
  80.  
  81. #include <api/amiga_api.h>
  82. #include <api/allocdatabuffer.h>
  83. #include <api/amiga_libcallentry.h>
  84. #include <api/apicalls.h>
  85.  
  86. #include <kern/amiga_subr.h>
  87. #include <kern/amiga_log.h>
  88.  
  89. #if sizeof (fd_mask) != 4 || sizeof (long) != 4
  90. #error AmiTCP/IP currently depends on fd_mask and longword size of 32 bits.
  91. #endif
  92.  
  93. #define SOCLIBNAME "bsdsocket.library\0\0" /* space for ".n" at the end */
  94.  
  95. /*
  96.  *  Semaphore to prevent simultaneous access to library functions.
  97.  */
  98. struct SignalSemaphore syscall_semaphore = { 0 };
  99.  
  100. /*
  101.  *  some globals.
  102.  */
  103. struct Library *MasterSocketBase = NULL;
  104. struct List    socketBaseList;         /* list of opened socket library bases */
  105. struct List    garbageSocketBaseList; /* list of libray bases not active
  106.                       anymore (NOT YET IMPLEMENTED) */
  107. struct List    releasedSocketList; /* List for sockets that are in no-one's
  108.                        context, waiting for Obtain */
  109.  
  110. extern struct Task * AmiTCP_Task; /* reference to AmiTCP/IP task information */
  111.  
  112. /*
  113.  * Declaration of variable to hold message format string when one
  114.  * task tries to use other tasks' library base pointer. moved here
  115.  * from amiga_libcallentry.h so it doens't generate code.
  116.  */
  117. const char wrongTaskErrorFmt[] =
  118.   "Task %ld (%s) attempted to use library base of Task %ld (%s).";
  119.  
  120. /*
  121.  * Instead of using exec/initializers.h we looked it as a reference
  122.  * and wrote InitTable by hand
  123.  */
  124.  
  125. /*
  126.  * OFFSET needed to be casted LONG so compiler doesn't give warning
  127.  * about casting pointer to UWORD
  128.  */
  129. #define OFFSET(structName, structEntry) \
  130.   ((LONG)(&(((struct structName *) 0)->structEntry)))
  131.  
  132. /*
  133.  * original initTable of only UWORD items doesn't work, since compiler
  134.  * doesn't know address of SOCNAME and VSTRING at compile time, and
  135.  * those are broken to 2 WORDS. therefore initTable is a structure
  136.  * constructed by hand, and those (LONG) values are set longword aligned.
  137.  */
  138. #define id_byte 0xe000
  139. #define id_word 0xd000
  140. #define id_long 0xc000
  141.  
  142. struct {
  143.   UWORD byte1; UWORD offset1; UWORD ln_type;
  144.   UWORD byte2; UWORD offset2; UWORD lib_flags;
  145.   UWORD long3; UWORD offset3; ULONG ln_Name;
  146.   UWORD word4; UWORD offset4; UWORD lib_Version;
  147.   UWORD word5; UWORD offset5; UWORD lib_Revision;
  148.   UWORD long6; UWORD offset6; ULONG lib_IdString;
  149.   UWORD end7; 
  150.   } Library_initTable = {
  151.     id_byte, OFFSET(Node, ln_Type), NT_LIBRARY << 8,
  152.     id_byte, OFFSET(Library, lib_Flags), (LIBF_SUMUSED|LIBF_CHANGED) << 8,
  153.     id_long, OFFSET(Node, ln_Name), (ULONG)SOCLIBNAME,
  154.     id_word, OFFSET(Library, lib_Version), VERSION,
  155.     id_word, OFFSET(Library, lib_Revision), REVISION,
  156.     id_long, OFFSET(Library, lib_IdString), (ULONG)RELEASESTRING VSTRING,
  157.     0x0000
  158.     };
  159. #undef id_byte
  160. #undef id_word
  161. #undef id_long
  162.  
  163. /*
  164.  * Api show and hide functions.. during these calls system is not
  165.  * inside FOrbid()/Permit() pair
  166.  */
  167.  
  168. enum {    API_SCRATCH,        /* api not initialized */
  169.     API_INITIALIZED,    /* librarybase created */
  170.     API_SHOWN,        /* librarybase made visible */
  171.     API_HIDDEN,        /* librarybase hidden */
  172.     API_FUNCTIONPATCHED    /* Api functions set to return -1 */
  173. } api_state = API_SCRATCH;
  174.  
  175.   /*
  176.    * Setting the following variable to FALSE just before making
  177.    * new socket Library base prevents ELL_Expunge, the final
  178.    * expunging function to remove library base from memory
  179.    */
  180. BOOL AmiTCP_lets_you_do_expunge = FALSE;
  181.  
  182. BOOL SB_Expunged = FALSE; /* boolean value set by ELL_Expunge */
  183.  
  184.  
  185. struct Library * SAVEDS RAF2(ELL_Open,
  186.                  struct Library *,    libPtr,        a6,
  187.                  ULONG,        version,    d0)
  188. #if 0
  189. {
  190. #endif
  191.   
  192.   extern f_void UserLibrary_funcTable[];
  193.   struct SocketBase * newBase;
  194.   LONG error;
  195.   WORD * i;
  196.  
  197.   /*
  198.    * One task may open socket library more than once. In that case caller
  199.    * receives the base it has opened already.
  200.    */
  201.   if ((newBase = FindSocketBase(SysBase->ThisTask)) != NULL) {
  202.     newBase->libNode.lib_OpenCnt++;
  203.     return (struct Library *)newBase;
  204.   }
  205.   /*
  206.    * Create new library base.
  207.    * All fields in the base will first be initialized to zero and then
  208.    * modified by initializers in initTable.
  209.    */
  210.   newBase = (struct SocketBase *)MakeLibrary(UserLibrary_funcTable,
  211.                          (UWORD *)&Library_initTable,
  212.                          NULL,
  213.                          sizeof(struct SocketBase),
  214.                          NULL);
  215.   if (newBase == NULL)
  216.     return NULL;
  217.  
  218.   /*
  219.    * add this newly allocated library base to our list of opened
  220.    * socket libraries
  221.    */    
  222.   AddTail(&socketBaseList, (struct Node *)newBase); 
  223.  
  224.   /*
  225.    * Modify some MASTER library base fields
  226.    */
  227.   libPtr->lib_OpenCnt++;        /* mark us as having another opener */
  228.   libPtr->lib_Flags&= ~LIBF_DELEXP;    /* prevent delayed expunges */
  229.  
  230.   /*
  231.    * Initialize new library base
  232.    */
  233.   for (i = (WORD *)((struct Library *)newBase + 1);
  234.        i < (WORD *)(newBase + 1);
  235.        i++)
  236.     *i = 0L;
  237.   newBase->libNode.lib_OpenCnt = 1;
  238.   newBase->errnoPtr = (VOID *)&newBase->defErrno;
  239.   newBase->errnoSize = sizeof newBase->defErrno;
  240.   newBase->thisTask = SysBase->ThisTask;
  241.   newBase->sigIntrMask = SIGBREAKF_CTRL_C;
  242.   
  243.   /* initialize syslog variables */
  244. #if 0 /* initialization to zero is implicit */
  245.   newBase->LogTag = NULL; /* no tag by default, old apps print a tag already */
  246. #endif
  247.   newBase->LogFacility = LOG_USER;
  248.   newBase->LogMask = 0xff;
  249.  
  250.   /* initialize resolver variables */
  251.   newBase->hErrnoPtr = &newBase->defHErrno;
  252.   newBase->res_socket = -1;
  253.   res_init(&newBase->res_state);
  254.  
  255.   /* initialize dtable variables */
  256. #if 0 /* initialization to zero is implicit */
  257.   newBase->fdCallback = NULL;
  258. #endif
  259.   newBase->dTableSize = FD_SETSIZE;
  260.   if ((newBase->dTable =
  261.        AllocMem(newBase->dTableSize * sizeof (