/* * cn_user_send.c */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define GROUP 0x3 static int send_seq; static int send_cmd(int s) { static char txtmsg[] = "Ping pong"; void *m; struct cn_msg *cmsg; struct nlmsghdr *nlh; int size, err; size = NLMSG_SPACE(sizeof(struct cn_msg) + sizeof(txtmsg)); nlh = malloc(size); if (!nlh) return -ENOMEM; memset(nlh, 0, size); nlh->nlmsg_seq = send_seq++; nlh->nlmsg_pid = getpid(); nlh->nlmsg_type = NLMSG_DONE; nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(struct nlmsghdr)); nlh->nlmsg_flags = 0; cmsg = NLMSG_DATA(nlh); cmsg->id.idx = GROUP; cmsg->id.val = 0x1; cmsg->seq = nlh->nlmsg_seq; cmsg->ack = 0; cmsg->len = sizeof(txtmsg); m = (void *)(cmsg + 1); memcpy(m, (void *)txtmsg, sizeof(txtmsg)); err = send(s, nlh, size, 0); if (err == -1) { fprintf(stdout, "Failed to send: %s [%d].\n", strerror(errno), errno); } free(nlh); return err; } int main(int argc, char *argv[]) { int s; unsigned char buf[1024]; int len; send_seq = 0; struct sockaddr_nl l_local; struct cn_msg *msg; struct nlmsghdr *reply; memset(buf, 0, sizeof(buf)); s = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); if (s == -1) { perror("socket"); return -1; } l_local.nl_family = AF_NETLINK; l_local.nl_groups = GROUP; l_local.nl_pid = getpid(); if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) { perror("bind"); close(s); return -1; } int on = l_local.nl_groups; if (setsockopt(s, 270, 1, &on, sizeof(on))) { perror("setseckopt"); close(s); return -1; } send_cmd(s); close(s); return 0; }