From: Laurent Vivier <Laurent.Vivier@bull.net>
To: "Ciprian Dorin, Craciun" <ciprian.craciun@gmail.com>
Cc: nbd-general@lists.sourceforge.net, linux-newbie@vger.kernel.org,
qemu-devel@nongnu.org
Subject: Re: [Nbd] Transforming stdin and stdout pair into a socket
Date: Mon, 11 May 2009 10:03:43 +0200 [thread overview]
Message-ID: <1242029023.4638.3.camel@frecb07144> (raw)
In-Reply-To: <8e04b5820905101119u39ae84bfp1bae24be6e057086@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2270 bytes --]
Le dimanche 10 mai 2009 à 21:19 +0300, Ciprian Dorin, Craciun a écrit :
> Hello all!
Hi,
perhaps the attached patch I wrote last year (november) is what you
want...
I didn't try to apply it to an up-to-date qemu-nbd.
Regards,
Laurent
> Today I've played around with NBD (Network Block Disk), and
> qemu-nbd (a NBD client that exports QEMU disks as NBD's).
>
> My problem is the following: both NBD kernel module and qemu-nbd
> implementation expect to use a socket in order to communicate.
> This means that in order to securely tunnel the connection over
> SSH (OpenSSH), I need an intermediary process that creates a socket
> and forwards all input / output between this socket and stdin / stdout
> (which are in fact pipes received from OpenSSH).
>
> My question is: can I somehow make the pair of stdin / stdout seem
> as a socket to the Linux syscalls (read and write)? (I would have to
> make stdin / stdout pair look like a single file descriptor.) (This
> would eliminate the intermediate process that just pipes data, and
> thus reduce the overhead.)
>
> Just to be clear: I know how to trick an application to have it's
> stdin and stdout be an opened socket (by using dup syscall). But in
> this case I need to trick the Linux kernel into thinking that stdin /
> stdout pair is a socket (or a single file descriptor).
>
> Thank you,
> Ciprian Craciun.
>
> ------------------------------------------------------------------------------
> The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
> production scanning environment may not be a perfect world - but thanks to
> Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
> Series Scanner you'll get full speed at 300 dpi even with all image
> processing features enabled. http://p.sf.net/sfu/kodak-com
> _______________________________________________
> Nbd-general mailing list
> Nbd-general@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nbd-general
>
--
------------------ Laurent.Vivier@bull.net ------------------
"Tout ce qui est impossible reste à accomplir" Jules Verne
"Things are only impossible until they're not" Jean-Luc Picard
[-- Attachment #2: qemu-nbd-inetd.patch --]
[-- Type: text/x-vhdl, Size: 3645 bytes --]
---
qemu-nbd.c | 41 +++++++++++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 6 deletions(-)
Index: qemu/qemu-nbd.c
===================================================================
--- qemu.orig/qemu-nbd.c 2008-09-11 17:06:05.000000000 +0200
+++ qemu/qemu-nbd.c 2008-09-15 16:10:37.000000000 +0200
@@ -57,6 +57,7 @@ static void usage(const char *name)
" -d, --disconnect disconnect the specified device\n"
" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
" -t, --persistent don't exit on the last connection\n"
+" -i, --inetd inetd interface: use stdin/stdout instead of a socke\n"
" -v, --verbose display extra debugging information\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
@@ -183,14 +184,14 @@ int main(int argc, char **argv)
bool readonly = false;
bool disconnect = false;
const char *bindto = "0.0.0.0";
- int port = 1024;
+ int port = 0;
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
off_t fd_size;
char *device = NULL;
char *socket = NULL;
char sockpath[128];
- const char *sopt = "hVbo:p:rsnP:c:dvk:e:t";
+ const char *sopt = "hVbo:p:rsnP:c:dvk:e:ti";
struct option lopt[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
@@ -207,6 +208,7 @@ int main(int argc, char **argv)
{ "shared", 1, 0, 'e' },
{ "persistent", 0, 0, 't' },
{ "verbose", 0, 0, 'v' },
+ { "inetd", 0, 0, 'i' },
{ NULL, 0, 0, 0 }
};
int ch;
@@ -225,6 +227,7 @@ int main(int argc, char **argv)
int nb_fds = 0;
int max_fd;
int persistent = 0;
+ int inetd = 0;
while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
switch (ch) {
@@ -289,6 +292,9 @@ int main(int argc, char **argv)
case 't':
persistent = 1;
break;
+ case 'i':
+ inetd = 1;
+ break;
case 'v':
verbose = 1;
break;
@@ -326,6 +332,18 @@ int main(int argc, char **argv)
return 0;
}
+ if (inetd) {
+ if (shared != 1)
+ errx(EINVAL, "You cannot use inetd and shared");
+ if (socket)
+ errx(EINVAL, "You cannot use inetd and socket");
+ if (port)
+ errx(EINVAL, "You cannot use inetd and port");
+ } else {
+ if (!socket)
+ port = 1024;
+ }
+
bdrv_init();
bs = bdrv_new("hda");
@@ -412,9 +430,24 @@ int main(int argc, char **argv)
if (sharing_fds == NULL)
errx(ENOMEM, "Cannot allocate sharing fds");
+ data = qemu_memalign(512, NBD_BUFFER_SIZE);
+ if (data == NULL)
+ errx(ENOMEM, "Cannot allocate data buffer");
+
if (socket) {
sharing_fds[0] = unix_socket_incoming(socket);
} else {
+ if (inetd) {
+ /* read and write on stdin/stdout */
+ ret = nbd_negotiate(STDIN_FILENO, fd_size);
+ while (ret != -1) {
+ ret = nbd_trip(bs, STDIN_FILENO, fd_size, dev_offset,
+ &offset, readonly, data, NBD_BUFFER_SIZE);
+ }
+ qemu_free(data);
+ bdrv_close(bs);
+ return 0;
+ }
sharing_fds[0] = tcp_socket_incoming(bindto, port);
}
@@ -423,10 +456,6 @@ int main(int argc, char **argv)
max_fd = sharing_fds[0];
nb_fds++;
- data = qemu_memalign(512, NBD_BUFFER_SIZE);
- if (data == NULL)
- errx(ENOMEM, "Cannot allocate data buffer");
-
do {
FD_ZERO(&fds);
WARNING: multiple messages have this Message-ID (diff)
From: Laurent Vivier <Laurent.Vivier@bull.net>
To: "Ciprian Dorin, Craciun" <ciprian.craciun@gmail.com>
Cc: nbd-general@lists.sourceforge.net, linux-newbie@vger.kernel.org,
qemu-devel@nongnu.org
Subject: [Qemu-devel] Re: [Nbd] Transforming stdin and stdout pair into a socket
Date: Mon, 11 May 2009 10:03:43 +0200 [thread overview]
Message-ID: <1242029023.4638.3.camel@frecb07144> (raw)
In-Reply-To: <8e04b5820905101119u39ae84bfp1bae24be6e057086@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2270 bytes --]
Le dimanche 10 mai 2009 à 21:19 +0300, Ciprian Dorin, Craciun a écrit :
> Hello all!
Hi,
perhaps the attached patch I wrote last year (november) is what you
want...
I didn't try to apply it to an up-to-date qemu-nbd.
Regards,
Laurent
> Today I've played around with NBD (Network Block Disk), and
> qemu-nbd (a NBD client that exports QEMU disks as NBD's).
>
> My problem is the following: both NBD kernel module and qemu-nbd
> implementation expect to use a socket in order to communicate.
> This means that in order to securely tunnel the connection over
> SSH (OpenSSH), I need an intermediary process that creates a socket
> and forwards all input / output between this socket and stdin / stdout
> (which are in fact pipes received from OpenSSH).
>
> My question is: can I somehow make the pair of stdin / stdout seem
> as a socket to the Linux syscalls (read and write)? (I would have to
> make stdin / stdout pair look like a single file descriptor.) (This
> would eliminate the intermediate process that just pipes data, and
> thus reduce the overhead.)
>
> Just to be clear: I know how to trick an application to have it's
> stdin and stdout be an opened socket (by using dup syscall). But in
> this case I need to trick the Linux kernel into thinking that stdin /
> stdout pair is a socket (or a single file descriptor).
>
> Thank you,
> Ciprian Craciun.
>
> ------------------------------------------------------------------------------
> The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
> production scanning environment may not be a perfect world - but thanks to
> Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
> Series Scanner you'll get full speed at 300 dpi even with all image
> processing features enabled. http://p.sf.net/sfu/kodak-com
> _______________________________________________
> Nbd-general mailing list
> Nbd-general@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nbd-general
>
--
------------------ Laurent.Vivier@bull.net ------------------
"Tout ce qui est impossible reste à accomplir" Jules Verne
"Things are only impossible until they're not" Jean-Luc Picard
[-- Attachment #2: qemu-nbd-inetd.patch --]
[-- Type: text/x-vhdl, Size: 3645 bytes --]
---
qemu-nbd.c | 41 +++++++++++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 6 deletions(-)
Index: qemu/qemu-nbd.c
===================================================================
--- qemu.orig/qemu-nbd.c 2008-09-11 17:06:05.000000000 +0200
+++ qemu/qemu-nbd.c 2008-09-15 16:10:37.000000000 +0200
@@ -57,6 +57,7 @@ static void usage(const char *name)
" -d, --disconnect disconnect the specified device\n"
" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
" -t, --persistent don't exit on the last connection\n"
+" -i, --inetd inetd interface: use stdin/stdout instead of a socke\n"
" -v, --verbose display extra debugging information\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
@@ -183,14 +184,14 @@ int main(int argc, char **argv)
bool readonly = false;
bool disconnect = false;
const char *bindto = "0.0.0.0";
- int port = 1024;
+ int port = 0;
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
off_t fd_size;
char *device = NULL;
char *socket = NULL;
char sockpath[128];
- const char *sopt = "hVbo:p:rsnP:c:dvk:e:t";
+ const char *sopt = "hVbo:p:rsnP:c:dvk:e:ti";
struct option lopt[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
@@ -207,6 +208,7 @@ int main(int argc, char **argv)
{ "shared", 1, 0, 'e' },
{ "persistent", 0, 0, 't' },
{ "verbose", 0, 0, 'v' },
+ { "inetd", 0, 0, 'i' },
{ NULL, 0, 0, 0 }
};
int ch;
@@ -225,6 +227,7 @@ int main(int argc, char **argv)
int nb_fds = 0;
int max_fd;
int persistent = 0;
+ int inetd = 0;
while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
switch (ch) {
@@ -289,6 +292,9 @@ int main(int argc, char **argv)
case 't':
persistent = 1;
break;
+ case 'i':
+ inetd = 1;
+ break;
case 'v':
verbose = 1;
break;
@@ -326,6 +332,18 @@ int main(int argc, char **argv)
return 0;
}
+ if (inetd) {
+ if (shared != 1)
+ errx(EINVAL, "You cannot use inetd and shared");
+ if (socket)
+ errx(EINVAL, "You cannot use inetd and socket");
+ if (port)
+ errx(EINVAL, "You cannot use inetd and port");
+ } else {
+ if (!socket)
+ port = 1024;
+ }
+
bdrv_init();
bs = bdrv_new("hda");
@@ -412,9 +430,24 @@ int main(int argc, char **argv)
if (sharing_fds == NULL)
errx(ENOMEM, "Cannot allocate sharing fds");
+ data = qemu_memalign(512, NBD_BUFFER_SIZE);
+ if (data == NULL)
+ errx(ENOMEM, "Cannot allocate data buffer");
+
if (socket) {
sharing_fds[0] = unix_socket_incoming(socket);
} else {
+ if (inetd) {
+ /* read and write on stdin/stdout */
+ ret = nbd_negotiate(STDIN_FILENO, fd_size);
+ while (ret != -1) {
+ ret = nbd_trip(bs, STDIN_FILENO, fd_size, dev_offset,
+ &offset, readonly, data, NBD_BUFFER_SIZE);
+ }
+ qemu_free(data);
+ bdrv_close(bs);
+ return 0;
+ }
sharing_fds[0] = tcp_socket_incoming(bindto, port);
}
@@ -423,10 +456,6 @@ int main(int argc, char **argv)
max_fd = sharing_fds[0];
nb_fds++;
- data = qemu_memalign(512, NBD_BUFFER_SIZE);
- if (data == NULL)
- errx(ENOMEM, "Cannot allocate data buffer");
-
do {
FD_ZERO(&fds);
next prev parent reply other threads:[~2009-05-11 8:03 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-10 18:19 Transforming stdin and stdout pair into a socket Ciprian Dorin, Craciun
2009-05-10 18:19 ` [Qemu-devel] " Ciprian Dorin, Craciun
2009-05-11 8:03 ` Laurent Vivier [this message]
2009-05-11 8:03 ` [Qemu-devel] Re: [Nbd] " Laurent Vivier
2009-05-11 13:28 ` Ciprian Dorin, Craciun
2009-05-11 13:28 ` [Qemu-devel] Re: [Nbd] " Ciprian Dorin, Craciun
2009-05-11 12:02 ` [Qemu-devel] " Anthony Liguori
2009-05-11 12:02 ` Anthony Liguori
2009-05-11 13:32 ` Ciprian Dorin, Craciun
2009-05-11 13:32 ` Ciprian Dorin, Craciun
[not found] ` <8e04b5820905110632q1a3b9ad4u49d4f3208d994755-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2009-05-11 19:31 ` Wouter Verhelst
2009-05-11 19:31 ` [Nbd] " Wouter Verhelst
2009-05-11 23:07 ` Paul Brook
2009-05-11 23:07 ` Paul Brook
2009-05-12 2:24 ` Anthony Liguori
2009-05-12 2:24 ` Anthony Liguori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1242029023.4638.3.camel@frecb07144 \
--to=laurent.vivier@bull.net \
--cc=ciprian.craciun@gmail.com \
--cc=linux-newbie@vger.kernel.org \
--cc=nbd-general@lists.sourceforge.net \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.