summaryrefslogtreecommitdiffstats
path: root/rfc1035/rfc1035ifconf.c
blob: 47b24b22ab3cc62ccbb8ff29c6e9d6e23d76fc15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
** Copyright 2002 Double Precision, Inc.
** See COPYING for distribution information.
*/

#include "rfc1035.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif


#if HAVE_SIOCGIFCONF

#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#include <sys/ioctl.h>
#include <net/if.h>

static int getifconf(int fd, struct rfc1035_ifconf **ifconf_ptr)
{
	struct ifreq ifreq_buf[64];
	struct ifconf ifc;
	int i;
	const struct sockaddr_in *sin;
	RFC1035_ADDR addr;
	char ipaddr[RFC1035_NTOABUFSIZE];
	struct rfc1035_ifconf *ifcptr;
	struct rfc1035_ifconf **ifconf;

	ifc.ifc_len=sizeof(ifreq_buf);
	ifc.ifc_req=ifreq_buf;

	if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
		return (0);

	for (i=0; i * sizeof(struct ifreq) < ifc.ifc_len; i++)
	{
		sin=(const struct sockaddr_in *)&ifreq_buf[i].ifr_addr;

#if RFC1035_IPV6
		if (sin->sin_family == AF_INET6)
		{
			struct sockaddr_in6 sin6;

			memcpy(&sin6, sin, sizeof(sin6));
			addr=sin6.sin6_addr;
		}
		else if (sin->sin_family == AF_INET)
			rfc1035_ipv4to6(&addr, &sin->sin_addr);
		else
			continue;
#else
		if (sin->sin_family == AF_INET)
			addr=sin->sin_addr;
		else
			continue;
#endif

		rfc1035_ntoa(&addr, ipaddr);

		/*
		** Eliminate any dupes.
		*/

		ifconf=ifconf_ptr;

		while ( *ifconf )
		{
			char ipaddr2[RFC1035_NTOABUFSIZE];

			rfc1035_ntoa(&(*ifconf)->ifaddr, ipaddr2);

			if (strcmp(ipaddr, ipaddr2) == 0)
				break;

			ifconf= &(*ifconf)->next;
		}

		if ( *ifconf )
			continue;	/* Already have this IP addr */

		if ( (ifcptr=malloc(sizeof(struct rfc1035_ifconf))) == NULL ||
		     (ifcptr->ifname=strdup(ifreq_buf[i].ifr_name)) == NULL)
		{
			if (ifcptr)
				free(ifcptr);
			return (-1);
		}

		ifcptr->ifaddr=addr;
		ifcptr->next=NULL;

		*ifconf= ifcptr;
	}
	return (0);
}

/*
** On systems that support IPv6, issue an SIOCGIFCONF on both an IPv4 and
** an IPv6 socket, for good measure.
*/

static int doifconf(struct rfc1035_ifconf **ifconf_list)
{
	int fd;

	fd=socket(PF_INET, SOCK_STREAM, 0);

	if (fd >= 0)
	{
		if (getifconf(fd, ifconf_list))
		{
			close(fd);
			return (-1);
		}
		close(fd);
	}

#if RFC1035_IPV6

	fd=socket(PF_INET6, SOCK_STREAM, 0);

	if (fd >= 0)
	{
		if (getifconf(fd, ifconf_list))
		{
			close(fd);
			return (-1);
		}
		close(fd);
	}
#endif
	return (0);
}

struct rfc1035_ifconf *rfc1035_ifconf(int *errflag)
{
	struct rfc1035_ifconf *ifconf_list=NULL;
	int dummy;

	if (!errflag)
		errflag= &dummy;

	*errflag= -1;
	if (doifconf(&ifconf_list))
	{
		rfc1035_ifconf_free(ifconf_list);
		return (NULL);
	}

	*errflag=0;
	return ifconf_list;
}

#else

struct rfc1035_ifconf *rfc1035_ifconf(int *errflag)
{
	if (errflag)
		*errflag=0;
	return NULL;
}
#endif

void rfc1035_ifconf_free(struct rfc1035_ifconf *ifconf_list)
{
	while (ifconf_list)
	{
		struct rfc1035_ifconf *p=ifconf_list->next;

		free(ifconf_list->ifname);
		free(ifconf_list);
		ifconf_list=p;
	}
}