/*
 * Copyright 1989 Network Computing Devices, Inc., Mountain View, California.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of N.C.D. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  N.C.D. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 */

#ifndef _XDMCP_H_
#ifndef __stdlib_h
#include <stdlib.h>
#endif
#ifndef XMD_H
#include "Xmd.h"
#endif

#define _XDMCP_H_
#define XDM_PROTOCOL_VERSION	1
#define XDM_UDP_PORT		177
#define XDM_MAX_MSGLEN		8192
#define XDM_MIN_RTX		2
#define XDM_MAX_RTX		32
#define XDM_RTX_LIMIT		7
#define XDM_KA_RTX_LIMIT	4
#define XDM_DEF_DORMANCY	(3 * 60)	/* 3 minutes */
#define XDM_MAX_DORMANCY	(24 * 60 * 60)	/* 24 hours */

typedef enum {
    BROADCAST_QUERY = 1, QUERY, INDIRECT_QUERY, FORWARD_QUERY,
    WILLING, UNWILLING, REQUEST, ACCEPT, DECLINE, MANAGE, REFUSE, 
    FAILED, KEEPALIVE, ALIVE 
} xdmOpCode;

typedef enum {
    XDM_QUERY, XDM_BROADCAST, XDM_INDIRECT, XDM_COLLECT_QUERY,
    XDM_COLLECT_BROADCAST_QUERY, XDM_COLLECT_INDIRECT_QUERY,
    XDM_START_CONNECTION, XDM_AWAIT_REQUEST_RESPONSE,
    XDM_AWAIT_MANAGE_RESPONSE, XDM_MANAGE, XDM_RUN_SESSION, XDM_OFF,
    XDM_AWAIT_USER_INPUT, XDM_KEEPALIVE, XDM_AWAIT_ALIVE_RESPONSE
} xdmcp_states;

#if 0	/* Not defined */
/* table of hosts */

#define XDM_MAX_STR_LEN 21
#define XDM_MAX_HOSTS 20
struct xdm_host_table {
  struct sockaddr_in sockaddr;
  char name[XDM_MAX_STR_LEN];
  char status[XDM_MAX_STR_LEN];
};
#endif

typedef CARD8	*CARD8Ptr;
typedef CARD16	*CARD16Ptr;
typedef CARD32	*CARD32Ptr;

typedef struct _ARRAY8 {
    CARD16	length;
    CARD8Ptr	data;
} ARRAY8, *ARRAY8Ptr;

typedef struct _ARRAY16 {
    CARD8	length;
    CARD16Ptr	data;
} ARRAY16, *ARRAY16Ptr;

typedef struct _ARRAY32 {
    CARD8	length;
    CARD32Ptr	data;
} ARRAY32, *ARRAY32Ptr;

typedef struct _ARRAYofARRAY8 {
    CARD8	length;
    ARRAY8Ptr	data;
} ARRAYofARRAY8, *ARRAYofARRAY8Ptr;

typedef struct _XdmcpHeader {
    CARD16  version, opcode, length;
} XdmcpHeader, *XdmcpHeaderPtr;

typedef struct _XdmcpBuffer {
    BYTE    *data;
    int	    size;
    int	    pointer;
    int	    count;	    /* amount of data read */
} XdmcpBuffer, *XdmcpBufferPtr;

typedef struct _XdmAuthKey {
    BYTE    data[8];
} XdmAuthKeyRec, *XdmAuthKeyPtr;

extern int XdmcpWriteCARD8(XdmcpBufferPtr, int);
extern int XdmcpWriteCARD16(XdmcpBufferPtr, int);
extern int XdmcpWriteCARD32(XdmcpBufferPtr, CARD32);
extern int XdmcpWriteARRAY8(XdmcpBufferPtr, ARRAY8Ptr);
extern int XdmcpWriteARRAY16(XdmcpBufferPtr, ARRAY16Ptr);
extern int XdmcpWriteARRAY32(XdmcpBufferPtr, ARRAY32Ptr);
extern int XdmcpWriteARRAYofARRAY8(XdmcpBufferPtr, ARRAYofARRAY8Ptr);
extern int XdmcpWriteHeader(XdmcpBufferPtr, XdmcpHeaderPtr);

/* Need to declare sockaddr as a tag if not already declared. */
typedef struct sockaddr __dummy;
extern int XdmcpFlush(int fd, XdmcpBufferPtr buffer, struct sockaddr *to, int tolen);

extern int XdmcpReadCARD8(XdmcpBufferPtr, CARD8Ptr);
extern int XdmcpReadCARD16(XdmcpBufferPtr, CARD16Ptr);
extern int XdmcpReadCARD32(XdmcpBufferPtr, CARD32Ptr);
extern int XdmcpReadARRAY8(XdmcpBufferPtr, ARRAY8Ptr);
extern int XdmcpReadARRAY16(XdmcpBufferPtr, ARRAY16Ptr);
extern int XdmcpReadARRAY32(XdmcpBufferPtr, ARRAY32Ptr);
extern int XdmcpReadARRAYofARRAY8(XdmcpBufferPtr, ARRAYofARRAY8Ptr);
extern int XdmcpReadHeader(XdmcpBufferPtr, XdmcpHeaderPtr);
extern int XdmcpFill(int fd, XdmcpBufferPtr buffer, struct sockaddr *from, int *fromlen);

extern int  XdmcpReadRemaining(XdmcpBufferPtr);

extern void XdmcpDisposeARRAY8(ARRAY8Ptr);
extern void XdmcpDisposeARRAY16(ARRAY16Ptr);
extern void XdmcpDisposeARRAY32(ARRAY32Ptr);
extern void XdmcpDisposeARRAYofARRAY8(ARRAYofARRAY8Ptr);

extern int XdmcpCopyARRAY8(ARRAY8Ptr src, ARRAY8Ptr dst);

extern int XdmcpARRAY8Equal(ARRAY8Ptr, ARRAY8Ptr);


extern void XdmcpGenerateKey(XdmAuthKeyPtr);
extern void XdmcpIncrementKey(XdmAuthKeyPtr);
extern void XdmcpDecrementKey(XdmAuthKeyPtr);
extern void XdmcpEncrypt(const void *plain, const XdmAuthKeyRec *, void *crypto, int bytes);
extern void XdmcpDecrypt(const void *crypto, const XdmAuthKeyRec *, void *plain, int bytes);
extern int XdmcpCompareKeys(XdmAuthKeyPtr, XdmAuthKeyPtr);
extern void XdmcpSetKeyParity(XdmAuthKeyPtr);

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#ifdef _XDMCP_BUILD
/*
 * These are only here for use when building the library to
 * allow code to be shared with the server (which does not
 * set this flag).
 */
#define Xalloc	malloc
#define Xrealloc realloc
#define Xfree	free
#endif /* _XDMCP_BUILD */
#endif /* _XDMCP_H_ */
