🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #18391

Re: Direccion IP en C y PPP.

#18391
From: J.A. Gutierrez
Date: Wed, 15 Sep 1999 00:00
75 lines
2856 bytes
oscar@server.enmicasa.dhs.org wrote:
: Luis Pa�l Tomillo <luis@belcebu.ccr.es> wrote:

:> En teor�a podr�as especificar el nombre de la m�quina local y obtener
:> a trav�s de "si" la direcci�n IP asociada.

:> Otra forma podr�a ser lanzando desde C el programa hostname -i y recogiendo
:> la salida de este programa.

: Si nos estamos limitando a Linux, podemos ver nuestro nombre en
: /proc/sys/kernel/hosname
: Y si quieres obtener la IP directamente se puede con una serie de llamadas
: ioctl sobre un socket.
: Haz un strace del "ifconfig" y ver�s.

	hombre, tampoco hay que irse tan abajo:

===========================================================================
#include <stdio.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <errno.h>


#ifndef	INADDR_NONE
#define INADDR_NONE	0xffffffff
#endif

int	main(int argc, char **argv)
{
	char			hostname[256];
	struct hostent	*he;
	u_long			add;

	if (gethostname(hostname,sizeof(hostname)) < 0)
	{
		perror("gethostname: who am I ?");
		exit(1);
	}

	if ( (he=gethostbyname(hostname)) == NULL)
	{
		perror("gethostbyname: can't get an address");
		exit(1);
	}

	add=htonl( ((struct in_addr *)(he->h_addr_list[0]))->s_addr );
	printf("%d.%d.%d.%d\n",
	(add & 0xff000000)>>24,
	(add & 0x00ff0000)>>16,
	(add & 0x0000ff00)>>8,
	(add & 0x000000ff)
	);


	return(0);
}

===========================================================================

	por otra parte, este codigo supone que solo tienes un nombre
	principal y que a ese nombre solo le corresponde una IP, cosas
	ambas que no tienen por que ser ciertas.


-- 
PGP and other useless info at      \                      Oh, show us the way
http://www.cps.unizar.es/~spd/      \                  to the next whisky bar
finger://daphne.cps.unizar.es/spd    \                           Weill/Bretch
Lo que si que es cierto es que la felicidad no da el dinero...
Technical Details
Message-ID: <7rof3t$ltq$3@posta2.unizar.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7rlgo6$lgm$1@batcher.rediris.es> <5hslr7.714.ln@belcebu.ccr.es> <7rnqdn$3ec$1@titan.bt.es>

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,50e1a3e59813ade5
X-Google-Attributes: gidf86c8,public
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Subject: Re: Direccion IP en C y PPP.
Date: 1999/09/15
Message-ID: <7rof3t$ltq$3@posta2.unizar.es>
X-Deja-AN: 525466039
Distribution: es
Content-Transfer-Encoding: 8bit
References: <7rlgo6$lgm$1@batcher.rediris.es> <5hslr7.714.ln@belcebu.ccr.es> <7rnqdn$3ec$1@titan.bt.es>
Content-Type: text/plain; charset=ISO-8859-1
X-Complaints-To: usenet@news.unizar.es
X-Trace: posta2.unizar.es 937410493 22458 155.210.29.12 (15 Sep 1999 15:48:13 GMT)
Organization: Rank
Mime-Version: 1.0
User-Agent: tin/pre-1.4-19990413 ("Endemoniada") (UNIX) (IRIX/6.2 (IP22))
NNTP-Posting-Date: 15 Sep 1999 15:48:13 GMT
Newsgroups: es.comp.os.linux.programacion