LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2009, 11:48 AM   #1
avyadavnitw
LQ Newbie
 
Registered: Apr 2009
Posts: 16

Rep: Reputation: 0
Facing problem while using netlink sockets


*kernel space send application*/
#define MAX_PAYLOAD 1024
struct sock *nl_sk = NULL;
sturct sk_buff *skb = NULL;
struct nlmsghdr *nlh;
int err;
netlink_test()
{
nl_sk = netlink_kernel_create(NULL,NETLINK_TEST,
nl_data_ready,1,1);
if(nl_sk ==NULL)
printk("netlink create fail\n");
else
printk("netlink create pass\n");

skb=alloc_skb(NLMSG_SPACE(MAX_PAYLOAD),GFP_KERNEL);
if(skb == NULL)
printk("skb alloc fail\n");
else
printk("skb alloc pass\n");

nlh = (struct nlmsghdr *)skb->data;
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = 0;
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), "Msg from kernel!");

NETLINK_CB(skb).pid = 0; /* from kernel */
NETLINK_CB(skb).dst_groups = 0;

/*multicast the message to all listening processes*/
err = netlink_unicast(nl_sk, skb, 0,GFP_ATOMIC);
if(err < 0)
printk("netlink unicast fail\n");
else
printk("netlink unicast pass\n");
printk("message sent\n");
}
}
/*user space Receive application*/
#include <sys/socket.h>
#include <linux/netlink.h>

#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;

void main()
{
memset(&src_addr, 0, sizeof(src_addr));
src__addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
src_addr.nl_groups = 0; /* not in mcast groups */
bind(sock_fd, (struct sockaddr*)&src_addr,
sizeof(src_addr));

memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */

nlh=(struct nlmsghdr *)malloc(
NLMSG_SPACE(MAX_PAYLOAD));

iov.iov_base = (void *)nlh;
iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;

printf("Waiting for message from kernel\n");

/* Read message from kernel */
recvmsg(fd, &msg, 0);
printf(" Received message payload: %s\n",
NLMSG_DATA(nlh));
close(sock_fd);
}


when i insert the user space application
./userapp &

waiting for message from kernel

when i insert the kernel module
insmod kermod.ko

netlink create pass
skb alloc pass
netlink unicast pass
message sent

the kernel snippet i pasted is only part of my module code?

the print messages i am getting is all pass but i am not able to get message in the user space ?
Can anyone tell me what is the problem?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem w/ rmmod from netlink sockets ranthal Linux - Kernel 1 06-25-2009 05:58 PM
Generic Netlink sockets krishna123 Linux - Networking 0 03-04-2009 01:30 AM
A question about netlink sockets alfrag Linux - Newbie 1 12-26-2008 08:48 AM
Netlink Sockets Problem gio123 Programming 3 01-23-2005 06:17 PM
Adding Routes using Netlink Sockets mpls_linux Linux - Networking 0 05-22-2004 09:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration