net_rt_iflist.c 1.21 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2 3 4 5 6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

Kristin Wright's avatar
Kristin Wright committed
7 8 9 10 11 12 13
/* 
 * This is a very slightly modified version of
 * example code from Unix Netwok Programming, edition 2.
 */

#include "discvr.h"

14 15 16
//


Kristin Wright's avatar
Kristin Wright committed
17 18 19 20 21 22
char *
net_rt_iflist(int family, int flags, size_t *lenp)
{
	int		mib[6];
	char	*buf;

23 24 25 26 27 28 29 30 31 32 33
	// Meaning of each parameter is explained below -ik
	mib[0] = CTL_NET; 		// network related system information
	mib[1] = PF_ROUTE;		// get the routing messages
	mib[2] = 0;				// protocol number (always zero!)
	mib[3] = family;		// get addresses of only this family 
	mib[4] = NET_RT_IFLIST; // return "interface" list
	mib[5] = flags;			/* interface index, or 0*/ // "man" says NONE?!

	// Call "sysctl" to get the length of buffer needed for storing the list
	// of interfaces. The "oldp" parameter is supplied with NULL to get just
	// the size. -ik
Kristin Wright's avatar
Kristin Wright committed
34 35 36
	if (sysctl(mib, 6, NULL, lenp, NULL, 0) < 0)
		return(NULL);

37
	// Allocate a buffer to store the list of interfaces. -ik
Kristin Wright's avatar
Kristin Wright committed
38 39
	if ( (buf = malloc(*lenp)) == NULL)
		return(NULL);
40 41

	// Get the list of interfaces in the "buf". -ik
Kristin Wright's avatar
Kristin Wright committed
42 43 44
	if (sysctl(mib, 6, buf, lenp, NULL, 0) < 0)
		return(NULL);

45
	// Return the list of interfaces. -ik
Kristin Wright's avatar
Kristin Wright committed
46 47 48 49
	return(buf);
}