]> git.sur5r.net Git - ngadmin/commitdiff
Added basic packet dumper.
authordarkcoven <admin@darkcoven.tk>
Mon, 1 Apr 2013 21:11:51 +0000 (23:11 +0200)
committerdarkcoven <admin@darkcoven.tk>
Mon, 1 Apr 2013 21:11:51 +0000 (23:11 +0200)
.gitignore
Makefile
dump/Makefile [new file with mode: 0644]
dump/dump.c [new file with mode: 0644]

index 561952841617d47f6a5f3e44b8fe05a7492da4fc..4d338344e54c7005b4c8f5976761d27fd5dc9639 100644 (file)
@@ -3,7 +3,7 @@
 *.so
 *.a
 admin
-admind
+ngdump
 lib/doc/
 raw/doc/
 brouillon
index 8af2203a994c3713a14b4a1078c5c61111b01fd7..dacd00d4c75c86ac2b51fe56e6dc5b1d769a94e3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,9 @@ cli: lib force
        @+$(MAKE) -C cli
 
 
+dump: raw force
+       @+$(MAKE) -C dump
+
 
 force: 
        @true
@@ -29,10 +32,12 @@ clean:
        @+$(MAKE) -C raw clean
        @+$(MAKE) -C lib clean
        @+$(MAKE) -C cli clean
+       @+$(MAKE) -C dump clean
 
 mrproper: clean
        @+$(MAKE) -C raw mrproper
        @+$(MAKE) -C lib mrproper
        @+$(MAKE) -C cli mrproper
+       @+$(MAKE) -C dump mrproper
 
 
diff --git a/dump/Makefile b/dump/Makefile
new file mode 100644 (file)
index 0000000..9d347c1
--- /dev/null
@@ -0,0 +1,34 @@
+
+CC=gcc
+CFLAGS=-I../raw/include/ -W -Wall -Wextra -Os
+LDFLAGS=-L../raw -lrawnsdp
+EXEC=ngdump
+
+SRC=$(wildcard *.c)
+OBJ=$(SRC:.c=.o)
+
+
+ifeq ($(DEBUG), yes)
+CFLAGS+=-g
+LDFLAGS+=-g
+else
+CFLAGS+=-fomit-frame-pointer
+LDFLAGS+=-s
+endif
+
+
+$(EXEC): $(OBJ)
+       $(CC) $^ -o $@ $(LDFLAGS)
+
+%.o: %.c
+       $(CC) -c $^ -o $@ $(CFLAGS)
+
+
+clean:
+       @rm -f *.o
+
+mrproper: clean
+       @rm -f $(EXEC)
+
+
+
diff --git a/dump/dump.c b/dump/dump.c
new file mode 100644 (file)
index 0000000..d57fc82
--- /dev/null
@@ -0,0 +1,85 @@
+
+#include <stdio.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#include <protocol.h>
+#include <attr.h>
+
+
+int main (void)
+{
+       char buffer[1500];
+       struct ng_packet np;
+       int err = 0, s, len;
+       struct sockaddr_in local, remote;
+       socklen_t slen = sizeof(struct sockaddr_in);
+       unsigned char error;
+       unsigned short attr_error;
+       List *attr;
+       ListNode *ln;
+       struct attr *at;
+       
+       
+       s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+       if (s < 0) {
+               perror("socket");
+               err = 1;
+               goto end;
+       };
+       
+       
+       memset(&local, 0, sizeof(struct sockaddr_in));
+       local.sin_family = AF_INET;
+       local.sin_addr.s_addr = htonl(INADDR_ANY);
+       local.sin_port = htons(SWITCH_PORT);
+       
+       if (bind(s, (struct sockaddr*)&local, sizeof(struct sockaddr_in)) < 0) {
+               perror("bind");
+               err = 2;
+               goto end;
+       }
+       
+       while (1) {
+               
+               len = recvfrom(s, buffer, sizeof(buffer), 0, (struct sockaddr*)&remote, &slen);
+               if (len < 0) {
+                       perror("recvfrom");
+                       err = 3;
+                       goto end;
+               }
+               
+               printf("---------------------------------\n");
+               
+               np.buffer = buffer;
+               np.maxlen = len;
+               initNgPacket(&np);
+               
+               attr = createEmptyList();
+               
+                       if (ntohs(remote.sin_port) != CLIENT_PORT ||
+                   len < (int)sizeof(struct ng_header) ||
+                   !validateNgHeader(np.nh, 0, NULL, NULL, 0) ||
+                   extractPacketAttributes(&np, &error, &attr_error, attr, ATTR_END, 0) < 0) {
+                       printf("wrong packet\n");
+                       goto end;
+               }
+               
+               printf("received %d attribute(s)\n", attr->count);
+               
+               for (ln = attr->first; ln != NULL; ln = ln->next) {
+                       at = ln->data;
+                       printf("received attribute code = %04X, length = %d\n", at->attr, at->size);
+               }
+               
+               destroyList(attr, (void(*)(void*))freeAttr);
+               
+               printf("---------------------------------\n\n");
+       }
+       
+       close(s);
+       
+end:
+       return err;
+}
+