Networking Forums

Networking Forums > Computer Networking > Linux Networking > Linux kernel socket programming

Reply
Thread Tools Display Modes

Linux kernel socket programming

 
 
Shuang Liang
Guest
Posts: n/a

 
      04-06-2005, 06:50 AM
Hi,
I am trying to figure out how to write socket program in kernel mode.
From the kernel source of 2.4.20-8, I figured the following steps, but it
seems it does not work(the receiver is a user-space program, just listen and
recv udp packages. what happened is the receiver got nothing, while no error
msg at sender). I have tried to play with the same sender program in user
space, which works. So the problem seems to be incorrect use of socket
creation and msg send call. Could sb point a link for a working examples, or
nicely point out my mistake?

Thanks a lot!
Shuang,

struct socket * sock;
struct sockaddr_in serv_addr;

struct msghdr msg_header;
struct iovec msg_iov;

unsigned char send_buf[SIZE];

res.bytes[0] = 192;
res.bytes[1] = 168;
res.bytes[2] = 102;
res.bytes[3] = 1;

memset (send_buf, 255, SIZE);

memset (&serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = res.word|htonl(0);
serv_addr.sin_port = htons (PORT);


printk ("<1>Hello, world\n");
printk ("The process is %s (pid %i)\n", (char *) current->comm,
current->pid);
printk ("s_addr = %u", serv_addr.sin_addr.s_addr);

if (sock_create(AF_INET, SOCK_DGRAM, 0,&sock)<0)
{
printk(" Error Creating socket\n");
return -1;
}

if (sock_map_fd(sock) < 0)
{
printk(" Error mapping socket\n");
return -1;
}


sock->sk->allocation = GFP_NOIO;
msg_iov.iov_base = send_buf;
msg_iov.iov_len = 8;
msg_header.msg_name = (struct sockaddr*) &serv_addr;
msg_header.msg_namelen = sizeof (serv_addr);
msg_header.msg_iov = &msg_iov;
msg_header.msg_iovlen = 1;
msg_header.msg_control = NULL;
msg_header.msg_controllen = 0;

if (sock_sendmsg(sock, &msg_header, 8) < 0){
printk("Request send Error, \n");
}
printk(" Message sent\n");

return 0;


 
Reply With Quote
 
 
 
 
Tauno Voipio
Guest
Posts: n/a

 
      04-06-2005, 07:25 AM
Shuang Liang wrote:
> Hi,
> I am trying to figure out how to write socket program in kernel mode.
> From the kernel source of 2.4.20-8, I figured the following steps, but it
> seems it does not work(the receiver is a user-space program, just listen and
> recv udp packages. what happened is the receiver got nothing, while no error
> msg at sender). I have tried to play with the same sender program in user
> space, which works. So the problem seems to be incorrect use of socket
> creation and msg send call. Could sb point a link for a working examples, or
> nicely point out my mistake?
>


Why are you trying to use sockets in kernel mode?

They are intended to be used from user mode.

You should not use kernel mode for something that
can be handled in user mode - maybe using a
suitable daemon.

--

Tauno Voipio
tauno voipio (at) iki fi

 
Reply With Quote
 
Shuang Liang
Guest
Posts: n/a

 
      04-07-2005, 12:38 AM

"Tauno Voipio" <(E-Mail Removed)> wrote in message
news:u9M4e.63$(E-Mail Removed)...
> Shuang Liang wrote:
>> Hi,
>> I am trying to figure out how to write socket program in kernel mode.
>> From the kernel source of 2.4.20-8, I figured the following steps, but it
>> seems it does not work(the receiver is a user-space program, just listen
>> and recv udp packages. what happened is the receiver got nothing, while
>> no error msg at sender). I have tried to play with the same sender
>> program in user space, which works. So the problem seems to be incorrect
>> use of socket creation and msg send call. Could sb point a link for a
>> working examples, or nicely point out my mistake?
>>

>
> Why are you trying to use sockets in kernel mode?
>

Since I am trying to write a driver which needs network access. So I need to
figure out how to do that.

> They are intended to be used from user mode.
>
> You should not use kernel mode for something that
> can be handled in user mode - maybe using a
> suitable daemon.
>

I can do it in user space, but I will need to move data in and out of kernel
space. So I appreciate somebody can give me a hand.
> --
>
> Tauno Voipio
> tauno voipio (at) iki fi
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Commercial] Become an Expert in Linux Kernel Programming Concepts Systems Linux Networking 0 05-21-2008 11:45 AM
[Commercial] Linux System and Kernel Programming: Hands-On Trainingfrom Experts Concepts Systems Linux Networking 0 03-31-2008 06:23 AM
linux socket programming and HTTP Protocol Problem PGHULME Linux Networking 1 08-21-2006 09:35 PM
Help for socket programming as kernel module sank Linux Networking 1 02-05-2004 04:32 PM
linux socket programming sank Linux Networking 2 01-02-2004 05:36 PM



1 2 3 4 5 6 7 8 9 10 11