Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > The Linux Kernel > kernel sockets

Forgot Password?
 The Linux Kernel   Compiling, theory, programming or other discussion about the linux kernel

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 02-21-2006   #1 (permalink)
Just Joined!
 
Join Date: Feb 2006
Posts: 3
kernel sockets

Hello,
I want to implement sockets in the kernel 2.6(client and server both).How will they be handled in the kernel?
bpankaj_pict is offline  


Reply With Quote
Old 02-21-2006   #2 (permalink)
Linux Guru
 
kkubasik's Avatar
 
Join Date: Mar 2004
Location: Lat: 39:03:51N Lon: 77:14:37W
Posts: 2,397
Send a message via AIM to kkubasik
Why exactly are you trying to do this in kernel space? its seems more likely you just want raw socket access, which the kernel will give you in userspace.

Check the tutorial here for some help.
http://librenix.com/?inode=3044
__________________
Avoid the Gates of Hell. Use Linux
A Penny for your Thoughts

Formerly Known as qub333
kkubasik is offline   Reply With Quote
Old 02-24-2006   #3 (permalink)
Just Joined!
 
Join Date: Apr 2005
Location: Romania
Posts: 40
Send a message via Yahoo to cbogdan
Hi,

Writing applications in the kernel is different. First, take a look at Greg Kroah-Harman's article "Driving Me Nuts - Things You Never Should Do in the Kernel"
(http://www.linuxjournal.com/article/8110).

Then, another great article - about network/socket programming in the kernel:
http://www.linuxjournal.com/article/7660
cbogdan is offline   Reply With Quote
Old 04-30-2006   #4 (permalink)
Just Joined!
 
Join Date: Apr 2006
Posts: 1
Code to Implement Sockets in Kernel 2.6

Hi,

I have implemented sockets in kernel 2.6 and made simple functions like userspace. (I made for TCP but I think UDP can also be done in a similar manner)

Here is the complete code. I think the function names are self explanatory. First to use sockets, you have to create a struct socket objects.
For a server, use set_up_server_socket, followed by server_accept connection and for the client use set_up_client_socket. To send and recieve messages use the SendBuffer and RecvBuffer Functions.


/*
Sendbuffer sends "Length" bytes from "Buffer" through the socket "sock".


*/

size_t SendBuffer(struct socket *sock, const char *Buffer, size_t Length)
{
struct msghdr msg;
mm_segment_t oldfs; // mm_segment_t is just a long
struct iovec iov; // structure containing a base addr. and length
int len2;

//printk("Entering SendBuffer\n");


msg.msg_name = 0;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1; //point to be noted
msg.msg_control = NULL;
msg.msg_controllen = 0;

msg.msg_flags = MSG_NOSIGNAL;//0/*MSG_DONTWAIT*/;

iov.iov_base = (char*) Buffer; // as we know that iovec is
iov.iov_len = (__kernel_size_t) Length; // nothing but a base addr and length



// #define get_fs() (current_thread_info()->addr_limit)
// similar for set_fs;
/*
Therefore this line sets the "fs" to KERNEL_DS and saves its old value
*/
oldfs = get_fs(); set_fs(KERNEL_DS);

/* Actual Sending of the Message */
len2 = sock_sendmsg(sock,&msg,(size_t)(Length));

/* retrieve the old value of fs (whatever it is)*/
set_fs(oldfs);


return len2;
}


/*
Recieves data from the socket "sock" and puts it in the 'Buffer'.
Returns the length of data recieved

The Calling function must do a:
Buffer = (char*) get_free_page(GFP_KERNEL);
or a kmalloc to allocate kernel's memory
(or it can use the kernel's stack space [very small] )

*/


size_t RecvBuffer(struct socket *sock, const char *Buffer, size_t Length)
{
struct msghdr msg;
struct iovec iov;

int len;
mm_segment_t oldfs;

/* Set the msghdr structure*/
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 = 0;

/* Set the iovec structure*/
iov.iov_base = (void *) &Buffer[0];
iov.iov_len = (size_t)Length;

/* Recieve the message */
oldfs = get_fs(); set_fs(KERNEL_DS);
len = sock_recvmsg(sock,&msg,Length,0/*MSG_DONTWAIT*/); // let it wait if there is no message
set_fs(oldfs);

// if ((len!=-EAGAIN)&&(len!=0))
// printk("RecvBuffer Recieved %i bytes \n",len);

return len;
}



/*
Sets up a server-side socket

1. Create a new socket
2. Bind the address to the socket
3. Start listening on the socket
*/

struct socket* set_up_server_socket(int port_no) {
struct socket *sock;
struct sockaddr_in sin;

int error;



/* First create a socket */
error = sock_create(PF_INET,SOCK_STREAM,IPPROTO_TCP,&sock) ;
if (error<0)
printk("Error during creation of socket; terminating\n");



/* Now bind the socket */
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port_no);

error = sock->ops->bind(sock,(struct sockaddr*)&sin,sizeof(sin));
if (error<0)
{
printk("Error binding socket \n");
return 0;
}



/* Now, start listening on the socket */
error=sock->ops->listen(sock,32);
if (error!=0)
printk("Error listening on socket \n");

/* Now start accepting */
// Accepting is performed by the function server_accept_connection

return sock;
}


/*

Accepts a new connection (server calls this function)

1. Create a new socket
2. Call socket->ops->accept
3. return the newly created socket

*/

struct socket* server_accept_connection(struct socket *sock) {
struct socket * newsock;
int error;

/* Before accept: Clone the socket */

error = sock_create(PF_INET,SOCK_STREAM,IPPROTO_TCP,&newso ck);
if (error<0)
printk("Error during creation of the other socket; terminating\n");

newsock->type = sock->type;
newsock->ops=sock->ops;

/* Do the actual accept */

error = newsock->ops->accept(sock,newsock,0);


if (error<0) {
printk("Error accepting socket\n") ;
return 0;
}
return newsock;
}




struct socket * set_up_client_socket(unsigned int IP_addr, int port_no)
{
struct socket * clientsock;
struct sockaddr_in sin;
int error, i;

/* First create a socket */
error = sock_create(PF_INET,SOCK_STREAM,IPPROTO_TCP,&clien tsock);
if (error<0) {
printk("Error during creation of socket; terminating\n");
return 0;
}

/* Now bind and connect the socket */
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(IP_addr);
sin.sin_port = htons(port_no);

for(i=0;i<10;i++) {
error = clientsock->ops->connect(clientsock,(struct sockaddr*)&sin,sizeof(sin),0);
if (error<0) {
printk("Error connecting client socket to server: %i, retrying .. %d \n",error, i);
if(i==10-1) {
printk("Giving Up!\n"); return 0;
}
}
else break; //connected
}

return clientsock;

}


Regards

-Ankan Banerjee
ankan is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 07:36 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2