Files
@ e0b7e9dd5b54
Branch filter:
Location: DistRen/src/common/request.c
e0b7e9dd5b54
8.9 KiB
text/plain
File uploads are now supported using DISTREN_REQUEST_FILE_POST and friends.
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 | /*
* Copyright 2010 Nathan Phillip Brink
*
* This file is a part of DistRen.
*
* DistRen is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DistRen is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with DistRen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/config.h"
#include "common/request.h"
#include "common/protocol.h"
#include <arpa/inet.h>
#include <openssl/evp.h>
#include <stdlib.h>
#include <string.h>
int distren_request_free_with_data(struct distren_request *req, void *data)
{
free(data);
return distren_request_free(req);
}
int distren_request_version(struct distren_request **req, void **data, uint32_t servertype, const char *package_string)
{
struct distren_request_version *version;
distren_request_new(req, sizeof(struct distren_request_version), DISTREN_REQUEST_VERSION);
version = malloc(sizeof(struct distren_request_version));
if(!version || !*req)
{
free(version);
if(*req)
distren_request_free(*req);
return 1;
}
memset(version, 0, sizeof(struct distren_request_version));
version->servertype = servertype;
strncpy(version->package_string, package_string, DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN);
*data = version;
return 0;
}
int distren_request_parse_version(struct distren_request *req, void *data, struct distren_request_version *version)
{
if(req->len < sizeof(struct distren_request_version))
return 1;
memcpy(version, data, sizeof(struct distren_request_version));
/* there is space for another '\0' */
version->package_string[DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN] = '\0';
return 0;
}
int distren_request_poing(struct distren_request **req, void **data, short is_ping, const void *poing_cookie, size_t poing_data_len)
{
enum distren_request_type type;
if(is_ping)
type = DISTREN_REQUEST_PING;
else
type = DISTREN_REQUEST_PONG;
distren_request_new(req, poing_data_len, type);
(*data) = malloc(poing_data_len);
memcpy(*data, poing_cookie, poing_data_len);
return 0;
}
/*
* file posting stuffs
*/
struct distren_request_file_post_context
{
EVP_MD_CTX *digest_ctx;
/* stored in network-byte order */
uint32_t post_id;
};
/**
* Helper for distren_request_file_post_start() and
* distren_request_parse_file_post_start().
*/
static int _distren_request_file_post_context_new(distren_request_file_post_context_t *post_context,
uint32_t post_id)
{
*post_context = malloc(sizeof(struct distren_request_file_post_context));
if(!*post_context)
return 1;
(*post_context)->post_id = htonl(post_id);
(*post_context)->digest_ctx = EVP_MD_CTX_create();
EVP_DigestInit_ex((*post_context)->digest_ctx, EVP_sha1(), NULL);
return 0;
}
/**
* Frees a post_context and sets the pointer to NULL.
*/
static int _distren_request_file_post_context_free(distren_request_file_post_context_t *post_context)
{
if(!*post_context)
return 0;
EVP_MD_CTX_destroy((*post_context)->digest_ctx);
free(*post_context);
*post_context = NULL;
return 0;
}
int distren_request_file_post_context_free(distren_request_file_post_context_t post_context)
{
/*
* for the ``public'' function, we don't need to have post_context
* set to NULL. Thus, we have this wrapper around
* _distren_request_file_post_context_free().
*/
return _distren_request_file_post_context_free(&post_context);
}
int distren_request_file_post_start(struct distren_request **req,
void **data,
distren_request_file_post_context_t *post_context,
uint32_t post_id,
const char *filename)
{
struct distren_request_file_post_start *file_post_start;
distren_request_new(req, sizeof(struct distren_request_file_post_start), DISTREN_REQUEST_FILE_POST_START);
file_post_start = malloc(sizeof(struct distren_request_file_post_start));
_distren_request_file_post_context_new(post_context, post_id);
if(!*req
|| !file_post_start)
{
if(*req)
distren_request_free(*req);
free(file_post_start);
_distren_request_file_post_context_free(post_context);
return 1;
}
file_post_start->post_id = (*post_context)->post_id;
strncpy(file_post_start->filename, filename, DISTREN_REQUEST_FILE_POST_NAME_LEN);
*data = file_post_start;
return 0;
}
int distren_request_parse_file_post_start(struct distren_request *req,
void *data,
distren_request_file_post_context_t *post_context,
uint32_t *post_id,
char **filename)
{
int tmp;
struct distren_request_file_post_start *file_post_start;
/* keep our promises ;-) */
*post_context = NULL;
*filename = NULL;
if(req->len < sizeof(struct distren_request_file_post_start))
return 1;
file_post_start = data;
tmp = _distren_request_file_post_context_new(post_context, file_post_start->post_id);
*filename = malloc(DISTREN_REQUEST_FILE_POST_NAME_LEN + 1);
if(tmp
|| !*filename)
{
_distren_request_file_post_context_free(post_context);
free(*filename);
}
memcpy(*filename, file_post_start->filename, DISTREN_REQUEST_FILE_POST_NAME_LEN);
(*filename)[DISTREN_REQUEST_FILE_POST_NAME_LEN] = '\0';
*post_id = ntohl(file_post_start->post_id);
return 0;
}
int distren_request_file_post(struct distren_request **req,
struct distren_request_file_post *data_header,
distren_request_file_post_context_t post_context,
const void *data,
size_t len)
{
distren_request_new(req, sizeof(struct distren_request_file_post) + len, DISTREN_REQUEST_FILE_POST);
if(!*req)
return 1;
data_header->post_id = post_context->post_id;
EVP_DigestUpdate(post_context->digest_ctx, data, len);
return 0;
}
int distren_request_parse_file_post(struct distren_request *req,
void *data,
uint32_t *post_id,
distren_request_parse_file_post_find_context_func_t find_context_func,
void *find_post_context_data,
void **file_data,
size_t *file_data_len)
{
struct distren_request_file_post *file_post;
distren_request_file_post_context_t post_context;
if(req->type != DISTREN_REQUEST_FILE_POST
|| req->len < sizeof(struct distren_request_file_post))
return 1;
file_post = data;
*post_id = ntohl(file_post->post_id);
post_context = (*find_context_func)(*post_id, find_post_context_data);
if(!post_context
|| *post_id != post_context->post_id)
return 1;
*file_data = data + sizeof(struct distren_request_file_post);
*file_data_len = req->len - sizeof(struct distren_request_file_post);
EVP_DigestUpdate(post_context->digest_ctx, *file_data, *file_data_len);
return 0;
}
int distren_request_file_post_finish(struct distren_request **req,
void **data,
distren_request_file_post_context_t post_context,
uint32_t cancel)
{
struct distren_request_file_post_finish *file_post_finish;
distren_request_new(req, sizeof(struct distren_request_file_post_finish), DISTREN_REQUEST_FILE_POST_FINISH);
file_post_finish = malloc(sizeof(struct distren_request_file_post_finish));
if(!*req
|| !file_post_finish)
{
if(*req)
distren_request_free(*req);
free(file_post_finish);
_distren_request_file_post_context_free(&post_context);
return 1;
}
file_post_finish->post_id = post_context->post_id;
file_post_finish->cancel = htonl(cancel);
EVP_DigestFinal(post_context->digest_ctx, file_post_finish->sha, NULL);
_distren_request_file_post_context_free(&post_context);
*data = file_post_finish;
return 0;
}
int distren_request_parse_file_post_finish(struct distren_request *req,
void *data,
uint32_t *post_id,
distren_request_parse_file_post_find_context_func_t find_context_func,
void *find_post_context_data)
{
struct distren_request_file_post_finish *file_post_finish;
distren_request_file_post_context_t post_context;
unsigned int checksum_len;
unsigned char checksum[EVP_MAX_MD_SIZE];
if(req->type != DISTREN_REQUEST_FILE_POST_FINISH
|| req->len < sizeof(struct distren_request_file_post_finish))
return 1;
file_post_finish = data;
*post_id = ntohl(file_post_finish->post_id);
if(ntohl(file_post_finish->cancel))
return 2;
post_context = find_context_func(*post_id, find_post_context_data);
if(!post_context)
return 1;
/* finish up checksumming */
EVP_DigestFinal_ex(post_context->digest_ctx, checksum, &checksum_len);
if(checksum_len > EVP_MAX_MD_SIZE)
checksum_len = EVP_MAX_MD_SIZE;
if(memcmp(checksum, file_post_finish->sha, checksum_len))
return 3;
return 0;
}
|