diff options
author | Paul Jensen <pauljensen@google.com> | 2016-07-27 07:48:40 -0400 |
---|---|---|
committer | Paul Jensen <pauljensen@google.com> | 2016-07-27 08:54:24 -0400 |
commit | f9491fa2795120de428ffcea3a113616b63644f3 (patch) | |
tree | 9cf62cce29f939d017a8c6c376084509658c8e64 /apf_run.c | |
parent | ae7070075ebcbc8e262bc8897616f4b671e38d46 (diff) | |
download | apf-f9491fa2795120de428ffcea3a113616b63644f3.tar.gz |
Add host executable to try running an APF program against a packet.android-cts_7.1_r1android-cts-7.1_r9android-cts-7.1_r8android-cts-7.1_r7android-cts-7.1_r6android-cts-7.1_r5android-cts-7.1_r4android-cts-7.1_r3android-cts-7.1_r29android-cts-7.1_r28android-cts-7.1_r27android-cts-7.1_r26android-cts-7.1_r25android-cts-7.1_r24android-cts-7.1_r23android-cts-7.1_r22android-cts-7.1_r21android-cts-7.1_r20android-cts-7.1_r2android-cts-7.1_r19android-cts-7.1_r18android-cts-7.1_r17android-cts-7.1_r16android-cts-7.1_r15android-cts-7.1_r14android-cts-7.1_r13android-cts-7.1_r12android-cts-7.1_r11android-cts-7.1_r10android-cts-7.1_r1android-7.1.1_r9android-7.1.1_r8android-7.1.1_r7android-7.1.1_r61android-7.1.1_r60android-7.1.1_r6android-7.1.1_r59android-7.1.1_r58android-7.1.1_r57android-7.1.1_r56android-7.1.1_r55android-7.1.1_r54android-7.1.1_r53android-7.1.1_r52android-7.1.1_r51android-7.1.1_r50android-7.1.1_r49android-7.1.1_r48android-7.1.1_r47android-7.1.1_r46android-7.1.1_r45android-7.1.1_r44android-7.1.1_r43android-7.1.1_r42android-7.1.1_r41android-7.1.1_r40android-7.1.1_r4android-7.1.1_r39android-7.1.1_r38android-7.1.1_r35android-7.1.1_r33android-7.1.1_r32android-7.1.1_r31android-7.1.1_r3android-7.1.1_r28android-7.1.1_r27android-7.1.1_r26android-7.1.1_r25android-7.1.1_r24android-7.1.1_r23android-7.1.1_r22android-7.1.1_r21android-7.1.1_r20android-7.1.1_r2android-7.1.1_r17android-7.1.1_r16android-7.1.1_r15android-7.1.1_r14android-7.1.1_r13android-7.1.1_r12android-7.1.1_r11android-7.1.1_r10android-7.1.1_r1android-7.1.0_r7android-7.1.0_r6android-7.1.0_r5android-7.1.0_r4android-7.1.0_r3android-7.1.0_r2android-7.1.0_r1nougat-mr1.8-releasenougat-mr1.7-releasenougat-mr1.6-releasenougat-mr1.5-releasenougat-mr1.4-releasenougat-mr1.3-releasenougat-mr1.2-releasenougat-mr1.1-releasenougat-mr1-volantis-releasenougat-mr1-security-releasenougat-mr1-releasenougat-mr1-flounder-releasenougat-mr1-devnougat-mr1-cts-releasenougat-dr1-release
This program is just for debugging purposes and isn't shipped.
Bug: 30138831
Change-Id: I8b943c1333db4addb729830f7e15e32653fe7ca4
Diffstat (limited to 'apf_run.c')
-rw-r--r-- | apf_run.c | 74 |
1 files changed, 74 insertions, 0 deletions
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 |