Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Mar 2002 20:59:31 +0000 (GMT)
From:      Gavin Atkinson <gavin@ury.york.ac.uk>
To:        <freebsd-net@freebsd.org>
Subject:   How to detect link on unconfigered interface?
Message-ID:  <Pine.BSF.4.33.0203272056190.6118-200000@ury.york.ac.uk>

index | next in thread | raw e-mail

[-- Attachment #1 --]

Hi,

I have a problem I have so far been unsuccessful in solving. I want to
detect if a particular network interface has a link, before this interface
has an IP address configured.

From looking at the source to ifconfig, which successfully does this, the
attached code should work,  but doesn't. Can anyone help me please?

Thanks,

Gvain

[-- Attachment #2 --]
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_media.h>

char name[32] = "ep0";

#define FALSE 0
#define TRUE 1

int
media_status(s)
	int s;
{
	struct ifmediareq ifmr;
	int link = 0;

	(void) memset(&ifmr, 0, sizeof(ifmr));
	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));

	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
		err(1, "SIOCGIFMEDIA");
	else {
		printf ("ifm_status=%x\n", ifmr.ifm_status);
		if (ifmr.ifm_status & IFM_AVALID) {
			printf ("  success - AVALID is set\n");
			if (ifmr.ifm_status & IFM_ACTIVE) {
				link = 1;
			}
		}
	}
	return link;
}

int main (void)
{
	int s = -1;

	if (( s = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
		err(1, "socket");
	printf ("interface is %s\n", (media_status(s) ? "up" : "down"));
}
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.33.0203272056190.6118-200000>