LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-15-2006, 02:19 PM   #1
omkarlagu
LQ Newbie
 
Registered: Jan 2006
Posts: 13

Rep: Reputation: 1
socket programming in KERNEL (TCP/IP)


hello guys,
i am trying out socket programming in LINUX kernel ..that is a SERVER/CLIENT module in kernel 2.6
now i have succesfully created a two sockets and connected them
but i am facing problems with sock_recvmsg
sock_sendmsg is successfull rite now
underneath is my code module ..review it and tell me what s wrong
omkar lagu ...omkarlagu@yahoo.com


/*This the server module for comunication */

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/net.h>
#include<linux/socket.h>
#include<linux/in.h>
#include<linux/slab.h>
#include<net/inet_common.h>
#include<linux/sched.h>
#include <linux/syscalls.h>
#include <asm/uaccess.h>
#define SERVER_PORT 1234
#define OMK_BUF 1000

int init_module(void) //show_stack(NULL,NULL);
{
struct socket *sock,*client;
struct sockaddr_in *server,*cl_addr;
struct iovec *iov;
struct msghdr *sock_msg;
mm_segment_t oldms;
char *buff;
int size;
int error,bin,listen,accp,suc;


error=sock_create(AF_INET, SOCK_STREAM, 0, &sock,0);
if (error < 0)
{
printk(KERN_ALERT "\nCan`t create a socket");
}
iov = (struct iovec*) kmalloc(sizeof(struct iovec),GFP_KERNEL);
sock_msg = (struct msghdr*) kmalloc(sizeof(struct msghdr),GFP_KERNEL);
server=(struct sockaddr_in *) kmalloc(sizeof(struct sockaddr_in),GFP_KERNEL);
client=(struct socket *) kmalloc(sizeof(struct socket),GFP_KERNEL);
cl_addr=(struct sockaddr_in *) kmalloc(sizeof(struct sockaddr_in),GFP_KERNEL);
buff=(char *)kmalloc((sizeof(char)*200),GFP_KERNEL);

memset(server,0,sizeof(struct sockaddr_in));
server->sin_family = AF_INET;
server->sin_port = htons(SERVER_PORT);
server->sin_addr.s_addr =htonl(INADDR_ANY);

bin=inet_bind(sock,(struct sockaddr*) server,sizeof(struct sockaddr_in));
if(bin<0)
printk(KERN_ALERT "\nbind failed\n");

listen= inet_listen(sock,0);
if(listen<0)
printk(KERN_ALERT "listen failed\n");




accp = inet_accept(sock,client,0);
printk(KERN_ALERT "acept value %d\n",accp);

if(accp<0)
{
printk(KERN_ALERT "accept failed\n");
}


/*------------------------recv mess---------------------*/


memset(iov,0,sizeof(struct iovec));
memset(sock_msg,0,sizeof(struct msghdr));
memset(buff,0,OMK_BUF);

while(1)
{

iov[0].iov_base=(void *)buff;
size=OMK_BUF;
iov[0].iov_len=size;


sock_msg->msg_name=cl_addr;
sock_msg->msg_namelen=sizeof(struct sockaddr_in);
sock_msg->msg_iov=iov;
sock_msg->msg_iovlen=1;
sock_msg->msg_control=NULL;
sock_msg->msg_controllen=0;
sock_msg->msg_flags=0;


oldms = get_fs(); set_fs(KERNEL_DS);
suc=sock_recvmsg(client,sock_msg,size,0); ////problem area
set_fs(oldms);

printk(KERN_ALERT " msg recived sucess:%d\n",suc);
if(suc<0)
{
return 0;
}

printk(KERN_ALERT " msg recived sucess:%d\n",suc);
}
suc=inet_release(sock);
printk(KERN_ALERT "released in server%d\n",suc);
return (1);

}

void cleanup_module(void)
{
printk(KERN_ALERT "EXIT server module\n");
}

client module is running properly.................

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/socket.h>
#include<linux/in.h>
#include<linux/net.h>
#include<linux/slab.h>
#include<net/inet_common.h>
#include <linux/syscalls.h>
#include <asm/uaccess.h>
#define SERVER_PORT 1234

int init_module(void)
{
struct socket *sockt;
struct sockaddr_in *server;
struct iovec *iov;
struct msghdr *sock_msg;
int acc,cn,suc,rel;
char *buff;
mm_segment_t old_mm;
int size;


server=(struct sockaddr_in*) kmalloc(sizeof(struct sockaddr_in),GFP_KERNEL);
iov=(struct iovec*)kmalloc(sizeof(struct iovec),GFP_KERNEL);
sock_msg=(struct msghdr*)kmalloc(sizeof(struct msghdr),GFP_KERNEL);
buff=(char*)kmalloc(((sizeof(char))*200),GFP_KERNEL);
acc=sock_create(AF_INET, SOCK_STREAM, 0, &sockt,0);
printk(KERN_ALERT "client sock %d\n",acc);
if(acc<0)
{
printk(KERN_ALERT "socket failed %d\n",acc);
}
memset(server,0,sizeof(struct sockaddr_in));
server->sin_family = AF_INET;
server->sin_addr.s_addr =htonl(INADDR_ANY);
server->sin_port = htons(SERVER_PORT);

cn=inet_stream_connect(sockt, (struct sockaddr*) server,sizeof(struct sockaddr_in),0);
printk(KERN_ALERT"cn::%d\n",cn);
/*---------------------------send mess------------------*/
old_mm = get_fs(); set_fs(KERNEL_DS);
memset(iov,0,sizeof(struct iovec));
memset(sock_msg,0,sizeof(struct msghdr));


iov[0].iov_base=(void*)buff;
size=200;
iov[0].iov_len=size;
sock_msg->msg_name=NULL;
sock_msg->msg_namelen=0;
sock_msg->msg_iov=iov;
sock_msg->msg_iovlen=1;
sock_msg->msg_control=NULL;
sock_msg->msg_controllen=0;
//sock_msg->msg_flags=0;


suc=sock_sendmsg(sockt,sock_msg,size);
set_fs(old_mm);

printk(KERN_ALERT "send msg in client sucess:%d\n",suc);
rel=inet_release(sockt);
return 1;

}

void cleanup_module(void)
{
printk(KERN_ALERT "out of client program ");
}


the error message when i RUN both the code ..........

Jan 19 05:58:37 localhost kernel: server: module license 'unspecified' taints kernel.
Jan 19 05:58:44 localhost kernel: cli: module license 'unspecified' taints kernel.
Jan 19 05:58:44 localhost kernel: client sock 0
Jan 19 05:58:44 localhost kernel: cn::0
Jan 19 05:58:44 localhost kernel: send msg in client sucess:200
Jan 19 05:58:44 localhost kernel: acept value 0
Jan 19 05:58:44 localhost kernel: Unable to handle kernel paging request at virtual address 001af803
Jan 19 05:58:44 localhost kernel: printing eip:
Jan 19 05:58:44 localhost kernel: 001af803
Jan 19 05:58:44 localhost kernel: *pde = 00000000
Jan 19 05:58:44 localhost kernel: Oops: 0000 [#1]
Jan 19 05:58:44 localhost kernel: CPU: 0
Jan 19 05:58:44 localhost kernel: EIP: 0060:[<001af803>] Tainted: P
Jan 19 05:58:44 localhost kernel: EFLAGS: 00010246 (2.6.5-1.358)
Jan 19 05:58:44 localhost kernel: EIP is at 0x1af803
Jan 19 05:58:44 localhost kernel: eax: 05387ed4 ebx: 0a856000 ecx: 0b2313dc edx: 04962784
Jan 19 05:58:44 localhost kernel: esi: 04962784 edi: 000003e8 ebp: 0b2313dc esp: 05387ec8
Jan 19 05:58:44 localhost kernel: ds: 007b es: 007b ss: 0068
Jan 19 05:58:44 localhost kernel: Process insmod (pid: 5799, threadinfo=05387000 task=045a43b0)
Jan 19 05:58:44 localhost kernel: Stack: 0222fb86 000003e8 00000000 00000019 021c54a2 00000000 00000001 ffffffff
Jan 19 05:58:44 localhost kernel: 00000000 0e75e724 00000000 00000246 00000000 00000001 045a43b0 0fd1c130
Jan 19 05:58:44 localhost kernel: 023480a0 05387f2c 00000016 78cf18c0 000f49e0 00000000 000003e8 04962784

Last edited by omkarlagu; 01-19-2006 at 08:33 AM.
 
Old 01-15-2006, 03:11 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It sounds like a fun "Science Experiment"...
... but I can't think of any situations where you shouldn't be doing socket I/O in user space.

To the best of my knowledge, there are seldom any good reasons for initiating, reading and writing sockets in a kernel module.

IMHO .. PSM
 
Old 01-15-2006, 05:03 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,766

Rep: Reputation: 467Reputation: 467Reputation: 467Reputation: 467Reputation: 467
Quote:
... but I can't think of any situations where you shouldn't be doing socket I/O in user space.
Here's an example where it's done:
http://www.redhat.com/docs/manuals/t...ual/intro.html
 
Old 01-15-2006, 09:55 PM   #4
omkarlagu
LQ Newbie
 
Registered: Jan 2006
Posts: 13

Original Poster
Rep: Reputation: 1
why i need socket programming in KERNEL

i am doing a project known as Distributed shared memory ...
where i need to communicate betweeen two nodes KERNEL...
and thats possible(socket programming in kernel) as PERFORMANCE of the software increases

Last edited by omkarlagu; 01-15-2006 at 09:57 PM.
 
Old 01-15-2006, 11:10 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool. Are you using OpenMosix?
 
Old 01-16-2006, 01:09 AM   #6
omkarlagu
LQ Newbie
 
Registered: Jan 2006
Posts: 13

Original Poster
Rep: Reputation: 1
no i am not using OpenMosix??. by the way whats OPENMOSIX ellaborate can it help me ....

Last edited by omkarlagu; 01-16-2006 at 01:48 AM.
 
Old 01-16-2006, 08:58 AM   #7
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
I have few comments:

Quote:
Originally Posted by omkarlagu
hello guys,
.............
int error,bin,listen,accp,suc;
mm_segment_t oldmm;

.............
error=sock_create(AF_INET, SOCK_STREAM, 0, &sock,0);
error=sock_create_kern(AF_INET, SOCK_STREAM, 0, &sock);

.............
client=(struct socket *) kmalloc(sizeof(struct socket),GFP_KERNEL);
error=sock_create_lite(AF_INET, SOCK_STREAM, 0, &client);

.............
bin=inet_bind(sock,(struct sockaddr*) server,sizeof(struct sockaddr_in));
bin=sock->ops->bind(sock,(struct sockaddr*) server,sizeof(struct sockaddr_in));

.............
server->sin_port = SERVER_PORT;
server->sin_port = htons(SERVER_PORT);

.............
listen= inet_listen(sock,0);
listen= sock->ops->listen(sock,0);

.............
accp = inet_accept(sock,client,0);
accp = sock->ops->accept(sock,client,0);

.............
oldmm = get_fs(); set_fs(KERNEL_DS);
client->ops = sock->ops;

memset(iov,0,sizeof(struct iovec));
memset(sock_msg,0,sizeof(struct msghdr));
memset(buff,0,OMK_BUF);

...................
sock_msg->msg_flags=1;
sock_msg->msg_flags=0;

suc=sock_recvmsg(sock,sock_msg,size,1); //PROBLEM
suc=sock_recvmsg(client,sock_msg,size,0);
set_fs(oldmm);
..........
}

void cleanup_module(void)
{
printk(KERN_ALERT "EXIT server module\n");
}
Cheers,
GH

Last edited by freegianghu; 01-16-2006 at 09:00 AM.
 
Old 05-22-2010, 04:33 AM   #8
er.surendersharma
Member
 
Registered: May 2010
Posts: 47
Blog Entries: 2

Rep: Reputation: 15
kernel socket

Acctually I was also trying to same thing and I was feeling same problem... At rcvmsg end I am not getting the messege..............

yeah, you can try with the UDP socket may be it will be successfull...
if u wud find solution then mail to me...

best of luck...
 
Old 07-04-2011, 01:42 AM   #9
nuwansa
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
use ksocket

use ksocket if you want kernel socket programming. you can also look at the ksocket implementation from the source code.

ksocket at sourceforge

Last edited by nuwansa; 07-04-2011 at 01:44 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
TCP/IP book for socket programming hubabuba Programming 3 10-17-2005 03:46 AM
Linux TCP Socket swa_dur Linux - Networking 3 09-23-2005 11:03 AM
socket buffer TCP lucs Slackware 0 05-05-2005 07:26 AM
Kernel socket programming arunka Programming 2 03-05-2005 03:40 PM
filetransfer with socket and tcp biiiep Programming 2 06-11-2004 12:33 PM

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

All times are GMT -5. The time now is 01:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration