File: icp.c

package info (click to toggle)
echoping 6.0.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 9,296 kB
  • ctags: 515
  • sloc: sh: 8,951; ansic: 3,352; makefile: 170
file content (106 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Code contributed by Christian Grimm <grimm@rvs.uni-hannover.de> 
 * and patched by Stephane Bortzmeyer. 
 *
 * $Id: icp.c 377 2007-03-12 20:48:05Z bortz $
 *
 */

#include "echoping.h"

#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;
	if (shost)
		headerp->shostid = htonl(*shost);
	/* 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;
	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"
	};


	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");
	}
	opcode = headerp->opcode;
	length = ntohs(headerp->length);
	sprintf(retcode, "ICP reply: \42%s\42", icp_op_code[opcode]);
	return length;
}


#endif				/* ICP */