Hello,
I want to implement sockets in the kernel 2.6(client and server both).How will they be handled in the kernel?
Printable View
Hello,
I want to implement sockets in the kernel 2.6(client and server both).How will they be handled in the kernel?
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
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
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
Great!
But, What are the #includes for this code?
I need to program TCP listen socket in ubuntu...
Please start a new thread... this one is almost 4 years old, so locking.
Sorry for any inconvenience.