/* ************************************************************************
*   File: amiunix.h                               Part of Amiga CircleMUD *
*  Usage: header file for Unix/socket emulation defines/functions         *
*                                                                         *
*  All rights reserved.  See license.doc for complete information.        *
************************************************************************ */

#ifndef AMIUNIX_H
#define AMIUNIX_H

/* You can change MAXSOCKET to a higher value if you want more virtual 
   ports.. 20 means that 20-1=19 players at a maximum can be logged in.
*/
#define MAXSOCKET 20


/* DO NOT CHANGE ANYTHING BELOW !!!! */

#define FNDELAY 0                   
#define SIGBUS  0

#include <exec/ports.h>

static struct socket_entry {
    int  in_use;
    char *to_client;
    char *from_client;
    } sockets[MAXSOCKET];

struct connect_message {
    struct Message Msg;
    char   *port_name;
};

struct data_message {
    struct Message Msg;
    char   *buffer;
    int    length;
};

#ifndef DEVICES_TIMER_H
#include <devices/timer.h>
#endif

/*********************** ERROR STUFF *****************************/
#ifndef ERRNO_H
#define ERRNO_H

#ifndef SYS_ERRNO_H
#define SYS_ERRNO_H

#define	ENOENT	    2		/* No such file or directory */
#define	EINTR	    4		/* Interrupted system call */
#define	EIO	        5		/* Input/output error */
#define	ECHILD      10		/* No child processes */
#define	EDEADLK     11		/* Resource deadlock avoided */
#define	ENOMEM      12		/* Cannot allocate memory */
#define	EACCES      13		/* Permission denied */
#define	EFAULT      14		/* Bad address */
#define	ENOTBLK     15		/* Block device required */
#define	EBUSY       16		/* Device busy */
#define	EEXIST      17		/* File exists */
#define	EINVAL      22		/* Invalid argument */
#define	ENFILE      23		/* Too many open files in system */
#define EMFILE      24      /* Too many open files */
#define	EPIPE       32		/* Broken pipe */

#define	EAGAIN		35 		/* Resource temporarily unavailable */
#define	EWOULDBLOCK	EAGAIN	/* Operation would block */
#define	EADDRINUSE	48		/* Address already in use */
#define ENOBUFS     55      /* Could not allocate buffers */
#define	ESHUTDOWN	58		/* Can't send after socket shutdown */
#define	ECONNREFUSED	61	/* Connection refused */

#define	EHOSTUNREACH	65	/* No route to host */

extern const char * const __sys_errlist[];
extern const int __sys_nerr;

#endif /* sys_errno_h */

extern int errno;

#ifndef STRICT_ANSI
#define sys_errlist __sys_errlist
#define sys_nerr __sys_nerr
#endif

#endif /* errno_h */


#ifndef SYS_SOCKET_H
#define SYS_SOCKET_H

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

#ifndef	FD_SETSIZE
#define	FD_SETSIZE	256
#endif

typedef long fd_set;

#define	FD_SET(n, p)    fdset((n),(p))
#define	FD_CLR(n, p)	fdclr((n),(p))
#define	FD_ISSET(n, p)	fdisset((n),(p))
#define	FD_ZERO(p)	    bzero((char *)(p), sizeof(*(p)))
#endif /* sys_types_h */

/*
 * Socket Types and defines
 */

#define	SOCK_STREAM	1		/* stream socket */

/*
 * Option flags per-socket.
 */

#define	SO_REUSEADDR    0x0004		/* allow local address reuse */
#define	SO_LINGER       0x0080		/* linger on close if data present */


/*
 * Structure used for manipulating linger option.
 */
struct	linger {
	int	l_onoff;		/* option on/off */
	int	l_linger;		/* linger time */
};

/*
 * Level number for (get/set)sockopt() to apply to socket itself.
 */
#define	SOL_SOCKET	0xffff		/* options for socket level */

/*
 * Address families.
 */
#define	AF_INET		2		/* internetwork: UDP, TCP, etc. */

/*
 * Structure used by kernel to store most
 * addresses.
 */
struct sockaddr {
	u_char	sa_len;			/* total length */
	u_char	sa_family;		/* address family */
	char	sa_data[14];	/* actually longer; address value */
};

/*
 * Structure used by kernel to pass protocol
 * information in raw sockets.
 */
struct sockproto {
	u_short	sp_family;		/* address family */
	u_short	sp_protocol;	/* protocol */
};



#ifndef KERNEL

/*
 * Include socket protos/inlines/pragmas
 */

#ifndef EXEC_TYPES_H
#include <exec/types.h>
#endif

#ifndef SYS_TIME_H
#define SYS_TIME_H

struct compatible_timerequest {
    struct IORequest tr_node;
    struct timeval tr_time;
};
#define timerequest compatible_timerequest

#ifndef PROTO_TIMER_H
#include <proto/timer.h>
#endif

/*
 * predefine TimerBase to Library to follow SASC convention.
 */

#define BASE_EXT_DECL extern struct Library * TimerBase;
#define BASE_NAME (struct Device *)TimerBase

#include <clib/timer_protos.h>
#endif

#ifndef IN_H
#define IN_H

/*
 * Macros for network/external number representation conversion.
 */

#ifndef htons
#define	htons(x)	(x)
#define	HTONS(x)	(x)
#endif

/*
 * Internet address
 */
struct in_addr {
	u_long s_addr;
};


/*
 * Socket address, internet style.
 */
struct sockaddr_in {
	u_char	sin_len;
	u_char	sin_family;
	u_short	sin_port;
	struct	in_addr sin_addr;
	char	sin_zero[8];
};

#endif /* in_h */

#include <stdarg.h>

#endif /* kernel */

#endif /* sys_socket */



/*
 * Names of the interval timers, and structure
 * defining a timer setting.
 */
#define	ITIMER_REAL	    0
#define	ITIMER_VIRTUAL	1
#define	ITIMER_PROF	    2


struct	itimerval {
	struct	timeval it_interval;	/* timer interval */
	struct	timeval it_value;	/* current value */
};

#ifndef NETDB_H
#define NETDB_H

/*
 * Structures returned by network data base library.  All addresses are
 * supplied in host order, and returned in network order (suitable for
 * use in system calls).
 */
struct	hostent {
	char	*h_name;	        /* official name of host */
	char	**h_aliases;	    /* alias list */
	int	h_addrtype;	            /* host address type */
	int	h_length;	            /* length of address */
	char	**h_addr_list;	    /* list of addresses from name server */
#define	h_addr	h_addr_list[0]	/* address, for backward compatiblity */
};

#endif /* netdb_h */

/* definitions for TELNET protocol */

#define	IAC	    255		/* -1 interpret as command: */
#define	DONT	254		/* -2 you are not to use option */
#define	DO	    253		/* -3 please, you use option */
#define	WONT	252		/* -4 I won't use option */
#define	WILL	251		/* -5 I will use option */
#define	SB	250		/* interpret as subnegotiation */
#define	GA	249		/* you may reverse the line */
#define	EL	248		/* erase the current line */
#define	EC	247		/* erase the current character */
#define	AYT	246		/* are you there */
#define	AO	245		/* abort output--but let prog finish */
#define	IP	244		/* interrupt process--permanently */
#define	BREAK	243		/* break */
#define	DM	242		/* data mark--for connect. cleaning */
#define	NOP	241		/* nop */
#define	SE	240		/* end sub negotiation */
#define EOR     239             /* end of record (transparent mode) */

#define SYNCH	242		/* for telfunc calls */

/* telnet options, partly */
#define TELOPT_BINARY	0	/* 8-bit data path */
#define TELOPT_ECHO	    1	/* echo */
#define	TELOPT_RCP	    2	/* prepare to reconnect */
#define	TELOPT_SGA  	3	/* suppress go ahead */
#define	TELOPT_NAMS	    4	/* approximate message size */
#define TELOPT_NAOFFD   0       /* unknown option, so dummy value :^)   [JJ] */
#define TELOPT_NAOCRD   0       /* unknown option, so dummy value :^)   [JJ] */

#endif   /* amiunix_h */
