我正在尝试使用 C 通过 sendmsg 发送原始以太网数据包。此代码成功打开一个原始数据包套接字,尝试用单个字节数组(char message[])填充 struct iovec,然后用目标地址、地址长度和指向包含的 struct iovec 的指针填充 struct msghdr消息。sendmsg() 为每次调用返回 EINVAL 但我不知道哪些参数无效。(我删除了一些 perror() 调用以使此代码更易于阅读;输出是“无效参数”。)
我找不到任何关于 sendmsg() 如何处理原始套接字的示例,但使用 sendto() 的类似代码按预期工作。在该代码中,我明确地形成了以太网帧,包括标头和协议信息,但据我所知,这对于 sendmsg() 调用来说不是必需的?我还试图让 message.iov_base 指向一个缓冲区,该缓冲区包含显式形成的以太网帧,包括 14 字节的标头,但 sendmsg() 也拒绝这样做。
sendmsg() 和 sendmmsg() 可以处理原始以太网帧吗?我是否遗漏了 iovec 使其无效的某些内容?
30 int main(void) {
32 unsigned char dest[ETH_ALEN] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11}; // destination MAC address
33
34 // Socket variables
35 int s;
36 unsigned short protocol = htons(ETH_P_802_EX1);
38
39 // Message variables
40 char message[] = {"Test …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个需要提升功能的程序(而不是简单地使用 sudo 运行它)。但是,我使用 setcap 设置的所有功能似乎都不会在执行后转移到进程中。跨多个可执行文件并使用不同的功能会发生此问题。
此代码使用 cap_set_file() 为作为 CLA 传递的文件提供 CAP_NET_RAW 功能。(不要问我为什么需要这个。)
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/capability.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define handle_error(msg) \
do { printf("%s: %s\n", msg, strerror(errno)); exit(EXIT_FAILURE); } while (0)
void print_cap_buf(cap_t cur) {
char *buf;
buf = cap_to_text(cur, NULL);
printf("%s\n", buf);
cap_free(buf);
}
void get_and_print_cap_buf() {
cap_t cur = cap_get_proc();
print_cap_buf(cur);
cap_free(cur);
}
int main(int argc, char *argv[]) {
cap_t file_cap;
printf("Process capabilities: ");
get_and_print_cap_buf(); // Print the …Run Code Online (Sandbox Code Playgroud)