interface_id.c revision ffdae209526a77e75eb659dbd1fad51bf7e2ba98
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define IPIF_SEPARATOR_CHAR ":"
/*
* Given an interface name, this function retrives the associated
* index value. Returns index value if successful, zero otherwise.
* The length of the supplied interface name must be at most
* IF_NAMESIZE-1 bytes
*/
if_nametoindex(const char *ifname)
{
int s;
int save_err;
/* Make sure the given name is not NULL */
return (0);
}
/*
* Fill up the interface name in the ioctl
* request message. Make sure that the length of
* the given interface name <= (IF_NAMESIZE-1)
*/
return (0);
}
/* Check the v4 interfaces first */
if (s >= 0) {
(void) close(s);
return (lifr.lifr_index);
}
(void) close(s);
}
/* Check the v6 interface list */
if (s < 0)
return (0);
lifr.lifr_index = 0;
(void) close(s);
return (lifr.lifr_index);
}
/*
* Given an index, this function returns the associated interface
* name in the supplied buffer ifname.
* Returns physical interface name if successful, NULL otherwise.
* The interface name returned will be at most IF_NAMESIZE-1 bytes.
*/
char *
{
int n;
int s;
char *buf;
int numifs;
/* A interface index of 0 is invalid */
if (ifindex == 0) {
return (NULL);
}
if (s < 0) {
if (s < 0) {
return (NULL);
}
}
/* Prepare to send a SIOCGLIFNUM request message */
(void) close(s);
return (NULL);
}
/*
* NOTE: "+ 10" sleaze mitigates new IP interfaces showing up between
* the SIOCGLIFNUM and the SIOCGLIFCONF.
*/
/*
* Provide enough buffer to obtain the interface
* list from the kernel as response to a SIOCGLIFCONF
* request
*/
(void) close(s);
return (NULL);
}
(void) close(s);
return (NULL);
}
/*
* Obtain the index value of each interface, and
* match to see if the retrived index value matches
* the given one. If so we return the corresponding
* device name of that interface.
*/
if (index == 0)
/* Oops the interface just disappeared */
continue;
(char *)IPIF_SEPARATOR_CHAR);
break;
}
}
(void) close(s);
if (!found) {
return (NULL);
}
return (ifname);
}
/*
* This function returns all the interface names and indexes
*/
struct if_nameindex *
if_nameindex(void)
{
int n;
int s;
char *buf;
int numifs;
int index;
int i;
int physinterf_num;
struct if_nameindex *interface_list;
struct if_nameindex *interface_entry;
if (s < 0) {
if (s < 0)
return (NULL);
}
return (NULL);
(void) close(s);
return (NULL);
}
(void) close(s);
return (NULL);
}
(void) close(s);
/* Allocate the array of if_nameindex structure */
if (!interface_list) {
return (NULL);
}
/*
* Make sure that terminator structure automatically
* happens to be all zeroes.
*/
physinterf_num = 0;
/*
* Search the current array to see if this interface
* already exists. Only compare the physical name.
*/
for (i = 0; i < physinterf_num; i++) {
break;
}
}
/* New one. Allocate an array element and fill it */
if (!found) {
/*
* Obtain the index value for the interface
*/
/* The interface went away. Skip this entry. */
continue;
}
/*
* Truncate the name to ensure that it represents
* a physical interface.
*/
int save_err;
return (NULL);
}
}
}
/* Create the last one of the array */
/* Free up the excess array space */
sizeof (struct if_nameindex)));
return (interface_list);
}
/*
* This function frees the the array that is created while
* the if_nameindex function.
*/
void
{
return;
/* First free the if_name member in each array element */
ptr++;
}
/* Now free up the array space */
}