summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Jensen <pauljensen@google.com>2016-07-27 22:21:39 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-07-27 22:21:39 +0000
commitf80b9d17d393b75fbb41971a9ecc30b098a00d55 (patch)
tree9cf62cce29f939d017a8c6c376084509658c8e64
parentae7070075ebcbc8e262bc8897616f4b671e38d46 (diff)
parentf9491fa2795120de428ffcea3a113616b63644f3 (diff)
downloadapf-nougat-mr2-pixel-release.tar.gz
am: f9491fa279 Change-Id: I5aa3f0082621dc4da828d575c7df383e8dacc55b
-rw-r--r--Android.mk9
-rw-r--r--apf_run.c74
2 files changed, 83 insertions, 0 deletions
diff --git a/Android.mk b/Android.mk
index c32bb21..ca3bc69 100644
--- a/Android.mk
+++ b/Android.mk
@@ -27,3 +27,12 @@ LOCAL_MODULE := apf_disassembler
LOCAL_MODULE_TAGS := debug
include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_CFLAGS += $(APF_CFLAGS)
+LOCAL_SRC_FILES += apf_run.c apf_interpreter.c
+LOCAL_MODULE := apf_run
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/apf_run.c b/apf_run.c
new file mode 100644
index 0000000..32b4506
--- /dev/null
+++ b/apf_run.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Simple program to try running an APF program against a packet.
+
+#include <libgen.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "apf_interpreter.h"
+
+// Parses hex in "input". Allocates and fills "*output" with parsed bytes.
+// Returns length in bytes of "*output".
+int parse_hex(char* input, uint8_t** output) {
+ int length = strlen(input);
+ if (length & 1) {
+ fprintf(stderr, "Argument not even number of characters: %s\n", input);
+ exit(1);
+ }
+ length >>= 1;
+ *output = malloc(length);
+ if (*output == NULL) {
+ fprintf(stderr, "Out of memory, tried to allocate %d\n", length);
+ exit(1);
+ }
+ for (int i = 0; i < length; i++) {
+ char byte[3] = { input[i*2], input[i*2+1], 0 };
+ char* end_ptr;
+ (*output)[i] = strtol(byte, &end_ptr, 16);
+ if (end_ptr != byte + 2) {
+ fprintf(stderr, "Failed to parse hex %s\n", byte);
+ exit(1);
+ }
+ }
+ return length;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 4) {
+ fprintf(stderr,
+ "Usage: %s <program> <packet> <program age>\n"
+ " program: APF program, in hex\n"
+ " packet: Packet to run through program, in hex\n"
+ " program age: Age of program in seconds.\n",
+ basename(argv[0]));
+ exit(1);
+ }
+ uint8_t* program;
+ uint32_t program_len = parse_hex(argv[1], &program);
+ uint8_t* packet;
+ uint32_t packet_len = parse_hex(argv[2], &packet);
+ uint32_t filter_age = atoi(argv[3]);
+ int ret = accept_packet(program, program_len, packet, packet_len,
+ filter_age);
+ printf("Packet %sed\n", ret ? "pass" : "dropp");
+ free(program);
+ free(packet);
+ return ret;
+} \ No newline at end of file