Problem in Networking at Kernel Level
- From: "Prafulla T" <prafulla.tekawade@xxxxxxxxx>
- Date: 26 Dec 2006 21:12:27 -0800
Hello to everyone,
I am trying to write network-related code in Linux kernel
Moduile,Actually It is a client which makes connection with the Server
which is running in user space.
Though I can compile the code successfully,It gives the problem while
insmod'ing the module.
The code for it is as follows.
I've got it from http://www.linuxjournal.com/article/7660
Please help.
Thanks in advance..
#include <linux/kernel.h>
#include <linux/module.h>
/*For Network Code*/
#include <linux/net.h>
#include <net/sock.h>
/*Data Segment registers and setting */
#include <asm/uaccess.h>
/*For kmalloc and related stuff*/
#include <linux/slab.h>
MODULE_AUTHOR("Prafulla Tekawade(prafulla.tekawade@xxxxxxxxx)");
MODULE_DESCRIPTION("Just sends some data to some User Process throught
network,THIS IS A CLIENT");
MODULE_LICENSE("GPL");
#define SENDBUF 1024
#define RECVBUF 1024
static int sendData(struct socket *sock,char *data)
{
struct msghdr msg;
struct iovec iov;
int left=strlen(data),written=0;
int len;
mm_segment_t oldmm;
msg.msg_name=0;
msg.msg_namelen=0;
msg.msg_iov=&iov;
msg.msg_iovlen=1;
msg.msg_control=NULL;
msg.msg_controllen=0;
msg.msg_flags=MSG_DONTWAIT;
oldmm = get_fs(); //set_fs(KERNEL_DS);
msg.msg_iov->iov_len = left;
msg.msg_iov->iov_base = (char *) data + written;
len = sock_sendmsg(sock, &msg, left);
if(len==-1)
{
set_fs(oldmm);
printk("Could not send any data");
return -1;
}
return written ? written:len;
}
static void initialize()
{
struct sockaddr_in saddr,daddr; //Source & Destination address
struct socket *sock_data;
char *response=kmalloc(SENDBUF,GFP_KERNEL);
char *reply=kmalloc(RECVBUF,GFP_KERNEL);
int r=-1; //Response from the functions
/*Now create socket*/
r=sock_create(PF_INET,SOCK_STREAM,IPPROTO_TCP,&sock_data);
if(r==-1)
{
printk("Could not create socket");
}
memset(&saddr,0,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(5000);
saddr.sin_addr.s_addr=htonl(create_address(127,0,0,1));
r=sock_data->ops->connect(sock_data,(struct sockaddr *
)&saddr,sizeof(saddr),O_RDWR);
if(r==-1)
{
printk("Could not connect");
return;
}
sendData(sock_data,"Hi");
}
static void doOperation()
{
initialize();
}
static int init_module(void)
{
printk("Module Inserted");
doOperation();
}
static void clean_up_module(void)
{
}
//module_init(init_module);
module_exit(clean_up_module);
.
- Follow-Ups:
- Re: Problem in Networking at Kernel Level
- From: Geronimo W. Christ Esq
- Re: Problem in Networking at Kernel Level
- Prev by Date: Re: Create TCP syn packet with given seq num and few other TCP parameters
- Next by Date: Re: email account issue Linux pop3 mail server
- Previous by thread: pick up your free gift here
- Next by thread: Re: Problem in Networking at Kernel Level
- Index(es):
Relevant Pages
|