/****************************************************************************
*			BBCNB32B.DLL :	General Header file for BBC NetBIOS implemenation	 *
*																									 *
*			(c) BBC Manchester 1993-2008	:	Author Simon Dowson					 *
*																									 *
* History :																						 *
*																									 *
*				01/10/93		:	Beta release 1.00											 *
*				23/12/93		:	All receives now LocalAlloc()                    *
* 1.00.00	29/12/93		:	NetVersion() added										 *
*									Implementation specific error codes added			 *
*									with bit 7 of NCB return code set					 *
* 1.01.00	07/01/98		:  Allows adaptor to be specified in INI file		 *
* 2.00.00	12/08/98		:  Converted to use Netbios() call for 32 bit dll	 *
* 3.00.00   28/10/99		:  Ported to 32 bit BBCNB32B.DLL                    *
* 3.00.01   03/03/00    :  Expanded return code strings in RC file          *
* 3.00.02   07/11/02    :  Adaptor parameter added to all functions         *
* 3.00.03   07/02/03    :  Modified NCB to use calloc() and free() 			 *
* 3.00.04   07/02/07		:	Built from CVS
* 3.00.05   14/05/08    :  Added NCBSTAT call and session table functions   *  


* BBCNB32 : version history (combined bbcnb32b and bbcnb32m)
* 1.0.0.1   02/03/09	: first versioned release
* 1.0.0.2   02/03/09	: removes somewhat historical  references to bbc_nbif.ini 
*						  - V3 uses CSI.ini and V4 uses bncs_system.ini
* 1.0.0.3	05/03/09	uses sizedBuffer which by default has additional null character (doesn't 
						add to the returned buffer size but ensures that buffer is 
						null terminated if used directly as a string
* 1.0.0.4	17/03/09	Adds disklog to netretcode to log errors
* 1.0.0.5   19/03/09	fixes net reset
* 1.0.0.6   23/03/09    doesn't try and hang a buffer out when the previous call was cancelled
****************************************************************************/

#include <direct.h>
#include <dos.h>
#include <stdlib.h>
#include <nb30.h>
#include <queue>


using namespace std;
#include "sizedBuffer.h"

#define WM_NETBIOS			  WM_USER+2000
#define NETBIOS_NAME_LENGTH  16     /* Maximum NetBIOS name         */
#define NET_ERROR				  0x80
#define MAX_MSGLENGTH			  	512

#define DEBUG_OFF		0
#define DEBUG_ON		1
#define DEBUG_EXTRA	2

#ifdef __cplusplus
extern "C" {
#endif

// Function prototypes
/****************************************************************************
	 FILE: BBC_NB32.C - Contains Initialisiation Routines
****************************************************************************/
void __declspec(dllexport) FAR PASCAL NetCmdCode(LPSTR, BYTE);
void __declspec(dllexport) FAR PASCAL NetRetCode(LPSTR, BYTE);
UINT __declspec(dllexport) FAR PASCAL NetVersion(void);
VOID __declspec(dllexport) FAR PASCAL NetCopyName (LPSTR, LPSTR);

NCB_POST PostRoutine(PNCB);
NCB_POST PostRoutineNCBDGRECV(PNCB);
void __declspec(dllexport) FAR PASCAL NetDebugMode(BOOL);

/****************************************************************************
	 FILE: NCBALLOC.C  -  Contains Memory Allocation Routines
****************************************************************************/

PNCB __declspec(dllexport) FAR PASCAL NetAllocNcbAndBuf( DWORD dwDataLen );
VOID __declspec(dllexport) FAR PASCAL	NetNcbFree       ( PNCB  pncbNcbWithBuffer);

/****************************************************************************
	 FILE: NCBCALLS.C - Contains routines which submit NCBs for caller.
****************************************************************************/

//Synchronous calls
BYTE __declspec(dllexport) FAR PASCAL NetAddName   	 	(LPSTR, LPBYTE, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetAddGroupName 	(LPSTR, LPBYTE, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetDeleteName 	(LPSTR, BYTE, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetHangUp    	 	(BYTE, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetCancel    	 	(PNCB, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetReset			(BYTE, BYTE, BYTE);

//Asynchronous calls
BYTE __declspec(dllexport) FAR PASCAL NetAdaptorStatus (HWND, WORD, LPSTR, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetSessionStatus		(HWND, LPSTR, PNCB *, BYTE, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetCall 			(HWND, LPSTR, LPSTR, BYTE, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetListen 		 	(HWND, LPSTR, LPSTR, BYTE, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetSend			 	(HWND, LPSTR, WORD, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetSendDG			(HWND, LPSTR, WORD, LPSTR, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetSendBCDG		(HWND, LPSTR, WORD, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetReceive 		(HWND, WORD, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetReceiveAny	 	(HWND, WORD, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetReceiveDG		(HWND, WORD, BYTE, PNCB FAR *, BYTE);
BYTE __declspec(dllexport) FAR PASCAL NetReceiveBCDG	(HWND, WORD, BYTE, PNCB FAR *, BYTE);

/* Time out values */
#define CALL_TIME_OUT	 (double) (30 * 2) /* 2 Minutes */
#define SEND_TIME_OUT	 (double) (30 * 2) /* 2 Minutes */
#define RECEIVE_TIME_OUT (double) (60 * 2) /* 2 Minutes */

#define SEND_NCB_STO	(BYTE) ((2 * 30)* 1)   /* 1 Minutes in 1/2 sec intervals */
#define RECEIVE_NCB_RTO (BYTE) ((2 * 60)* 1)/* 1 Minutes in 1/2 sec intervals */

/****************************************************************************
	 FILE: NCBSUFU.C  -  General support Routines
****************************************************************************/

VOID FAR PASCAL NetCopyName 		(LPSTR, LPSTR);
VOID FAR PASCAL NetInitNCB			(void);
int  FAR PASCAL NetSetNCB			(PNCB, int);
int  FAR PASCAL NetGetNCB			(PNCB, PNCB);
int  FAR PASCAL NetClrNCB			(PNCB);
BYTE __declspec(dllexport) FAR PASCAL NetDumpSessionStatus(LPSTR acSessionName, BYTE bAdaptor);
void NetODS(LPCSTR szFmt, ...);

#define MAX_MESSAGELENGTH 512
#define MAXNCBSTACK 256

#ifdef __cplusplus
}
#endif
int __declspec(dllexport) dgMsgQueueSize( void );
sizedBuffer __declspec(dllexport) dgMsgQueueNextItem( void );
