100
queue->partial.data = talloc_realloc_size(queue, queue->partial.data,
101
num_ready + queue->partial.length);
103
104
if (queue->partial.data == NULL) {
104
DEBUG(DEBUG_ERR,("%s: read error alloc failed for %u\n",
105
queue->name, num_ready + queue->partial.length));
105
/* starting fresh, allocate buf for size bytes */
106
sz_bytes_req = sizeof(pkt_size);
107
queue->partial.data = talloc_size(queue, sz_bytes_req);
108
if (queue->partial.data == NULL) {
109
DEBUG(DEBUG_ERR,("read error alloc failed for %u\n",
113
} else if (queue->partial.length < sizeof(pkt_size)) {
114
/* yet to find out the packet length */
115
sz_bytes_req = sizeof(pkt_size) - queue->partial.length;
117
/* partial packet, length known, full buf allocated */
120
data = queue->partial.data;
122
if (sz_bytes_req > 0) {
123
to_read = MIN(sz_bytes_req, num_ready);
124
nread = read(queue->fd, data + queue->partial.length,
127
DEBUG(DEBUG_ERR,("read error nread=%d\n", (int)nread));
130
queue->partial.length += nread;
132
if (nread < sz_bytes_req) {
133
/* not enough to know the length */
134
DEBUG(DEBUG_DEBUG,("Partial packet length read\n"));
137
/* size now known, allocate buffer for the full packet */
138
queue->partial.data = talloc_realloc_size(queue, data,
140
if (queue->partial.data == NULL) {
141
DEBUG(DEBUG_ERR,("read error alloc failed for %u\n",
145
data = queue->partial.data;
149
pkt_size = *(uint32_t *)data;
151
DEBUG(DEBUG_CRIT,("Invalid packet of length 0\n"));
109
nread = read(queue->fd, queue->partial.data + queue->partial.length, num_ready);
155
pkt_bytes_remaining = pkt_size - queue->partial.length;
156
to_read = MIN(pkt_bytes_remaining, num_ready);
157
nread = read(queue->fd, data + queue->partial.length,
110
159
if (nread <= 0) {
111
DEBUG(DEBUG_ERR,("%s: read error nread=%d\n",
112
queue->name, (int)nread));
160
DEBUG(DEBUG_ERR,("read error nread=%d\n",
116
partlen = queue->partial.length;
164
queue->partial.length += nread;
118
data = queue->partial.data;
119
nread += queue->partial.length;
166
if (queue->partial.length < pkt_size) {
167
DEBUG(DEBUG_DEBUG,("Partial packet data read\n"));
121
171
queue->partial.data = NULL;
122
172
queue->partial.length = 0;
124
if (nread >= 4 && *(uint32_t *)data == nread) {
125
/* it is the responsibility of the incoming packet
126
function to free 'data' */
127
queue->callback(data, nread, queue->private_data);
133
while (nread >= 4 && *(uint32_t *)data <= nread) {
134
/* we have at least one packet */
137
bool destroyed = false;
139
len = *(uint32_t *)data;
141
/* bad packet! treat as EOF */
142
DEBUG(DEBUG_CRIT,("%s: Invalid packet of length 0 (nread = %zu, totread = %zu, partlen = %zu)\n",
143
queue->name, nread, totread, partlen));
144
dump_packet(data_base, totread + partlen);
147
d2 = talloc_memdup(queue, data, len);
149
DEBUG(DEBUG_ERR,("%s: read error memdup failed for %u\n",
155
queue->destroyed = &destroyed;
156
queue->callback(d2, len, queue->private_data);
157
/* If callback freed us, don't do anything else. */
161
queue->destroyed = NULL;
168
/* we have only part of a packet */
169
if (data_base == data) {
170
queue->partial.data = data;
171
queue->partial.length = nread;
173
queue->partial.data = talloc_memdup(queue, data, nread);
174
if (queue->partial.data == NULL) {
175
DEBUG(DEBUG_ERR,("%s: read error memdup partial failed for %u\n",
176
queue->name, (unsigned)nread));
179
queue->partial.length = nread;
180
talloc_free(data_base);
185
talloc_free(data_base);
173
/* it is the responsibility of the callback to free 'data' */
174
queue->callback(data, pkt_size, queue->private_data);