#ifdef WIN32
#define _WILLWINSOCK_
#endif
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/ICE/ICElib.h>
#include "ICElibint.h"
#include <X11/Xtrans/Xtrans.h>
#include <stdio.h>
#ifdef WIN32
#include <X11/Xwinsock.h>
#include <X11/Xw32defs.h>
#endif
char *
IceAllocScratch (
IceConn iceConn,
unsigned long size
)
{
if (!iceConn->scratch || size > iceConn->scratch_size)
{
if (iceConn->scratch)
free (iceConn->scratch);
iceConn->scratch = (char *) malloc ((unsigned) size);
iceConn->scratch_size = size;
}
return (iceConn->scratch);
}
int
IceFlush (
IceConn iceConn
)
{
_IceWrite (iceConn,
(unsigned long) (iceConn->outbufptr - iceConn->outbuf),
iceConn->outbuf);
iceConn->outbufptr = iceConn->outbuf;
return 1;
}
int
IceGetOutBufSize (
IceConn iceConn
)
{
return (iceConn->outbufmax - iceConn->outbuf);
}
int
IceGetInBufSize (
IceConn iceConn
)
{
return (iceConn->inbufmax - iceConn->inbuf);
}
IceConnectStatus
IceConnectionStatus (
IceConn iceConn
)
{
return (iceConn->connection_status);
}
char *
IceVendor (
IceConn iceConn
)
{
return strdup(iceConn->vendor);
}
char *
IceRelease (
IceConn iceConn
)
{
return strdup(iceConn->release);
}
int
IceProtocolVersion (
IceConn iceConn
)
{
return (_IceVersions[iceConn->my_ice_version_index].major_version);
}
int
IceProtocolRevision (
IceConn iceConn
)
{
return (_IceVersions[iceConn->my_ice_version_index].minor_version);
}
int
IceConnectionNumber (
IceConn iceConn
)
{
return (_IceTransGetConnectionNumber (iceConn->trans_conn));
}
char *
IceConnectionString (
IceConn iceConn
)
{
if (iceConn->connection_string)
{
return strdup(iceConn->connection_string);
}
else
return (NULL);
}
unsigned long
IceLastSentSequenceNumber (
IceConn iceConn
)
{
return (iceConn->send_sequence);
}
unsigned long
IceLastReceivedSequenceNumber (
IceConn iceConn
)
{
return (iceConn->receive_sequence);
}
Bool
IceSwapping (
IceConn iceConn
)
{
return (iceConn->swap);
}
Status
_IceRead (
register IceConn iceConn,
unsigned long nbytes,
register char *ptr
)
{
register unsigned long nleft;
nleft = nbytes;
while (nleft > 0)
{
int nread;
if (iceConn->io_ok)
nread = _IceTransRead (iceConn->trans_conn, ptr, (int) nleft);
else
return (1);
if (nread <= 0)
{
#ifdef WIN32
errno = WSAGetLastError();
#endif
if (iceConn->want_to_close)
{
_IceConnectionClosed (iceConn);
_IceFreeConnection (iceConn);
return (0);
}
else
{
iceConn->io_ok = False;
if (iceConn->connection_status == IceConnectPending)
{
return (1);
}
if (iceConn->process_msg_info)
{
int i;
for (i = iceConn->his_min_opcode;
i <= iceConn->his_max_opcode; i++)
{
_IceProcessMsgInfo *process;
process = &iceConn->process_msg_info[
i - iceConn->his_min_opcode];
if ((process != NULL) && process->in_use)
{
IceIOErrorProc IOErrProc = process->accept_flag ?
process->protocol->accept_client->io_error_proc :
process->protocol->orig_client->io_error_proc;
if (IOErrProc)
(*IOErrProc) (iceConn);
}
}
}
(*_IceIOErrorHandler) (iceConn);
return (1);
}
}
nleft -= nread;
ptr += nread;
}
return (1);
}
void
_IceReadSkip (
register IceConn iceConn,
register unsigned long nbytes
)
{
char temp[512];
while (nbytes > 0)
{
unsigned long rbytes = nbytes > 512 ? 512 : nbytes;
_IceRead (iceConn, rbytes, temp);
nbytes -= rbytes;
}
}
void
_IceWrite (
register IceConn iceConn,
unsigned long nbytes,
register char *ptr
)
{
register unsigned long nleft;
nleft = nbytes;
while (nleft > 0)
{
int nwritten;
if (iceConn->io_ok)
nwritten = _IceTransWrite (iceConn->trans_conn, ptr, (int) nleft);
else
return;
if (nwritten <= 0)
{
#ifdef WIN32
errno = WSAGetLastError();
#endif
iceConn->io_ok = False;
if (iceConn->connection_status == IceConnectPending)
{
return;
}
if (iceConn->process_msg_info)
{
int i;
for (i = iceConn->his_min_opcode;
i <= iceConn->his_max_opcode; i++)
{
_IceProcessMsgInfo *process;
process = &iceConn->process_msg_info[
i - iceConn->his_min_opcode];
if (process->in_use)
{
IceIOErrorProc IOErrProc = process->accept_flag ?
process->protocol->accept_client->io_error_proc :
process->protocol->orig_client->io_error_proc;
if (IOErrProc)
(*IOErrProc) (iceConn);
}
}
}
(*_IceIOErrorHandler) (iceConn);
return;
}
nleft -= nwritten;
ptr += nwritten;
}
}
#ifdef WORD64
IceWriteData16 (
IceConn iceConn,
unsigned long nbytes,
short *data
)
{
int numShorts = nbytes / 2;
int index = 0;
while (index < numShorts)
{
int spaceLeft, count, i;
int shortsLeft = numShorts - index;
spaceLeft = iceConn->outbufmax - iceConn->outbufptr - 1;
if (spaceLeft < 2)
{
IceFlush (iceConn);
spaceLeft = iceConn->outbufmax - iceConn->outbufptr - 1;
}
count = (shortsLeft < spaceLeft / 2) ? shortsLeft : spaceLeft / 2;
for (i = 0; i < count; i++)
STORE_CARD16 (iceConn->outbufptr, data[index++]);
}
}
IceWriteData32 (
IceConn iceConn,
unsigned long nbytes,
int *data
)
{
int numLongs = nbytes / 4;
int index = 0;
while (index < numLongs)
{
int spaceLeft, count, i;
int longsLeft = numLongs - index;
spaceLeft = iceConn->outbufmax - iceConn->outbufptr - 1;
if (spaceLeft < 4)
{
IceFlush (iceConn);
spaceLeft = iceConn->outbufmax - iceConn->outbufptr - 1;
}
count = (longsLeft < spaceLeft / 4) ? longsLeft : spaceLeft / 4;
for (i = 0; i < count; i++)
STORE_CARD32 (iceConn->outbufptr, data[index++]);
}
}
IceReadData16 (
IceConn iceConn,
Bool swap,
unsigned long nbytes,
short *data
)
{
}
IceReadData32 (
IceConn iceConn,
Bool swap,
unsigned long nbytes,
int *data
)
{
}
#endif
void
_IceAddOpcodeMapping (
IceConn iceConn,
int hisOpcode,
int myOpcode
)
{
if (hisOpcode <= 0 || hisOpcode > 255)
{
return;
}
else if (iceConn->process_msg_info == NULL)
{
iceConn->process_msg_info = (_IceProcessMsgInfo *) malloc (
sizeof (_IceProcessMsgInfo));
iceConn->his_min_opcode = iceConn->his_max_opcode = hisOpcode;
}
else if (hisOpcode < iceConn->his_min_opcode)
{
_IceProcessMsgInfo *oldVec = iceConn->process_msg_info;
int oldsize = iceConn->his_max_opcode - iceConn->his_min_opcode + 1;
int newsize = iceConn->his_max_opcode - hisOpcode + 1;
int i;
iceConn->process_msg_info = (_IceProcessMsgInfo *) malloc (
newsize * sizeof (_IceProcessMsgInfo));
memcpy (&iceConn->process_msg_info[
iceConn->his_min_opcode - hisOpcode], oldVec,
oldsize * sizeof (_IceProcessMsgInfo));
free ((char *) oldVec);
for (i = hisOpcode + 1; i < iceConn->his_min_opcode; i++)
{
iceConn->process_msg_info[i -
iceConn->his_min_opcode].in_use = False;
iceConn->process_msg_info[i -
iceConn->his_min_opcode].protocol = NULL;
}
iceConn->his_min_opcode = hisOpcode;
}
else if (hisOpcode > iceConn->his_max_opcode)
{
_IceProcessMsgInfo *oldVec = iceConn->process_msg_info;
int oldsize = iceConn->his_max_opcode - iceConn->his_min_opcode + 1;
int newsize = hisOpcode - iceConn->his_min_opcode + 1;
int i;
iceConn->process_msg_info = (_IceProcessMsgInfo *) malloc (
newsize * sizeof (_IceProcessMsgInfo));
memcpy (iceConn->process_msg_info, oldVec,
oldsize * sizeof (_IceProcessMsgInfo));
free ((char *) oldVec);
for (i = iceConn->his_max_opcode + 1; i < hisOpcode; i++)
{
iceConn->process_msg_info[i -
iceConn->his_min_opcode].in_use = False;
iceConn->process_msg_info[i -
iceConn->his_min_opcode].protocol = NULL;
}
iceConn->his_max_opcode = hisOpcode;
}
iceConn->process_msg_info[hisOpcode -
iceConn->his_min_opcode].in_use = True;
iceConn->process_msg_info[hisOpcode -
iceConn->his_min_opcode].my_opcode = myOpcode;
iceConn->process_msg_info[hisOpcode -
iceConn->his_min_opcode].protocol = &_IceProtocols[myOpcode - 1];
}
char *
IceGetPeerName (IceConn iceConn)
{
return (_IceTransGetPeerNetworkId (iceConn->trans_conn));
}
char *
_IceGetPeerName (IceConn iceConn)
{
return (IceGetPeerName(iceConn));
}