diff options
-rw-r--r-- | Android.mk | 9 | ||||
-rw-r--r-- | apf_run.c | 74 |
2 files changed, 83 insertions, 0 deletions
@@ -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 |