/*
 * $Id: amiga_api.h,v 1.27 1993/06/07 12:37:20 too Exp $
 * 
 * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
 *                    Helsinki University of Technology, Finland.
 *                    All rights reserved.
 *
 * Created: Tue Jan 26 20:35:28 1993 too
 * Last modified: Mon Jun  7 14:59:35 1993 too
 * 
 * $Log: amiga_api.h,v $
 * Revision 1.27  1993/06/07  12:37:20  too
 * Changed inet_ntoa, netdatabase functions and WaitSelect() use
 * separate buffers for their dynamic buffers
 *
 * Revision 1.26  1993/06/04  11:16:15  jraja
 * Fixes for first public release.
 *
 * Revision 1.25  1993/05/30  08:15:05  too
 * Changed struct selbuf to struct newselbuf
 *
 * Revision 1.24  1993/05/17  01:02:04  ppessi
 * Changed RCS version
 *
 * Revision 1.23  1993/04/26  13:18:04  jraja
 * Changed buffer field to type caddr_t.
 *
 * Revision 1.22  93/04/25  15:03:37  15:03:37  too (Tomi Ollila)
 * Changed buffer to a pointer type
 * 
 * Revision 1.21  93/04/25  14:57:18  14:57:18  too (Tomi Ollila)
 * Added field for size of temporary buffer for valirous library functions
 * 
 * Revision 1.20  93/04/12  00:08:21  00:08:21  jraja (Jarno Tapio Rajahalme)
 * Changed errnoPtr from WORD * to UBYTE *.
 * 
 * Revision 1.19  93/04/06  17:58:28  17:58:28  too (Tomi Ollila)
 * Added AmiTCP signals to socketbase, removed sigIO and chanfed errnosize
 * type to WORD
 * 
 * Revision 1.18  93/03/30  16:53:30  16:53:30  too (Tomi Ollila)
 * put errnoSIze in place of pad1 in SocketBase.
 * Prototype of writeErrnoValue is here, non in amiga_libcallentry.h
 * extern struct List releasedSocketList
 * 
 * Revision 1.17  93/03/19  14:14:33  14:14:33  too (Tomi Ollila)
 * Code changes at night 17-18 March 1993
 * 
 * Revision 1.16  93/03/12  15:11:35  15:11:35  too (Tomi Ollila)
 * Added nextDToSearch variable to speed up free fd search.
 * now this and dTablesize are WORDs
 * 
 * Revision 1.15  93/03/12  01:02:30  01:02:30  too (Tomi Ollila)
 * Added default storage for errno, now errnoPtr points to defErrno by default/
 * 
 * Revision 1.14  93/03/10  21:03:28  21:03:28  too (Tomi Ollila)
 * Added errnoPtr, changed pre process buffer to union type.
 * 
 * Revision 1.13  93/03/06  02:09:58  02:09:58  jraja (Jarno Tapio Rajahalme)
 * Added per process field 'buffer', which is used to hold temporary data
 * returned by library functions.
 * 
 * Revision 1.12  93/03/05  15:54:36  15:54:36  too (Tomi Ollila)
 * added prototypes for api_setfunctions and api_deinit
 * 
 * Revision 1.11  93/03/04  09:41:06  09:41:06  jraja (Jarno Tapio Rajahalme)
 * Fixed includes, changed _AMIGA_API_H_ to AMIGA_API_H.
 * 
 * Revision 1.10  93/02/26  13:22:32  13:22:32  too (Tomi Ollila)
 * code checked w/ too, ppessi and jraja
 * 
 * Revision 1.9  93/02/25  18:52:27  18:52:27  too (Tomi Ollila)
 * api_init() now returns BOOL value (always TRUE)
 * 
 * Revision 1.8  93/02/25  17:09:19  17:09:19  too (Tomi Ollila)
 * removed definition of SOCNAME, no-one needs it anyway.
 * 
 * Revision 1.7  93/02/25  13:01:38  13:01:38  too (Tomi Ollila)
 * Added static inlines, sys/cdefs etc.
 * 
 * Revision 1.6  93/02/24  18:47:18  18:47:18  too (Tomi Ollila)
 * Now FindSocketBase goes through compiler
 * 
 * Revision 1.5  93/02/24  15:49:07  15:49:07  too (Tomi Ollila)
 * Added FindSocketBase(): finds socket library base of a task if any.
 * 
 * Revision 1.4  93/02/24  15:11:35  15:11:35  too (Tomi Ollila)
 * some cosmetic changes (i quess ?)
 * 
 * Revision 1.3  93/02/16  16:02:45  16:02:45  too (Tomi Ollila)
 * added ObtainSyscallSemaphore and ReleaseSyscallSemaphore() for use.
 * 
 * Revision 1.2  93/02/16  14:52:29  14:52:29  too (Tomi Ollila)
 * Added task priority structure items, sysCallSemaphore ->syscall_semaphore
 * 
 * Revision 1.1  93/01/27  03:15:39  03:15:39  too (Tomi Ollila)
 * Initial revision
 * 
 * 
 */

#ifndef AMIGA_API_H
#define AMIGA_API_H

#ifndef EXEC_TYPES_H
#error <exec/types.h> not included.
#endif

#ifndef EXEC_LIBRARIES_H
#error <exec/libraries.h> not included.
#endif

#ifndef EXEC_SEMAPHORES_H
#error <exec/semaphores.h> not included.
#endif

#ifndef EXEC_TASKS_H
#include <exec/tasks.h>
#endif

#ifndef SYS_CDEFS_H
#include <sys/cdefs.h>
#endif

#ifndef SYS_TYPES_H
#include <sys/types.h>
#endif

#ifndef SYS_QUEUE_H
#include <sys/queue.h>
#endif

struct newselbuf;

/*
 * structure for holding size and address of some dynamically allocated buffers
 * such as selitems for WaitSelect() and netdatabase entry structures
 */
struct DataBuffer {
  int		db_Size;
  void *	db_Addr;
};

struct SocketBase {
  struct Library	libNode;
/* "Global" Errno */
  WORD          	errnoSize; 
/* -- now we are longword aligned -- */
  UBYTE *		errnoPtr; /* this points to errno */
  LONG			defErrno;
/* Task pointer of owner task */
  struct Task *		thisTask;
/* task priority changes (WORDS so we keep structure longword aligned) */  
  WORD			myPri;      /* task's priority just after libcall */
  WORD			libCallPri; /* task's priority during library call */
/* -- descriptor sets -- */
  WORD			dTableSize;
  WORD			nextDToSearch;
  struct socket	**	dTable;
/* AmiTCP signal masks */
  ULONG			sigIntrMask;
  ULONG			sigIOMask;
  ULONG			sigUrgMask;
/* -- these are used by tsleep()/wakeup() -- */
  char *		p_wmesg;
  queue_chain_t 	p_sleep_link;
  caddr_t       	p_wchan;	/* event process is awaiting */
  struct timerequest *	tsleep_timer;
  struct MsgPort *	timerPort;
/* -- pointer to select buffer during Select() -- */
  struct newselbuf *	p_sb;
/* -- per process fields used by various 'library' functions -- */
/* buffer for inet_ntoa */
  char			inet_ntoa[20]; /* xxx.xxx.xxx.xxx\0 */
/* pointers for data buffers that MAY be used */
  struct DataBuffer 	selitems;
  struct DataBuffer 	hostents;
  struct DataBuffer 	netents;
  struct DataBuffer 	protoents;
  struct DataBuffer	servents;
};

extern struct SignalSemaphore syscall_semaphore;
extern struct List releasedSocketList;

/*
 *  Functions to put and remove application library to/from exec library list
 */
BOOL api_init(VOID);
BOOL api_show(VOID);
VOID api_hide(VOID);
VOID api_setfunctions(VOID);
VOID api_deinit(VOID);

/* Function which set Errno value */

VOID writeErrnoValue(struct SocketBase *, int);

/*
 * inline functions which changes (raises) task priority while it is
 * executing library functions
 */

static inline void ObtainSyscallSemaphore(struct SocketBase *libPtr)
{
  extern struct Task *AmiTCP_Task;

  ObtainSemaphore(&syscall_semaphore);
  libPtr->myPri = SetTaskPri(libPtr->thisTask,
			     libPtr->libCallPri = AmiTCP_Task->tc_Node.ln_Pri);
}

static inline void ReleaseSyscallSemaphore(struct SocketBase *libPtr)
{
  if (libPtr->libCallPri != (libPtr->myPri = SetTaskPri(libPtr->thisTask,
							libPtr->myPri)))
    SetTaskPri(libPtr->thisTask, libPtr->myPri);
  ReleaseSemaphore(&syscall_semaphore);
}

/*
 * inline function for searching library base when taskpointer is known
 */

static inline struct SocketBase *FindSocketBase(struct Task *task)
{
  extern struct List socketBaseList;
  struct Node *libNode;

  Forbid();
  for (libNode = socketBaseList.lh_Head; libNode->ln_Succ;
       libNode = libNode->ln_Succ)
    if (((struct SocketBase *)libNode)->thisTask == task) {
      Permit();
      return (struct SocketBase *)libNode;
    }
  /* here if Task wasn't in socketBaseList */
  Permit();
  return NULL;
}

#endif /* !AMIGA_API_H */
