[Nstx-ng-devel] r16 - in /trunk: ChangeLog nstx_pstack.c nstx_pstack.h nstx_tuntap.c nstxd.c nstxfun.h

qsr-guest at users.alioth.debian.org qsr-guest at users.alioth.debian.org
Wed May 6 21:46:54 UTC 2009


Author: qsr-guest
Date: Wed May  6 21:46:54 2009
New Revision: 16

URL: http://svn.debian.org/wsvn/nstx-ng/?sc=1&rev=16
Log:
 * Handle multi-channel communications.


Modified:
    trunk/ChangeLog
    trunk/nstx_pstack.c
    trunk/nstx_pstack.h
    trunk/nstx_tuntap.c
    trunk/nstxd.c
    trunk/nstxfun.h

Modified: trunk/ChangeLog
URL: http://svn.debian.org/wsvn/nstx-ng/trunk/ChangeLog?rev=16&op=diff
==============================================================================
--- trunk/ChangeLog (original)
+++ trunk/ChangeLog Wed May  6 21:46:54 2009
@@ -1,3 +1,6 @@
+2009-05-06  Benjamin Lerman   <nstx at ambre.net>
+	* Handle multi-channel communications.
+
 2009-05-06  Benjamin Lerman   <nstx at ambre.net>
 	* Add a -c option to the client to choose the channel.
 	  Have 16 queues on the server to separate packets depending on

Modified: trunk/nstx_pstack.c
URL: http://svn.debian.org/wsvn/nstx-ng/trunk/nstx_pstack.c?rev=16&op=diff
==============================================================================
--- trunk/nstx_pstack.c (original)
+++ trunk/nstx_pstack.c Wed May  6 21:46:54 2009
@@ -46,7 +46,7 @@
  * This method returns the channel used.
  */
 int nstx_handlepacket(const char *ptr, size_t len,
-                  void (*nstx_usepacket)(const char*, size_t)) {
+                  void (*nstx_usepacket)(int,const char*, size_t)) {
     struct nstxhdr nh;
     struct nstx_item *nstxitem;
     char *netpacket;
@@ -67,7 +67,7 @@
 
     if (add_data(nstxitem, &nh, ptr + nstxhdr_size, len - nstxhdr_size)) {
         netpacket = dealloc_item(nstxitem, &netpacketlen);
-        nstx_usepacket(netpacket, netpacketlen);
+        nstx_usepacket(nh.chan,netpacket, netpacketlen);
     }
     check_timeouts();
     return nh.chan;

Modified: trunk/nstx_pstack.h
URL: http://svn.debian.org/wsvn/nstx-ng/trunk/nstx_pstack.h?rev=16&op=diff
==============================================================================
--- trunk/nstx_pstack.h (original)
+++ trunk/nstx_pstack.h Wed May  6 21:46:54 2009
@@ -56,7 +56,7 @@
 };
 
 int nstx_handlepacket(const char *, size_t,
-                       void(*)(const char*, size_t));
+                       void(*)(int,const char*, size_t));
 void init_pstack(int len);
 
 #endif

Modified: trunk/nstx_tuntap.c
URL: http://svn.debian.org/wsvn/nstx-ng/trunk/nstx_tuntap.c?rev=16&op=diff
==============================================================================
--- trunk/nstx_tuntap.c (original)
+++ trunk/nstx_tuntap.c Wed May  6 21:46:54 2009
@@ -250,7 +250,7 @@
     return NULL;
 }
 
-void sendtun(const char *data, size_t len) {
+void sendtun(int channel, const char *data, size_t len) {
 //   printf("Sent len %d, csum %d\n", len, checksum(data, len));
     write(tfd, data, len);
 }

Modified: trunk/nstxd.c
URL: http://svn.debian.org/wsvn/nstx-ng/trunk/nstxd.c?rev=16&op=diff
==============================================================================
--- trunk/nstxd.c (original)
+++ trunk/nstxd.c Wed May  6 21:46:54 2009
@@ -46,10 +46,14 @@
 #define BUFLEN 2000
 
 static void nstx_getpacket (void);
-static struct nstx_senditem * alloc_senditem(void);
-static void queue_senditem(const char *buf, int len);
-static char *dequeue_senditem (int *len);
-struct nstx_senditem * nstx_sendlist = NULL;
+static struct nstx_senditem * alloc_senditem(int);
+static void queue_senditem(int, const char *, int);
+static char *dequeue_senditem (int, int *);
+static void register_ip_and_sendtun (int, const char*, size_t);
+struct nstx_senditem * nstx_sendlist[16] = {
+    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+};
+uint32_t ipsByChannel[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
 static void usage(const char *prog, int code) {
     fprintf (stderr, "usage: %s [options] <domainname>\n"
@@ -156,14 +160,13 @@
     exit(0);
 }
 
-struct nstx_senditem * nstx_get_senditem(void) {
-    struct nstx_senditem *ptr = nstx_sendlist;
-
-    if (!nstx_sendlist)
+struct nstx_senditem * nstx_get_senditem(int channel) {
+    struct nstx_senditem *ptr = nstx_sendlist[channel];
+
+    if (!ptr)
         return NULL;
 
-    ptr = nstx_sendlist;
-    nstx_sendlist = nstx_sendlist->next;
+    nstx_sendlist[channel] = nstx_sendlist[channel]->next;
 
     return ptr;
 }
@@ -183,6 +186,7 @@
 }
 
 void nstx_getpacket (void) {
+    uint32_t ip;
     int len, link, channel;
     const char *name, *inbuf, *data;
     char *outbuf = NULL;
@@ -202,21 +206,34 @@
                            name);
                     if ((data = dns_fqdn2data(name)) &&
                             (inbuf = nstx_decode((unsigned char*)data, &len))) {
-                        int channel = nstx_handlepacket(inbuf, len, &sendtun);
-                        if(channel >= 0) {
+                        int channel = nstx_handlepacket(inbuf, len, &register_ip_and_sendtun);
+                        if (channel >= 0) {
                             queueitem(channel, pkt->id, name, &msg->peer);
                         }
                     }
                 }
                 dns_free(pkt);
             }
-        } else if (msg->src == FROMTUN)
-            queue_senditem(msg->data, msg->len);
-    }
-
-    for(channel = 0; channel < 16; channel++) {
+        } else if (msg->src == FROMTUN) {
+            // An IPv4 Header is at least 20 bytes long.
+            if (msg->len >= 20) {
+                // Destination Ip starts at offset 16.
+                ip = *((uint32_t *) (msg->data + 16));
+                for (channel = 0; channel < 16; channel ++) {
+                    if (ipsByChannel[channel] == ip) {
+                        break;
+                    }
+                }
+                if (channel < 16) {
+                    queue_senditem(channel, msg->data, msg->len);
+                }
+            }
+        }
+    }
+
+    for (channel = 0; channel < 16; channel++) {
         while (queuelen(channel)) {
-            if (!nstx_sendlist)
+            if (!nstx_sendlist[channel])
                 break;
             qitem = dequeueitem(channel, -1);
             pkt = dns_alloc();
@@ -224,7 +241,7 @@
             dns_settype(pkt, DNS_RESPONSE);
             link = dns_addquery(pkt, qitem->name);
             len = dns_getfreespace(pkt, DNS_RESPONSE);
-            outbuf = dequeue_senditem(&len);
+            outbuf = dequeue_senditem(channel, &len);
             dns_addanswer(pkt, outbuf, len, link);
             outbuf = (char*)dns_constructpacket(pkt, &len);
             sendns(outbuf, len, &qitem->peer);
@@ -237,11 +254,11 @@
 
 
 
-static struct nstx_senditem * alloc_senditem(void) {
-    struct nstx_senditem *ptr = nstx_sendlist;
-
-    if (!nstx_sendlist) {
-        ptr = nstx_sendlist = malloc(sizeof(struct nstx_senditem));
+static struct nstx_senditem * alloc_senditem(int channel) {
+    struct nstx_senditem *ptr = nstx_sendlist[channel];
+
+    if (!ptr) {
+        ptr = nstx_sendlist[channel] = malloc(sizeof(struct nstx_senditem));
     } else {
         while (ptr->next)
             ptr = ptr->next;
@@ -254,20 +271,20 @@
     return ptr;
 }
 
-static void queue_senditem(const char *buf, int len) {
+static void queue_senditem(int channel, const char *buf, int len) {
     static int id = 0;
     struct nstx_senditem *item;
 
-    item = alloc_senditem();
+    item = alloc_senditem(channel);
     item->data = malloc(len);
     memcpy(item->data, buf, len);
     item->len = len;
     item->id = ++id;
 }
 
-static char *dequeue_senditem (int *len) {
+static char *dequeue_senditem (int channel, int *len) {
     static char *buf;
-    struct nstx_senditem *item = nstx_sendlist;
+    struct nstx_senditem *item = nstx_sendlist[channel];
     struct nstxhdr nh;
     int remain, dlen;
 
@@ -285,7 +302,7 @@
     item->offset += dlen;
     if (item->offset == item->len) {
         nh.flags = NSTX_LF;
-        nstx_sendlist = item->next;
+        nstx_sendlist[channel] = item->next;
         free(item->data);
         free(item);
     }
@@ -293,3 +310,15 @@
 
     return buf;
 }
+
+static void register_ip_and_sendtun(int channel, const char *data, size_t len) {
+    uint32_t ip;
+    // An IPv4 Header is at least 20 bytes long.
+    if (len >= 20) {
+        // Source Ip starts at offset 12.
+        ip = *((uint32_t *) (data + 12));
+        ipsByChannel[channel] = ip;
+    }
+    sendtun(channel, data, len);
+}
+

Modified: trunk/nstxfun.h
URL: http://svn.debian.org/wsvn/nstx-ng/trunk/nstxfun.h?rev=16&op=diff
==============================================================================
--- trunk/nstxfun.h (original)
+++ trunk/nstxfun.h Wed May  6 21:46:54 2009
@@ -73,7 +73,7 @@
 void open_ns (const char *ip);
 void open_ns_bind(in_addr_t ip);
 
-void sendtun (const char*, size_t);
+void sendtun (int,const char*, size_t);
 void sendns (const char*, size_t, const struct sockaddr*);
 
 #define MAXPKT 2000




More information about the Nstx-ng-devel mailing list