首先,您可以从我以前的帖子中找到我的情况的详细信息
过去一天左右,我一直在尝试第一次尝试 netlink 套接字程序。
而且我并没有试图让它听起来好像我没有从我在互联网上找到的其他人的示例代码中获得所有这些代码,因为我们都知道我就是这样做的。
但是我一直在尝试通过代码对我的方式进行逆向工程(见结尾),经过数小时的调试,我终于让它编译没有错误。但是在尝试运行它之后,我收到消息“分段错误(核心转储)”。
现在我知道这个错误与尝试从无效的内存位置读取有关,但老实说,我不理解拼接在一起的一半代码,所以如果有人能指出我哪里出错了,我将不胜感激。
如果您能解释我在尝试通过一般的 netlink 套接字发送消息时做对/错的事情,我也将非常感谢您。例如,我会将消息放在哪里?因为我真的不知道我现在用我的代码原样发送什么消息。任何帮助表示赞赏!
#include <sys/socket.h> /* socket definitions */
#include <sys/types.h> /* socket types */
#include <linux/netlink.h> /* netlink functions */
#include <sys/uio.h> /* scatter-gather definition for iovec */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* Global constants */
#define ECHO_PORT (2002)
#define MAX_LINE (1000)
int socketHolder; // declare variable to hold Socket
// Set up configuration for destination which holds the socket to be used to communicate with the Kernel
struct sockaddr_nl dest; // declare variable to hold Socket Address
// Set up configuration for the netlink message header
struct nlmsghdr *netlinkHeader; // declare variable to hold message to be sent
// Set up configuration for the scatter-gather function for expediting writing to buffer
struct iovec iov;
// Set up configuration for the message header for simplifying message parameters to be sent
struct msghdr message;
int main()
{
memset(&dest, 0, sizeof(dest)); // set socket Address to all 0
dest.nl_family = AF_NETLINK; // set socket family to AF_NETLINK
dest.nl_pad = 0; // defaults to 0
dest.nl_pid = 0; // set address of netlink socket to 0 because we're going to the kernel
dest.nl_groups = 0; // set to 0 because we're doing unicast
netlinkHeader->nlmsg_pid = 0; // set sender port number to 0 because we're in kernel
iov.iov_base = (caddr_t)netlinkHeader; // set address of buffer
iov.iov_len = netlinkHeader->nlmsg_len; // set length of buffer
message.msg_name = (caddr_t) &dest; // set socket name
message.msg_namelen = sizeof(dest); // set length of name
message.msg_iov = &iov; // I have no idea what this is
message.msg_iovlen = 1; // number of iov blocks
message.msg_control = NULL; // ignore
message.msg_controllen = 0; // ignore
message.msg_flags = 0; //ignore
socketHolder = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); // declare Socket and assign to holder variable
bind(socketHolder, (struct sockaddr *) &dest, sizeof(dest)); // bind the destination settings to socketHolder
int sender = sendmsg(socketHolder, &message, 0); // send the message
}