File: request.c

package info (click to toggle)
pdbg 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,212 kB
  • sloc: ansic: 21,934; cpp: 3,363; sh: 3,343; makefile: 314; asm: 11
file content (169 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright 2019 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>

#include "buffer.h"
#include "instruction.h"
#include "libcronus_private.h"

int cronus_request(struct cronus_context *cctx,
		   uint32_t key, uint32_t out_len,
		   struct cronus_buffer *request,
		   struct cronus_buffer *reply)
{
	uint8_t *buf, *ptr;
	size_t len = 0, buflen;
	ssize_t n;
	int ret;

	assert(cctx);
	assert(cctx->fd != -1);

	ptr = cbuf_finish(request, &len);
	assert(len > 0);

	buflen = 1024 + out_len;
	buf = malloc(buflen);
	if (!buf)
		return ENOMEM;

	n = write(cctx->fd, ptr, len);
	if (n == -1) {
		free(buf);
		ret = errno;
		perror("write");
		return ret;
	}
	if (n != len) {
		free(buf);
		fprintf(stderr, "Short write (%zu of %zu) to server\n", n, len);
		return EIO;
	}

	n = read(cctx->fd, buf, buflen);
	if (n == -1) {
		free(buf);
		ret = errno;
		perror("read");
		return ret;
	}

	ret = cbuf_new_from_buf(reply, buf, n);
	free(buf);
	if (ret)
		return ret;

	return 0;
}

static int cronus_parse_ecmd_dbuf(struct cronus_buffer *cbuf,
				  uint32_t size,
				  struct cronus_reply *reply)
{
	reply->data = malloc(size);
	if (!reply->data)
		return ENOMEM;
	reply->data_len = size;

	cbuf_read(cbuf, reply->data, reply->data_len);
	return 0;

}

static int cronus_parse_instruction_status(struct cronus_buffer *cbuf,
					   uint32_t size,
					   struct cronus_reply *reply)
{
	uint32_t offset;

	if (size < 4 * sizeof(uint32_t))
		return EPROTO;

	cbuf_read_uint32(cbuf, &reply->status_version);
	cbuf_read_uint32(cbuf, &reply->instruction_version);
	cbuf_read_uint32(cbuf, &reply->rc);
	cbuf_read_uint32(cbuf, &reply->status_len);
	offset = 4 * sizeof(uint32_t);

	if (size < offset + reply->status_len)
		return EPROTO;

	reply->status = malloc(reply->status_len);
	if (!reply->status)
		return ENOMEM;

	cbuf_read(cbuf, reply->status, reply->status_len);
	return 0;
}

int cronus_parse_reply(uint32_t key,
		       struct cronus_buffer *cbuf,
		       struct cronus_reply *reply)
{
	uint32_t num_replies;
	int i;

	memset(reply, 0, sizeof(*reply));

	cbuf_read_uint32(cbuf, &num_replies);
	if (num_replies != 2) {
		fprintf(stderr, "Invalid number of replies (%u) from server\n", num_replies);
		return EPROTO;
	}

	for (i=0; i<num_replies; i++) {
		uint32_t rkey, type, size;
		int ret = -1;

		cbuf_read_uint32(cbuf, &rkey);
		if (rkey != key) {
			fprintf(stderr, "Wrong key %u, expected %u\n", rkey, key);
			return EPROTO;
		}

		cbuf_read_uint32(cbuf, &type);
		cbuf_read_uint32(cbuf, &size);

		if (type == RESULT_TYPE_ECMD_DBUF) {
			ret = cronus_parse_ecmd_dbuf(cbuf, size, reply);
		} else if (type == RESULT_TYPE_INSTRUCTION_STATUS) {
			ret = cronus_parse_instruction_status(cbuf, size, reply);
		}

		if (ret != 0)
			return ret;
	}

	if (reply->rc != SERVER_COMMAND_COMPLETE) {
		uint32_t size;

		size = cbuf_size(cbuf) - cbuf_offset(cbuf);

		reply->error = malloc(size);
		if (!reply->error)
			return ENOMEM;

		cbuf_read(cbuf, (uint8_t *)reply->error, size);
	}

	return 0;
}