Making client and server use different ethernet protocols
authorviric@llimona
Sun, 17 Feb 2008 21:03:23 +0100
changeset 87 be4ee314545c
parent 86 c972d3312fbd
child 88 a7f546938313
Making client and server use different ethernet protocols
eth_client.c
eth_linux.c
eth_linux.h
eth_proto.c
eth_server.c
main.c
main.h
--- a/eth_client.c	Fri Jan 11 15:17:36 2008 +0100
+++ b/eth_client.c	Sun Feb 17 21:03:23 2008 +0100
@@ -18,7 +18,7 @@
 void c_eth_init()
 {
     eth_proto_init();
-    myfd = eth_proto_open();
+    myfd = eth_proto_open(ETH_CLIENT);
     if (myfd < 0)
         error("Cannot eth_proto_open");
 }
--- a/eth_linux.c	Fri Jan 11 15:17:36 2008 +0100
+++ b/eth_linux.c	Sun Feb 17 21:03:23 2008 +0100
@@ -28,10 +28,12 @@
 #include "eth_linux.h"
 
 enum {
-    ETH_PROTO = 0xCACA
+    ETH_SERVER_PROTO = 0xCACA,
+    ETH_CLIENT_PROTO = 0xCACC
 };
 
 static int fd;
+static enum Eth_type eth_type;
 static char srcaddr[6];
 static int debug = 0;
 
@@ -60,21 +62,38 @@
 }
 
 // get us a raw connection to an interface
-int eth_open(char *eth)
+int eth_open(char *eth, enum Eth_type type)
 {
     int i, n;
     struct sockaddr_ll sa;
     struct ifreq xx;
+    short int proto_num;
+
+    if (type == ETH_SERVER)
+    {
+        dump_line("eth_open for server\n");
+        proto_num = htons(ETH_SERVER_PROTO);
+    }
+    else if (type == ETH_CLIENT)
+    {
+        dump_line("eth_open for client\n");
+        proto_num = htons(ETH_CLIENT_PROTO);
+    }
+    else
+        return -1;
+
+    /* Note the type server or client for this process */
+    eth_type = type;
 
     memset(&sa, 0, sizeof sa);
-    fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_PROTO));
+    fd = socket(PF_PACKET, SOCK_DGRAM, proto_num);
     if (fd == -1) {
         perror("got bad socket");
         return -1;
     }
     i = getindx(fd, eth);
     sa.sll_family = AF_PACKET;
-    sa.sll_protocol = htons(ETH_PROTO);
+    sa.sll_protocol = proto_num;
     sa.sll_ifindex = i;
     n = bind(fd, (struct sockaddr *)&sa, sizeof sa);
     if (n == -1) {
@@ -114,16 +133,25 @@
     struct sockaddr_ll sa;
     int i;
 
-    dump_line("eth_send\n");
-
     i = getindx(fd, dev);
     sa.sll_family = AF_PACKET;
-    sa.sll_protocol = htons(ETH_PROTO);
+    if (eth_type == ETH_SERVER)
+    {
+        dump_line("eth_send from server to client\n");
+        sa.sll_protocol = htons(ETH_CLIENT_PROTO);
+    }
+    else if (eth_type == ETH_CLIENT)
+    {
+        dump_line("eth_send from client to server\n");
+        sa.sll_protocol = htons(ETH_SERVER_PROTO);
+    }
+    else
+        return -1;
     sa.sll_ifindex = i;
     sa.sll_halen = 6;
     memcpy(sa.sll_addr, mac, 6);
     if (debug) {
-        printf("sending %d bytes\r\n", len);
+        printf("sending %d bytes to port %hi\r\n", len, sa.sll_protocol);
         dump(p, len);
     }
     if (len > 1500)
--- a/eth_linux.h	Fri Jan 11 15:17:36 2008 +0100
+++ b/eth_linux.h	Sun Feb 17 21:03:23 2008 +0100
@@ -1,3 +1,3 @@
-int eth_open(char *eth);
+int eth_open(char *eth, enum Eth_type type);
 int eth_recv(char *buf, int len, char *partner_mac);
 int eth_send(char *dev, char *mac, void *p, int len);
--- a/eth_proto.c	Fri Jan 11 15:17:36 2008 +0100
+++ b/eth_proto.c	Sun Feb 17 21:03:23 2008 +0100
@@ -10,8 +10,8 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include "main.h"
 #include "eth_linux.h"
-#include "main.h"
 
 enum {
     MAXPACKET = 1500,
@@ -99,9 +99,9 @@
     return val;
 }
 
-int eth_proto_open()
+int eth_proto_open(enum Eth_type type)
 {
-    edata.socket = eth_open(command_line.eth_device);
+    edata.socket = eth_open(command_line.eth_device, type);
     if (edata.socket == -1)
         error("Cannot open device %s", command_line.eth_device);
 
--- a/eth_server.c	Fri Jan 11 15:17:36 2008 +0100
+++ b/eth_server.c	Sun Feb 17 21:03:23 2008 +0100
@@ -15,7 +15,7 @@
 void s_eth_init()
 {
     eth_proto_init();
-    myfd = eth_proto_open();
+    myfd = eth_proto_open(ETH_SERVER);
     if (myfd < 0)
         error("Cannot eth_proto_open");
 }
--- a/main.c	Fri Jan 11 15:17:36 2008 +0100
+++ b/main.c	Sun Feb 17 21:03:23 2008 +0100
@@ -172,6 +172,7 @@
                 break;
             case 'e':
                 command_line.s_param.serve_eth = 1;
+                command_line.s_param.serve_unix = 0;
                 command_line.eth_device = strdup(optarg);
                 command_line.c_param.transport = ETHERNET;
                 break;
--- a/main.h	Fri Jan 11 15:17:36 2008 +0100
+++ b/main.h	Sun Feb 17 21:03:23 2008 +0100
@@ -89,8 +89,13 @@
 void dump_line(const char *msg, ...);
 
 /* eth_proto.c */
+enum Eth_type
+{
+    ETH_SERVER,
+    ETH_CLIENT
+};
 void eth_proto_init();
-int eth_proto_open();
+int eth_proto_open(enum Eth_type type);
 int eth_proto_recv(char *data, int size);
 int eth_proto_send(const char *data, int size);
 int eth_proto_allow_sending();