aboutsummaryrefslogtreecommitdiff
path: root/nbd.c
diff options
context:
space:
mode:
authorths <ths@c046a42c-6fe2-441c-8c8c-71466251a162>2008-07-03 10:23:51 +0000
committerths <ths@c046a42c-6fe2-441c-8c8c-71466251a162>2008-07-03 10:23:51 +0000
commitcd831bd7874916c4eb3f0f47218e4a65c46bb905 (patch)
treed8a831cdf1c7c31839afc131f6e0db661cd5cc87 /nbd.c
parentf8d39c01c376dc91f66936cfaf5dae3d44e9d268 (diff)
Merge NBD client/server, by Laurent Vivier.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@4834 c046a42c-6fe2-441c-8c8c-71466251a162
Diffstat (limited to 'nbd.c')
-rw-r--r--nbd.c74
1 files changed, 68 insertions, 6 deletions
diff --git a/nbd.c b/nbd.c
index c138d362ef..9f8fda9247 100644
--- a/nbd.c
+++ b/nbd.c
@@ -25,6 +25,7 @@
#include <ctype.h>
#include <inttypes.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
@@ -183,6 +184,65 @@ error:
return -1;
}
+int unix_socket_incoming(const char *path)
+{
+ int s;
+ struct sockaddr_un addr;
+ int serrno;
+
+ s = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (s == -1) {
+ return -1;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
+
+ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+ goto error;
+ }
+
+ if (listen(s, 128) == -1) {
+ goto error;
+ }
+
+ return s;
+error:
+ serrno = errno;
+ close(s);
+ errno = serrno;
+ return -1;
+}
+
+int unix_socket_outgoing(const char *path)
+{
+ int s;
+ struct sockaddr_un addr;
+ int serrno;
+
+ s = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (s == -1) {
+ return -1;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
+
+ if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+ goto error;
+ }
+
+ return s;
+error:
+ serrno = errno;
+ close(s);
+ errno = serrno;
+ return -1;
+}
+
+
/* Basic flow
Server Client
@@ -225,12 +285,10 @@ int nbd_negotiate(BlockDriverState *bs, int csock, off_t size)
return 0;
}
-int nbd_receive_negotiate(int fd, int csock)
+int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
{
char buf[8 + 8 + 8 + 128];
uint64_t magic;
- off_t size;
- size_t blocksize;
TRACE("Receiving negotation.");
@@ -241,8 +299,8 @@ int nbd_receive_negotiate(int fd, int csock)
}
magic = be64_to_cpup((uint64_t*)(buf + 8));
- size = be64_to_cpup((uint64_t*)(buf + 16));
- blocksize = 1024;
+ *size = be64_to_cpup((uint64_t*)(buf + 16));
+ *blocksize = 1024;
TRACE("Magic is %c%c%c%c%c%c%c%c",
isprint(buf[0]) ? buf[0] : '.',
@@ -254,7 +312,7 @@ int nbd_receive_negotiate(int fd, int csock)
isprint(buf[6]) ? buf[6] : '.',
isprint(buf[7]) ? buf[7] : '.');
TRACE("Magic is 0x%" PRIx64, magic);
- TRACE("Size is %" PRIu64, size);
+ TRACE("Size is %" PRIu64, *size);
if (memcmp(buf, "NBDMAGIC", 8) != 0) {
LOG("Invalid magic received");
@@ -269,7 +327,11 @@ int nbd_receive_negotiate(int fd, int csock)
errno = EINVAL;
return -1;
}
+ return 0;
+}
+int nbd_init(int fd, int csock, off_t size, size_t blocksize)
+{
TRACE("Setting block size to %lu", (unsigned long)blocksize);
if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {