From 904b266392947cea81c74d4e8e273cb972bd3e30 Mon Sep 17 00:00:00 2001
From: Sven Robertz <sven@cs.lth.se>
Date: Thu, 23 Feb 2012 10:33:39 +0100
Subject: [PATCH] cleanup and fixes

---
 examples/simple/datagram/thr_example2.c       |  2 +-
 lib/c/experimental/ThrottleDrv/ethernet_drv.c | 35 +++++++++++--------
 lib/c/experimental/ThrottleDrv/ethernet_drv.h |  4 ++-
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/examples/simple/datagram/thr_example2.c b/examples/simple/datagram/thr_example2.c
index 8b862a7..ab61c66 100644
--- a/examples/simple/datagram/thr_example2.c
+++ b/examples/simple/datagram/thr_example2.c
@@ -79,7 +79,7 @@ static void handle_simple_TwoInts(simple_TwoInts *v,void *context) {
   unsigned char ch_id = get_channel((struct thr_chn_t *)context); 
   printf("Got TwoInts. a=%d, b=%d\n", v->a, v->b);
   printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0],  src[1], src[2], src[3], src[4], src[5]);
-  struct thr_chan_t *ch = thr_open_chn(src, ch_id, (unsigned short)50, (unsigned short)100, NULL);
+  struct thr_chn_t *ch = thr_open_chn(src, ch_id, (unsigned short)50, (unsigned short)100, NULL);
   struct labcomm_encoder *enc = labcomm_encoder_new(labcomm_thr_writer, ch);
   labcomm_encoder_register_simple_TwoInts(enc);
   v->a *= 2;
diff --git a/lib/c/experimental/ThrottleDrv/ethernet_drv.c b/lib/c/experimental/ThrottleDrv/ethernet_drv.c
index d0f5f02..fe0004c 100644
--- a/lib/c/experimental/ThrottleDrv/ethernet_drv.c
+++ b/lib/c/experimental/ThrottleDrv/ethernet_drv.c
@@ -5,10 +5,12 @@
 #include <errno.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
-#include <linux/if_ether.h>
-#include <linux/if_packet.h>
-#include <linux/if_arp.h>
-#include <netinet/in.h>
+//#include <linux/if_ether.h>
+//#include <linux/if_packet.h>
+//#include <linux/if_arp.h>
+#include <net/if.h>
+#include <netpacket/packet.h>
+//#include <netinet/in.h>
 #include "ethernet_drv.h"
 #include "display.h"
 
@@ -18,7 +20,7 @@
 /* local type for the ethernet interface */
 struct eth_int_t
 {
-	unsigned char mac_adr[ETH_ALEN];  /* MAC address                */
+	struct ether_addr mac_adr;        /* MAC address                */
 	int socket_id;                    /* socket file descriptor     */
 	int index;                        /* index of the eth interface */
 	unsigned char validity;
@@ -72,7 +74,7 @@ struct eth_int_t* eth_open(const char* eth_int_name)
             else
             {
                /* copy the MAC address into the eth interface struct */
-               memcpy(tmp_eth->mac_adr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+               memcpy(tmp_eth->mac_adr.ether_addr_octet, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
 
                /*retrieve the Ethernet interface index*/
                if (ioctl(tmp_eth->socket_id, SIOCGIFINDEX, &ifr) == -1)/* error during the retrieve of index */
@@ -87,7 +89,7 @@ struct eth_int_t* eth_open(const char* eth_int_name)
                   /* copy the interface index into the eth interface struct */
                   tmp_eth->index = ifr.ifr_ifindex;
                   printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
-                          tmp_eth->mac_adr[0],tmp_eth->mac_adr[1],tmp_eth->mac_adr[2],tmp_eth->mac_adr[3],tmp_eth->mac_adr[4],tmp_eth->mac_adr[5]);
+                          tmp_eth->mac_adr.ether_addr_octet[0],tmp_eth->mac_adr.ether_addr_octet[1],tmp_eth->mac_adr.ether_addr_octet[2],tmp_eth->mac_adr.ether_addr_octet[3],tmp_eth->mac_adr.ether_addr_octet[4],tmp_eth->mac_adr.ether_addr_octet[5]);
                   printf("Successfully got interface index for %s: %i\n",eth_int_name, tmp_eth->index);
                }
             }
@@ -130,7 +132,7 @@ int eth_close(struct eth_int_t* eth_int)
 /***
 Returns the MAC address of the Ethernet Interface
 ***/
-int eth_getMACadr(const struct eth_int_t* eth_int, unsigned char* mac_adr)
+int eth_getMACadr(const struct eth_int_t* eth_int, struct ether_addr * mac_adr)
 {
    int ret = 0;
 
@@ -143,7 +145,7 @@ int eth_getMACadr(const struct eth_int_t* eth_int, unsigned char* mac_adr)
    else
    {
       /* TO-DO: introduce a validity flag to be check before accessing to eth_int (better a crc) */
-      memcpy(mac_adr, eth_int->mac_adr, ETH_ALEN);
+      memcpy(mac_adr, &eth_int->mac_adr, sizeof(mac_adr));
    }
    return(ret);
 }
@@ -170,11 +172,6 @@ int eth_send(const struct eth_int_t* eth_int, const unsigned char* eth_frame, un
 
       /*prepare sockaddr_ll (address structure for PACKET_SOCKET) */
       socket_address.sll_family   = AF_PACKET;
-      socket_address.sll_protocol = htons(ETH_P_IP);  /* Physical layer protocol           */
-      socket_address.sll_ifindex  = eth_int->index;   /* Ethernet Interface index          */
-      socket_address.sll_hatype   = ARPHRD_ETHER;     /* ARP hardware identifier: Ethernet */
-      socket_address.sll_pkttype  = PACKET_OTHERHOST; /* Packet type: Another host         */
-      socket_address.sll_halen    = ETH_ALEN;         /* Length of the MAC address         */
       socket_address.sll_addr[0]  = eth_header->h_dest[0];
       socket_address.sll_addr[1]  = eth_header->h_dest[1];
       socket_address.sll_addr[2]  = eth_header->h_dest[2];
@@ -183,6 +180,16 @@ int eth_send(const struct eth_int_t* eth_int, const unsigned char* eth_frame, un
       socket_address.sll_addr[5]  = eth_header->h_dest[5];
       socket_address.sll_addr[6]  = 0x00; /* not used */
       socket_address.sll_addr[7]  = 0x00; /* not used */
+      socket_address.sll_halen    = ETH_ALEN;         /* Length of the MAC address         */
+      socket_address.sll_ifindex  = eth_int->index;   /* Ethernet Interface index          */
+      // The rest should be zero for sending, and are set by the system for receiving.
+      socket_address.sll_hatype   = 0;            
+      socket_address.sll_protocol = 0;
+      socket_address.sll_pkttype  = 0;
+
+      //socket_address.sll_protocol = htons(ETH_P_IP);  /* Physical layer protocol           */
+      //socket_address.sll_hatype   = ARPHRD_ETHER;     /* ARP hardware identifier: Ethernet */
+      //socket_address.sll_pkttype  = PACKET_OTHERHOST; /* Packet type: Another host         */
 
       /*send the Ethernet frame */
       ret = sendto(eth_int->socket_id, eth_frame, length, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
diff --git a/lib/c/experimental/ThrottleDrv/ethernet_drv.h b/lib/c/experimental/ThrottleDrv/ethernet_drv.h
index 1dc786e..de139de 100644
--- a/lib/c/experimental/ThrottleDrv/ethernet_drv.h
+++ b/lib/c/experimental/ThrottleDrv/ethernet_drv.h
@@ -1,11 +1,13 @@
 #ifndef _ETHERNET_DRV_H_
 #define _ETHERNET_DRV_H_
 
+#include <net/ethernet.h>
+
 struct eth_int_t;
 
 struct eth_int_t* eth_open(const char* eth_int_name);
 int eth_close(struct eth_int_t* eth_int);
-int eth_getMACadr(const struct eth_int_t* eth_int, unsigned char* mac_adr);
+int eth_getMACadr(const struct eth_int_t* eth_int, struct ether_addr *mac_adr);
 int eth_send(const struct eth_int_t* eth_int, const unsigned char* eth_frame, unsigned short length);
 int eth_receive (const struct eth_int_t* eth_int, unsigned char* eth_frame, unsigned short length);
 
-- 
GitLab