From 87d88effece676e33dc03af6b826f366f64da6e4 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 20 Sep 2011 03:32:13 -0700 Subject: [PATCH] I know have a pretty horrible NBD server that serves up zeros. Just need to improve it and modularize it. --- abuse.c | 37 +++++++++++++++++++++++++++++++++++++ abuse_example.c | 5 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/abuse.c b/abuse.c index 5ac3f24..7391847 100644 --- a/abuse.c +++ b/abuse.c @@ -2,8 +2,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -15,7 +17,13 @@ int abuse_main(int argc, char *argv[], struct abuse_operations *aop) { int sp[2]; int nbd, sk, err, tmp_fd; + u_int64_t from; + u_int32_t len; + int bytes_written, bytes_read; char *dev_file; + struct nbd_request request; + struct nbd_reply reply; + void *chunk; assert(argc == 2); dev_file = argv[1]; @@ -54,5 +62,34 @@ int abuse_main(int argc, char *argv[], struct abuse_operations *aop) close(sp[1]); sk = sp[0]; + reply.magic = htonl(NBD_REPLY_MAGIC); + reply.error = htonl(0); + + while (1) { + bytes_read = read(sk, &request, sizeof(request)); + assert(bytes_read == sizeof(request)); + memcpy(reply.handle, request.handle, sizeof(reply.handle)); + + /* FIXME: these might need conversion from the network byte order. */ + len = ntohl(request.len); + from = request.from; + assert(request.magic == htonl(NBD_REQUEST_MAGIC)); + + switch(ntohl(request.type)) { + case NBD_CMD_READ: + chunk = malloc(len + sizeof(struct nbd_reply)); + memset((char *)chunk + sizeof(struct nbd_reply), 0, len); + memcpy(chunk, &reply, sizeof(struct nbd_reply)); + bytes_written = write(sk, chunk, len + sizeof(struct nbd_reply)); + fprintf(stderr, "Wrote %d bytes.\n", bytes_written); + assert(bytes_written == len + sizeof(struct nbd_reply)); + free(chunk); + break; + /* We'll not worry about the other cases for now. */ + default: + break; + } + } + return 0; } diff --git a/abuse_example.c b/abuse_example.c index 42e27fc..eb6ce03 100644 --- a/abuse_example.c +++ b/abuse_example.c @@ -4,5 +4,8 @@ int main(int argc, char *argv[]) { - return abuse_main(argc, argv, NULL); + struct abuse_operations aop; + aop.size = 1024; + + return abuse_main(argc, argv, &aop); }