目标:获取特定类型连接(我实际使用的连接)的信息(信号强度)。
问题:我不确定到底什么是 reperesentCellInfo以及如何从中提取信息getAllCellInfo
问题:
1)来自android开发者网站
CellInfo:来自某个时间点的不可变单元格信息。
这是什么意思?
2) 我想了解我使用的是 wcdma、umts、gsm 还是 lte。我在这里找到了一段使用getAllCellInfo的代码,低于我的改编版本以获得信号强度
for (final CellInfo info : telephonyManager.getAllCellInfo()) {
if (info instanceof CellInfoGsm) {
final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
if(gsm.getLevel() >= localMaxSignalStrength)
localMaxSignalStrength = gsm.getLevel();
Toast.makeText(getApplicationContext(), "gsm rssi" + localMaxSignalStrength, Toast.LENGTH_LONG).show();
} else if (info instanceof CellInfoWcdma) {
final CellSignalStrengthWcdma cdma = ((CellInfoWcdma) info).getCellSignalStrength();
if(cdma.getLevel() >= localMaxSignalStrength)
localMaxSignalStrength = cdma.getLevel();
Toast.makeText(getApplicationContext(), "wcdma rssi" + localMaxSignalStrength, Toast.LENGTH_LONG).show();
} else if (info instanceof …Run Code Online (Sandbox Code Playgroud) 我正在寻找 eBPF 的示例来编写 seccomp 过滤器,但我找不到。有人能告诉我是否可以使用 eBPF 编写 seccomp 过滤器吗?
由于现在 ebpf 程序中允许有界循环https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git/commit/?id=2589726d12a1b12eaaa93c7f1ea64287e383c7a5 验证程序是否仍然检查如果程序控制流是有向无环图,则第一遍?
来自 bpf 手册页:
eBPF 程序可以用受限制的 C 语言编写,并编译(使用 clang 编译器)为 eBPF 字节码。此受限 C 语言省略了各种功能,例如循环、全局变量、可变参数函数、浮点数以及将结构体作为函数参数传递。
AFAIK 手册页尚未更新。我想知道使用受限C编写eBPF程序时到底禁止什么?手册页所说的仍然正确吗?
阅读一些附加到跟踪点的ebpf示例后,我注意到每个结构都是从这样的填充开始构建的(来自samples/bpf/xdp_redirect_cpu_kern.c)
/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format
* Code in: kernel/include/trace/events/xdp.h
*/
struct cpumap_enqueue_ctx {
u64 __pad; // First 8 bytes are not accessible by bpf code
int map_id; // offset:8; size:4; signed:1;
u32 act; // offset:12; size:4; signed:0;
int cpu; // offset:16; size:4; signed:1;
unsigned int drops; // offset:20; size:4; signed:0;
unsigned int processed; // offset:24; size:4; signed:0;
int to_cpu; // offset:28; size:4; signed:1;
};
Run Code Online (Sandbox Code Playgroud)
我发现的所有评论都是bpf代码无法访问前8个字节,但我不明白为什么。
当我尝试执行sudo ./mineonlyret加载 ebpf 程序的用户空间程序时,我在 ubuntu 19.04 上收到此错误消息,稍后对此进行描述。我在 ubuntu 18.04 上尝试了相同的配置,并且没有错误。导致此错误的原因可能是什么?\n如果您需要更多详细信息,请告诉我。
libbpf: Error loading ELF section .BTF: 0.\nRun Code Online (Sandbox Code Playgroud)\n\nmineonlyret_user.c
\n\n// SPDX-License-Identifier: GPL-2.0\n#include <stdio.h>\n#include <assert.h>\n#include <linux/bpf.h>\n#include "libbpf.h"\n#include <unistd.h>\n#include <arpa/inet.h>\n#include <linux/if_ether.h>\n\nint main(int ac, char **argv)\n{\n int sock, prog_fd;\n struct bpf_object *obj;\n char filename[256];\n\n if (bpf_prog_load("mineonlyret_kern.o", BPF_PROG_TYPE_SOCKET_FILTER,\n &obj, &prog_fd))\n return 1;\n\n /* open up a packet socket */\n sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); \n if(sock < 0){\n printf("socket");\n return -1;\n }\n\n /* attach the filter */\n assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, …Run Code Online (Sandbox Code Playgroud) 我已经看到使用此宏的返回值,但是从定义上我不能理解执行返回的值。
// packet parsing state machine helpers
#define cursor_advance(_cursor, _len) \
({ void *_tmp = _cursor; _cursor += _len; _tmp; })
Run Code Online (Sandbox Code Playgroud) 问题: \n我正在尝试使用 seccomp,但我无法理解为什么 gcc 告诉我 seccomp() 函数调用有一个隐式声明。
\n\n#define _GNU_SOURCE\n#include <stddef.h> // offsetof\n#include <stdio.h>\n#include <stdlib.h>\n#include <unistd.h>\n#include <linux/audit.h> // arch\n#include <linux/filter.h>\n#include <linux/seccomp.h>\n#include <sys/prctl.h>\n#include <sys/syscall.h> // syscall numbers\n\nstruct sock_filter bpfcode[] = {\n\n /* validate the architecture */\n BPF_STMT(BPF_LD+BPF_W+BPF_ABS, (offsetof(struct seccomp_data, arch))),\n BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, AUDIT_ARCH_X86_64, 0, 7),\n /* load syscall number in the accumulator */\n BPF_STMT(BPF_LD+BPF_W+BPF_ABS, (offsetof (struct seccomp_data, nr))),\n /* check if the syscall number is allowed */\n BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_nanosleep, 5, 0), // for sleep\n BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_exit, 4, 0),\n BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_exit_group, 3, 0),\n …Run Code Online (Sandbox Code Playgroud) 我已经看到,对于每种类型的 ebpf 程序,该程序都有不同的输入(上下文)。例如,在 BPF_PROG_TYPE_SOCKET_FILTER 程序中,指向 struct __sk_buff 的指针作为参数传递。每个程序类型的上下文在哪里定义?
当我尝试执行时,sudo opensnoop-bpfcc我收到此消息:
In file included from /virtual/main.c:4:
In file included from include/linux/sched.h:14:
In file included from include/linux/pid.h:5:
In file included from include/linux/rculist.h:11:
In file included from include/linux/rcupdate.h:40:
In file included from include/linux/preempt.h:81:
In file included from ./arch/x86/include/asm/preempt.h:7:
In file included from include/linux/thread_info.h:38:
In file included from ./arch/x86/include/asm/thread_info.h:53:
./arch/x86/include/asm/cpufeature.h:150:2: warning: "Compiler lacks ASM_GOTO support. Add -D __BPF_TRACING__ to your compiler arguments"
[-W#warnings]
#warning "Compiler lacks ASM_GOTO support. Add -D __BPF_TRACING__ to your compiler arguments"
^
1 warning generated.
Traceback …Run Code Online (Sandbox Code Playgroud) int main(int argc, char *argv[])
{
struct sockaddr_in src = { .sin_family=AF_INET, .sin_addr.s_addr=INADDR_ANY, .sin_port=htons(90) };
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
bind(fd, (struct sockaddr *)&src, sizeof(src));
char buf[1024];
ssize_t res = recvfrom(fd, buf, sizeof(buf), 0, NULL, 0);
printf("res=%zi\n", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并执行此程序之后。在另一个终端上执行
nc -u localhost 90
Run Code Online (Sandbox Code Playgroud)
测试自从我使用INADDR_ANY以来,我是否确实从“任何接口”接收到一些udp流量。但是程序只是挂起。我想念什么?