linux-um archives
 help / color / mirror / Atom feed
From: Patrick Kilian <petschge@gmx.de>
To: BlaisorBlade <blaisorblade_spam@yahoo.it>,
	user-mode-linux-devel@lists.sourceforge.net
Subject: [uml-devel] Re: [uml-user] UNIX socket networking
Date: Tue, 2 Mar 2004 22:40:25 +0100	[thread overview]
Message-ID: <200403022240.25616.petschge@gmx.de> (raw)
In-Reply-To: <200403021909.47013.blaisorblade_spam@yahoo.it>

Hi all,

Am Dienstag, 2. März 2004 19:09 schrieb BlaisorBlade:
>> Well bind is returning -2 which seams to be -ENOENT. Does somebody
>> know why this happens?
> Hmm, I'm no expert with sockets; possibly the folder in which you
> create the socket does not exist. However, possibly code it like a
> normal program which just opens the socket and calls bind() and try
> it.
No the folder exists and is on a rw filesystem. This problem was the reason why I abandond the the driver the first time.

> However, posting the code would be *very* helpful ... do not be shy!
Well you asked for it. Here we go:

--- unix_sock_driver.patch

diff -Nur linux-2.6.0-test11-uml/.config linux-2.6.0-test11-uml2/.config
--- linux-2.6.0-test11-uml/.config	Fri Dec 12 17:52:41 2003
+++ linux-2.6.0-test11-uml2/.config	Wed Dec 31 19:21:29 2003
@@ -15,6 +15,7 @@
 CONFIG_BINFMT_ELF=y
 CONFIG_BINFMT_MISC=y
 CONFIG_HOSTFS=y
+CONFIG_HOSTSOCKET=y
 # CONFIG_HPPFS is not set
 CONFIG_MCONSOLE=y
 CONFIG_MAGIC_SYSRQ=y
Binary files linux-2.6.0-test11-uml/.tmp_vmlinux1 and linux-2.6.0-test11-uml2/.tmp_vmlinux1 differ
Binary files linux-2.6.0-test11-uml/.tmp_vmlinux2 and linux-2.6.0-test11-uml2/.tmp_vmlinux2 differ
diff -Nur linux-2.6.0-test11-uml/arch/um/Kconfig linux-2.6.0-test11-uml2/arch/um/Kconfig
--- linux-2.6.0-test11-uml/arch/um/Kconfig	Fri Dec 12 17:38:50 2003
+++ linux-2.6.0-test11-uml2/arch/um/Kconfig	Wed Dec 31 19:20:39 2003
@@ -99,6 +99,15 @@
         If you'd like to be able to work with files stored on the host, 
         say Y or M here; otherwise say N.

+config HOSTSOCKET
+	bool "Hostsocket Support"
+	default n
+	depends on EXPERIMENTAL
+	help
+	Support for tunneling of unix sockets. Currently very b0rken.
+
+	If unsure say N.
+
 config HPPFS
 	tristate "HoneyPot ProcFS"
 	help
diff -Nur linux-2.6.0-test11-uml/arch/um/drivers/Makefile linux-2.6.0-test11-uml2/arch/um/drivers/Makefile
--- linux-2.6.0-test11-uml/arch/um/drivers/Makefile	Fri Dec 12 17:30:43 2003
+++ linux-2.6.0-test11-uml2/arch/um/drivers/Makefile	Tue Mar  2 21:12:59 2004
@@ -1,4 +1,4 @@
-#
+#
 # Copyright (C) 2000, 2002, 2003 Jeff Dike (jdike@karaya.com)
 # Licensed under the GPL
 #
@@ -16,6 +16,7 @@
 net-objs := net_kern.o net_user.o
 mconsole-objs := mconsole_kern.o mconsole_user.o
 hostaudio-objs := hostaudio_kern.o hostaudio_user.o
+hostsocket-objs := hostsocket_user.o hostsocket_kern.o
 ubd-objs := ubd_kern.o ubd_user.o
 port-objs := port_kern.o port_user.o
 harddog-objs := harddog_kern.o harddog_user.o
@@ -30,8 +31,9 @@
 obj-$(CONFIG_UML_NET) += net.o
 obj-$(CONFIG_MCONSOLE) += mconsole.o
 obj-$(CONFIG_MMAPPER) += mmapper_kern.o
 obj-$(CONFIG_HOSTAUDIO) += hostaudio.o
+obj-$(CONFIG_HOSTSOCKET) += hostsocket.o
 obj-$(CONFIG_FD_CHAN) += fd.o 
 obj-$(CONFIG_NULL_CHAN) += null.o
 obj-$(CONFIG_PORT_CHAN) += port.o
diff -Nur linux-2.6.0-test11-uml/arch/um/drivers/hostsocket.h linux-2.6.0-test11-uml2/arch/um/drivers/hostsocket.h
--- linux-2.6.0-test11-uml/arch/um/drivers/hostsocket.h	Thu Jan  1 01:00:00 1970
+++ linux-2.6.0-test11-uml2/arch/um/drivers/hostsocket.h	Tue Mar  2 22:22:19 2004
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2004 Patrick Kilian <petschge@web.de>
+ * Licensed under the GPL
+ */
+
+// Socket we are talking to at the host side
+#define HOSTSOCKET "/tmp/hostsocket"
+
+// Socket we are talking from at the host side
+#define SENDSOCKET "/tmp/sendsocket"
+
+// Socket we show inside the guest
+// #define GUESTSOCKET "/tmp/guestsocket"
+// #define GUESTSOCKET "\0guestsocket"
+#define GUESTSOCKET "@guestsocket"
+// #define GUESTSOCKET "guestsocket"
+
+#define H_MESSAGE "This is the uml saying hello"
+#define B_MESSAGE "This is the uml saying byebye"
+
+#define DEBUG_HS
+
+/* host side socket file operations */
+int init_socket_user(void);
+void exit_socket_user(void);
+
+/* package passing functions */
+
+/* package is a wrapper araound a unix socket data packet */
+typedef struct {
+    int len;
+	unsigned char data[4096];
+} package;
+
+// All have to be non-blocking
+// package must be some wrapper araound a unix socket data packet
+package read_from_host(void);
+void write_to_host(package p);
+package read_from_uml(void);
+void write_to_host(package p);
+
+/*
+ * Overrides for Emacs so that we follow Linus's tabbing style.
+ * Emacs will notice this stuff at the end of the file and automatically
+ * adjust the settings for this buffer only.  This must remain at the end
+ * of the file.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-file-style: "linux"
+ * End:
+ *
+ */
diff -Nur linux-2.6.0-test11-uml/arch/um/drivers/hostsocket_kern.c linux-2.6.0-test11-uml2/arch/um/drivers/hostsocket_kern.c
--- linux-2.6.0-test11-uml/arch/um/drivers/hostsocket_kern.c	Thu Jan  1 01:00:00 1970
+++ linux-2.6.0-test11-uml2/arch/um/drivers/hostsocket_kern.c	Tue Mar  2 22:22:23 2004
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2004 Patrick Kilian <petschge@web.de>
+ * Licensed under the GPL
+ */
+
+#include "hostsocket.h"
+
+#include "linux/delay.h"
+#include "linux/init.h"
+#include "linux/module.h"
+#include "linux/kernel.h"
+#include "linux/sched.h"
+#include "linux/signal.h"
+#include "linux/types.h"
+#include "linux/uio.h"
+#include "linux/sched.h"
+#include "linux/net.h"
+#include "net/sock.h"
+#include "linux/un.h"
+#include "kern.h"
+
+int error;
+
+struct socket *guestsock;
+
+pid_t socketpid;
+int st_kill = 0;
+
+
+package read_from_uml(void) {
+	package out;
+	out.len = 0;
+	return(out);
+	/*
+	mm_segment_t oldfs;
+	struct msghdr msg;
+	struct iovec iov;
+	static char Buffer[4096];
+
+	msg.msg_name     = NULL;	// Socket name
+	msg.msg_namelen  = 0;		// Length of name
+	msg.msg_iov	 = &iov;		// Data blocks
+	msg.msg_iovlen   = 1;		// Number of blocks
+	msg.msg_control  = NULL;	// Per protocol magic
+	msg.msg_controllen = 0;		// Length of cmsg list
+	msg.msg_flags    = 0;		// 0 vs MSG_DONTWAIT
+
+	msg.msg_iov->iov_base = &Buffer[0];
+	msg.msg_iov->iov_len  = (__kernel_size_t)1024;
+
+	oldfs = get_fs();
+	set_fs(KERNEL_DS);
+	len = sock_recvmsg(guestsock, &msg, 4096, 0);
+	// len = sock_recvmsg(guestsock, &msg, 4096, MSG_DONTWAIT);
+	set_fs(oldfs);
+	*/
+}
+
+void write_to_uml(package p) {
+	printk(KERN_INFO "packet.length= %d\n", p.len);
+}
+
+// Create the socket in the uml
+static int setup_guestsocket(void) {
+	struct sockaddr_un sun;
+
+    error = sock_create(PF_UNIX, SOCK_DGRAM, 0, &guestsock);
+    if (error < 0) {
+		return(error);
+    }
+
+    sun.sun_family = AF_UNIX;
+    strncpy (sun.sun_path, GUESTSOCKET, sizeof (GUESTSOCKET));
+    strncpy (sun.sun_path, "\0", 1);
+    error = guestsock->ops->bind(guestsock, (struct sockaddr *)&sun, sizeof(sun));
+    if (error<0) {
+        printk(KERN_INFO "socket thread: can't bind guest socket, error=%d\n", error);
+        printk(KERN_INFO "ENOENT: %d\n", ENOENT);
+    }
+	return(error);
+
+}
+
+static int socketfunc(void *data) {
+	package p;
+
+	// Set thread name
+	sprintf(current->comm, "socket thread");
+
+	/* Startup of the uml side */
+
+	// Create the socket in the uml
+	error = setup_guestsocket();
+	if (error < 0) {
+		printk(KERN_INFO "Socket thread: can't create guest socket, error=%d\n", error);
+	}
+
+	printk(KERN_INFO "Socket thread entering recv loop\n");
+	while (1) {
+	    if (st_kill != 0) {
+			break;
+		}
+
+		p = read_from_uml();
+		if (p.len < 0) {
+			printk(KERN_INFO "Socket thread recv failed, p.len=%d\n", p.len);
+		} else {
+			printk(KERN_INFO "Socket thread got: p.len=%d\n", p.len);
+			write_to_host(p);
+		}
+
+		p = read_from_host();
+		if (p.len < 0) {
+            printk(KERN_INFO "Socket thread read_from_host failed, p.len=%d\n", p.len);
+        } else {
+            printk(KERN_INFO "Socket thread read_from_host: p.len=%d\n", p.len);
+            write_to_uml(p);
+        }
+
+		printk(":");
+		current->state = TASK_UNINTERRUPTIBLE;
+		schedule_timeout(1*HZ);
+	}
+
+	return(0);
+}
+
+static int init_socket(void) {
+
+	printk(KERN_INFO "Registering Socket Relay...\n");
+	/* Startup of the host side */
+	init_socket_user();
+
+	/* Start the kernelthread */
+	socketpid = kernel_thread(socketfunc, NULL, 0);
+	if (socketpid < 0) {
+		printk(KERN_INFO "Can't start socket thread, socketpid=%d\n", socketpid);
+	}
+
+	printk(KERN_INFO "Finished Socket Relay\n");
+	return(0);
+}
+
+static void exit_socket(void) {
+	//XXX: Kill order
+	//kill_proc(socketpid, SIGKILL, 1);
+	exit_socket_user();
+	st_kill=1;
+}
+
+late_initcall(init_socket);
+uml_exitcall(exit_socket);
+
+
+/*
+ * Overrides for Emacs so that we follow Linus's tabbing style.
+ * Emacs will notice this stuff at the end of the file and automatically
+ * adjust the settings for this buffer only.  This must remain at the end
+ * of the file.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-file-style: "linux"
+ * End:
+ */
diff -Nur linux-2.6.0-test11-uml/arch/um/drivers/hostsocket_user.c linux-2.6.0-test11-uml2/arch/um/drivers/hostsocket_user.c
--- linux-2.6.0-test11-uml/arch/um/drivers/hostsocket_user.c	Thu Jan  1 01:00:00 1970
+++ linux-2.6.0-test11-uml2/arch/um/drivers/hostsocket_user.c	Tue Mar  2 22:22:21 2004
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2004 Patrick Kilian <petschge@web.de>
+ * Licensed under the GPL
+ */
+#include "hostsocket.h"
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+struct sockaddr_un name;
+size_t size;
+int nbytes;
+int sendsock;
+
+/* host side operations */
+package read_from_host(void) {
+	// Do not block
+	package out;
+	out.len=0;
+	return(out);
+}
+EXPORT_SYMBOL(read_from_host);
+
+void write_to_host(package p) {
+	// Do not block
+	// printf("p.len=%d, p=%s\n", p.len, p.data);
+    nbytes = sendto (sendsock, p.data, p.len, 0, (struct sockaddr *) &name, size);
+    if (nbytes < 0) {
+        perror("Can't sendto hello to host socket\n");
+    }
+}
+EXPORT_SYMBOL(write_to_host);
+
+int init_socket_user(void) {
+	package hello;
+	printf("Registering Socket Relay on the Host Side...\n");
+	
+	/* Make the sendsocket. */
+	// Remove old sendsocket
+	unlink(SENDSOCKET);
+	// Create socket
+	sendsock = socket (PF_LOCAL, SOCK_DGRAM, 0);
+	if (sendsock < 0) {
+		perror("Can't create sending host socket\n");
+		exit (-1);
+	}
+	// Bind a name to the socket.
+	name.sun_family = AF_LOCAL;
+	strncpy (name.sun_path, SENDSOCKET, sizeof (name.sun_path));
+	size = SUN_LEN (&name);
+	if (bind (sendsock, (struct sockaddr *) &name, size) < 0) {
+		perror("Can't bind sending host socket\n");
+		exit (-1);
+	}
+
+	/* Initialize the server socket address. */
+	name.sun_family = AF_LOCAL;
+	strcpy (name.sun_path, HOSTSOCKET);
+	size = strlen (name.sun_path) + sizeof (name.sun_family);
+
+	/* Send a hello. */
+	hello.len = strlen (H_MESSAGE) + 1;
+	strcpy (hello.data, H_MESSAGE);
+	write_to_host(hello);
+
+	printf("Finished host side setup\n");
+	return(0);
+}
+
+void exit_socket_user(void) {
+	package bye;
+
+	/* Send a goodbye */
+    bye.len = strlen (B_MESSAGE) + 1;
+    strcpy (bye.data, B_MESSAGE);
+    write_to_host(bye);
+    
+	/* Remove sendsocket */
+	// XXX: Remove order
+	unlink(SENDSOCKET);
+	close(sendsock);
+}
+
+/*
+ * Overrides for Emacs so that we follow Linus's tabbing style.
+ * Emacs will notice this stuff at the end of the file and automatically
+ * adjust the settings for this buffer only.  This must remain at the end
+ * of the file.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-file-style: "linux"
+ * End:
+ */
diff -Nur linux-2.6.0-test11-uml/include/config/hostsocket.h linux-2.6.0-test11-uml2/include/config/hostsocket.h
--- linux-2.6.0-test11-uml/include/config/hostsocket.h	Thu Jan  1 01:00:00 1970
+++ linux-2.6.0-test11-uml2/include/config/hostsocket.h	Wed Dec 31 19:22:37 2003
@@ -0,0 +1 @@
+#define CONFIG_HOSTSOCKET 1
diff -Nur linux-2.6.0-test11-uml/include/linux/autoconf.h linux-2.6.0-test11-uml2/include/linux/autoconf.h
--- linux-2.6.0-test11-uml/include/linux/autoconf.h	Fri Dec 12 17:52:41 2003
+++ linux-2.6.0-test11-uml2/include/linux/autoconf.h	Wed Dec 31 19:21:29 2003
@@ -16,6 +16,7 @@
 #define CONFIG_BINFMT_ELF 1
 #define CONFIG_BINFMT_MISC 1
 #define CONFIG_HOSTFS 1
+#define CONFIG_HOSTSOCKET 1
 #undef CONFIG_HPPFS
 #define CONFIG_MCONSOLE 1
 #define CONFIG_MAGIC_SYSRQ 1
diff -Nur linux-2.6.0-test11-uml/net/unix/af_unix.c linux-2.6.0-test11-uml2/net/unix/af_unix.c
--- linux-2.6.0-test11-uml/net/unix/af_unix.c	Mon Nov 24 02:32:13 2003
+++ linux-2.6.0-test11-uml2/net/unix/af_unix.c	Mon Mar  1 20:28:05 2004
@@ -686,7 +686,9 @@
 		 * Get the parent directory, calculate the hash for last
 		 * component.
 		 */
+		printk("sunaddr->sun_path: %s\n", sunaddr->sun_path);
 		err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
+		printk("err: %d\n", err);
 		if (err)
 			goto out_mknod_parent;
 		/*

-- unix_sock_driver.patch

It also need's a listener on the host-side on /tmp/hostsocket. Here is some sample source code.

--- sserver.c
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SERVER  "/tmp/hostsocket"
#define MAXMSG  512


int make_named_socket (const char *filename) {
    struct sockaddr_un name;
    int sock;
    size_t size;

    /* Create the socket. */
    sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
    if (sock < 0) {
        perror ("socket");
        exit (EXIT_FAILURE);
    }

    /* Bind a name to the socket. */
    name.sun_family = AF_LOCAL;
    strncpy (name.sun_path, filename, sizeof (name.sun_path));
    size = SUN_LEN (&name);

    if (bind (sock, (struct sockaddr *) &name, size) < 0) {
        perror ("bind");
        exit (EXIT_FAILURE);
    }

    return sock;
}

int main (void) {
    int sock;
    char message[MAXMSG];
    struct sockaddr_un name;
    size_t size;
    int nbytes;

    /* Remove the filename first, it's ok if the call fails */
    unlink (SERVER);

    /* Make the socket, then loop endlessly. */
    sock = make_named_socket (SERVER);
    while (1) {
        /* Wait for a datagram. */
        size = sizeof (name);
        nbytes = recvfrom (sock, message, MAXMSG, 0, (struct sockaddr *) & name, &size);
        if (nbytes < 0) {
            perror ("recfrom (server)");
            exit (EXIT_FAILURE);
        }

        /* Give a diagnostic message. */
        fprintf (stderr, "Server: got message: %s\n", message);
    }
}
--- sserver.c

Most of the above code should probably be edited with rm, but you
wanted to have my results so far. Now go and find a brown paper bag.


mfg,
Patrick "Petschge" Kilian

-- 
Spam ist weniger ein technisches als ein soziales Problem. Die einzigen
technischen Mittel, die soziale Probleme halbwegs brauchbar loesen,
sind schwere Waffen. Und das auch nur bei wirklich konsequenter
Anwendung.	-- Alexander Schreiber in dasr



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id\x1356&alloc_id438&opÌk
_______________________________________________
User-mode-linux-devel mailing list
User-mode-linux-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

  reply	other threads:[~2004-03-02 21:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <200402290158.54770.jdizzl@xs4all.nl>
     [not found] ` <20040229120513.934193BCB7@home.petschge.de>
     [not found]   ` <200402291720.12216.blaisorblade_spam@yahoo.it>
2004-02-29 17:13     ` [uml-devel] Re: [uml-user] UNIX socket networking Patrick Kilian
2004-03-01 20:14       ` Patrick "Petschge" Kilian
2004-03-02 18:09         ` BlaisorBlade
2004-03-02 21:40           ` Patrick Kilian [this message]
2004-03-07 10:54             ` Patrick "Petschge" Kilian
2004-03-08 21:33               ` Jeff Dike
2004-03-07 11:28             ` BlaisorBlade
2004-03-07 11:56               ` Patrick Kilian
2004-03-07 12:12                 ` BlaisorBlade
2004-03-07 13:06                   ` [uml-devel] " Patrick Kilian

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=200403022240.25616.petschge@gmx.de \
    --to=petschge@gmx.de \
    --cc=blaisorblade_spam@yahoo.it \
    --cc=petschge@web.de \
    --cc=user-mode-linux-devel@lists.sourceforge.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox