From 7384d380bc69c076ad811b2409a339e49649fbce Mon Sep 17 00:00:00 2001
From: fptk <fptk@zedat.fu-berlin.de>
Date: Sun, 21 Feb 2021 11:57:55 +0100
Subject: [PATCH] working form on arty

---
 ArtyImplementation/zone1/main.c             | 28 ++-------------------
 ArtyImplementation/zone1/message_handling.c | 19 ++++++++++++++
 ArtyImplementation/zone1/message_handling.h | 21 ++++++++++++++++
 ArtyImplementation/zone1/server_utility.h   | 19 +++++---------
 ArtyImplementation/zone1/tcp_cb.c           |  6 ++---
 5 files changed, 51 insertions(+), 42 deletions(-)
 create mode 100644 ArtyImplementation/zone1/message_handling.c
 create mode 100644 ArtyImplementation/zone1/message_handling.h

diff --git a/ArtyImplementation/zone1/main.c b/ArtyImplementation/zone1/main.c
index 6362767..1470204 100644
--- a/ArtyImplementation/zone1/main.c
+++ b/ArtyImplementation/zone1/main.c
@@ -24,6 +24,7 @@
 #include "queue.h"
 
 #include "tcp_cb.h"
+#include "message_handling.h"
 
 // ----------------------------------------------------------------------------
 
@@ -59,16 +60,7 @@ time_t time(time_t *timer) {
 
 // ----------------------------------------------------------------------------
 
-#define printf2(format, args...) { /* Multi-part printf()  */\
-	char *str = malloc(64);									 \
-	if (str != NULL){										 \
-		const int len = snprintf(str, 64, format, ## args);	 \
-		for (int i=0; i<(len/16)+1; i++){					 \
-			while(!MZONE_SEND(2, (str+16*i))) MZONE_YIELD(); \
-		}													 \
-		free(str);											 \
-	}														 \
-}															 \
+
 
 static struct netif netif;
 static struct queue *rx_queue;
@@ -145,23 +137,7 @@ __attribute__((interrupt())) void plic_handler(void) { // machine external inter
 
 }
 
-static int msg_handler(const int zone, const char *msg){
-
-    // consumed messages are processed locally and not forwarded to the broker
-    int consumed = 0;
-
-    if (strcmp("ping", msg)==0){
-        MZONE_SEND(zone, "pong");
-        consumed = 1;
-
-    } else if (strcmp("broker hello", msg)==0){
-        MZONE_SEND(zone, "online");
 
-    }
-
-    return consumed;
-
-}
 
 //The flags in lwip/opts.h need to be 1, otherwise these CBs won't be called
 static void netif_link_callback(struct netif *netif) {
diff --git a/ArtyImplementation/zone1/message_handling.c b/ArtyImplementation/zone1/message_handling.c
new file mode 100644
index 0000000..11c71d4
--- /dev/null
+++ b/ArtyImplementation/zone1/message_handling.c
@@ -0,0 +1,19 @@
+#include "message_handling.h"
+
+static int msg_handler(const int zone, const char *msg) {
+
+    // consumed messages are processed locally and not forwarded to the broker
+    int consumed = 0;
+
+    if (strcmp("ping", msg)==0){
+        MZONE_SEND(zone, "pong");
+        consumed = 1;
+
+    } else if (strcmp("broker hello", msg)==0){
+        MZONE_SEND(zone, "online");
+
+    }
+
+    return consumed;
+
+}
diff --git a/ArtyImplementation/zone1/message_handling.h b/ArtyImplementation/zone1/message_handling.h
new file mode 100644
index 0000000..ee73250
--- /dev/null
+++ b/ArtyImplementation/zone1/message_handling.h
@@ -0,0 +1,21 @@
+#ifndef ZONE1_MESSAGE_HANDLING_H_
+#define ZONE1_MESSAGE_HANDLING_H_
+
+//taken from the multizone-iot-sdk source
+#include "multizone.h"
+
+#define printf2(format, args...) { /* Multi-part printf()  */\
+	char *str = malloc(64);									 \
+	if (str != NULL){										 \
+		const int len = snprintf(str, 64, format, ## args);	 \
+		for (int i=0; i<(len/16)+1; i++){					 \
+			while(!MZONE_SEND(2, (str+16*i))) MZONE_YIELD(); \
+		}													 \
+		free(str);											 \
+	}														 \
+}															 \
+
+
+static int msg_handler(const int zone, const char *msg);
+
+#endif /* ZONE1_MESSAGE_HANDLING_H_ */
diff --git a/ArtyImplementation/zone1/server_utility.h b/ArtyImplementation/zone1/server_utility.h
index 0bb35f1..52f8c58 100644
--- a/ArtyImplementation/zone1/server_utility.h
+++ b/ArtyImplementation/zone1/server_utility.h
@@ -1,15 +1,9 @@
-/*
- * server_utility.h
- *
- *  Created on: Feb 19, 2021
- *      Author: riscv
- */
-
 #ifndef SERVER_UTILITY_H_
 #define SERVER_UTILITY_H_
 
 #include "http_helpers.h"
 #include "webserver.h"
+#include "message_handling.h"
 
 #define INDEX_PAGE \
 	"<!DOCTYPE html>\n"\
@@ -44,15 +38,14 @@ int is_target(char *test) {
 	return strncmp(test, target, strlen(target)) == 0;
 }
 
-/*
- * char message[128];
-printf("Framepointer: 0x%x\n", *(uint32_t *)&message[140]);
-printf("Rücksprungadresse: 0x%x\n", *(uint32_t *)&message[140] - 204);
- */
+
 char *attack_me(char *data, uint32_t data_len) {
 	// nobody could possibly enter a name longer than 20 characters, so this buffer
 	// is definitely large enough
 	char message[128];
+	//for debugging purpose
+	printf2("Framepointer: 0x%x\n", *(uint32_t *)&message[140]);
+	printf2("Rücksprungadresse: 0x%x\n", *(uint32_t *)&message[140] - 204);
 	memset(message, 'B', sizeof(message));
 	memcpy(message, "Hallo, ", 7);
 
@@ -80,7 +73,7 @@ char *server_request_handler(char *location, enum request_type type, char *data,
 			// nobody could possibly enter a name longer than 20 characters, so this buffer
 			// is definitely large enough
 			char *return_string;
-			return_string = attack_me(data, name_len);
+			return_string = attack_me(&data[5], name_len);
 
 			char* response = http_prepare_response(return_string, strlen(return_string), 200, out_len);
 			free(return_string);
diff --git a/ArtyImplementation/zone1/tcp_cb.c b/ArtyImplementation/zone1/tcp_cb.c
index 3750cca..c3723a6 100644
--- a/ArtyImplementation/zone1/tcp_cb.c
+++ b/ArtyImplementation/zone1/tcp_cb.c
@@ -8,11 +8,11 @@
 
 
 
-#include "tcp_cb.h"
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-//for enums
+
+#include "tcp_cb.h"
 #include "server_utility.h"
 
 
@@ -94,7 +94,7 @@ static err_t tcp_cb_parse_http(struct altcp_pcb *package_control_block, struct t
 		if(free_result) {
 			free(response);
 		}
-
+		tcp_state->state = TCP_CLOSING;
 		return error_rv;
 }
 
-- 
GitLab