GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/net/if_nametoindex.c Lines: 7 11 63.6 %
Date: 2017-11-07 Branches: 4 6 66.7 %

Line Branch Exec Source
1
/*	$OpenBSD: if_nametoindex.c,v 1.10 2015/10/23 13:09:19 claudio Exp $	*/
2
/*	$KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $	*/
3
4
/*-
5
 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6
 * Copyright (c) 1997, 2000
7
 *	Berkeley Software Design, Inc.  All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 *
27
 *	BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28
 */
29
30
#include <sys/types.h>
31
#include <sys/socket.h>
32
#include <net/if.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <errno.h>
36
37
/*
38
 * From RFC 2553:
39
 *
40
 * 4.1 Name-to-Index
41
 *
42
 *
43
 *    The first function maps an interface name into its corresponding
44
 *    index.
45
 *
46
 *       #include <net/if.h>
47
 *
48
 *       unsigned int  if_nametoindex(const char *ifname);
49
 *
50
 *    If the specified interface name does not exist, the return value is
51
 *    0, and errno is set to ENXIO.  If there was a system error (such as
52
 *    running out of memory), the return value is 0 and errno is set to the
53
 *    proper value (e.g., ENOMEM).
54
 */
55
56
unsigned int
57
if_nametoindex(const char *ifname)
58
{
59
	struct if_nameindex *ifni, *ifni2;
60
	unsigned int index;
61
62
8
	if ((ifni = if_nameindex()) == NULL)
63
		return(0);
64
65
72
	for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) {
66
36
		if (strcmp(ifni2->if_name, ifname) == 0) {
67
4
			index = ifni2->if_index;
68
4
			if_freenameindex(ifni);
69
4
			return index;
70
		}
71
	}
72
73
	if_freenameindex(ifni);
74
	errno = ENXIO;
75
	return 0;
76
4
}
77
DEF_WEAK(if_nametoindex);