echoping/SRC/icp.c

107 lines
2.3 KiB
C
Raw Normal View History

2000-06-25 14:07:09 +00:00
/*
* Code contributed by Christian Grimm <grimm@rvs.uni-hannover.de>
* and patched by Stephane Bortzmeyer.
*
* $Id$
*
*/
2000-06-25 14:05:33 +00:00
#include "echoping.h"
2000-04-13 09:19:23 +00:00
#ifdef ICP
void *
make_icp_sendline (url, shost, opcode, length)
const char *url;
u_num32 *shost;
icp_opcode opcode;
int *length;
{
icp_common_t *headerp = NULL;
char *buf = NULL;
char *urloffset = NULL;
u_num32 flags = ICP_FLAG_SRC_RTT;
u_num32 pad = 0;
u_num32 reqnum = 4711;
unsigned short buf_len;
buf_len = sizeof (icp_common_t) + strlen (url) + 1;
if (opcode == ICP_OP_QUERY)
buf_len += sizeof (u_num32);
buf = calloc (buf_len, 1);
headerp = (icp_common_t *) buf;
headerp->opcode = opcode;
headerp->version = ICP_VERSION_CURRENT;
headerp->length = htons (buf_len);
headerp->reqnum = htonl (reqnum);
headerp->flags = htonl (flags);
headerp->pad = pad;
headerp->shostid = htonl (*shost);
2000-04-13 09:19:23 +00:00
/* urloffset = (char *) ((int) buf + sizeof(icp_common_t)); */
urloffset = (char *) (buf + sizeof (icp_common_t));
if (opcode == ICP_OP_QUERY)
urloffset += sizeof (u_num32);
memcpy (urloffset, url, strlen (url));
*length = buf_len;
return buf;
}
int
recv_icp (sockfd, buf, retcode)
int sockfd;
char *buf;
char *retcode;
{ /*
* based on draft-wessels-icp-v2-02.txt
*/
icp_common_t *headerp = (icp_common_t *) buf;
int nr, length;
unsigned char opcode;
2001-01-25 15:25:13 +00:00
static char *icp_op_code[] = {
/* 0 */ "ICP_OP_INVALID",
/* 1 */ "ICP_OP_QUERY",
/* 2 */ "ICP_OP_HIT",
/* 3 */ "ICP_OP_MISS",
/* 4 */ "ICP_OP_ERR",
/* 5 */ "",
/* 6 */ "",
/* 7 */ "",
/* 8 */ "",
/* 9 */ "",
/* 10 */ "ICP_OP_SECHO",
/* 11 */ "ICP_OP_DECHO",
/* 12 */ "",
/* 13 */ "",
/* 14 */ "",
/* 15 */ "",
/* 16 */ "",
/* 17 */ "",
/* 18 */ "",
/* 19 */ "",
/* 20 */ "",
/* 21 */ "ICP_OP_MISS_NOFETCH",
/* 22 */ "ICP_OP_DENIED",
/* 23 */ "ICP_OP_HIT_OBJ"
2000-04-13 09:19:23 +00:00
};
2001-01-25 15:25:13 +00:00
nr = recvfrom (sockfd, buf, DEFLINE, 0, (struct sockaddr *) 0, (int *) 0);
if (nr < 0)
{
if (timeout_flag)
err_quit ("Timeout while reading");
else
err_sys ("No reply from ICP proxy server");
}
2000-04-13 09:19:23 +00:00
opcode = headerp->opcode;
length = ntohs (headerp->length);
sprintf (retcode, "ICP reply: \42%s\42", icp_op_code[opcode]);
return length;
}
#endif /* ICP */