Adding port number on the ethernet protocol.
authorviric@llimona
Sun, 17 Feb 2008 22:27:55 +0100
changeset 88 a7f546938313
parent 87 be4ee314545c
child 89 2692e4742267
Adding port number on the ethernet protocol.
eth_linux.c
eth_proto.c
main.c
main.h
--- a/eth_linux.c	Sun Feb 17 21:03:23 2008 +0100
+++ b/eth_linux.c	Sun Feb 17 22:27:55 2008 +0100
@@ -151,7 +151,7 @@
     sa.sll_halen = 6;
     memcpy(sa.sll_addr, mac, 6);
     if (debug) {
-        printf("sending %d bytes to port %hi\r\n", len, sa.sll_protocol);
+        printf("sending %d bytes to eth_protocol %hi\r\n", len, sa.sll_protocol);
         dump(p, len);
     }
     if (len > 1500)
--- a/eth_proto.c	Sun Feb 17 21:03:23 2008 +0100
+++ b/eth_proto.c	Sun Feb 17 22:27:55 2008 +0100
@@ -16,7 +16,7 @@
 enum {
     MAXPACKET = 1500,
     MAXSEQ = 100,
-    HEAD = 9
+    HEAD = 13
 };
 
 static struct
@@ -31,6 +31,7 @@
     unsigned int wrong_recv;
     char send_buffer[MAXPACKET];
     int send_buffer_size;
+    int port;
 } edata;
 
 enum Control {
@@ -48,17 +49,20 @@
 {
     *((unsigned int *) data) = htonl(seq);
     data[4] = (unsigned char) c;
-    *((unsigned int *)(data+5)) = htonl(size);
+    *((unsigned int *)(data+5)) = htonl(edata.port);
+    *((unsigned int *)(data+5+4)) = htonl(size);
     return HEAD;
 }
 
 static int parse_head(unsigned char *data, unsigned int *seq, enum Control *c,
-        int *size)
+        int *port, int *size)
 {
     *seq = ntohl( *((unsigned int*) data) );
     *c = data[4];
+    if (port)
+        *port = ntohl(*((unsigned int *)(data+5)));
     if (size)
-        *size = ntohl(*((unsigned int *)(data+5)));
+        *size = ntohl(*((unsigned int *)(data+5+4)));
     return HEAD;
 }
 
@@ -70,6 +74,7 @@
 void eth_proto_init()
 {
     edata.socket = -1;
+    edata.port = command_line.eth_port;
     edata.seq_send = 0;
     edata.seq_wait = 0;
     edata.send_acked = 1; /* Fine at the beginning, as if the last data was acked */
@@ -125,6 +130,7 @@
     int res;
     int seq;
     int data_length;
+    int port;
     enum Control c;
     char partner[6];
 
@@ -132,7 +138,10 @@
             res = eth_recv(eth_buffer, sizeof(eth_buffer), partner);
             edata.partner_set = 1;
     } while(res < HEAD);
-    parse_head(eth_buffer, &seq, &c, &data_length);
+    parse_head(eth_buffer, &seq, &c, &port, &data_length);
+    if (port != edata.port)
+        return -1; /* Nothing the parent should care about. Not a packet for us. */
+
     /* We admit any first connection */
     if (seq == 0 && c == INIT)
     {
--- a/main.c	Sun Feb 17 21:03:23 2008 +0100
+++ b/main.c	Sun Feb 17 22:27:55 2008 +0100
@@ -25,7 +25,7 @@
 
 struct Command_line command_line;
 
-static const char version[] = "0.4.1";
+static const char version[] = "0.5";
 
 static int showhelp(const char *pname)
 {
@@ -53,7 +53,7 @@
            "        Unix socket.\n");
     printf(" -E     Echo remote input to the server terminal.\n");
 #ifdef linux
-    printf(" -e dev Also serve/connect using raw ethernet, device 'dev'.\n");
+    printf(" -e dev[:port] Also serve/connect using raw ethernet, device 'dev'.\n");
     printf(" -c adr Connect to address (MAC if eth).\n");
 #endif /* linux */
     printf(" -x     Send xterm's resize control string to clients.\n");
@@ -82,6 +82,21 @@
     return res;
 }
 
+static void parse_eth_device(char *str)
+{
+    char *p;
+    for (p = str; *p != 0 && *p != ':'; ++p);
+    if (*p == ':')
+    {
+        *p = '\0'; /* We want only the eth device name in str */
+        ++p;
+        command_line.eth_port = atoi(p);
+    }
+
+    /* The string will not have any colon or port. */
+    command_line.eth_device = strdup(str);
+}
+
 static void default_command_line()
 {
     command_line.is_server = 0;
@@ -99,6 +114,7 @@
     command_line.s_param.serve_eth = 0;
 
     command_line.tcp_port = 40000; /* Arbitrary */
+    command_line.eth_port = 100;   /* Arbitrary */
     command_line.buffer_size = 4096; /* Arbitrary */
     command_line.eth_device = 0;
     get_unix_path(); /* for command_line.unix_path */
@@ -173,7 +189,7 @@
             case 'e':
                 command_line.s_param.serve_eth = 1;
                 command_line.s_param.serve_unix = 0;
-                command_line.eth_device = strdup(optarg);
+                parse_eth_device(optarg);
                 command_line.c_param.transport = ETHERNET;
                 break;
             case '?':
--- a/main.h	Sun Feb 17 21:03:23 2008 +0100
+++ b/main.h	Sun Feb 17 22:27:55 2008 +0100
@@ -10,6 +10,7 @@
     char *unix_path; /* path or 0 if not to be used */
     int tcp_port;
     char *eth_device;
+    int eth_port;
     struct {
         int serve_tcp; /* yes/no */
         int serve_unix; /* yes/no */