Unix socket as serial

[email protected] Fri, 15 Nov 2013 15:13:01 +0100
Newsgroups gmane.comp.emulators.bochs.devel
Message-ID <[email protected]>
Hi bochs developers,

I am using bochs 2.6.2 for my tests with Windowx XP DDK, and I had the  
need to redirect COM1 on a unix socket. The file attached here  
contains the patch. It has solved my particular problem, although it's  
a quite rude solution. I have just duplicated and slightly modified  
the part that interprets the configuration string "host:port". It now  
interprets "host" as an unix socket's path if the "port" field is left  
empty (i.e. no characters after ":"). I know that the code is horribly  
duplicated and that the configuration syntax is - uhm - not perfect. I  
hope it can be helpful anyway.

Bye

------------------------------------------------------------------------------
DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps
OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access
Free app hosting. Or install the open source package on any LAMP server.
Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native!
http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk

_______________________________________________
bochs-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bochs-developers
bochs-2_6_6-serial_on_unix_socket.diff (text/x-c, 3 KB)
diff -Naur bochs-2.6.2.orig/iodev/serial.cc bochs-2.6.2.new/iodev/serial.cc
--- bochs-2.6.2.orig/iodev/serial.cc	2013-02-23 16:15:59.000000000 +0100
+++ bochs-2.6.2.new/iodev/serial.cc	2013-11-15 12:55:08.160640123 +0100
@@ -33,6 +33,7 @@
 #ifndef WIN32
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <netinet/in.h>
 #include <netdb.h>
 #define closesocket(s)    close(s)
@@ -415,6 +416,7 @@
                  (mode == BX_SER_MODE_SOCKET_SERVER)) {
         BX_SER_THIS s[i].io_mode = mode;
         struct sockaddr_in  sin;
+        struct sockaddr_un  sun;
         struct hostent      *hp;
         char                host[BX_PATHNAME_LEN];
         int                 port;
@@ -438,9 +440,50 @@
         strcpy(host, dev);
         char *substr = strtok(host, ":");
         substr = strtok(NULL, ":");
-        if (!substr) {
-          BX_PANIC(("com%d: inet address is wrong (%s)", i+1, dev));
+
+if ((!substr) ||  (substr[0] == '\0')) {
+        //  BX_PANIC(("com%d: inet address is wrong (%s)", i+1, dev));
+        port = 0;
+
+        memset ((char*) &sun, 0, sizeof (sun));
+        strncpy (sun.sun_path, host, sizeof(sun.sun_path)-1);
+        sun.sun_path[sizeof(sun.sun_path)-1] = '\0';
+        sun.sun_family = AF_UNIX;
+
+        socket = ::socket (PF_UNIX, SOCK_STREAM, 0);
+        if (socket < 0)
+          BX_PANIC(("com%d: socket() failed",i+1));
+
+        // server mode
+        if (server) {
+          if (::bind (socket, (sockaddr *) &sun, sizeof (sun)) < 0 ||
+              ::listen (socket, SOMAXCONN) < 0) {
+            closesocket(socket);
+            socket = (SOCKET) -1;
+            BX_PANIC(("com%d: bind() or listen() failed (host:%s, port:%d)",i+1, host, port));
+          }
+          else {
+            BX_INFO(("com%d: waiting for client to connect (host:%s, port:%d)",i+1, host, port));
+            SOCKET client;
+            if ((client = ::accept (socket, NULL, 0)) < 0)
+              BX_PANIC(("com%d: accept() failed (host:%s, port:%d)",i+1, host, port));
+            closesocket(socket);
+            socket = client;
+          }
+        }
+        // client mode
+        else if (::connect (socket, (sockaddr *) &sun, sizeof (sun)) < 0) {
+          closesocket(socket);
+          socket = (SOCKET) -1;
+          BX_INFO(("com%d: connect() failed (host:%s, port:%d)",i+1, host, port));
         }
+
+        BX_SER_THIS s[i].socket_id = socket;
+        if (socket > 0)
+          BX_INFO(("com%d - inet %s - socket_id: %d, ip:%s, port:%d",
+            i+1, server ? "server" : "client", socket, host, port));
+}
+else {
         port = atoi(substr);
 
         hp = gethostbyname(host);
@@ -488,6 +531,7 @@
         if (socket > 0)
           BX_INFO(("com%d - inet %s - socket_id: %d, ip:%s, port:%d",
             i+1, server ? "server" : "client", socket, host, port));
+}
       } else if ((mode == BX_SER_MODE_PIPE_CLIENT) ||
                  (mode == BX_SER_MODE_PIPE_SERVER)) {
         if (strlen(dev) > 0) {