public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* GETHOSTBYNAME()
@ 2003-03-24  4:18 shesha bhushan
  2003-03-24  4:55 ` GETHOSTBYNAME() Prasad
  2003-03-24  9:23 ` GETHOSTBYNAME() Martin Mares
  0 siblings, 2 replies; 5+ messages in thread
From: shesha bhushan @ 2003-03-24  4:18 UTC (permalink / raw)
  To: linux-kernel, kernelnewbies

Hi All
I am trying to find the gethostbyname() equivalent function in kernel space. 
Does any one know that.
The reason is...
I am using UDP to transfer data from one machine to another. It is not one 
time transfer. Once I get a message from machine A; I need to send some 
message back to Machine A from Machine B. For that I was using the following 
lines in user space program. I need to do the same in kernel space. Could 
any one help me out in this.

struct hostent *data;
struct sockaddr_in server;
int sock;

  sock = socket(AF_INET, SOCK_DGRAM , 0)

/* binding and all are done here */

  data = gethostbyname("158.168.1.1");
  memcpy (&server.sin_addrs, data->h_addr, data->h_length);
  retval = sendto(sock,msg,sizeof(msg), 0, (struct sockaddr *) &server, 
sizeof server);


Thanking You
Shesha

_________________________________________________________________
Cricket - World Cup 2003 http://server1.msn.co.in/msnspecials/worldcup03/ 
News, Views and Match Reports.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: GETHOSTBYNAME()
  2003-03-24  4:18 GETHOSTBYNAME() shesha bhushan
@ 2003-03-24  4:55 ` Prasad
  2003-03-24  6:43   ` GETHOSTBYNAME() Jan Hudec
  2003-03-24  9:23 ` GETHOSTBYNAME() Martin Mares
  1 sibling, 1 reply; 5+ messages in thread
From: Prasad @ 2003-03-24  4:55 UTC (permalink / raw)
  To: shesha bhushan; +Cc: linux-kernel, kernelnewbies


hi,
	i dont think there is one similar to gethostbyname in the 
kernel-space.  I do think so because most of the work done by 
gethostbyname is based on the nameservices and is generally not required 
in the kernel-space.  Implementing it should not be that big a problem.
once you have the IP of the name server (/etc/resolve.conf???) you can 
always connect to it and get the IP of the given host.

Prasad.


On Mon, 24 Mar 2003, shesha bhushan wrote:

> Hi All
> I am trying to find the gethostbyname() equivalent function in kernel space. 
> Does any one know that.
> The reason is...
> I am using UDP to transfer data from one machine to another. It is not one 
> time transfer. Once I get a message from machine A; I need to send some 
> message back to Machine A from Machine B. For that I was using the following 
> lines in user space program. I need to do the same in kernel space. Could 
> any one help me out in this.
> 
> struct hostent *data;
> struct sockaddr_in server;
> int sock;
> 
>   sock = socket(AF_INET, SOCK_DGRAM , 0)
> 
> /* binding and all are done here */
> 
>   data = gethostbyname("158.168.1.1");
>   memcpy (&server.sin_addrs, data->h_addr, data->h_length);
>   retval = sendto(sock,msg,sizeof(msg), 0, (struct sockaddr *) &server, 
> sizeof server);
> 
> 
> Thanking You
> Shesha
> 
> _________________________________________________________________
> Cricket - World Cup 2003 http://server1.msn.co.in/msnspecials/worldcup03/ 
> News, Views and Match Reports.
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
> 

-- 
Failure is not an option


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: GETHOSTBYNAME()
       [not found] <F64RsqSoQHK3gSkQsfl0000279b@hotmail.com>
@ 2003-03-24  5:51 ` Prasad
  0 siblings, 0 replies; 5+ messages in thread
From: Prasad @ 2003-03-24  5:51 UTC (permalink / raw)
  To: shesha bhushan; +Cc: lkml


Hi,

Just have a look at the code at the bottom... that should make it clear.  
This again is what i implemented for my project and its working fine.

> Thanks for the help. But if I am opening a socket in a lodable kernel module 
> and need to send a message to a perticular IP say 158.168.1.1, then how to 
> populate the structure sock_data.sin_addrs


	// We dont have a inet_addr in user space.
	unsigned int inet_addr(char *str)
	{
	  int a,b,c,d;
	  char arr[4];
	  sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d);
	  arr[0] = a; arr[1] = b; arr[2] = c; arr[3] = d;
	  return *(unsigned int*)arr;
	}

	// Get the host...
	int gethost(struct sockaddr_in *addr)
	{
	  unsigned long ad;
	  addr->sin_family = AF_INET;
	  addr->sin_port = htons(sysctl_dos_tcpport );
	  ad = inet_addr("172.16.7.12");
	  if( ad == INADDR_NONE ){
	    printk("dos_select_host: Host not found");
	    return -EHOSTUNREACH;
	  }
	  addr->sin_addr.s_addr = ad;
	  return 0;
	}


	// Connect to the remote host...
	struct socket* connect(struct sockaddr_in* sock_addr)
	{
	  int retval;
	  struct socket *sock;
	
	  retval = sock_create(AF_INET,SOCK_STREAM,0,&sock);
	
	  if (retval < 0) {
	    printk("(connect) could not create a socket\n");
	    return NULL;
	  }

	  retval = sock->ops->connect(sock,
			(struct sockaddr *)sock_addr,
			sizeof(struct sockaddr_in),0);
	
	  if (retval < 0) {
	    printk("(connect) error on connect: %d\n",retval);
	    sock_release(sock);
	    return NULL;
	  }
	
	  return sock;
	}

Hope this serves your purpose...

Prasad.

-- 
Failure is not an option



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: GETHOSTBYNAME()
  2003-03-24  4:55 ` GETHOSTBYNAME() Prasad
@ 2003-03-24  6:43   ` Jan Hudec
  0 siblings, 0 replies; 5+ messages in thread
From: Jan Hudec @ 2003-03-24  6:43 UTC (permalink / raw)
  To: linux-kernel, kernelnewbies

On Mon, Mar 24, 2003 at 10:25:11AM +0530, Prasad wrote:
> 
> hi,
> 	i dont think there is one similar to gethostbyname in the 
> kernel-space.  I do think so because most of the work done by 
> gethostbyname is based on the nameservices and is generally not required 
> in the kernel-space.  Implementing it should not be that big a problem.
> once you have the IP of the name server (/etc/resolve.conf???) you can 
> always connect to it and get the IP of the given host.

Implementing it is a big problem, since gethostbyname usualy means a DNS
lookup. Best way to do it in kernel is
call_usermode_helper('/sbin/khost') and call gethostbyname in there.

> On Mon, 24 Mar 2003, shesha bhushan wrote:
> 
> > Hi All
> > I am trying to find the gethostbyname() equivalent function in kernel space. 
> > Does any one know that.
> > The reason is...
> > I am using UDP to transfer data from one machine to another. It is not one 
> > time transfer. Once I get a message from machine A; I need to send some 
> > message back to Machine A from Machine B. For that I was using the following 
> > lines in user space program. I need to do the same in kernel space. Could 
> > any one help me out in this.
> > 
> > struct hostent *data;
> > struct sockaddr_in server;
> > int sock;
> > 
> >   sock = socket(AF_INET, SOCK_DGRAM , 0)
> > 
> > /* binding and all are done here */
> > 
> >   data = gethostbyname("158.168.1.1");

However as long as you are resolving IP address, you should not ask for
gethostbyname. You should ask for inet_aton, which does exist (the name
was mentioned in some list (linux-kernel or this one) not long ago, but
I do not remember it).

> >   memcpy (&server.sin_addrs, data->h_addr, data->h_length);
> >   retval = sendto(sock,msg,sizeof(msg), 0, (struct sockaddr *) &server, 
> > sizeof server);
> > 
> > 
> > Thanking You
> > Shesha
> > 
> > _________________________________________________________________
> > Cricket - World Cup 2003 http://server1.msn.co.in/msnspecials/worldcup03/ 
> > News, Views and Match Reports.
> > 
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at  http://www.tux.org/lkml/
> > 
> 
> -- 
> Failure is not an option
> 
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 
-------------------------------------------------------------------------------
						 Jan 'Bulb' Hudec <bulb@ucw.cz>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: GETHOSTBYNAME()
  2003-03-24  4:18 GETHOSTBYNAME() shesha bhushan
  2003-03-24  4:55 ` GETHOSTBYNAME() Prasad
@ 2003-03-24  9:23 ` Martin Mares
  1 sibling, 0 replies; 5+ messages in thread
From: Martin Mares @ 2003-03-24  9:23 UTC (permalink / raw)
  To: shesha bhushan; +Cc: linux-kernel, kernelnewbies

Hello!

>  data = gethostbyname("158.168.1.1");

If you use gethostbyname only on numerical addresses, then it's fine to
replace it by htonl(0x98a80101) etc.

				Have a nice fortnight
-- 
Martin `MJ' Mares   <mj@ucw.cz>   http://atrey.karlin.mff.cuni.cz/~mj/
Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth
A Bash poem: time for echo in canyon; do echo $echo $echo; done

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-03-24  9:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-03-24  4:18 GETHOSTBYNAME() shesha bhushan
2003-03-24  4:55 ` GETHOSTBYNAME() Prasad
2003-03-24  6:43   ` GETHOSTBYNAME() Jan Hudec
2003-03-24  9:23 ` GETHOSTBYNAME() Martin Mares
     [not found] <F64RsqSoQHK3gSkQsfl0000279b@hotmail.com>
2003-03-24  5:51 ` GETHOSTBYNAME() Prasad

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox