r4214 - in developers: . zecke zecke/serial_forward

zecke at sita.openmoko.org zecke at sita.openmoko.org
Mon Mar 17 16:11:54 CET 2008


Author: zecke
Date: 2008-03-17 16:11:45 +0100 (Mon, 17 Mar 2008)
New Revision: 4214

Added:
   developers/zecke/
   developers/zecke/serial_forward/
   developers/zecke/serial_forward/GNUmakefile
   developers/zecke/serial_forward/forward.c
   developers/zecke/serial_forward/forward.h
   developers/zecke/serial_forward/pty_forward.c
Log:
Add my serial_forward tool. This forwards the modem serial via tcp/ip and offers a pty on the other side


Added: developers/zecke/serial_forward/GNUmakefile
===================================================================
--- developers/zecke/serial_forward/GNUmakefile	2008-03-15 21:37:01 UTC (rev 4213)
+++ developers/zecke/serial_forward/GNUmakefile	2008-03-17 15:11:45 UTC (rev 4214)
@@ -0,0 +1,7 @@
+all: forward pty_forward
+
+forward: forward.c
+	$(CC) -o forward forward.c
+
+pty_forward: pty_forward.c
+	$(CC) -o pty_forward -lutil pty_forward.c

Added: developers/zecke/serial_forward/forward.c
===================================================================
--- developers/zecke/serial_forward/forward.c	2008-03-15 21:37:01 UTC (rev 4213)
+++ developers/zecke/serial_forward/forward.c	2008-03-17 15:11:45 UTC (rev 4214)
@@ -0,0 +1,117 @@
+/*
+ * Forward a serial over TCP/IP
+ *
+ * Copyright (C) 2008 Openmoko Inc.
+ * 
+ * Author: Holger Hans Peter Freyther <zecke at openmoko.org>
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <netinet/in.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <termios.h>
+#include <strings.h>
+
+#include "forward.h"
+
+
+static const char* DEFAULT_DEVICE = "/dev/ttySAC0";
+static const int DEFAULT_PORT = 5621;
+
+/*
+ * Read from modem_fd   => write to remote_fd
+ * Read from remote_fd  => write to modem_fd
+ */
+
+static void set_termios(int modem_fd)
+{
+    struct termios t; 
+    bzero(&t, sizeof(t));
+
+    t.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
+    t.c_iflag = IGNPAR;
+    speed_t speed = B115200;
+    cfsetispeed(&t, speed);
+    cfsetospeed(&t, speed);
+
+    t.c_oflag = 0;
+    t.c_lflag = ICANON;
+    t.c_cc[VINTR]    = 0;     /* Ctrl-c */ 
+    t.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
+    t.c_cc[VERASE]   = 0;     /* del */
+    t.c_cc[VKILL]    = 0;     /* @ */
+    t.c_cc[VEOF]     = 4;     /* Ctrl-d */
+    t.c_cc[VTIME]    = 0;     /* inter-character timer unused */
+    t.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
+    t.c_cc[VSWTC]    = 0;     /* '\0' */
+    t.c_cc[VSTART]   = 0;     /* Ctrl-q */ 
+    t.c_cc[VSTOP]    = 0;     /* Ctrl-s */
+    t.c_cc[VSUSP]    = 0;     /* Ctrl-z */
+    t.c_cc[VEOL]     = 0;     /* '\0' */
+    t.c_cc[VREPRINT] = 0;     /* Ctrl-r */
+    t.c_cc[VDISCARD] = 0;     /* Ctrl-u */
+    t.c_cc[VWERASE]  = 0;     /* Ctrl-w */
+    t.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
+    t.c_cc[VEOL2]    = 0;     /* '\0' */
+
+    tcflush(modem_fd, TCIFLUSH);
+    if(tcsetattr(modem_fd, TCSANOW, &t) < 0)
+        perror("tcsetattr()");
+
+    int status = TIOCM_DTR | TIOCM_RTS;
+    ioctl(modem_fd, TIOCMBIS, &status);
+}
+
+int main(int argc, char** argv)
+{
+    int modem_fd = open(DEFAULT_DEVICE, O_RDWR | O_NOCTTY);
+    if (modem_fd < 0) {
+        perror("Failed to open modem");
+        return EXIT_FAILURE;
+    }
+
+    set_termios(modem_fd);
+
+
+
+    int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
+    if (socket_fd < 0) {
+        perror("Failed to create network socket");
+        return EXIT_FAILURE;
+    }
+
+
+    struct sockaddr_in addr = { 0, };
+    addr.sin_family = PF_INET;
+    addr.sin_port = htons(DEFAULT_PORT);
+    addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+    if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
+        perror("Error binding");
+        return EXIT_FAILURE;
+    }
+
+
+    if (listen(socket_fd, 1) == -1) {
+        perror("Error listening");
+        return EXIT_FAILURE;
+    }
+
+    int connection_fd = -1;
+    socklen_t length = sizeof(addr);
+    while ((connection_fd = accept(socket_fd, (struct sockaddr*)&addr, &length)) >= 0) {
+        printf("New connection from: '%s'\n", inet_ntoa(addr.sin_addr));
+
+        forward_data(modem_fd, connection_fd);
+        close(connection_fd);
+    }
+}


Property changes on: developers/zecke/serial_forward/forward.c
___________________________________________________________________
Name: svn:eol-style
   + native

Added: developers/zecke/serial_forward/forward.h
===================================================================
--- developers/zecke/serial_forward/forward.h	2008-03-15 21:37:01 UTC (rev 4213)
+++ developers/zecke/serial_forward/forward.h	2008-03-17 15:11:45 UTC (rev 4214)
@@ -0,0 +1,38 @@
+
+static void forward_data(int source_fd, int dest_fd)
+{
+    fd_set rfds;
+    int retval;
+
+    unsigned char buf[4096];
+
+    int max_fd = source_fd > dest_fd ? source_fd + 1 : dest_fd + 1;
+
+    for (;;) {
+        FD_ZERO(&rfds);
+        FD_SET(source_fd, &rfds);
+        FD_SET(dest_fd, &rfds);
+
+        retval = select(max_fd, &rfds, NULL, NULL, NULL);
+        if (retval == -1) {
+            perror("select()");
+            break;
+        } else if (retval){
+            if (FD_ISSET(source_fd, &rfds)) {
+                ssize_t size = read(source_fd, &buf, sizeof(buf));
+                if (size <= 0)
+                    break;
+
+                printf("Sending from source %d\n", size);
+                write(dest_fd, &buf, size);
+            } else if (FD_ISSET(dest_fd, &rfds)) {
+                ssize_t size = read(dest_fd, &buf, sizeof(buf));
+                if (size <= 0)
+                    break;
+
+                printf("Sending from destination %d\n", size);
+                write(source_fd, &buf, size);
+            }
+        }
+    }
+}


Property changes on: developers/zecke/serial_forward/forward.h
___________________________________________________________________
Name: svn:eol-style
   + native

Added: developers/zecke/serial_forward/pty_forward.c
===================================================================
--- developers/zecke/serial_forward/pty_forward.c	2008-03-15 21:37:01 UTC (rev 4213)
+++ developers/zecke/serial_forward/pty_forward.c	2008-03-17 15:11:45 UTC (rev 4214)
@@ -0,0 +1,76 @@
+/*
+ * Consume forwarded serial and provide a local pty
+ *
+ * Copyright (C) 2008 Openmoko Inc.
+ * 
+ * Author: Holger Hans Peter Freyther <zecke at openmoko.org>
+ *
+ */
+
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <netdb.h>
+#include <netinet/in.h>
+
+#include <pty.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "forward.h"
+
+
+
+static const int DEFAULT_PORT = 5621;
+static const char DEFAULT_IP[] = "192.168.0.202";
+
+
+/*
+ * Setup networking
+ * Setup pty
+ *
+ * Forward
+ */
+int main(int argc, char** argv)
+{
+    int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
+    if (socket_fd < 0) {
+        perror("Failed to create network socket");
+        return EXIT_FAILURE;
+    }
+
+    struct hostent* host = gethostbyname(DEFAULT_IP);
+    if (!host) {
+        printf("Failed to get the hostent\n");
+        return EXIT_FAILURE;
+    }
+
+    struct sockaddr_in addr = { 0, };
+    addr.sin_family = PF_INET;
+    addr.sin_port = htons(DEFAULT_PORT);
+    addr.sin_addr = *(struct in_addr*)host->h_addr;
+
+    int result = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr));
+    if (result < 0) {
+        perror("Connection failed");
+        return EXIT_FAILURE;
+    }
+
+    /*
+     *  PTY code
+     */
+    int master_fd;
+    int slave_fd;
+    char slave_name[PATH_MAX];
+    result = openpty(&master_fd, &slave_fd, slave_name, NULL, NULL);
+    if (result < 0) {
+        perror("openpty()");
+        return EXIT_FAILURE;
+    }
+
+    printf("You can now use '%s' %d %d %d\n", slave_name, socket_fd, master_fd, slave_fd);
+    forward_data(socket_fd, master_fd);
+
+    return EXIT_SUCCESS;
+}


Property changes on: developers/zecke/serial_forward/pty_forward.c
___________________________________________________________________
Name: svn:eol-style
   + native





More information about the commitlog mailing list