[BACK]Return to ifaddr.c CVS log [TXT][DIR] Up to [local] / src / sbin / ipf

File: [local] / src / sbin / ipf / Attic / ifaddr.c (download)

Revision 1.7, Tue Jan 30 04:26:01 2001 UTC (23 years, 4 months ago) by kjell
Branch: MAIN
CVS Tags: OPENBSD_2_9_BASE, OPENBSD_2_9
Changes since 1.6: +2 -1 lines

reimplement local ipf mods and import some missed patches

/*	$OpenBSD: ifaddr.c,v 1.7 2001/01/30 04:26:01 kjell Exp $	*/

#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 <string.h>
#include <err.h>
#include "ifaddr.h"

/*
 * if_addr():
 *      given a string containing an interface name (e.g. "ppp0")
 *      return the IP address it represents
 *
 * The OpenBSD community considers this feature to be quite useful and
 * suggests inclusion into other platforms. The closest alternative is
 * to define /etc/networks with suitable values.
 */
int if_addr(name, ap)
	char *name;
	struct in_addr *ap;
{
	struct sockaddr_in *sin;
	struct ifreq ifr;
	int s;

	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		warn("socket");
		return (0);
	}

	strncpy(ifr.ifr_name, name, IFNAMSIZ);
	ifr.ifr_name[IFNAMSIZ - 1] = '\0';

	if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
		return (0);

	sin = (struct sockaddr_in *)&ifr.ifr_addr;
	*ap = sin->sin_addr;

	return (1);
}