Chinaunix首页 | 论坛 | 博客
  • 博客访问: 492743
  • 博文数量: 63
  • 博客积分: 1187
  • 博客等级: 少尉
  • 技术积分: 706
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-05 16:53
个人简介

Must Be

文章分类

全部博文(63)

文章存档

2019年(1)

2017年(4)

2016年(6)

2015年(2)

2014年(1)

2013年(3)

2012年(10)

2011年(36)

我的朋友

分类: 嵌入式

2012-04-11 09:30:34

Netlink套接字内核广播原理程序 调试需改动

点击(此处)折叠或打开

  1. multicast_app
  2. ---------------------------
  3. #include <asm/types.h>
  4. #include <sys/socket.h>
  5. #include <linux/netlink.h>

  6. #define MAX_PAYLOAD 1024 /* maximum payload size*/
  7. #define NETLINK_TEST 17

  8. struct sockaddr_nl src_addr, dest_addr;
  9. struct nlmsghdr *nlh = NULL;
  10. struct iovec iov;
  11. struct msghdr msg;
  12. int sock_fd;

  13. void main() {
  14. sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_TEST);
  15. memset(&src_addr, 0, sizeof(src_addr));
  16. src_addr.nl_family = AF_NETLINK;
  17. src_addr.nl_pid = getpid(); /* self pid */
  18. /* interested in group 1<<0 */
  19. src_addr.nl_groups = 1;
  20. bind(sock_fd, (struct sockaddr*)&src_addr,
  21. sizeof(src_addr));
  22. memset(&dest_addr, 0, sizeof(dest_addr));
  23. nlh = (struct nlmsghdr *)malloc(
  24. NLMSG_SPACE(MAX_PAYLOAD));
  25. memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));

  26. iov.iov_base = (void *)nlh;
  27. iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
  28. msg.msg_name = (void *)&dest_addr;
  29. msg.msg_namelen = sizeof(dest_addr);
  30. msg.msg_iov = &iov;
  31. msg.msg_iovlen = 1;
  32. printf("Waiting for message from kernel\n");
  33. /* Read message from kernel */
  34. recvmsg(sock_fd, &msg, 0);
  35. printf(" Received message payload: %s\n",
  36. NLMSG_DATA(nlh));
  37. close(sock_fd);
  38. }

  39. multicast_knl.c
  40. --------------------------
  41. #ifndef __KERNEL__
  42. #define __KERNEL__
  43. #endif

  44. #ifndef MODULE
  45. #define MODULE
  46. #endif

  47. #include <linux/string.h>
  48. #include <linux/stat.h>

  49. #include <linux/module.h>
  50. #include <linux/version.h>
  51. #include <linux/socket.h>
  52. #include <linux/types.h>

  53. #include <linux/capability.h>
  54. #include <linux/errno.h>
  55. #include <linux/stat.h>

  56. #include <linux/skbuff.h>
  57. #include <linux/netlink.h>
  58. #include <net/sock.h>

  59. #define NETLINK_TEST 17
  60. #define MAX_PAYLOAD 1024

  61. struct sock *nl_sk = NULL;

  62. static void nl_data_ready (struct sock *sk, int len)
  63. {
  64. wake_up_interruptible(sk->sleep);
  65. }

  66. void netlink_test()
  67. {
  68. struct sk_buff *skb = NULL;
  69. struct nlmsghdr *nlh;
  70. int err;
  71. nl_sk = netlink_kernel_create(NETLINK_TEST,
  72. nl_data_ready);
  73. skb=alloc_skb(NLMSG_SPACE(MAX_PAYLOAD),GFP_KERNEL);
  74. memset(skb->data,0,NLMSG_SPACE(MAX_PAYLOAD));
  75. nlh = (struct nlmsghdr *)skb->data;
  76. skb_put(skb,sizeof(struct nlmsghdr));
  77. nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
  78. nlh->nlmsg_pid = 0; /* from kernel */
  79. nlh->nlmsg_flags = 0;

  80. skb_put(skb,strlen("Greeting from kernel!"));
  81. strcpy(NLMSG_DATA(nlh), "Greeting from kernel!");
  82. nlh->nlmsg_len = skb->len;
  83. /* sender is in group 1<<0 */
  84. NETLINK_CB(skb).groups = 1;
  85. NETLINK_CB(skb).pid = 0; /* from kernel */
  86. NETLINK_CB(skb).dst_pid = 0; /* multicast */
  87. /* to mcast group 1<<0 */
  88. NETLINK_CB(skb).dst_groups = 1;
  89. /*multicast the message to all listening processes*/
  90. netlink_broadcast(nl_sk, skb, 0, 1, GFP_KERNEL);
  91. sock_release(nl_sk->socket);
  92. }

  93. static int __init my_module_init(void)
  94. {
  95. printk(KERN_INFO "Initializing Netlink Socket");
  96. netlink_test();
  97. return 0;
  98. }

  99. static void __exit my_module_exit(void)
  100. {
  101. printk(KERN_INFO "Goodbye");
  102. }

  103. module_init(my_module_init);
  104. module_exit(my_module_exit);

阅读(2547) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~