/**
* Function: To check whether a given network interface is
* up or down. 
*
* Author: Asanga Udugama (adu@comnets.uni-bremen.de)
* License: GPL
*/
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <stdio.h>
	
int main(int argc, char *argv[])
{
	int sfd;
	struct ifreq ifr;
	int is_up = 0;
	
	if(argc != 2) {
		printf("usage : %s ifc-name\n", argv[0]);
		exit(1);
	}
	
	// set the given interface name in the structure
	// given to the IOCTL
	memset(&ifr, 0, sizeof ifr);
	strcpy(ifr.ifr_name, argv[1]);
	
	// open socket
	if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket()");
		exit(1);
	}

	// call the relevent IOCTL
	if (ioctl(sfd, SIOCGIFFLAGS, &ifr) < 0) {
		perror("ioctl()");
		exit(1);
	}
	
	// check to see if the interface is up
	if((ifr.ifr_flags & IFF_UP) == IFF_UP) {
		is_up = 1;
	}

	// print message
	printf("Interface %s is %s\n", argv[1], (is_up? "UP": "DOWN"));
	
	exit(0);
}
