Article View: es.comp.os.linux.programacion
Article #18254Re: Direccion IP en C y PPP.
From: leumas
Date: Fri, 17 Sep 1999 00:00
Date: Fri, 17 Sep 1999 00:00
87 lines
3506 bytes
3506 bytes
El d�a 15 Sep 1999 09:55:03 GMT, un nota de nombre oscar@server.enmicasa.dhs.org
(oscar@server.enmicasa.dhs.org), fue a decir...
>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.
yo voy a exponer esa serie de llamadas :-)
<---- interface.c ---->
/*
* Funciones para obtener informaci�n sobre los interfaces activos.
*
* Consegu� hacer esto gracias al codigo del
* ifconfig (Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
* and others. Copyright 1993 MicroWalt Corporation)
* tras 3 horas de dar vueltas por el codigo y otras cuantas por los
* includes... :-)
*
* Author de esto: leumas (1999)
*
*/
//#include <sys/types.h>
//#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
//#include <stdio.h>
//#include <errno.h>
//#include <stdlib.h>
//#include <string.h>
//#include <unistd.h>
//#include <ctype.h>
/*
* ip_interface
*
* obtiene la ip (de cara a Internet) de un interface, siempre y cuando
* este activo; ifname debe ser algo como 'lo','ppp0',...
*
*/
unsigned long ip_interface (unsigned char *ifname)
{
int inet_sock;
struct ifreq ifr;
struct sockaddr_in *ifaddr;
short ifflags;
inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
// el socket para hacerle las ioctl...
// cogemos los flags del interface
strcpy(ifr.ifr_name, ifname);
if (ioctl(inet_sock, SIOCGIFFLAGS, &ifr) < 0) return(0);
ifflags = ifr.ifr_flags;
// nos aseguramos de que el interface esta activo
if ((ifflags & IFF_UP) == 0) {
close(inet_sock);
return(0);
}
// ya que est� activo,miramos la ip del interface
strcpy(ifr.ifr_name, ifname);
/ / le damos el nombre del device ("lo","ppp0",...)
if (inet_sock < 0 || ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0) {
close (inet_sock);
return (0); // me parece que el interface no existe...
} else {
close (inet_sock);
ifaddr=(struct sockaddr_in *)&ifr.ifr_addr; // cosas del type casting...
return ((unsigned long) ifaddr->sin_addr.s_addr); // devolvemos la ip
}
<---- fin de interface.c ---->
Technical Details
Message-ID:
<slrn7u511k.2l1.noticiasMEPREGUNTOQUESERAELSPAM@localhost.localdomain>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<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: noticiasMEPREGUNTOQUESERAELSPAM@rebeldin.dhis.org (leumas) Subject: Re: Direccion IP en C y PPP. Date: 1999/09/17 Message-ID: <slrn7u511k.2l1.noticiasMEPREGUNTOQUESERAELSPAM@localhost.localdomain> X-Deja-AN: 526482923 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: newsmanager@rediris.es X-Trace: batcher.rediris.es 937604491 5721 62.82.108.10 (17 Sep 1999 21:41:31 GMT) Organization: LAR Mime-Version: 1.0 User-Agent: slrn/0.9.5.3 (UNIX) NNTP-Posting-Date: 17 Sep 1999 21:41:31 GMT Newsgroups: es.comp.os.linux.programacion