Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 315 lines (284 sloc) 7.417 kb
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
/*
* CC Network Filesystem (ccgfs)
*
* Copyright © CC Computer Consultants GmbH, 2007
* Contact: Jan Engelhardt <jengelh [at] computergmbh de>
*
* This file is part of CCGFS. CCGFS is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 or 3 of the License.
*/
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "packet.h"

/**
* @type: type
* @length: desired initial length (without header)
*/
struct lo_packet *pkt_init(unsigned int type, unsigned int length)
{
struct ccgfs_pkt_header *hdr;
struct lo_packet *pkt;

length += sizeof(*hdr);
if ((pkt = malloc(sizeof(*pkt))) == NULL) {
fprintf(stderr, "%s: malloc(): %s\n",
__func__, strerror(errno));
abort();
}
if ((hdr = pkt->data = malloc(length)) == NULL) {
fprintf(stderr, "%s: malloc(): %s\n",
__func__, strerror(errno));
abort();
}
pkt->alloc = length;
pkt->length = sizeof(*hdr);
pkt->shift = sizeof(*hdr);
hdr->opcode = cpu_to_le32(type);
return pkt;
}

/**
* @size: new size, including header
*/
void *pkt_resize(struct lo_packet *pkt, unsigned int size)
{
void *nu = realloc(pkt->data, size);
if (nu == NULL) {
fprintf(stderr, "%s: realloc(): %s\n",
__func__, strerror(errno));
abort();
}
pkt->alloc = size;
pkt->data = nu;
return nu;
}

static inline void pkt_resize_plus(struct lo_packet *pkt, unsigned int size)
{
pkt_resize(pkt, pkt->length + size + size / 4);
}

static inline uint32_t deref_get_32(const void *ptr)
{
uint32_t ret;
memcpy(&ret, ptr, sizeof(ret));
return ret;
}

static inline uint64_t deref_get_64(const void *ptr)
{
uint64_t ret;
memcpy(&ret, ptr, sizeof(ret));
return ret;
}

static inline void deref_put_32(void *ptr, uint32_t value)
{
memcpy(ptr, &value, sizeof(value));
}

/**
* pkt_push - push an object into the buffer
* @pkt: packet buffer to operate on
*/
void pkt_push(struct lo_packet *pkt, const void *input_data,
    unsigned int input_length, unsigned int type)
{
unsigned int nsz;
void *dest;

nsz = input_length + sizeof(uint32_t);

if (input_length >= 0x7F000000) {
fprintf(stderr, "%s: length too big\n", __func__);
abort();
}

if (pkt->length + nsz > pkt->alloc)
pkt_resize_plus(pkt, nsz);

dest = pkt->data + pkt->length;
switch (type) {
case PT_DATA:
deref_put_32(dest,
cpu_to_le32(input_length | (1 << 31)));
memcpy(dest + sizeof(uint32_t), input_data, input_length);
break;
default:
deref_put_32(dest, cpu_to_le32(type));
memcpy(dest + sizeof(uint32_t), input_data, input_length);
break;
}
pkt->length += nsz;
}

/**
* pkt_shift_32 - get the next object
* @pkt: packet buffer to operate on
*
* Verifies that the next object is a 32-bit integer object
* and returns the value.
*/
uint32_t pkt_shift_32(struct lo_packet *pkt)
{
uint32_t *ptr = pkt->data + pkt->shift;
if (le32_to_cpu(deref_get_32(ptr)) != PT_32) {
fprintf(stderr, "%s: protocol mismatch\n", __func__);
abort();
}
++ptr;
pkt->shift += 2 * sizeof(uint32_t);
return le32_to_cpu(deref_get_32(ptr));
}

/**
* pkt_shift_64 - get the next object
* @pkt: packet buffer to operate on
*
* Verifies that the next object is a 64-bit integer object
* and returns the value.
*/
uint64_t pkt_shift_64(struct lo_packet *pkt)
{
uint32_t *ptr = pkt->data + pkt->shift;

if (le32_to_cpu(deref_get_32(ptr)) != PT_64) {
fprintf(stderr, "%s: protocol mismatch\n", __func__);
abort();
}

++ptr;
pkt->shift += sizeof(uint32_t) + sizeof(uint64_t);
return le64_to_cpu(deref_get_64(ptr));
}

/**
* pkt_shift_s - get the next object
* @pkt: packet buffer to operate on
*
* Verifies that the next object is a binary blob object and returns a pointer
* to it in the transmission buffer. Do not free it.
*/
const void *pkt_shift_s(struct lo_packet *pkt)
{
uint32_t *ptr = pkt->data + pkt->shift;
uint32_t data = le32_to_cpu(deref_get_32(ptr));
uint32_t len = data & ~PT_DATA_BIT;

if (!(data & PT_DATA_BIT)) {
fprintf(stderr, "%s: protocol mismatch\n", __func__);
abort();
}
++ptr;
pkt->shift += sizeof(uint32_t) + len;
return ptr;
}

static ssize_t reliable_write(int fd, void *buf, size_t count)
{
size_t count_left = count;
int ret;

    //fprintf(stderr, "reliable_write fd=%d buf=%p count=%d\n", fd, buf, count);
while (count_left > 0) {
ret = send(fd, buf, count_left, 0);
if (ret > 0) {
count_left -= ret;
buf += ret;
} else if (ret == 0) {
return count - count_left;
} else if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
/* immediate retry */
} else {
            fprintf(stderr, "fd=%d ret=%d errno=%d\n", fd, ret, errno);
#ifdef WIN32
            fprintf(stderr, "WLE=%d\n", WSAGetLastError());
#endif
perror("reliable_write unable to recover");
return ret;
}
}

return count;
}

static ssize_t reliable_read(int fd, void *buf, size_t count)
{
size_t count_left = count;
int ret;

    //fprintf(stderr, "reliable_read fd=%d buf=%p count=%d\n", fd, buf, count);
while (count_left > 0) {
ret = recv(fd, buf, count_left, 0);
if (ret > 0) {
count_left -= ret;
buf += ret;
} else if (ret == 0) {
return count - count_left;
} else if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
/* immediate retry */
} else {
            fprintf(stderr, "fd=%d ret=%d errno=%d\n", fd, ret, errno);
#ifdef WIN32
            fprintf(stderr, "WLE=%d\n", WSAGetLastError());
#endif
perror("reliable_read unable to recover");
return ret;
}
}

return count;
}

/**
* pkt_recv - receive packet
* @fd: file descriptor to read
*
* Reads the next packet from @fd. A new lo_packet is created and returned.
* On error, %NULL is returned, in which case the stream (@fd) will be in
* an undefined state.
*/
struct lo_packet *pkt_recv(int fd)
{
struct ccgfs_pkt_header *hdr;
struct lo_packet *pkt;
ssize_t ret;
int err;

pkt = pkt_init(0, 0);
hdr = pkt->data;
ret = reliable_read(fd, hdr, sizeof(*hdr));
if (ret != sizeof(*hdr)) {
err = errno;
pkt_destroy(pkt);
errno = err;
return NULL;
}

hdr->opcode = le32_to_cpu(hdr->opcode);
hdr->length = le32_to_cpu(hdr->length);
hdr = pkt_resize(pkt, hdr->length);
pkt->length = hdr->length;

ret = reliable_read(fd, pkt->data + sizeof(*hdr),
pkt->length - sizeof(*hdr));
if (ret != pkt->length - sizeof(*hdr)) {
err = errno;
pkt_destroy(pkt);
errno = err;
return NULL;
}
return pkt;
}

static void __pkt_destroy(struct lo_packet *pkt)
{
free(pkt->data);
free(pkt);
}

/**
* pkt_send - send packet and destroy
* @fd: file descriptor to write it to
* @pkt: packet structure
*
* Writes the packet length into the actual transmission buffer, sends the
* packet out to @fd and then destroys it.
*/
void pkt_send(int fd, struct lo_packet *pkt)
{
struct ccgfs_pkt_header *hdr = pkt->data;
hdr->length = cpu_to_le32(pkt->length);
reliable_write(fd, hdr, pkt->length);
__pkt_destroy(pkt);
}

void pkt_destroy(struct lo_packet *pkt)
{
if (pkt->shift != pkt->length) {
perror("short packet");
fprintf(stderr, "packet %p[%u,%u] has not been "
"properly consumed\n",
pkt, pkt->shift, pkt->length);
}
__pkt_destroy(pkt);
}
Something went wrong with that request. Please try again.