I am doing one kernel socket client server chat program Is not working

Submitted by jayakrishnan.aj
on December 11, 2009 - 4:29am

. i used some net resourses and some additions by myself ,i think my server program is working ,i can connect to it from a user client program,but in kernel level my server and client modules are not workis,,,, No more kernel panics,but it suspends in a prompt,

I am using Redhat 5 with a kernel of 2.6.18

my kernel modules are following , i am using ioctl for a future controll

SERVER#####

#include <linux/kernel.h>	/* We're doing kernel work */
#include <linux/module.h>	/* Specifically, a module */
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/net.h>
#include <net/sock.h>
#include <linux/tcp.h>
#include <linux/in.h>
#include <asm/uaccess.h>
#include <linux/file.h>
#include <linux/socket.h>
#include <linux/smp_lock.h>
#include <linux/slab.h>
#include "chardev.h"
#define SUCCESS 0
#define DEVICE_NAME "char_dev"
#define BUF_LEN 80
MODULE_LICENSE("GPL");
struct sockaddr_in sin;
int error;
struct socket *udpsock,*new;
int device_ioctl(struct inode *inode,	/* see include/linux/fs.h */
		 struct file *file,	/* ditto */
		 unsigned int ioctl_num,	/* number and param for ioctl */
		 unsigned long ioctl_param)
{
printk(KERN_ALERT"IOCTL NUM =%d",ioctl_num);
/*	 struct sockaddr_in sin;
        printk ( KERN_ALERT"\nImin\n");
*/	switch (ioctl_num) {
	case IOCTL_SET_MSG:
			printk(KERN_ALERT"IOCTL NUM =SET MSG");
			
			error = sock_create_kern(PF_INET, SOCK_STREAM, 0,  &udpsock);
		    if (error<0)
		    {
                    printk("Error during creation of socket - %d; terminating\n",error);
		    udpsock->ops->release(udpsock);
                    return -error;
        	    }
			printk(KERN_ALERT"\nSOCKET SUCCESSFULLY CREATED\n");

        		sin.sin_family = AF_INET;
        		sin.sin_port = htons(7000);
        		sin.sin_addr.s_addr = INADDR_ANY;
        		error = udpsock->ops->bind(udpsock,(struct sockaddr*)&sin,sizeof(sin));
        if (error<0)
        {
                printk("Error binding socket %d",error);
		udpsock->ops->release(udpsock);
                return error;
        }
	printk(KERN_ALERT"\nSUCCESSFULLY BINDED\n");
	error = udpsock->ops->listen(udpsock,3);
	printk("\nListening......\n");
           if (error<0)
        {
                printk("Error listening socket : %d", error);
		udpsock->ops->release(udpsock);
                return error;
        }

                  error = sock_create_kern(PF_INET, SOCK_STREAM, 0,
&new);
                   new->type = udpsock->type;
                   new->ops=udpsock->ops;
                   if (error<0)
                    {
                    printk("Error during creation of NEW socket - %d; terminating\n",error);
                    new->ops->release(new);
                        udpsock->ops->release(udpsock);
                    return -error;
                    }
                        printk(KERN_ALERT"\nACCEPTING SOCKET SUCCESSFULLY CREATED\n");


			error = new->ops->accept(udpsock,new,0);
           if (error<0)
        {
                printk("Error Accepting socket %d", error);
		udpsock->ops->release(udpsock);
                return error;
        }
                printk(KERN_ALERT"\nSUCCESSFULLY ACCEPTED\n");
		break;


	case IOCTL_GET_MSG:/*
			error = udpsock->ops->listen(udpsock,3);
			printk("\nListening......\n");
			error = udpsock->ops->accept(udpsock,new,0);
           if (error<0)
        {
                printk("Error Accepting socket");
                return -1;
        }
                printk(KERN_ALERT"\nSUCCESSFULLY ACCEPTED\n");*/
                break;
}
return 0;
}
struct file_operations Fops = {
	.ioctl = device_ioctl
};
int init_module()
{
	int ret_val;
	ret_val = register_chrdev(MAJOR_NUM,DEVICE_NAME,&Fops);

	if (ret_val < 0) {
		printk(KERN_ALERT "%s failed with %d\n",
		       "Sorry, registering the character device ", ret_val);
		return ret_val;
	}

	printk(KERN_INFO "%s The major device number is %d.\n",
	       "Registeration is a success", MAJOR_NUM);
	printk(KERN_INFO "If you want to talk to the device driver,\n");
	printk(KERN_INFO "you'll have to create a device file. \n");
	printk(KERN_INFO "We suggest you use:\n");
	printk(KERN_INFO "mknod %s c %d 0\n", DEVICE_NAME, MAJOR_NUM);
	printk(KERN_INFO "The device file name is important, because\n");
	printk(KERN_INFO "the ioctl program assumes that's the\n");
	printk(KERN_INFO "file you'll use.\n");

	return 0;
}
void cleanup_module()
{
	int ret;
	ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
	printk(KERN_ALERT "ITS OVER \n");
	if (ret < 0)
		printk(KERN_ALERT "Error: unregister_chrdev: %d\n", ret);
}








USER PROGRAM FOR CONTROLLING SERVER#############

#include "chardev.h"
#include 
#include 
#include 		/* open */
#include 		/* exit */
#include 		/* ioctl */

/* 
 * Functions for the ioctl calls 
 */

int ioctl_set_msg(int file_desc, char *message)
{
	int ret_val;

	ret_val = ioctl(file_desc, IOCTL_SET_MSG, message);

	if (ret_val < 0) {
		printf("ioctl_set_msg failed:%d\n", ret_val);
		return ret_val;
	}
}

int ioctl_get_msg(int file_desc)
{
	int ret_val,i,j;
	char message[100];

	/* 
	 * Warning - this is dangerous because we don't tell
	 * the kernel how far it's allowed to write, so it
	 * might overflow the buffer. In a real production
	 * program, we would have used two ioctls - one to tell
	 * the kernel the buffer length and another to give
	 * it the buffer to fill
	 */
	printf("\nIm HERE\n");
	ret_val = ioctl(file_desc, IOCTL_GET_MSG, message);

	if (ret_val < 0) {
		printf("ioctl_get_msg failed:%d\n", ret_val);
		exit(-1);
	}

	printf("get_msg message:%s\n", message);
}

ioctl_get_nth_byte(int file_desc)
{
	int i;
	char c;
	printf("get_nth_byte message:");
	i = 0;
	do {
		c = ioctl(file_desc, IOCTL_GET_NTH_BYTE, i++);

		if (c < 0) {
			printf
			    ("ioctl_get_nth_byte failed at the %d'th byte:\n",
			     i);
			exit(-1);
		}

		putchar(c);
	} while (c != 0);
	putchar('\n');
}

/* 
 * Main - Call the ioctl functions 
 */
main()
{
	int file_desc, ret_val;
	char *msg = "Message passed by ioctl\n";

	file_desc = open(DEVICE_FILE_NAME, 0);
	if (file_desc < 0) {
		printf("Can't open device file: %s\n", DEVICE_FILE_NAME);
		exit(-1);
	}
 printf("\n%d\n",file_desc);
	//ioctl_get_nth_byte(file_desc);
//	ioctl_get_msg(file_desc);
	ret_val = ioctl_set_msg(file_desc, msg);
	ioctl_get_msg(file_desc);
	close(file_desc);
}






CLIENT MODULE###############

#include 	/* We're doing kernel work */
#include 	/* Specifically, a module */
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "chardev.h"
#define SUCCESS 0
#define DEVICE_NAME "char_dev1"
#define BUF_LEN 80
MODULE_LICENSE("GPL");

struct sockaddr_in csin;
int error;
struct socket *csock,*new;

int device_ioctl(struct inode *inode,	/* see include/linux/fs.h */
		 struct file *file,	/* ditto */
		 unsigned int ioctl_num,	/* number and param for ioctl */
		 unsigned long ioctl_param)
{

	struct sockaddr_storage address;
printk(KERN_ALERT"IOCTL NUM =%d",ioctl_num);
/*	 struct sockaddr_in sin;
        printk ( KERN_ALERT"\nImin\n");
*/	switch (ioctl_num) {
	case IOCTL_SET_MSG:

		error = sock_create(PF_INET, SOCK_STREAM, 0,&csock);
		    if (error<0)
		    {
                    printk("IN CLIENT:Error during creation of socket - %d; terminating\n",error);
		    csock->ops->release(csock);
                    return -error;
        	    }
       		else
                    {
			printk(KERN_ALERT"\nIN CLIENT:SOCKET SUCCESSFULLY CREATED\n");
                    }
       		csin.sin_family = AF_INET;
		csin.sin_addr.s_addr = htonl(in_aton("192.168.1.105"));
		csin.sin_port = htons(7000);
		printk("\nIN CLIENT:Control is here\n");
   error = csock->ops->connect(csock,(struct sockaddr*)&csin,sizeof(csin),0);
	 printk("IN CLIENT: %d ",error);
	if (error<0)
	   {
          printk("IN CLIENT:Error during connection - %d; terminating\n",error);
          csock->ops->release(csock);
          return -error;
          }
	else
	{
	printk("\nIN CLIENT:SUCCESSFULLY CONNECTED\n");
}

}
return 0;
}


struct file_operations Fops = {
	.ioctl = device_ioctl
};
int init_module()
{
	int ret_val;
	ret_val = register_chrdev(MAJOR_NUM,DEVICE_NAME,&Fops);

	if (ret_val < 0) {
		printk(KERN_ALERT "%s failed with %d\n",
		       "Sorry, registering the character device ", ret_val);
		return ret_val;
	}

	printk(KERN_INFO "%s The major device number is %d.\n",
	       "Registeration is a success", MAJOR_NUM);
	printk(KERN_INFO "If you want to talk to the device driver,\n");
	printk(KERN_INFO "you'll have to create a device file. \n");
	printk(KERN_INFO "We suggest you use:\n");
	printk(KERN_INFO "mknod %s c %d 0\n", DEVICE_NAME, MAJOR_NUM);
	printk(KERN_INFO "The device file name is important, because\n");
	printk(KERN_INFO "the ioctl program assumes that's the\n");
	printk(KERN_INFO "file you'll use.\n");

	return 0;
}
void cleanup_module()
{
	int ret;
	ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
	printk(KERN_ALERT "ITS OVER \n");
	if (ret < 0)
		printk(KERN_ALERT "Error: unregister_chrdev: %d\n", ret);
}
	



SAME USER PROGRAM IS USER FOR CLIENT ALSO WITH SOME MODIFICATIONSS########


#include "chardev.h"
#include 
#include 
#include 		/* open */
#include 		/* exit */
#include 		/* ioctl */

/* 
 * Functions for the ioctl calls 
 */

int ioctl_set_msg(int file_desc, char *message)
{
	int ret_val;

	ret_val = ioctl(file_desc, IOCTL_SET_MSG, message);

	if (ret_val < 0) {
		printf("ioctl_set_msg failed:%d\n", ret_val);
		return ret_val;
	}
}

int ioctl_get_msg(int file_desc)
{
	int ret_val,i,j;
	char message[100];

	/* 
	 * Warning - this is dangerous because we don't tell
	 * the kernel how far it's allowed to write, so it
	 * might overflow the buffer. In a real production
	 * program, we would have used two ioctls - one to tell
	 * the kernel the buffer length and another to give
	 * it the buffer to fill
	 */
	printf("\nIm HERE\n");
	ret_val = ioctl(file_desc, IOCTL_GET_MSG, message);

	if (ret_val < 0) {
		printf("ioctl_get_msg failed:%d\n", ret_val);
		exit(-1);
	}

	printf("get_msg message:%s\n", message);
}

ioctl_get_nth_byte(int file_desc)
{
	int i;
	char c;
	printf("get_nth_byte message:");
	i = 0;
	do {
		c = ioctl(file_desc, IOCTL_GET_NTH_BYTE, i++);

		if (c < 0) {
			printf
			    ("ioctl_get_nth_byte failed at the %d'th byte:\n",
			     i);
			exit(-1);
		}

		putchar(c);
	} while (c != 0);
	putchar('\n');
}

/* 
 * Main - Call the ioctl functions 
 */
main()
{
	int file_desc, ret_val;
	char *msg = "Message passed by ioctl\n";

	file_desc = open(DEVICE_FILE_NAME, 0);
	if (file_desc < 0) {
		printf("Can't open device file: %s\n", DEVICE_FILE_NAME);
		exit(-1);
	}
 printf("\n%d\n",file_desc);
	//ioctl_get_nth_byte(file_desc);
//	ioctl_get_msg(file_desc);
	ret_val = ioctl_set_msg(file_desc, msg);
	//ioctl_get_msg(file_desc);
	close(file_desc);
}

PLs help me i think the problem is with the connect part reply me plsss

XPQPxZLoiuoMbEgQ

ReneeMission (not verified)
on
August 28, 2010 - 7:32am

qPRDLUHtWRQz

Gatevvay (not verified)
on
September 12, 2010 - 6:11pm

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.