diff options
author | sr | 2012-08-24 20:54:29 +0200 |
---|---|---|
committer | sr | 2012-08-24 20:54:29 +0200 |
commit | 6d659475c248f5030c44acb0096771fcd09a7dee (patch) | |
tree | 2ca98bd13a775848974cf59ca2781941f7e91c1c /src/server | |
parent | [SERVER] IPC, delete image (diff) | |
download | dnbd3-6d659475c248f5030c44acb0096771fcd09a7dee.tar.gz dnbd3-6d659475c248f5030c44acb0096771fcd09a7dee.tar.xz dnbd3-6d659475c248f5030c44acb0096771fcd09a7dee.zip |
[*] Cleanup, comments, more error checks
[SERVER] Fix a few off-by-one bugs
[SERVER] Make log available through IPC
[KERNEL] Hot-Swap to faster connection on load-balance
[KERNEL] Report I/O error to block layer if no server replies within a given time frame
[*] Modified network protocol to handle volume names instead of ids
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/ipc.c | 91 | ||||
-rw-r--r-- | src/server/net.c | 314 | ||||
-rw-r--r-- | src/server/serialize.c | 5 | ||||
-rw-r--r-- | src/server/server.c | 83 | ||||
-rw-r--r-- | src/server/server.h | 32 | ||||
-rw-r--r-- | src/server/utils.c | 420 | ||||
-rw-r--r-- | src/server/utils.h | 3 |
7 files changed, 679 insertions, 269 deletions
diff --git a/src/server/ipc.c b/src/server/ipc.c index 917d6e5..5da811c 100644 --- a/src/server/ipc.c +++ b/src/server/ipc.c @@ -34,16 +34,17 @@ #include <libxml/xpath.h> #include "ipc.h" -#include "config.h" +#include "../config.h" #include "server.h" #include "utils.h" +#include "memlog.h" void* dnbd3_ipc_receive() { GSList *iterator = NULL; struct tm * timeinfo; - char time_buff[64]; + char time_buff[64], rid[20], ipaddr[100]; dnbd3_ipc_t header; int server_sock, client_sock; @@ -118,7 +119,7 @@ void* dnbd3_ipc_receive() grp = getgrnam(UNIX_SOCKET_GROUP); if (grp == NULL) { - printf("WARN: Group '%s' not found.\n", UNIX_SOCKET_GROUP); + memlogf("WARN: Group '%s' not found.\n", UNIX_SOCKET_GROUP); } else { @@ -129,9 +130,12 @@ void* dnbd3_ipc_receive() while (1) { - int i = 0, size = 0; + int size; char* buf; xmlDocPtr doc; + xmlNodePtr root_node, images_node, clients_node, tmp_node, log_parent_node, log_node; + xmlChar *xmlbuff; + int buffersize; // Accept connection if ((client_sock = accept(server_sock, &client, &len)) < 0) @@ -151,7 +155,7 @@ void* dnbd3_ipc_receive() switch (header.cmd) { case IPC_EXIT: - printf("INFO: Server shutdown...\n"); + memlogf("INFO: Server shutdown...\n"); header.size = ntohl(0); header.error = ntohl(0); send(client_sock, (char *) &header, sizeof(header), MSG_WAITALL); @@ -161,21 +165,13 @@ void* dnbd3_ipc_receive() break; case IPC_RELOAD: - printf("INFO: Reloading configuration...\n"); - dnbd3_reload_config(_config_file_name); - header.size = ntohl(0); - header.error = ntohl(0); + header.size = ntohl(0); + header.error = ntohl(ERROR_UNKNOWN); send(client_sock, (char *) &header, sizeof(header), MSG_WAITALL); close(client_sock); break; case IPC_INFO: - pthread_spin_lock(&_spinlock); - - xmlNodePtr root_node, images_node, clients_node, tmp_node; - xmlChar *xmlbuff; - int buffersize; - doc = xmlNewDoc(BAD_CAST "1.0"); root_node = xmlNewNode(NULL, BAD_CAST "info"); xmlDocSetRootElement(doc, root_node); @@ -183,38 +179,46 @@ void* dnbd3_ipc_receive() // Images images_node = xmlNewNode(NULL, BAD_CAST "images"); xmlAddChild(root_node, images_node); - for (i = 0; i < _num_images; i++) - { - char vid[20], rid[20]; - sprintf(vid,"%d",_images[i].vid); - sprintf(rid,"%d",_images[i].rid); - timeinfo = localtime(&_images[i].atime); + pthread_spin_lock(&_spinlock); + for (iterator = _dnbd3_images; iterator; iterator = iterator->next) + { + const dnbd3_image_t *image = iterator->data; + sprintf(rid,"%d",image->rid); + timeinfo = localtime(&image->atime); strftime(time_buff,64,"%d.%m.%y %H:%M:%S",timeinfo); tmp_node = xmlNewNode(NULL, BAD_CAST "image"); - xmlNewProp(tmp_node, BAD_CAST "group", BAD_CAST _images[i].group); + xmlNewProp(tmp_node, BAD_CAST "name", BAD_CAST image->name); xmlNewProp(tmp_node, BAD_CAST "atime", BAD_CAST time_buff); - xmlNewProp(tmp_node, BAD_CAST "vid", BAD_CAST vid); xmlNewProp(tmp_node, BAD_CAST "rid", BAD_CAST rid); - xmlNewProp(tmp_node, BAD_CAST "file", BAD_CAST _images[i].file); - xmlNewProp(tmp_node, BAD_CAST "servers", BAD_CAST _images[i].serverss); - xmlNewProp(tmp_node, BAD_CAST "cache", BAD_CAST _images[i].cache_file); + xmlNewProp(tmp_node, BAD_CAST "file", BAD_CAST image->file); + xmlNewProp(tmp_node, BAD_CAST "servers", BAD_CAST "???"); + xmlNewProp(tmp_node, BAD_CAST "cache", BAD_CAST image->cache_file); xmlAddChild(images_node, tmp_node); } - // Clients clients_node = xmlNewNode(NULL, BAD_CAST "clients"); - xmlAddChild(root_node, clients_node); + log_node = xmlAddChild(root_node, clients_node); for (iterator = _dnbd3_clients; iterator; iterator = iterator->next) { dnbd3_client_t *client = iterator->data; if (client->image) { tmp_node = xmlNewNode(NULL, BAD_CAST "client"); - xmlNewProp(tmp_node, BAD_CAST "ip", BAD_CAST client->ip); + *ipaddr = '\0'; + inet_ntop(client->addrtype, client->ipaddr, ipaddr, 100); + xmlNewProp(tmp_node, BAD_CAST "ip", BAD_CAST ipaddr); xmlNewProp(tmp_node, BAD_CAST "file", BAD_CAST client->image->file); xmlAddChild(clients_node, tmp_node); } } + pthread_spin_unlock(&_spinlock); + + // Log + log_parent_node = xmlNewChild(root_node, NULL, BAD_CAST "log", NULL); + char *log = fetchlog(0); + if (log == NULL) log = "LOG IS NULL"; + log_node = xmlNewCDataBlock(doc, BAD_CAST log, strlen(log)); + xmlAddChild(log_parent_node, log_node); // Dump and send xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1); @@ -224,10 +228,10 @@ void* dnbd3_ipc_receive() send(client_sock, (char *) xmlbuff, buffersize, MSG_WAITALL); // Cleanup - pthread_spin_unlock(&_spinlock); close(client_sock); xmlFree(xmlbuff); xmlFreeDoc(doc); + free(log); break; case IPC_ADDIMG: @@ -257,11 +261,10 @@ void* dnbd3_ipc_receive() if(cur->type == XML_ELEMENT_NODE) { dnbd3_image_t image; - image.group = (char *) xmlGetNoNsProp(cur, BAD_CAST "group"); - image.vid = atoi((char *) xmlGetNoNsProp(cur, BAD_CAST "vid")); + memset(&image, 0, sizeof(dnbd3_image_t)); + image.name = (char *) xmlGetNoNsProp(cur, BAD_CAST "name"); image.rid = atoi((char *) xmlGetNoNsProp(cur, BAD_CAST "rid")); image.file = (char *) xmlGetNoNsProp(cur, BAD_CAST "file"); - image.serverss = (char *) xmlGetNoNsProp(cur, BAD_CAST "servers"); image.cache_file = (char *) xmlGetNoNsProp(cur, BAD_CAST "cache"); header.error = htonl(dnbd3_add_image(&image, _config_file_name)); } @@ -308,11 +311,10 @@ void* dnbd3_ipc_receive() if(cur->type == XML_ELEMENT_NODE) { dnbd3_image_t image; - image.group = (char *) xmlGetNoNsProp(cur, BAD_CAST "group"); - image.vid = atoi((char *) xmlGetNoNsProp(cur, BAD_CAST "vid")); + memset(&image, 0, sizeof(dnbd3_image_t)); + image.name = (char *) xmlGetNoNsProp(cur, BAD_CAST "name"); image.rid = atoi((char *) xmlGetNoNsProp(cur, BAD_CAST "rid")); image.file = (char *) xmlGetNoNsProp(cur, BAD_CAST "file"); - image.serverss = (char *) xmlGetNoNsProp(cur, BAD_CAST "servers"); image.cache_file = (char *) xmlGetNoNsProp(cur, BAD_CAST "cache"); header.error = htonl(dnbd3_del_image(&image, _config_file_name)); } @@ -333,7 +335,7 @@ void* dnbd3_ipc_receive() break; default: - printf("ERROR: Unknown command: %i\n", header.cmd); + memlogf("ERROR: Unknown command: %i\n", header.cmd); header.size = htonl(0); header.error = htonl(ERROR_UNKNOWN); send(client_sock, (char *) &header, sizeof(header), MSG_WAITALL); @@ -343,6 +345,7 @@ void* dnbd3_ipc_receive() } } close(server_sock); + pthread_exit((void *) 0); } void dnbd3_ipc_send(int cmd) @@ -414,11 +417,21 @@ void dnbd3_ipc_send(int cmd) xmlNodeSetPtr nodes; xmlNodePtr cur; + // Print log + xpathExpr = BAD_CAST "/info/log"; + xpathCtx = xmlXPathNewContext(doc); + xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); + if (xpathObj->nodesetval && xpathObj->nodesetval->nodeTab && xpathObj->nodesetval->nodeTab[0]) { + printf("--- Last log lines ----\n%s\n\n", xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0])); + } + xmlXPathFreeObject(xpathObj); + xmlXPathFreeContext(xpathCtx); + // Print images xpathExpr = BAD_CAST "/info/images/image"; xpathCtx = xmlXPathNewContext(doc); xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); - printf("Exported images (atime, vid, rid, file):\n"); + printf("Exported images (atime, name, rid, file):\n"); printf("========================================\n"); nodes = xpathObj->nodesetval; n = (nodes) ? nodes->nodeNr : 0; @@ -428,7 +441,7 @@ void dnbd3_ipc_send(int cmd) { cur = nodes->nodeTab[i]; xmlChar *atime = xmlGetNoNsProp(cur, BAD_CAST "atime"); - xmlChar *vid = xmlGetNoNsProp(cur, BAD_CAST "vid"); + xmlChar *vid = xmlGetNoNsProp(cur, BAD_CAST "name"); xmlChar *rid = xmlGetNoNsProp(cur, BAD_CAST "rid"); xmlChar *file = xmlGetNoNsProp(cur, BAD_CAST "file"); printf("%s\t%s\t%s\t%s\n", atime, vid, rid, file); diff --git a/src/server/net.c b/src/server/net.c index cd93c0d..9ae168a 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -34,6 +34,83 @@ #include "server.h" #include "utils.h" +#include "memlog.h" +#include "../serialize.h" +#include "../config.h" + + +static char recv_request_header(int sock, dnbd3_request_t *request) +{ + // Read request heade from socket + if (recv(sock, request, sizeof(dnbd3_request_t), MSG_WAITALL) != sizeof(dnbd3_request_t)) + { + printf("[DEBUG] Error receiving request: Could not read message header\n"); + return 0; + } + // Make sure all bytes are in the right order (endianness) + fixup_request(*request); + if (request->magic != dnbd3_packet_magic) + { + printf("[DEBUG] Magic in client request incorrect\n"); + return 0; + } + // Payload sanity check + if (request->size > MAX_PAYLOAD) + { + memlogf("[WARNING] Client tries to send a packet of type %d with %d bytes payload. Dropping client.", (int)request->cmd, (int)request->size); + return 0; + } + return 1; +} + +static char recv_request_payload(int sock, uint32_t size, serialized_buffer_t *payload) +{ + if (size == 0) + { + memlogf("[BUG] Called recv_request_payload() to receive 0 bytes"); + return 0; + } + if (size > MAX_PAYLOAD) + { + memlogf("[BUG] Called recv_request_payload() for more bytes than the passed buffer could hold!"); + return 0; + } + if (recv(sock, payload->buffer, size, MSG_WAITALL) != size) + { + printf("[ERROR] Could not receive request payload of length %d\n", (int)size); + return 0; + } + // Prepare payload buffer for reading + serializer_reset_read(payload, size); + return 1; +} + +static char send_reply(int sock, dnbd3_reply_t *reply, void *payload) +{ + fixup_reply(*reply); + if (!payload || reply->size == 0) + { + if (send(sock, reply, sizeof(dnbd3_reply_t), MSG_WAITALL) != sizeof(dnbd3_reply_t)) + { + printf("[DEBUG] Send failed (header-only)\n"); + return 0; + } + } + else + { + struct iovec iov[2]; + iov[0].iov_base = reply; + iov[0].iov_len = sizeof(dnbd3_reply_t); + iov[1].iov_base = payload; + iov[1].iov_len = reply->size; + if (writev(sock, iov, 2) != sizeof(dnbd3_reply_t) + reply->size) + { + printf("[DEBUG] Send failed (reply with payload of %d bytes)\n", (int)reply->size); + return 0; + } + } + return 1; +} void *dnbd3_handle_query(void *dnbd3_client) { @@ -41,87 +118,146 @@ void *dnbd3_handle_query(void *dnbd3_client) dnbd3_request_t request; dnbd3_reply_t reply; - int cork = 1; - int uncork = 0; + const int cork = 1; + const int uncork = 0; dnbd3_image_t *image = NULL; - int image_file, image_cache = -1; + int image_file = -1, image_cache = -1; - struct in_addr alt_server; - int i = 0; + int i, num; uint64_t map_y; char map_x, bit_mask; + serialized_buffer_t payload; + char *image_name; + uint16_t rid, client_version; uint64_t todo_size = 0; uint64_t todo_offset = 0; uint64_t cur_offset = 0; uint64_t last_offset = 0; + dnbd3_server_entry_t server_list[NUMBER_SERVERS]; + int dirty = 0; - while (recv(client->sock, &request, sizeof(dnbd3_request_t), MSG_WAITALL) > 0) + reply.magic = dnbd3_packet_magic; + + // Receive first packet. This must be CMD_GET_SIZE by protocol specification + if (recv_request_header(client->sock, &request)) + { + if (request.cmd != CMD_GET_SIZE) + { + printf("[DEBUG] Client sent invalid handshake (%d). Dropping Client\n", (int)request.cmd); + } + else + { + if (recv_request_payload(client->sock, request.size, &payload)) + { + client_version = serializer_get_uint16(&payload); + image_name = serializer_get_string(&payload); + rid = serializer_get_uint16(&payload); + if (request.size < 3 || !image_name || client_version < MIN_SUPPORTED_CLIENT) + { + if (client_version < MIN_SUPPORTED_CLIENT) + { + printf("[DEBUG] Client too old\n"); + } + else + { + printf("[DEBUG] Incomplete handshake received\n"); + } + } + else + { + pthread_spin_lock(&_spinlock); + image = dnbd3_get_image(image_name, rid, 0); + if (!image) + { + printf("[DEBUG] Client requested non-existent image '%s'\n", image_name); + } + else + { + serializer_put_uint16(&payload, PROTOCOL_VERSION); + serializer_put_string(&payload, image->low_name); + serializer_put_uint16(&payload, image->rid); + serializer_put_uint64(&payload, image->filesize); + reply.cmd = CMD_GET_SIZE; + reply.size = serializer_get_written_length(&payload); + if (!send_reply(client->sock, &reply, &payload)) + { + image = NULL; + } + else + { + image_file = open(image->file, O_RDONLY); + if (image_file == -1) + { + image = NULL; + } + else + { + client->image = image; + image->atime = time(NULL); // TODO: check if mutex is needed + + if (image->cache_map && image->cache_file) + image_cache = open(image->cache_file, O_RDWR); + } + } + } + pthread_spin_unlock(&_spinlock); + } + } + } + } + + if (image) while (recv_request_header(client->sock, &request)) { - reply.cmd = request.cmd; - reply.size = 0; - memcpy(reply.handle, request.handle, sizeof(request.handle)); - pthread_spin_lock(&client->spinlock); switch (request.cmd) { - case CMD_GET_SERVERS: - image = dnbd3_get_image(request.vid, request.rid); - if(!image) - goto error; - - int num = (image->num_servers < NUMBER_SERVERS) ? image->num_servers : NUMBER_SERVERS; - reply.vid = image->vid; - reply.rid = dnbd3_get_image(request.vid, 0)->rid; - reply.size = num * sizeof(struct in_addr); - send(client->sock, (char *) &reply, sizeof(dnbd3_reply_t), 0); - - for (i = 0; i < num; i++) - { - inet_aton(image->servers[i], &alt_server); - send(client->sock, (char *) &alt_server, sizeof(struct in_addr), 0); - } - client->image = image; - image->atime = time(NULL); // TODO: check if mutex is needed - break; - - case CMD_GET_SIZE: - image = dnbd3_get_image(request.vid, request.rid); - if(!image) - goto error; - - reply.vid = image->vid; - reply.rid = image->rid; - reply.size = sizeof(uint64_t); - send(client->sock, (char *) &reply, sizeof(dnbd3_reply_t), 0); - - send(client->sock, &image->filesize, sizeof(uint64_t), 0); - image_file = open(image->file, O_RDONLY); - client->image = image; - image->atime = time(NULL); // TODO: check if mutex is needed - - if (image->cache_file) - image_cache = open(image->cache_file, O_RDWR); - - break; case CMD_GET_BLOCK: - if (image_file < 0) - goto error; + if (request.offset >= image->filesize) + { // Sanity check + memlogf("[WARNING] Client requested non-existent block"); + reply.size = 0; + reply.cmd = CMD_ERROR; + send_reply(client->sock, &reply, NULL); + break; + } + if (request.offset + request.size > image->filesize) + { // Sanity check + memlogf("[WARNING] Client requested data block that extends beyond image size"); + reply.size = 0; + reply.cmd = CMD_ERROR; + send_reply(client->sock, &reply, NULL); + break; + } + if (request.size > image->filesize) + { // Sanity check + memlogf("[WARNING] Client requested data block that is bigger than the image size"); + reply.size = 0; + reply.cmd = CMD_ERROR; + send_reply(client->sock, &reply, NULL); + break; + } + // TODO: Try MSG_MORE instead of cork+uncork if performance ever becomes an issue.. setsockopt(client->sock, SOL_TCP, TCP_CORK, &cork, sizeof(cork)); + reply.cmd = CMD_GET_BLOCK; reply.size = request.size; - send(client->sock, (char *) &reply, sizeof(dnbd3_reply_t), 0); + reply.handle = request.handle; + send_reply(client->sock, &reply, NULL); // caching is off - if (!image->cache_file) + if (image_cache == -1) { - if (sendfile(client->sock, image_file, (off_t *) &request.offset, request.size) < 0) - printf("ERROR: Sendfile failed (sock)\n"); + if (sendfile(client->sock, image_file, (off_t *)&request.offset, request.size) != request.size) + { + printf("[ERROR] sendfile failed (image to net)\n"); + close(client->sock); + } setsockopt(client->sock, SOL_TCP, TCP_CORK, &uncork, sizeof(uncork)); break; @@ -134,10 +270,11 @@ void *dnbd3_handle_query(void *dnbd3_client) cur_offset = request.offset; last_offset = request.offset + request.size; + // first make sure the whole requested part is in the local cache file while(cur_offset < last_offset) { map_y = cur_offset >> 15; - map_x = (cur_offset >> 12) & 7; // mod 8 + map_x = (cur_offset >> 12) & 7; // mod 256 bit_mask = 0b00000001 << (map_x); cur_offset += 4096; @@ -147,8 +284,15 @@ void *dnbd3_handle_query(void *dnbd3_client) if (todo_size != 0) // fetch missing chunks { lseek(image_cache, todo_offset, SEEK_SET); - if (sendfile(image_cache, image_file, (off_t *) &todo_offset, todo_size) < 0) - printf("ERROR: Sendfile failed (cache)\n"); + if (sendfile(image_cache, image_file, (off_t *) &todo_offset, todo_size) != todo_size) + { + printf("[ERROR] sendfile failed (copy to cache 1)\n"); + close(client->sock); + // Reset these so we don't update the cache map with false information + dirty = 0; + todo_size = 0; + break; + } todo_size = 0; dirty = 1; } @@ -164,13 +308,16 @@ void *dnbd3_handle_query(void *dnbd3_client) if (todo_size != 0) { lseek(image_cache, todo_offset, SEEK_SET); - if (sendfile(image_cache, image_file, (off_t *) &todo_offset, todo_size) < 0) - printf("ERROR: Sendfile failed (cache)\n"); - + if (sendfile(image_cache, image_file, (off_t *) &todo_offset, todo_size) != todo_size) + { + printf("[ERROR] sendfile failed (copy to cache 2)\n"); + close(client->sock); + break; + } dirty = 1; } - if (dirty) + if (dirty) // cache map needs to be updated as something was missing locally { // set 1 in cache map for whole request cur_offset = request.offset; @@ -185,35 +332,42 @@ void *dnbd3_handle_query(void *dnbd3_client) } // send data to client - if (sendfile(client->sock, image_cache, (off_t *) &request.offset, request.size) < 0) - printf("ERROR: Sendfile failed (net)\n"); + if (sendfile(client->sock, image_cache, (off_t *) &request.offset, request.size) != request.size) + { + memlogf("[ERROR] sendfile failed (cache to net)\n"); + close(client->sock); + } setsockopt(client->sock, SOL_TCP, TCP_CORK, &uncork, sizeof(uncork)); break; + + case CMD_GET_SERVERS: + // Build list of known working alt servers + num = 0; + for (i = 0; i < NUMBER_SERVERS; i++) + { + if (image->servers[i].addrtype == 0 || image->servers[i].failures > 200) continue; + memcpy(server_list + num++, image->servers + i, sizeof(dnbd3_server_entry_t)); + } + reply.cmd = CMD_GET_SERVERS; + reply.size = num * sizeof(dnbd3_server_entry_t); + send_reply(client->sock, &reply, server_list); + break; + default: - printf("ERROR: Unknown command\n"); + memlogf("ERROR: Unknown command\n"); break; } - pthread_spin_unlock(&client->spinlock); - continue; - - error: - printf("ERROR: Client requested an unknown image id.\n"); - send(client->sock, (char *) &reply, sizeof(dnbd3_reply_t), 0); - pthread_spin_unlock(&client->spinlock); - continue; - } close(client->sock); - close(image_file); - close(image_cache); + if (image_file != -1) close(image_file); + if (image_cache != -1) close(image_cache); pthread_spin_lock(&_spinlock); _dnbd3_clients = g_slist_remove(_dnbd3_clients, client); pthread_spin_unlock(&_spinlock); - printf("INFO: Client %s exit\n", client->ip); free(client); pthread_exit((void *) 0); } @@ -227,7 +381,7 @@ int dnbd3_setup_socket() sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { - printf("ERROR: Socket failure\n"); + memlogf("ERROR: Socket setup failure\n"); return -1; } @@ -239,14 +393,14 @@ int dnbd3_setup_socket() // Bind to socket if (bind(sock, (struct sockaddr*) &server, sizeof(server)) < 0) { - printf("ERROR: Bind failure\n"); + memlogf("ERROR: Bind failure\n"); return -1; } // Listen on socket if (listen(sock, 100) == -1) { - printf("ERROR: Listen failure\n"); + memlogf("ERROR: Listen failure\n"); return -1; } diff --git a/src/server/serialize.c b/src/server/serialize.c new file mode 100644 index 0000000..4934132 --- /dev/null +++ b/src/server/serialize.c @@ -0,0 +1,5 @@ +#include <stdio.h> +#include <string.h> +#include <stdint.h> + +#include "../serialize.c" diff --git a/src/server/server.c b/src/server/server.c index 371d27a..0a206d1 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -34,14 +34,14 @@ #include "utils.h" #include "net.h" #include "ipc.h" +#include "memlog.h" int _sock; pthread_spinlock_t _spinlock; GSList *_dnbd3_clients = NULL; char *_config_file_name = DEFAULT_SERVER_CONFIG_FILE; -dnbd3_image_t *_images; -size_t _num_images = 0; +GSList *_dnbd3_images; // of dnbd3_image_t void dnbd3_print_help(char* argv_0) { @@ -65,8 +65,10 @@ void dnbd3_print_version() void dnbd3_cleanup() { - int i, fd; - printf("INFO: Cleanup...\n"); + int fd; + memlogf("INFO: Cleanup...\n"); + + close(_sock); pthread_spin_lock(&_spinlock); GSList *iterator = NULL; @@ -74,39 +76,38 @@ void dnbd3_cleanup() { dnbd3_client_t *client = iterator->data; shutdown(client->sock, SHUT_RDWR); - pthread_join(*client->thread, NULL); + pthread_join(client->thread, NULL); + g_free(client); } g_slist_free(_dnbd3_clients); - for (i = 0; i < _num_images; i++) + for (iterator = _dnbd3_images; iterator; iterator = iterator->next) { // save cache maps to files - if (_images[i].cache_file) + dnbd3_image_t *image = iterator->data; + if (image->cache_file) { - char tmp[strlen(_images[i].cache_file)+4]; - strcpy(tmp, _images[i].cache_file); + char tmp[strlen(image->cache_file)+4]; + strcpy(tmp, image->cache_file); strcat(tmp, ".map"); fd = open(tmp, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (fd > 0) - write(fd, _images[i].cache_map, (_images[i].filesize >> 15) * sizeof(char)); + write(fd, image->cache_map, ((image->filesize + (1 << 15) - 1) >> 15) * sizeof(char)); close(fd); } - free(_images[i].group); - free(_images[i].file); - free(_images[i].servers); - free(_images[i].serverss); - free(_images[i].cache_file); - free(_images[i].cache_map); + free(image->name); + g_free(image->file); + g_free(image->cache_file); + free(image->cache_map); + g_free(image); } + g_slist_free(_dnbd3_images); pthread_spin_unlock(&_spinlock); - - close(_sock); - free(_images); #ifndef IPC_TCP unlink(UNIX_SOCKET); #endif @@ -161,6 +162,7 @@ int main(int argc, char* argv[]) break; case '?': dnbd3_print_help(argv[0]); + break; } opt = getopt_long(argc, argv, optString, longOpts, &longIndex); } @@ -168,6 +170,10 @@ int main(int argc, char* argv[]) if (demonize) daemon(1, 0); + pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); + + initmemlog(); + // load config file dnbd3_load_config(_config_file_name); @@ -191,9 +197,7 @@ int main(int argc, char* argv[]) pthread_t thread_ipc; pthread_create(&(thread_ipc), NULL, dnbd3_ipc_receive, NULL); - pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); - - printf("INFO: Server is ready...\n"); + memlogf("[INFO] Server is ready..."); // main loop while (1) @@ -201,28 +205,45 @@ int main(int argc, char* argv[]) fd = accept(_sock, (struct sockaddr*) &client, &len); if (fd < 0) { - printf("ERROR: Accept failure\n"); + memlogf("[ERROR] Accept failure"); continue; } - printf("INFO: Client %s connected\n", inet_ntoa(client.sin_addr)); + //memlogf("INFO: Client %s connected\n", inet_ntoa(client.sin_addr)); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, sizeof(timeout)); - pthread_t thread; - dnbd3_client_t *dnbd3_client = (dnbd3_client_t *) malloc(sizeof(dnbd3_client_t)); - pthread_spin_init(&dnbd3_client->spinlock, PTHREAD_PROCESS_PRIVATE); - strcpy(dnbd3_client->ip, inet_ntoa(client.sin_addr)); + dnbd3_client_t *dnbd3_client = g_new0(dnbd3_client_t, 1); + if (dnbd3_client == NULL) + { + memlogf("[ERROR] Could not alloc dnbd3_client_t for new client."); + close(fd); + continue; + } + // TODO: Extend this if you ever want to add IPv6 (something like:) + // dnbd3_client->addrtype = AF_INET6; + // memcpy(dnbd3_client->ipaddr, &(client.sin6_addr), 16); + dnbd3_client->addrtype = AF_INET; + memcpy(dnbd3_client->ipaddr, &(client.sin_addr), 4); dnbd3_client->sock = fd; - dnbd3_client->thread = &thread; dnbd3_client->image = NULL; + // This has to be done before creating the thread, otherwise a race condition might occur when the new thread dies faster than this thread adds the client to the list after creating the thread pthread_spin_lock(&_spinlock); _dnbd3_clients = g_slist_append(_dnbd3_clients, dnbd3_client); pthread_spin_unlock(&_spinlock); - pthread_create(&(thread), NULL, dnbd3_handle_query, (void *) (uintptr_t) dnbd3_client); - pthread_detach(thread); + if (0 != pthread_create(&(dnbd3_client->thread), NULL, dnbd3_handle_query, (void *) (uintptr_t) dnbd3_client)) + { + memlogf("[ERROR] Could not start thread for new client."); + pthread_spin_lock(&_spinlock); + _dnbd3_clients = g_slist_remove(_dnbd3_clients, dnbd3_client); + pthread_spin_unlock(&_spinlock); + g_free(dnbd3_client); + close(fd); + continue; + } + pthread_detach(dnbd3_client->thread); } dnbd3_cleanup(); diff --git a/src/server/server.h b/src/server/server.h index f499acc..69d597b 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -30,33 +30,31 @@ typedef struct { - char *group; - char *file; - uint64_t filesize; - size_t num_servers; - char **servers; - char *serverss; - int vid; - int rid; - time_t atime; - char *cache_map; - char *cache_file; + char *name; // full name of image, eg. "uni-freiburg.ubuntu-12.04" + char *low_name; // full name of image, lowercased for comparison + int rid; // revision of provided image + char *file; // path to image file or device + uint64_t filesize; // size of image + dnbd3_server_entry_t servers[NUMBER_SERVERS]; // known alt servers that also offer that image + time_t atime; // last access time + uint8_t *cache_map; // cache map telling which parts are locally cached + char *cache_file; // path to local cache of image (in case the image is read from a dnbd3 device) + char working; // whether this image is considered working. local images are "working" if the local file exists, proxied images have to have at least one working upstream server or a complete local cache file } dnbd3_image_t; typedef struct { int sock; - char ip[16]; - pthread_t *thread; + uint8_t ipaddr[16]; + uint8_t addrtype; // ip version (AF_INET or AF_INET6) + pthread_t thread; dnbd3_image_t *image; - pthread_spinlock_t spinlock; } dnbd3_client_t; -extern GSList *_dnbd3_clients; +extern GSList *_dnbd3_clients; // of dnbd3_client_t extern pthread_spinlock_t _spinlock; extern char *_config_file_name; -extern dnbd3_image_t *_images; -extern size_t _num_images; +extern GSList *_dnbd3_images; // of dnbd3_image_t void dnbd3_cleanup(); diff --git a/src/server/utils.c b/src/server/utils.c index 0be1569..15e51f8 100644 --- a/src/server/utils.c +++ b/src/server/utils.c @@ -23,14 +23,124 @@ #include <sys/stat.h> #include <pthread.h> #include <string.h> +#include <glib.h> +#include <netinet/in.h> +#include <arpa/inet.h> #include "server.h" #include "utils.h" +#include "memlog.h" + +/** + * Parse IPv4 or IPv6 address in string representation to a suitable format usable by the BSD socket library + * @string eg. "1.2.3.4" or "2a01::10:5", optially with port appended, eg "1.2.3.4:6666" or "2a01::10:5:6666" + * @af will contain either AF_INET or AF_INET6 + * @addr will contain the address in network representation + * @port will contain the port in network representation, defaulting to #define PORT if none was given + * returns 1 on success, 0 in failure. contents of af, addr and port are undefined in the latter case + */ +static char parse_address(char *string, uint8_t *af, uint8_t *addr, uint16_t *port) +{ + struct in_addr v4; + struct in6_addr v6; + + // Try IPv4 without port + if (1 == inet_pton(AF_INET, string, &v4)) + { + *af = AF_INET; + memcpy(addr, &v4, 4); + *port = htons(PORT); + return 1; + } + // Try IPv6 without port + if (1 == inet_pton(AF_INET6, string, &v6)) + { + *af = AF_INET6; + memcpy(addr, &v6, 16); + *port = htons(PORT); + return 1; + } + + // Scan for port + char *portpos = NULL, *ptr = string; + while (*ptr) + { + if (*ptr == ':') portpos = ptr; + ++ptr; + } + if (portpos == NULL) return 0; // No port in string + // Consider IP being surrounded by [ ] + if (*string == '[' && *(portpos-1) == ']') + { + ++string; + *(portpos-1) = '\0'; + } + *portpos++ = '\0'; + int p = atoi(portpos); + if (p < 1 || p > 65535) return 0; // Invalid port + *port = htons((uint16_t)p); + + // Try IPv4 with port + if (1 == inet_pton(AF_INET, string, &v4)) + { + *af = AF_INET; + memcpy(addr, &v4, 4); + return 1; + } + // Try IPv6 with port + if (1 == inet_pton(AF_INET6, string, &v6)) + { + *af = AF_INET6; + memcpy(addr, &v6, 16); + return 1; + } + + // FAIL + return 0; +} + +static char is_valid_namespace(char *namespace) +{ + if (*namespace == '\0' || *namespace == '/') return 0; // Invalid: Length = 0 or starting with a slash + while (*namespace) + { + if (*namespace != '/' && *namespace != '-' + && (*namespace < 'a' || *namespace > 'z') + && (*namespace < 'A' || *namespace > 'Z')) return 0; + ++namespace; + } + if (*(namespace - 1) == '/') return 0; // Invalid: Ends in a slash + return 1; +} + +static char is_valid_imagename(char *namespace) +{ + if (*namespace == '\0' || *namespace == ' ') return 0; // Invalid: Length = 0 or starting with a space + while (*namespace) + { // Check for invalid chars + if (*namespace != '.' && *namespace != '-' && *namespace != ' ' + && *namespace != '(' && *namespace != ')' + && (*namespace < 'a' || *namespace > 'z') + && (*namespace < 'A' || *namespace > 'Z')) return 0; + ++namespace; + } + if (*(namespace - 1) == ' ') return 0; // Invalid: Ends in a space + return 1; +} + +static void strtolower(char *string) +{ + while (*string) + { + if (*string >= 'A' && *string <= 'Z') *string += 32; + ++string; + } +} void dnbd3_load_config(char *file) { int fd; - gint i; + gint i, j, k; GKeyFile* gkf; gkf = g_key_file_new(); @@ -40,111 +150,209 @@ void dnbd3_load_config(char *file) exit(EXIT_FAILURE); } + char *namespace = g_key_file_get_string(gkf, "settings", "default_namespace", NULL); + if (namespace && !is_valid_namespace(namespace)) + { + memlogf("[ERROR] Ignoring default namespace: '%s' is not a valid namespace", namespace); + g_free(namespace); + namespace = NULL; + } + gchar **groups = NULL; - groups = g_key_file_get_groups(gkf, &_num_images); - _images = calloc(_num_images, sizeof(dnbd3_image_t)); + gsize section_count; + groups = g_key_file_get_groups(gkf, §ion_count); - for (i = 0; i < _num_images; i++) + for (i = 0; i < section_count; i++) { - _images[i].group = malloc(strlen(groups[i])); - strcpy(_images[i].group, groups[i]); - _images[i].file = g_key_file_get_string(gkf, groups[i], "file", NULL); - _images[i].servers = g_key_file_get_string_list(gkf, groups[i], "servers", &_images[i].num_servers, NULL); - _images[i].serverss = g_key_file_get_string(gkf, groups[i], "servers", NULL); - _images[i].vid = g_key_file_get_integer(gkf, groups[i], "vid", NULL); - _images[i].rid = g_key_file_get_integer(gkf, groups[i], "rid", NULL); - _images[i].cache_file = g_key_file_get_string(gkf, groups[i], "cache", NULL); - _images[i].atime = 0; - - if (_images[i].num_servers > NUMBER_SERVERS) - printf("WARN: Max allowed servers %i\n", NUMBER_SERVERS); - - fd = open(_images[i].file, O_RDONLY); - if (fd > 0) - _images[i].filesize = lseek(fd, 0, SEEK_END); + // Special group + if (strcmp(groups[i], "settings") == 0 || strcmp(groups[i], "trusted") == 0) + { + continue; + } + + // An actual image definition + + if (!is_valid_imagename(groups[i])) + { + memlogf("[ERROR] Invalid image name: '%s'", groups[i]); + continue; + } + + int rid = g_key_file_get_integer(gkf, groups[i], "rid", NULL); + if (rid <= 0) + { + memlogf("[ERROR] Invalid rid '%d' for image '%s'", rid, groups[i]); + continue; + } + + if (strchr(groups[i], '.') == NULL && namespace == NULL) + { + memlogf("[ERROR] Image '%s' has local name and no default namespace is defined; entry ignored.", groups[i]); + continue; + } + + dnbd3_image_t *image = g_new0(dnbd3_image_t, 1); + if (image == NULL) + { + memlogf("[ERROR] Could not allocate dnbd3_image_t while reading config"); + continue; + } + + if (strchr(groups[i], '/') == NULL) + { // Local image, build global name + image->name = calloc(strlen(namespace) + strlen(groups[i]) + 2, sizeof(char)); + sprintf(image->name, "%s/%s", namespace, groups[i]); + } else - printf("ERROR: Image file not found: %s\n", _images[i].file); - - close(fd); - - if (_images[i].cache_file) { - // read cache map from file - _images[i].cache_map = calloc(_images[i].filesize >> 15, sizeof(char)); - memset(_images[i].cache_map, 0, (_images[i].filesize >> 15) * sizeof(char)); - char tmp[strlen(_images[i].cache_file)+4]; - strcpy(tmp, _images[i].cache_file); - strcat(tmp, ".map"); - fd = open(tmp, O_RDONLY); - if (fd > 0) - read(fd, _images[i].cache_map, (_images[i].filesize >> 15) * sizeof(char)); - close(fd); - - // open cache file - fd = open(_images[i].cache_file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); - if (fd < 1) - printf("ERROR: Could't create cache file\n"); - - if (_images[i].filesize != lseek(fd, 0, SEEK_END)) - fallocate(fd, 0, 0, _images[i].filesize); - - close(fd); + image->name = strdup(groups[i]); } - } - g_strfreev(groups); - g_key_file_free(gkf); -} - -void dnbd3_reload_config(char* config_file_name) -{ - int i, fd; - pthread_spin_lock(&_spinlock); - GSList *iterator = NULL; - for (iterator = _dnbd3_clients; iterator; iterator = iterator->next) - { - dnbd3_client_t *client = iterator->data; - pthread_spin_lock(&client->spinlock); - client->image = NULL; - } - - for (i = 0; i < _num_images; i++) - { - // save cache maps - if (_images[i].cache_file) + if (dnbd3_get_image(image->name, rid, 0)) + { + memlogf("[ERROR] Duplicate image in config: '%s' rid:%d", image->name, rid); + free(image->name); + g_free(image); + continue; + } + + image->low_name = strdup(image->name); + strtolower(image->low_name); + + image->rid = rid; + image->file = g_key_file_get_string(gkf, groups[i], "file", NULL); + char relayed = image->file == NULL || *image->file == '\0'; + if (relayed && image->file) + { + g_free(image->file); + image->file = NULL; + } + + if (relayed) // Image is relayed (this server acts as proxy) + { + if (strchr(groups[i], '.') == NULL) + { + memlogf("[ERROR] Relayed image without global name in config: '%s'", groups[i]); + g_free(image); + continue; + } + image->cache_file = g_key_file_get_string(gkf, groups[i], "cache", NULL); + if (image->cache_file && *image->cache_file == '\0') g_free(image->cache_file); + } + else // Image is a local one, open file to get size { - char tmp[strlen(_images[i].cache_file)+4]; - strcpy(tmp, _images[i].cache_file); - strcat(tmp, ".map"); - fd = open(tmp, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); - - if (fd > 0) - write(fd, _images[i].cache_map, (_images[i].filesize >> 15) * sizeof(char)); + fd = open(image->file, O_RDONLY); + if (fd > 0) { + image->filesize = lseek(fd, 0, SEEK_END); + if (image->filesize & 4095) { + memlogf("[WARNING] Size of image '%s' is not a multiple of 4096. Last incomplete block will be ignored!", image->file); + image->filesize &= ~(uint64_t)4095; + } + close(fd); + image->working = 1; + } else { + memlogf("[ERROR] Image file not found: '%s'", image->file); + } + } - close(fd); + // A list of servers that are known to also host or relay this image + gsize num_servers; + gchar **servers = g_key_file_get_string_list(gkf, groups[i], "servers", &num_servers, NULL); + if (servers) for (k = 0, j = 0; j < MIN(num_servers, NUMBER_SERVERS); ++j) + { + if (parse_address(servers[j], &(image->servers[k].addrtype), image->servers[k].ipaddr, &(image->servers[k].port))) + { + ++k; continue; + } + image->servers[k].addrtype = 0; } + g_strfreev(servers); - free(_images[i].group); - free(_images[i].file); - free(_images[i].servers); - free(_images[i].serverss); - free(_images[i].cache_file); - free(_images[i].cache_map); + if (image->cache_file) + { + // Determine size of cached image + fd = open(image->cache_file, O_RDONLY); + if (fd > 0) + { + image->filesize = lseek(fd, 0, SEEK_END); + close(fd); + } + if (image->filesize & 4095) + { // Cache files should always be trincated to 4kib boundaries already + memlogf("[WARNING] Size of cache file '%s' is not a multiple of 4096. Something's fishy!", image->cache_file); + image->filesize = 0; + } + else if (image->filesize > 0) + { + const size_t map_len_bytes = (image->filesize + (1 << 15) - 1) >> 15; + image->cache_map = calloc(map_len_bytes, sizeof(uint8_t)); + // read cache map from file + // one byte in the map covers 8 4kib blocks, so 32kib per byte + // "+ (1 << 15) - 1" is required to account for the last bit of + // the image that is smaller than 32kib + // this would be the case whenever the image file size is not a + // multiple of 32kib (= the number of blocks is not dividable by 8) + // ie: if the image is 49152 bytes and you do 49152 >> 15 you get 1, + // but you actually need 2 bytes to have a complete cache map + char tmp[strlen(image->cache_file)+4]; + strcpy(tmp, image->cache_file); + strcat(tmp, ".map"); + fd = open(tmp, O_RDONLY); + if (fd > 0) + { + read(fd, image->cache_map, map_len_bytes * sizeof(uint8_t)); + close(fd); + // If the whole image is cached, mark it as working right away without waiting for an upstream server + image->working = 1; + for (j = 0; j < map_len_bytes - 1; ++j) + { + if (image->cache_map[j] != 0xFF) + { + image->working = 0; + break; + } + } + const int blocks_in_last_byte = (image->filesize >> 12) & 7; + uint8_t last_byte = 0; + if (blocks_in_last_byte == 0) + last_byte = 0xFF; + else + for (j = 0; j < k; ++j) last_byte = (last_byte << 1) | 1; + if ((image->cache_map[map_len_bytes-1] & last_byte) != last_byte) + image->working = 0; + else + memlogf("[INFO] Publishing relayed image '%s' because the local cache copy is complete", image->name); + } + + /* + // TODO: Do this as soon as a connection to a upstream server is established + // open cache file + fd = open(_images[i].cache_file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (fd < 1) + memlogf("ERROR: Could't create cache file '%s'", _images[i].cache_file); + + if (_images[i].filesize != lseek(fd, 0, SEEK_END)) + fallocate(fd, 0, 0, _images[i].filesize); + + close(fd); + */ + } + } // end cache_file handling + pthread_spin_lock(&_spinlock); + _dnbd3_images = g_slist_append(_dnbd3_images, image); + pthread_spin_unlock(&_spinlock); + // DONE IMAGE } - _num_images = 0; - free(_images); - dnbd3_load_config(config_file_name); - for (iterator = _dnbd3_clients; iterator; iterator = iterator->next) - { - dnbd3_client_t *client = iterator->data; - pthread_spin_unlock(&client->spinlock); - } - pthread_spin_unlock(&_spinlock); + g_free(namespace); + g_strfreev(groups); + g_key_file_free(gkf); } int dnbd3_add_image(dnbd3_image_t *image, char *file) { + return ERROR_IMAGE_ALREADY_EXISTS; // TODO: Make it work with image names + /* FILE* f = fopen(image->file,"r"); if (f == NULL) { @@ -198,10 +406,13 @@ int dnbd3_add_image(dnbd3_image_t *image, char *file) printf("ERROR: Config file is not writable: %s\n", file); return ERROR_CONFIG_FILE_PERMISSIONS; } + */ } int dnbd3_del_image(dnbd3_image_t *image, char *file) { + return ERROR_IMAGE_NOT_FOUND; // TODO: Make it work with image names + /* if (image->rid == 0) { printf("ERROR: Delete with rid=0 is not allowed\n"); @@ -254,38 +465,47 @@ int dnbd3_del_image(dnbd3_image_t *image, char *file) printf("ERROR: Config file is not writable: %s\n", file); return ERROR_CONFIG_FILE_PERMISSIONS; } + */ } -dnbd3_image_t* dnbd3_get_image(int vid, int rid) +dnbd3_image_t* dnbd3_get_image(char *name_orig, int rid, const char do_lock) { - int i, max = 0; - dnbd3_image_t *result = NULL; - for (i = 0; i < _num_images; ++i) + dnbd3_image_t *result = NULL, *image; + GSList *iterator; + char name[strlen(name_orig) + 1]; + strcpy(name, name_orig); + strtolower(name); + if (do_lock) pthread_spin_lock(&_spinlock); + for (iterator = _dnbd3_images; iterator; iterator = iterator->next) { + image = iterator->data; if (rid != 0) // rid was specified { - if (_images[i].vid == vid && _images[i].rid == rid) - result = &_images[i]; + if (image->rid == rid && strcmp(name, image->low_name) == 0) + { + result = image; + break; + } } else // search max. rid available { - if (_images[i].vid == vid && _images[i].rid > max) + if (strcmp(name, image->low_name) == 0 && (result == NULL || result->rid < image->rid)) { - result = &_images[i]; - max = _images[i].rid; + result = image; } } } + if (do_lock) pthread_spin_unlock(&_spinlock); return result; } void dnbd3_handle_sigpipe(int signum) { - printf("ERROR: SIGPIPE received!\n"); + memlogf("ERROR: SIGPIPE received!\n"); } void dnbd3_handle_sigterm(int signum) { - printf("INFO: SIGTERM or SIGINT received!\n"); + memlogf("INFO: SIGTERM or SIGINT received!\n"); dnbd3_cleanup(); } diff --git a/src/server/utils.h b/src/server/utils.h index d9d3ebc..ec83e64 100644 --- a/src/server/utils.h +++ b/src/server/utils.h @@ -35,11 +35,10 @@ #define ERROR_UNKNOWN 10 void dnbd3_load_config(char *file); -void dnbd3_reload_config(char* config_file_name); int dnbd3_add_image(dnbd3_image_t *image, char *file); int dnbd3_del_image(dnbd3_image_t *image, char *file); -dnbd3_image_t* dnbd3_get_image(int vid, int rid); +dnbd3_image_t* dnbd3_get_image(char *name, int rid, const char do_lock); void dnbd3_handle_sigpipe(int signum); void dnbd3_handle_sigterm(int signum); |