Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Jan 2000 15:46:30 +0200
From:      Ruslan Ermilov <ru@ucb.crimea.ua>
To:        Matt Gostick <matt@crazylogic.net>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: ioctl programming probs
Message-ID:  <20000105154630.I18632@relay.ucb.crimea.ua>
In-Reply-To: <Pine.BSF.4.10.9912282005040.40430-100000@thunk.crazylogic.net>; from Matt Gostick on Tue, Dec 28, 1999 at 10:09:30PM -0500
References:  <Pine.BSF.4.10.9912282005040.40430-100000@thunk.crazylogic.net>

next in thread | previous in thread | raw e-mail | index | archive | help

--M9NhX3UHpAaciwkO
Content-Type: text/plain; charset=us-ascii

That's why you should have been compile your program with -Wall
and read netintro(4).

Beware, that broadcast address is only meaningful on broadcast
interfaces, and dstaddr is only meaningful on P2P interfaces.

On Tue, Dec 28, 1999 at 10:09:30PM -0500, Matt Gostick wrote:
> 
> I'm trying to make a simple little program that gets the ip address of a
> given interface.  I'm using ioctl to do this and am having a wierd problem
> that I can't figure out.  Any help is appretiated.  
> 
> >From what I understand about ioctl ... the below program should display
> the interface name, ip address, destination address and the broadcast
> address.  Unfortuanely it's not.  It's not crashing ... it just always 
> prints:
> 
> name      : lo0
> addr      : 16.2.0.0
> dstaddr   : 16.2.0.0
> broadaddr : 16.2.0.0
> 
> I've tried this on two different FreeBSD-3.2 machines with the same 
> result, even the same IP's.  I've even tried switching the interface
> names to something different, and am still getting the same IP's.
> 
> I have searched newsgroups and mailing lists only to come up with no
> solution.  I was hoping that those familiar with ioctl under FreeBSD will
> spot my mistake in a couple of seconds... :)  I'm new to it so any help is
> very much appretiated.
> 
> /* --<get_if_ip.c>------------------------------------- */
> 
[...]
> 
> /* --<end get_if_ip.c>---------------------------------- */
> 
> 
> Thanks,
> Matt

-- 
Ruslan Ermilov		Sysadmin and DBA of the
ru@ucb.crimea.ua	United Commercial Bank,
ru@FreeBSD.org		FreeBSD committer,
+380.652.247.647	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age

--M9NhX3UHpAaciwkO
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="get_if_ip.c"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <err.h>

int
main(void)
{
	int fd;
	struct ifreq ifr;

	/* Open a socket to use for ioctl(2). */
	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
		err(1, "socket");

	strcpy(ifr.ifr_name, "lo0");

	/* Get if address. */
	if (ioctl(fd, SIOCGIFADDR, &ifr, sizeof(ifr)) == -1)
		err(1, "ioctl(SIOCGIFADDR)");

	printf("name      : %s\n", ifr.ifr_name);
	printf("addr      : %s\n",
		inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

	/* Get destination address. */
	if (ioctl(fd, SIOCGIFDSTADDR, &ifr, sizeof(ifr)) == -1)
		warn("ioctl(SIOCGIFDSTADDR)");
	else
		printf("dstaddr   : %s\n",
			inet_ntoa(((struct sockaddr_in *)&ifr.ifr_dstaddr)->sin_addr));

	/* Get broadcast address. */
	if (ioctl(fd, SIOCGIFBRDADDR, &ifr, sizeof(ifr)) == -1)
		warn("ioctl(SIOCGIFBRDADDR)");
	else
		printf("broadaddr : %s\n",
			inet_ntoa(((struct sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr));

	close(fd);
	return 0;
}

--M9NhX3UHpAaciwkO--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000105154630.I18632>