LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-20-2006, 08:03 AM   #1
omkarlagu
LQ Newbie
 
Registered: Jan 2006
Posts: 13

Rep: Reputation: 1
plz help regarding TCP/IP socket progamming in LINUX kernel


hello everybody,

i am in a great need of HELP .....
i am implementing socket programming in linux kernel
i am able to send message using sock_sendmsg
but can`t recv the same messsage
my client module is running perfectly
there are problems with server module

steps to run this program
1. underneath is my client module first and then its make file
2. then there is a server module and the make file
3. save them both in two directory`s
then first compile both modules using MAKE command

then insert server first using EXAMPLE insmod server.ko
then insert client module

you can also comment show_stack function in traps.c to avoid the stack messages
plz reply .........omkarlagu@yahoo.com

//client module name as cli.c...........................


#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 12345

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


mm_segment_t old_mm;

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);

/*create a client socket-------------------*/
acc=sock_create(AF_INET, SOCK_STREAM, 0, &sockt);
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_port = htons(SERVER_PORT);
server->sin_addr.s_addr =htonl(INADDR_ANY);

/*connect the socket with server-------*/

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));

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

memset(sock_msg,0,sizeof(struct msghdr));

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);


/* release the client------------------------*/
sock_release(sockt);
return 1;

}

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

//make file for client module
obj-m := cli.o



KDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)



default:

$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules



//server module name as server.c

/*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 12345
#define OMK_BUF 300

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



error=sock_create(AF_INET, SOCK_STREAM, 0, &socks);
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);
buff=(char *)kmalloc((sizeof(char)*300),GFP_KERNEL);
memset(buff,0,OMK_BUF);


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);

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

/*listen mode-------------------------------*/
listen= inet_listen(socks,0);
if(listen<0)
printk(KERN_ALERT "listen failed\n");



/*accept call for server-------------------*/
accp = inet_accept(socks,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));


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

memset(sock_msg,0,sizeof(struct msghdr));
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_recvmsg(client,sock_msg,size,0);
set_fs(oldms);
printk(KERN_ALERT " msg recived sucess:%d\n",suc);


/*----------release---------------*/
sock_release(socks);


return 1;

}







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


//// make file for server

obj-m := server.o



KDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)



default:

$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules


////

error seen on the screen


Jan 20 05:36:35 localhost kernel: server: module license 'unspecified' taints kernel.
Jan 20 05:36:41 localhost kernel: client sock 0
Jan 20 05:36:41 localhost kernel: cn::0
Jan 20 05:36:41 localhost kernel: send msg in client sucess:200
Jan 20 05:36:41 localhost kernel: acept value 0
Jan 20 05:36:41 localhost kernel: Unable to handle kernel NULL pointer dereference at virtual address 0000003d
Jan 20 05:36:41 localhost kernel: printing eip:
Jan 20 05:36:41 localhost kernel: c02a5b59
Jan 20 05:36:41 localhost kernel: *pde = 00000000
Jan 20 05:36:41 localhost kernel: Oops: 0000 [#1]
Jan 20 05:36:41 localhost kernel: Modules linked in: cli server ppp_synctty ppp_async crc_ccitt ppp_generic slhc snd_mixer_oss snd_via82xx gameport snd_ac97_codec snd_ac97_bus snd_pcm snd_timer snd_page_alloc snd_mpu401_uart snd_rawmidi snd_seq_device snd soundcore parport_pc lp parport autofs4 sunrpc ipt_REJECT ipt_state ip_conntrack iptable_filter ip_tables via_rhine mii floppy sg scsi_mod nls_iso8859_1 nls_cp437 vfat fat dm_mod uhci_hcd ehci_hcd button battery ac ipv6 ext3 jbd
Jan 20 05:36:41 localhost kernel: CPU: 0
Jan 20 05:36:41 localhost kernel: EIP: 0060:[<c02a5b59>] Tainted: P VLI
Jan 20 05:36:41 localhost kernel: EFLAGS: 00010246 (2.6.14.5)
Jan 20 05:36:41 localhost kernel: EIP is at sock_recvmsg+0x109/0x110
Jan 20 05:36:41 localhost kernel: eax: c43f9ee4 ebx: 00000001 ecx: c9815540 edx: c8c2c660
Jan 20 05:36:41 localhost kernel: esi: 0000012c edi: c8c2c660 ebp: c9815540 esp: c43f9e8c
Jan 20 05:36:41 localhost kernel: ds: 007b es: 007b ss: 0068
Jan 20 05:36:41 localhost kernel: Process insmod (pid: 3720, threadinfo=c43f8000 task=c0c64ad0)
Jan 20 05:36:41 localhost kernel: Stack: Code: 00 00 5b 5e 5f 5d c3 8d 44 24 58 e8 22 c8 ec ff eb ea 89 34 24 8b 84 24 ec 00 00 00 89 e9 8b 5f 08 89 fa 89 44 24 04 8d 44 24 58 <ff> 53 3c eb c5 89 f6 83 ec 0c 89 5c 24 04 bb 00 e0 ff ff 89 74
 
  


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
socket programming in KERNEL (TCP/IP) omkarlagu Programming 8 07-04-2011 01:42 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
filetransfer with socket and tcp biiiep Programming 2 06-11-2004 12:33 PM
Graphic progamming under Linux so_o Programming 9 09-16-2003 06:38 PM

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

All times are GMT -5. The time now is 12:05 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