[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[coldsync-hackers] experimental inetd patch (multiple connections)



Hello All!

  This patch add support for multiple TCP/IP connections using INETD and
coldsync 2.1.0. The UDP part of the hotsync protocol is currently
handled with the "pi-csd" daemon from the pilot-link package. The patch
is alpha code!

Note: I' m using de option "-l /var/log/coldsync" to redir log messages
to a file, maybe the patch not work without this option because the log
msgs go to stdout and interferes with the connection between coldsync
and inetd.

Frank

diff -urN coldsync-2.1.0/include/pconn/PConnection.h coldsync-2.1.0-inetd/include/pconn/PConnection.h
--- coldsync-2.1.0/include/pconn/PConnection.h	Sun Apr 15 01:32:50 2001
+++ coldsync-2.1.0-inetd/include/pconn/PConnection.h	Fri Apr 20 12:31:48 2001
@@ -39,6 +39,7 @@
 #define LISTEN_NET	2	/* Listen on TCP/UDP port (not
 				 * implemented yet). */
 #define LISTEN_USB	3	/* USB for Handspring Visor */
+#define LISTEN_INETD	4	/* Listen using standard i/o */
 
 /* PConnection
  * This struct is an opaque type that contains all of the state about
@@ -61,8 +62,8 @@
 	int (*io_bind)(struct PConnection *p, const void *addr,
 		       const int addrlen);
 	int (*io_read)(struct PConnection *p, unsigned char *buf, int len);
-	int (*io_write)(struct PConnection *p, unsigned const char *buf,
-			const int len);
+	int (*io_write)(struct PConnection *p, unsigned char *buf,
+			int len);
 	int (*io_connect)(struct PConnection *p, const void *addr,
 			  const int addrlen);
 	int (*io_accept)(struct PConnection *p);
diff -urN coldsync-2.1.0/libpconn/Makefile coldsync-2.1.0-inetd/libpconn/Makefile
--- coldsync-2.1.0/libpconn/Makefile	Fri Mar 30 03:10:55 2001
+++ coldsync-2.1.0-inetd/libpconn/Makefile	Fri Apr 20 12:17:53 2001
@@ -18,7 +18,8 @@
 		PConnection.c \
 		PConnection_serial.c \
 		PConnection_usb.c \
-		PConnection_net.c
+		PConnection_net.c \
+		PConnection_inetd.c
 
 LIBOBJS =	cfmakeraw.o \
 		dlp_cmd.o \
@@ -33,7 +34,8 @@
 		PConnection.o \
 		PConnection_serial.o \
 		PConnection_usb.o \
-		PConnection_net.o
+		PConnection_net.o \
+		PConnection_inetd.o
 
 CLEAN =		${LIBOBJS} ${LIBRARY} \
 		*.ln *.bak *~ core *.core .depend
diff -urN coldsync-2.1.0/libpconn/PConnection.c coldsync-2.1.0-inetd/libpconn/PConnection.c
--- coldsync-2.1.0/libpconn/PConnection.c	Tue Feb 20 09:36:38 2001
+++ coldsync-2.1.0-inetd/libpconn/PConnection.c	Fri Apr 20 11:37:14 2001
@@ -24,9 +24,15 @@
 extern int pconn_serial_open(PConnection *pconn,
 			     char *fname,
 			     Bool prompt_for_hotsync);
+
 extern int pconn_net_open(PConnection *pconn,
 			  char *fname,
 			  int prompt_for_hotsync);
+
+extern int pconn_inetd_open(PConnection *pconn,
+			  char *fname,
+			  int prompt_for_hotsync);
+
 #if WITH_USB
 extern int pconn_usb_open(PConnection *pconn,
 			  char *fname,
@@ -77,6 +83,14 @@
 
 	    case LISTEN_NET:
 		if (pconn_net_open(pconn, fname, promptHotSync) < 0)
+		{
+			free(pconn);
+			return NULL;
+		}
+		return pconn;
+
+	    case LISTEN_INETD:
+		if (pconn_inetd_open(pconn, fname, promptHotSync) < 0)
 		{
 			free(pconn);
 			return NULL;
diff -urN coldsync-2.1.0/libpconn/PConnection_inetd.c coldsync-2.1.0-inetd/libpconn/PConnection_inetd.c
--- coldsync-2.1.0/libpconn/PConnection_inetd.c	Wed Dec 31 21:00:00 1969
+++ coldsync-2.1.0-inetd/libpconn/PConnection_inetd.c	Fri Apr 20 12:29:47 2001
@@ -0,0 +1,249 @@
+/* PConnection_net.c
+ */
+#include "config.h"
+#include <stdio.h>
+#include <sys/types.h>
+#include <string.h>		/* For bzero() */
+
+#if HAVE_LIBINTL_H
+#  include <libintl.h>		/* For i18n */
+#endif	/* HAVE_LIBINTL_H */
+
+#include "pconn/PConnection.h"
+#include "pconn/netsync.h"
+#include "pconn/util.h"
+
+static int inetd_tcp_listen(PConnection *pconn);
+
+/* Ritual statements
+ * These packets are sent back and forth during the initial handshaking
+ * phase. I don't know what they mean. The sequence is:
+ * client sends UDP wakeup packet
+ * server sends UDP wakeup ACK
+ * client sends ritual response 1
+ * server sends ritual statement 2
+ * client sends ritual response 2
+ * server sends ritual statement 3
+ * client sends ritual response 3
+ *
+ * The comments are mostly conjecture and speculation.
+ */
+
+/* XXX - Could these be CMP 2.0? When answering this question, might want
+ * to keep in mind the underlying protocol, the one with the (other) XIDs,
+ * implemented by netsync_read() and netsync_write().
+ */
+
+static ubyte ritual_stmt2[] = {
+	0x12,				/* Command */
+	0x01,				/* argc */
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x20,		/* Arg ID */
+	0x00, 0x00, 0x00, 0x24,		/* Arg length */
+	/* Arg data */
+	0xff, 0xff, 0xff, 0xff,
+	0x3c, 0x00,			/* These are reversed in the
+					 * response */
+	0x3c, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0xc0, 0xa8, 0xa5, 0x1f,		/* 192.168.165.31 */
+	0x04, 0x27, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+};
+
+static ubyte ritual_stmt3[] = {
+	0x13,				/* Command */
+	0x01,				/* argc */
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x20,		/* Arg ID */
+	0x00, 0x00, 0x00, 0x20,		/* Arg length */
+	/* Arg data
+	 * This is very similar to ritual statement/response 2.
+	 */
+	0xff, 0xff, 0xff, 0xff,
+	0x00, 0x3c,
+	0x00, 0x3c,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x01,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+};
+
+static int
+inetd_bind(PConnection *pconn,
+	 const void *addr,
+	 const int addrlen)
+{
+	return 0;
+}
+
+static int
+inetd_read(PConnection *p, unsigned char *buf, int len)
+{
+	return read(0, buf, len);
+}
+
+static int
+inetd_write(PConnection *p, unsigned char *buf, int len)
+{
+	return write(1, buf, len);
+}
+
+/* inetd_connect
+ *	- dummy
+ */
+static int
+inetd_connect(PConnection *pconn, const void *addr, const int addrlen)
+{
+	return -1;
+}
+
+static int
+inetd_accept(PConnection *p)
+{
+
+	inetd_tcp_listen(p);
+	/* XXX - Error-checking */
+
+	return 0;
+}
+
+static int
+inetd_close(PConnection *p)
+{
+	/* Clean up the protocol stack elements */
+	dlp_tini(p);
+	netsync_tini(p);
+
+	return (p->fd >= 0 ? close(p->fd) : 0);
+}
+
+static int
+inetd_select(PConnection *p,
+	   pconn_direction which,
+	   struct timeval *tvp)
+{
+	fd_set fds;
+
+	FD_ZERO(&fds);
+	FD_SET(p->fd, &fds);
+
+	return (which == forReading) ? select(p->fd+1, &fds, NULL, NULL, tvp)
+				     : select(p->fd+1, NULL, &fds, NULL, tvp);
+}
+
+static int
+inetd_drain(PConnection *p)
+{
+	/* I don't think there's a network equivalent of flushing a stream
+	 * or tty connection.
+	 */
+	return 0;
+}
+
+int
+pconn_inetd_open(PConnection *pconn, char *device, int prompt)
+{
+	IO_TRACE(1)
+		fprintf(stderr, "Opening net connection.\n");
+
+	/* Initialize the various protocols that the network connection
+	 * will use.
+	 */
+	/* Initialize the DLP part of the PConnection */
+	if (dlp_init(pconn) < 0)
+	{
+		dlp_tini(pconn);
+		return -1;
+	}
+
+	/* Initialize the NetSync part of the PConnnection */
+	if (netsync_init(pconn) < 0)
+	{
+		dlp_tini(pconn);
+		netsync_tini(pconn);
+		return -1;
+	}
+
+	/* Set the methods used by the network connection */
+	pconn->io_bind = &inetd_bind;
+	pconn->io_read = &inetd_read;
+	pconn->io_write = &inetd_write;
+	pconn->io_connect = &inetd_connect;
+	pconn->io_accept = &inetd_accept;
+	pconn->io_close = &inetd_close;
+	pconn->io_select = &inetd_select;
+	pconn->io_drain = &inetd_drain;
+	pconn->io_private = 0;
+
+	return 0;
+}
+
+
+static int
+inetd_tcp_listen(PConnection *pconn)
+{
+	int err;
+	const ubyte *inbuf;
+	uword inlen;
+
+	IO_TRACE(4)
+		fprintf(stderr, "Inside inetd_tcp_listen()\n");
+
+
+	pconn->fd = 0;
+
+	/* Receive ritual response 1 */
+	err = netsync_read(pconn, &inbuf, &inlen);
+	IO_TRACE(5)
+	{
+		fprintf(stderr,
+			"netsync_read(ritual resp 1) returned %d\n",
+			err);
+		if (err > 0)
+			debug_dump(stderr, "<<<", inbuf, inlen);
+	}
+
+	/* Send ritual statement 2 */
+	err = netsync_write(pconn, ritual_stmt2, sizeof(ritual_stmt2));
+	IO_TRACE(5)
+		fprintf(stderr, "netsync_write(ritual stmt 2) returned %d\n",
+			err);
+
+	/* Receive ritual response 2 */
+	err = netsync_read(pconn, &inbuf, &inlen);
+	IO_TRACE(5)
+	{
+		fprintf(stderr, "netsync_read returned %d\n", err);
+		if (err > 0)
+			debug_dump(stderr, "<<<", inbuf, inlen);
+	}
+
+	/* Send ritual statement 3 */
+	err = netsync_write(pconn, ritual_stmt3, sizeof(ritual_stmt3));
+	IO_TRACE(5)
+		fprintf(stderr, "netsync_write(ritual stmt 3) returned %d\n",
+			err);
+
+	/* Receive ritual response 3 */
+	err = netsync_read(pconn, &inbuf, &inlen);
+	IO_TRACE(5)
+	{
+		fprintf(stderr, "netsync_read returned %d\n", err);
+		if (err > 0)
+			debug_dump(stderr, "<<<", inbuf, inlen);
+	}
+
+	return 0;
+}
+
+/* This is for Emacs's benefit:
+ * Local Variables: ***
+ * fill-column:	75 ***
+ * End: ***
+ */
diff -urN coldsync-2.1.0/libpconn/netsync.c coldsync-2.1.0-inetd/libpconn/netsync.c
--- coldsync-2.1.0/libpconn/netsync.c	Sun Apr 15 01:44:18 2001
+++ coldsync-2.1.0-inetd/libpconn/netsync.c	Fri Apr 20 11:59:38 2001
@@ -97,7 +97,7 @@
 		fprintf(stderr, "Inside netsync_read()\n");
 
 	/* Read packet header */
-	err = read(pconn->fd, hdr_buf, NETSYNC_HDR_LEN);
+	err = (*pconn->io_read)(pconn, hdr_buf, NETSYNC_HDR_LEN);
 	if (err < 0)
 	{
 		fprintf(stderr, _("Error reading NetSync packet header.\n"));
@@ -143,7 +143,7 @@
 	got = 0;
 	while (want > got)
 	{
-		err = read(pconn->fd, pconn->net.inbuf+got, want-got);
+		err = (*pconn->io_read)(pconn, pconn->net.inbuf+got, want-got);
 		if (err < 0)
 		{
 			perror("netsync_read: read");
diff -urN coldsync-2.1.0/src/config.c coldsync-2.1.0-inetd/src/config.c
--- coldsync-2.1.0/src/config.c	Sun Apr  8 05:26:02 2001
+++ coldsync-2.1.0-inetd/src/config.c	Fri Apr 20 12:07:06 2001
@@ -1277,6 +1277,8 @@
 		return LISTEN_NET;
 	if (strcasecmp(str, "usb") == 0)
 		return LISTEN_USB;
+	if (strcasecmp(str, "inetd") == 0)
+		return LISTEN_INETD;
 	return -1;		/* None of the above */
 }
 
diff -urN coldsync-2.1.0/src/lexer.l coldsync-2.1.0-inetd/src/lexer.l
--- coldsync-2.1.0/src/lexer.l	Sun Apr  1 04:15:32 2001
+++ coldsync-2.1.0-inetd/src/lexer.l	Fri Apr 20 12:40:06 2001
@@ -155,6 +155,7 @@
 "forward"	{ KEYWORD(FORWARD);	}
 "listen"	{ KEYWORD(LISTEN);	}
 "net"		{ KEYWORD(NET);	}
+"inetd"		{ KEYWORD(INETD);	}
 "network"	{ KEYWORD(NET);		/* Synonym */	}
 "path"		{ KEYWORD(PATH);	}
 "pda"		{ KEYWORD(PDA);		}
diff -urN coldsync-2.1.0/src/parser.y coldsync-2.1.0-inetd/src/parser.y
--- coldsync-2.1.0/src/parser.y	Tue Feb 20 11:04:15 2001
+++ coldsync-2.1.0-inetd/src/parser.y	Fri Apr 20 12:12:14 2001
@@ -95,6 +95,7 @@
 %token SERIAL
 %token USB
 %token NET
+%token INETD
 
 %type <commtype> comm_type
 %type <crea_type> creator_type
@@ -223,6 +224,12 @@
 		PARSE_TRACE(5)
 			fprintf(stderr, "Found commtype: Net\n");
 		$$ = LISTEN_NET;
+	}
+	| INETD
+	{
+		PARSE_TRACE(5)
+			fprintf(stderr, "Found commtype: Inetd\n");
+		$$ = LISTEN_INETD;
 	}
 	| error
 	{