1/*
2 *   $Id: device-common.c,v 1.10 2009/06/19 07:28:06 psavola Exp $
3 *
4 *   Authors:
5 *    Lars Fenneberg		<lf@elemental.net>
6 *
7 *   This software is Copyright 1996,1997 by the above mentioned author(s),
8 *   All Rights Reserved.
9 *
10 *   The license which is distributed with this software in the file COPYRIGHT
11 *   applies to this software. If your distribution is missing this file, you
12 *   may request it from <pekkas@netcore.fi>.
13 *
14 */
15
16#include <config.h>
17#include <includes.h>
18#include <radvd.h>
19#include <defaults.h>
20
21int
22check_device(int sock, struct Interface *iface)
23{
24	struct ifreq	ifr;
25
26	strncpy(ifr.ifr_name, iface->Name, IFNAMSIZ-1);
27	ifr.ifr_name[IFNAMSIZ-1] = '\0';
28
29	if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
30	{
31		if (!iface->IgnoreIfMissing)
32			flog(LOG_ERR, "ioctl(SIOCGIFFLAGS) failed for %s: %s",
33				iface->Name, strerror(errno));
34		return (-1);
35	}
36
37	if (!(ifr.ifr_flags & IFF_UP))
38	{
39		if (!iface->IgnoreIfMissing)
40                	flog(LOG_ERR, "interface %s is not UP", iface->Name);
41		return (-1);
42	}
43	if (!(ifr.ifr_flags & IFF_RUNNING))
44	{
45		if (!iface->IgnoreIfMissing)
46                	flog(LOG_ERR, "interface %s is not RUNNING", iface->Name);
47		return (-1);
48	}
49
50	if (! iface->UnicastOnly && !(ifr.ifr_flags & IFF_MULTICAST))
51	{
52		flog(LOG_WARNING, "interface %s does not support multicast",
53			iface->Name);
54		flog(LOG_WARNING, "   do you need to add the UnicastOnly flag?");
55	}
56
57	if (! iface->UnicastOnly && !(ifr.ifr_flags & IFF_BROADCAST))
58	{
59		flog(LOG_WARNING, "interface %s does not support broadcast",
60			iface->Name);
61		flog(LOG_WARNING, "   do you need to add the UnicastOnly flag?");
62	}
63
64	return 0;
65}
66
67int
68get_v4addr(const char *ifn, unsigned int *dst)
69{
70        struct ifreq    ifr;
71        struct sockaddr_in *addr;
72        int fd;
73
74        if( ( fd = socket(AF_INET,SOCK_DGRAM,0) ) < 0 )
75        {
76                flog(LOG_ERR, "create socket for IPv4 ioctl failed for %s: %s",
77                        ifn, strerror(errno));
78                return (-1);
79        }
80
81        memset(&ifr, 0, sizeof(ifr));
82        strncpy(ifr.ifr_name, ifn, IFNAMSIZ-1);
83        ifr.ifr_name[IFNAMSIZ-1] = '\0';
84        ifr.ifr_addr.sa_family = AF_INET;
85
86        if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
87        {
88                flog(LOG_ERR, "ioctl(SIOCGIFADDR) failed for %s: %s",
89                        ifn, strerror(errno));
90                close( fd );
91                return (-1);
92        }
93
94        addr = (struct sockaddr_in *)(&ifr.ifr_addr);
95
96        dlog(LOG_DEBUG, 3, "IPv4 address for %s is %s", ifn,
97                inet_ntoa( addr->sin_addr ) );
98
99        *dst = addr->sin_addr.s_addr;
100
101        close( fd );
102
103        return 0;
104}
105