problem upload modules into kernel in linux

upload modules into kernel in linux

Page 1 of 1

1 Replies - 1267 Views - Last Post: 25 March 2008 - 12:41 PM Rate Topic: -----

#1 steven26   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-March 08

problem upload modules into kernel in linux

Posted 22 March 2008 - 07:56 AM

below coding is my project but i cannot pload into kernel in linux, please help me.
//rip.c
#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <sys/socket.h> 
#include <sys/wait.h> 
#include <unistd.h>

#define RIP_PORT  520

struct rip_message
{
	unsigned short family;
	unsigned short tag;
	unsigned long ip;
	unsigned long netmask;
	unsigned long gateway;
	unsigned long metric;
};

struct rip
{
	unsigned char command;
	unsigned char version;
	unsigned short domain;
	struct rip_message routes[1];
};

void usage(void)
{
	fprintf(stderr,"Usage: rprobe [-av] target\n");
	fprintf(stderr," a:\t\tquery active mode daemons (requires root priveleges)\n");
	fprintf(stderr," v:\t\tspecify rip version 2\n");
	fprintf(stderr," target:\tdestination\n");
	
	exit(42);
}

int main(int argc, char *argv[])
{
	int sockfd;
	struct sockaddr_in their_addr;
	struct sockaddr_in ours;
	struct hostent *he;
	char hostname[256];
	int localport;
	int numbytes;
	int ch,version;
	struct rip evil;
	long on=1;
 
	localport=4242;
	version=1;

	while ( (ch=getopt(argc,argv,"va")) != -1)
	{
		switch (ch)
		{
			case 'v': version=2;
				break;
			case 'a': localport=RIP_PORT;
				break;
			default : usage();
		}
	}

	argc-=optind;
	argv+=optind;
	
	if (argc!=1) usage();

	strncpy(hostname,argv[0],sizeof(hostname));

	if ((he=gethostbyname(hostname)) == NULL) 
	{ 
		herror("gethostbyname");
		exit(1);
	}

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 
	{
		perror("socket");
		exit(1);
	}

	if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))==-1)
	{
		perror("setsockopt");
		exit(1);
	}

	their_addr.sin_family = AF_INET;	  /* host byte order */
	their_addr.sin_port = htons(RIP_PORT);  /* short, network byte order */
	their_addr.sin_addr = *((struct in_addr *)he->h_addr);
	bzero(&(their_addr.sin_zero), 8);	 /* zero the rest of the struct */

	bzero(&ours,sizeof(struct sockaddr));
	ours.sin_family = AF_INET;	  /* host byte order */
	ours.sin_port = htons(localport);  /* short, network byte order */
	bzero(&(ours.sin_zero), 8);	 /* zero the rest of the struct */

	bind(sockfd,(struct sockaddr *)&ours,sizeof(struct sockaddr));

	bzero(&evil,sizeof(struct rip));

	evil.command=1;
	evil.version=version;
	evil.routes[0].metric=htonl(16);

	printf("Sending packet.\n");
	if ((numbytes=sendto(sockfd, &evil, sizeof(struct rip), 0, \
				(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
	{
		perror("recvfrom");
		exit(1);
	   	}
	printf("Sent %d bytes.\n",numbytes);
	close(sockfd);
	exit(0);
}


Is This A Good Question/Topic? 0
  • +

Replies To: problem upload modules into kernel in linux

#2 skaoth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: problem upload modules into kernel in linux

Posted 25 March 2008 - 12:41 PM

You cannot just load any program into the kernel. What you load into the kernel has to follow a strict interface otherwise the kernel won't know how to call your module.

If you are interested in writing a Linux Kernel Module I would start here
http://tldp.org/HOWTO/Module-HOWTO/

You will need to decide what kind of module to write: character, block, network, filesystem, etc. I would start with a simple hello world program as a character driver. This will allow you to use the open/read/write system calls to access the driver.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1