diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/file.c | 58 | ||||
-rw-r--r-- | src/server/file.h | 7 | ||||
-rw-r--r-- | src/server/server.c | 43 |
3 files changed, 23 insertions, 85 deletions
diff --git a/src/server/file.c b/src/server/file.c deleted file mode 100644 index c53ca15..0000000 --- a/src/server/file.c +++ /dev/null @@ -1,58 +0,0 @@ -#include <fcntl.h> -#include <errno.h> -#include "file.h" - -int file_open(char *filename) -{ - int fd = open(filename, O_RDONLY); - if (fd == -1) - return -1; - - struct stat st; - if (fstat(fd, &st) == -1) - return -1; - - return fd; -} - -int file_getsize(int fd, off_t *size) -{ - *size = lseek64(fd, 0, SEEK_END); - - if (*size == -1) - return -1; - - return 0; -} - -int file_read(int fd, void *buf, size_t size, off_t pos) -{ - off_t newpos = lseek(fd, pos, SEEK_SET); - - if (newpos == -1) - return -1; - - size_t nleft = size; - ssize_t nread; - char *ptr = buf; - - while (nleft > 0) - { - if ((nread = read(fd, ptr, nleft)) < 0) - { - if (errno == EINTR) - continue; - - return -1; - } - if (nread == 0) - { - break; - } - - nleft -= nread; - ptr += nread; - } - - return 0; -} diff --git a/src/server/file.h b/src/server/file.h deleted file mode 100644 index e61849a..0000000 --- a/src/server/file.h +++ /dev/null @@ -1,7 +0,0 @@ -#include <unistd.h> - -int file_open(char *filename); - -int file_getsize(int fd, off_t *size); - -int file_read(int fd, void *buf, size_t size, off_t pos); diff --git a/src/server/server.c b/src/server/server.c index e38a0e3..dfa53d7 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -12,12 +12,12 @@ #include <getopt.h> #include <netinet/in.h> #include <sys/sendfile.h> +#include <sys/stat.h> #include <pthread.h> -#include "../include/types.h" +#include "../types.h" #include "../version.h" -#include "file.h" int file; @@ -40,40 +40,44 @@ void print_version() void handle_sigpipe(int signum) { - printf("Program received signal SIGPIPE, Broken pipe (errno: %i)\n", errno); + printf("ERROR: Received signal SIGPIPE, Broken pipe (errno: %i)\n", errno); return; } -void *echo(void *client_socket) +void *handle_query(void *client_socket) { int sock = (int) client_socket; struct dnbd3_request request; struct dnbd3_reply reply; uint16_t cmd; off_t filesize; + struct stat st; + fstat(file, &st); + filesize = st.st_size; while (recv(sock, &request, sizeof(struct dnbd3_request), MSG_WAITALL) > 0) { cmd = request.cmd; -// char buf[request.size]; switch (cmd) { case CMD_GET_SIZE: reply.cmd = request.cmd; - file_getsize(file, &filesize); reply.filesize = filesize; send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); break; case CMD_GET_BLOCK: -// printf("CMD: %i, Byte: %llu, Size: %llu\n",request.cmd, request.offset, request.size); -// file_read(file, buf, request.size, request.offset); -// send(sock, (char *) buf, request.size, 0); - sendfile(sock, file, (off_t *) &request.offset, request.size); + reply.cmd = request.cmd; + memcpy(reply.handle, request.handle, sizeof(request.handle)); + send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); + + if (sendfile(sock, file, (off_t *) &request.offset, request.size) <0) + printf("ERROR: sendfile returned -1\n"); + break; default: - ; + printf("ERROR: Unknown command\n"); } } @@ -102,7 +106,7 @@ int main(int argc, char* argv[]) switch (opt) { case 'f': - file = file_open(optarg); + file = open(optarg, O_RDONLY); break; case 'h': print_help(argv[0]); @@ -119,11 +123,9 @@ int main(int argc, char* argv[]) signal(SIGPIPE, handle_sigpipe); struct sockaddr_in server; - struct sockaddr_in client[50]; + struct sockaddr_in client; int sock, fd; unsigned int len; - pthread_t thread[50]; - int i=1; // Create socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -154,20 +156,21 @@ int main(int argc, char* argv[]) printf("INFO: Server is ready...\n"); - // TODO: dyn threads while (1) { len = sizeof(client); - fd = accept(sock, (struct sockaddr*) &client[i], &len); + fd = accept(sock, (struct sockaddr*) &client, &len); if (fd < 0) { printf("ERROR: Accept failure\n"); exit(EXIT_FAILURE); } + printf("INFO: Client: %s connected\n", inet_ntoa(client.sin_addr)); - printf("INFO: Client: %s connected\n", inet_ntoa(client[i].sin_addr)); - pthread_create(&(thread[i]), NULL, echo, (void *) fd); - pthread_detach(thread[i++]); + // FIXME: catch SIGKILL/SIGTERM and close all socket before exit + pthread_t thread; + pthread_create(&(thread), NULL, handle_query, (void *) fd); + pthread_detach(thread); } return EXIT_SUCCESS; } |