Permalink
Browse files

Identify target node id by mac address

  • Loading branch information...
1 parent 33b55cc commit 8cb6233fcac29e70aa94996d990215655b6ba2dc @simon3z simon3z committed Oct 7, 2010
Showing with 50 additions and 60 deletions.
  1. +26 −4 knet.c
  2. +4 −0 knet.h
  3. +1 −1 main.c
  4. +0 −32 packet.c
  5. +0 −8 packet.h
  6. +6 −1 tests/Makefile.am
  7. +13 −14 tests/check_packet.c
View
30 knet.c
@@ -14,11 +14,17 @@
#include <linux/if.h>
#include <linux/if_tun.h>
#include <arpa/inet.h>
+#include <net/ethernet.h>
#include "knet.h"
#include "logging.h"
#include "utils.h"
+
+/* make this configurable */
+uint8_t knet_hwvend[2] = { 0x16, 0x07 };
+
+
int knet_open(char *dev, size_t dev_size)
{
struct ifreq ifr;
@@ -68,11 +74,11 @@ int knet_set_hwid(char *dev, uint32_t nodeid)
ret = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
if (ret != 0) goto exit_clean;
- ifr.ifr_hwaddr.sa_data[0] = 0x16;
- ifr.ifr_hwaddr.sa_data[1] = 0x07;
-
machwid = htonl(nodeid);
- memmove(ifr.ifr_hwaddr.sa_data + 2, &machwid, ETH_ALEN - 2);
+
+ memmove(ifr.ifr_hwaddr.sa_data, knet_hwvend, sizeof(knet_hwvend));
+ memmove(ifr.ifr_hwaddr.sa_data + sizeof(knet_hwvend),
+ &machwid, ETH_ALEN - sizeof(knet_hwvend));
ret = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
@@ -81,6 +87,22 @@ int knet_set_hwid(char *dev, uint32_t nodeid)
return ret;
}
+uint32_t knet_hwtoid(void *packet)
+{
+ uint32_t nodeid;
+ struct ether_header *eth_h = packet;
+
+ if (memcmp(eth_h->ether_dhost, knet_hwvend, sizeof(knet_hwvend)) != 0)
+ return 0;
+
+ memmove(&nodeid, eth_h->ether_dhost + sizeof(knet_hwvend),
+ ETH_ALEN - sizeof(knet_hwvend));
+
+ nodeid = ntohl(nodeid);
+
+ return nodeid;
+}
+
int knet_get_mtu(char *dev)
{
struct ifreq ifr;
View
4 knet.h
@@ -2,6 +2,7 @@
#define __KNET_H__
#include <stdlib.h>
+#include <stdint.h>
/*
* TODO: Make this configurable
@@ -10,11 +11,14 @@
int knet_open(char *dev, size_t dev_size);
int knet_set_hwid(char *dev, uint32_t nodeid);
+uint32_t knet_hwtoid(void *packet);
int knet_get_mtu(char *dev);
int knet_close(int fd);
int knet_read(int fd, char *buf, int len);
int knet_write(int fd, char *buf, int len);
extern int knet_up(const char *dev_name, int mtu);
extern int knet_add_ip(const char *dev_name, const char *ip);
+extern uint8_t knet_hwvend[2];
+
#endif
View
2 main.c
@@ -393,7 +393,7 @@ static void *eth_to_knet_thread(void *arg)
if (FD_ISSET(eth_fd, &rfds)) {
read_len = read(eth_fd, knet_h + 1, TX_KNET_DATASIZE);
if (read_len > 0) {
- // dst_nodeid = packet_to_nodeid(knet_h + 1);
+ // dst_nodeid = knet_hwtoid(knet_h + 1);
decode_pckt(knet_h + 1);
knet_h->seq_num++;
dispatch_buffer(mainconf, dst_nodeid, knet_h, read_len + sizeof(struct knet_header));
View
@@ -1,32 +0,0 @@
-#include "config.h"
-
-#include <stdio.h>
-#include <arpa/inet.h>
-
-#include "packet.h"
-#include "netsocket.h"
-
-
-static uint8_t ipv4_bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-static uint8_t ipv6_bcast[] = { 0x33, 0x33 };
-
-
-inline uint32_t packet_to_nodeid(void *packet, struct node *node)
-{
- struct ether_header *eth_h = packet;
-
- if (memcmp(ipv4_bcast, eth_h->ether_dhost, sizeof(ipv4_bcast)) == 0)
- return 0;
-
- if (memcmp(ipv6_bcast, eth_h->ether_dhost, sizeof(ipv6_bcast)) == 0)
- return 0;
-
- while (node) {
- if (memcmp(node->hwaddress, eth_h->ether_dhost, ETH_ALEN) == 0)
- return node->nodeid;
- node = node->next;
- }
-
- return 0;
-}
-
View
@@ -1,8 +0,0 @@
-#ifndef __PACKET_H__
-#define __PACKET_H__
-
-#include "nodes.h"
-
-uint32_t packet_to_nodeid(void *packet, struct node *node);
-
-#endif
View
@@ -1,3 +1,5 @@
+MAINTAINERCLEANFILES = Makefile.in
+
VALGRIND = valgrind -q --error-exitcode=127 --leak-check=full
check-valgrind: $(check_PROGRAMS)
@@ -8,5 +10,8 @@ TESTS = $(check_PROGRAMS)
check_packet_SOURCES = \
check_packet.c \
- ../packet.c
+ ../knet.c \
+ ../utils.c
+check_packet_LDFLAGS = \
+ $(logsys_LIBS)
View
@@ -3,14 +3,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include <error.h>
-#include "nodes.h"
-#include "packet.h"
+#include "knet.h"
char eth_frame[] = {
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* ether_dhost */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ether_dhost */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ether_shost */
0x08, 0x00, /* ether_type */
};
@@ -27,31 +27,30 @@ char bcast6_mac[] = {
int main(int argc, char *argv[])
{
uint32_t nid;
- struct node n;
- memset(&n, 0, sizeof(struct node));
- n.nodeid = 1;
- memmove(n.hwaddress, eth_frame, sizeof(n.hwaddress));
+ memmove(eth_frame, knet_hwvend, sizeof(knet_hwvend));
- nid = packet_to_nodeid(eth_frame, &n);
- if (nid != n.nodeid)
+ eth_frame[5] = 0x01;
+
+ nid = knet_hwtoid(eth_frame);
+ if (nid != 0x01)
error(EXIT_FAILURE, -EINVAL, "eth_frame1 failed");
- memset(eth_frame, 0x00, ETH_ALEN);
+ eth_frame[5] = 0x02;
- nid = packet_to_nodeid(eth_frame, &n);
- if (nid != 0)
+ nid = knet_hwtoid(eth_frame);
+ if (nid != 0x02)
error(EXIT_FAILURE, -EINVAL, "eth_frame2 failed");
memmove(eth_frame, bcast4_mac, sizeof(bcast4_mac));
- nid = packet_to_nodeid(eth_frame, &n);
+ nid = knet_hwtoid(eth_frame);
if (nid != 0)
error(EXIT_FAILURE, -EINVAL, "bcast4_mac failed");
memmove(eth_frame, bcast6_mac, sizeof(bcast6_mac));
- nid = packet_to_nodeid(eth_frame, &n);
+ nid = knet_hwtoid(eth_frame);
if (nid != 0)
error(EXIT_FAILURE, -EINVAL, "bcast6_mac failed");

0 comments on commit 8cb6233

Please sign in to comment.